原理与结构

ROS 与移动底座的串口通信过程。

ROS下串口通信P2.通信原理图

ROS下的serial包

mkdir -p ~/catkin_ws/src/my_serial/
cd ~/catkin_ws/src/my_serial/
git clone https://github.com/wjwwood/serial.git

再下载我写好的代码

git clone https://github.com/threefruits/ros_serial_code.git
cd ~/catkin_ws
catkin_make

下面是cpp代码原码与注释

#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <geometry_msgs/Twist.h>

serial::Serial ros_ser;
#define	sBUFFERSIZE	10//send buffer size 串口发送缓存长度
unsigned char s_buffer[sBUFFERSIZE];//发送缓存
int l_v=0;
int r_v=0;
int l_d=1;
int r_d=1;
const float d_between=0.165;//半轮间距
const float d=0.125;//轮子半径

typedef union{
	unsigned char cvalue[4];
	int ivalue;
}int_union;

void cmd_to_serial(const geometry_msgs::Twist msg)
{
    r_v=27*(msg.linear.x + msg.angular.z * d_between)/(3.1416*d);
    l_v=27*(msg.linear.x - msg.angular.z * d_between)/(3.1416*d);
    if(r_v>=0)
    {
        r_d=0;
        r_v=r_v;
    }             //电机正转
    else
    {
        r_d=1;
        r_v=-r_v;
    }           //电机反转

    if(l_v>=0)
    {
        l_d=0;
        l_v=l_v;
    }
    else
    {
        l_d=1;
        l_v=-l_v;
    }

}

void data_to_serial(const int left, const int right, const int left_d, const int right_d)
{
     int_union left_v,right_v,left_v_d,right_v_d;
     left_v.ivalue=left;
     right_v.ivalue

相关文章: