• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

ROS下树莓派USB串口通信

人工智能 cabinx 2093次浏览 0个评论

做工程时需要在树莓派进行串口通信。具体为接收到某个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 <ros/ros.h>
#include <string>
#include <serial/serial.h>
#include <std_msgs/Int64.h>
 
#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<std::string>("serial_port", serial_port_, "/dev/ttyUSB0");
	nh_private.param<int>("baudrate", baudrate_, 9600);
	
	std::cout<<"The serial port is  "<<serial_port_<<"   ..."<<&std::endl;
	std::cout<<"The baudrate is  "<<baudrate_<<"  ..."<<&std::endl;
	ros::Subscriber write_sub = nh.subscribe("joystick/manipulator", 1000, write_action_cmd);
	try
	{
		ser.setPort(serial_port_);
		ser.setBaudrate(baudrate_);
		serial::Timeout to = serial::Timeout::simpleTimeout(1000);
		ser.setTimeout(to);
		ser.open();
		}
	catch(serial::IOException& e){
		ROS_ERROR_STREAM("Unable to open port!!!");
		return -1;
		}
		
	if(ser.isOpen()){
		ROS_INFO_STREAM("Serial Port initialized...");
		}
	else{
		return -1;
		}
		
 
    ros::Rate loop_rate(50); 
    while(ros::ok()) { 
       ros::spinOnce(); 
       loop_rate.sleep(); 
     } 
 
}

  5、USB端口号和波特率都能通过launch文件配置。 manipulator_ros_serial.launch  

<launch>
    <node pkg="manipulator_ros_serial" name="manipulator_ros_serial" type="manipulator_ros_serial" output="screen">
      <param name="serial_port" type="string" value="/dev/ttyUSB0" />
      <param name="baudrate" type="int" value="9600" />
    </node>
</launch>

    方法二:借助boost 1、运行环境为raspbian,raspbian没有serial库; 2、代码范例 manipulator_ros.h  

#ifndef MANIPULATOR_SERIAL_H_
#define MANIPULATOR_SERIAL_H_
#include <ros/ros.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp> 
#include <std_msgs/Int64.h>
#include <string>
 
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 <manipulator_serial/manipulator_serial.h>
 
#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<std::string>("serial_port", serial_port_, "/dev/ttyUSB0");
	    nh_private.param<int>("baudrate", baudrate_, 9600);
	
	    std::cout<<"The serial port is  "<<serial_port_<<"   ..."<<&std::endl;
	    std::cout<<"The baudrate is  "<<baudrate_<<"  ..."<<&std::endl;
	    
	    sp.open(serial_port_);
	   
	   write_sub = nh.subscribe("joystick/manipulator", 1000, &ManipulatorSerial::write_action_cmd, this);
	   ros::spin();
	}
	
	ManipulatorSerial::~ManipulatorSerial(){}
	
	void ManipulatorSerial::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;
		}
 
	       boost::asio::write(sp, boost::asio::buffer(s_buffer));  
	}
} 
 
int main(int argc, char** argv) {
	::ros::init(argc, argv, "manipulator_serial_node");
	::ros::start();
	manipulator_serial_namespace::ManipulatorSerial a;
	::ros::shutdown();
}

    5、USB端口号和波特率都能通过launch文件进行配置。 manipulator_serial.launch    

<launch>
    <node pkg="manipulator_serial" name="manipulator_serial" type="manipulator_serial" output="screen">
      <param name="serial_port" type="string" value="/dev/ttyUSB0" />
      <param name="baudrate" type="int" value="9600" />
    </node>
</launch>

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明ROS下树莓派USB串口通信
喜欢 (0)

您必须 登录 才能发表评论!

加载中……