本文主要是介绍ROS下树莓派USB串口通信(具体为接收到某个ROS的topic数据后,向串口下发数据。),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
USB端口号和波特率都能通过launch文件配置。
manipulator_ros_serial.launch
感觉应该就像apm.launch一样,可以在里面修改指定对应串口和波特率。或者可以命令行启动的时候指定。
摘自:https://www.it610.com/article/1279946651775025152.htm
ROS下树莓派USB串口通信
- c++
做工程时需要在树莓派进行串口通信。具体为接收到某个ROS的topic数据后,向串口下发数据。代码编写有两种方法。
方法一:借助serial库
1、运行环境为ubuntu,首先安装serial库:
sudo apt-get install serial
2、配置编写的package中的CMakeLists.txt
在find_package中添加serial;
3、给设备的USB端口权限;
4、代码范例:
manipulator_ros_serial.cpp
#include
#include
#include
#include #define sBUFFERSIZE 7
unsigned char s_buffer[sBUFFERSIZE]; using namespace std;serial::Serial ser;//trans different serial cmds according to different action_cmd(joystick button)
void write_action_cmd(const std_msgs::Int64& action_cmd){memset(s_buffer,0,sizeof(s_buffer));if( 1 == action_cmd.data ){s_buffer[0] = 0x55;s_buffer[1] = 0x55;s_buffer[2] = 0x05;s_buffer[3] = 0x06;s_buffer[4] = 0x01;s_buffer[5] = 0x01;s_buffer[6] = 0x00;//std::cout<<"cmd has been received and published!!!"<<&std::endl;}ser.write(s_buffer,sBUFFERSIZE);}int main(int argc, char** argv){ros::init(argc, argv, "manipulator_ros_serial_node");ros::NodeHandle nh;ros::NodeHandle nh_private("~");std::string serial_port_;int baudrate_;nh_private.param("serial_port", serial_port_, "/dev/ttyUSB0");nh_private.param("baudrate", baudrate_, 9600);std::cout<<"The serial port is "<
5、USB端口号和波特率都能通过launch文件配置。
manipulator_ros_serial.launch
方法二:借助boost
1、运行环境为raspbian,raspbian没有serial库;
2、代码范例
manipulator_ros.h
#ifndef MANIPULATOR_SERIAL_H_
#define MANIPULATOR_SERIAL_H_
#include
#include
#include
#include
#include boost::asio::io_service io;
boost::asio::serial_port sp(io);namespace manipulator_serial_namespace{
class ManipulatorSerial{public:ManipulatorSerial();~ManipulatorSerial();ros::Subscriber write_sub;//boost::asio::io_service io;void write_action_cmd(const std_msgs::Int64& action_cmd);};}#endif
manipulator_serial.cpp
#include #define sBUFFERSIZE 7
unsigned char s_buffer[sBUFFERSIZE];using namespace std;
using namespace boost::asio;namespace manipulator_serial_namespace{ManipulatorSerial::ManipulatorSerial(){ros::NodeHandle nh;ros::NodeHandle nh_private("~");std::string serial_port_;int baudrate_;nh_private.param("serial_port", serial_port_, "/dev/ttyUSB0");nh_private.param("baudrate", baudrate_, 9600);std::cout<<"The serial port is "<
5、USB端口号和波特率都能通过launch文件进行配置。
manipulator_serial.launch
这篇关于ROS下树莓派USB串口通信(具体为接收到某个ROS的topic数据后,向串口下发数据。)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!