三面超声波测距及坐标位置转换
打算使用三个超声波测距避障模块部署到机器人智能车上,之间间隔45度角。以笛卡尔坐标系为基准,可以根据每个测出的距离
求出所对应的障碍物的位置。假设车头面向正前方,theta = 90度,那么根据三角函数可求出左右障碍物的位置,这样就可以自己做一个简单的
地图map。代码如下
#include<iostream> #include<vector> #include<array> #include<cmath> #include <unistd.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <sched.h> #include <errno.h> #include <string.h> #include <stdint.h> #include <iostream> #include <string> #include <vector> int main() { float theta =90.0; float degrees = theta *3.1415/180; float c_x = 0.0 + 1.0 * std::cos(degrees); float c_y = 0.0 + 1.0 * std::sin(degrees); float l_x = 0.0 + 1.0 * std::cos(degrees +45 *3.1415/180); float l_y = 0.0 + 1.0 * std::sin(degrees +45 *3.1415/180); float r_x = 0.0 + 1.0 * std::cos(degrees -45 *3.1415/180); float r_y = 0.0 + 1.0 * std::sin(degrees -45 *3.1415/180); printf("theta :%.1f degree \n",theta); printf("face:%.1f,%.1f \n",c_x,c_y); printf("left:%.1f,%.1f \n",l_x,l_y); printf("right:%.1f,%.1f \n",r_x,r_y); }
theta :90.0 degree face:0.0,1.0 left:-0.7,0.7 right:0.7,0.7