Advanced ROS - serial communication

Environment: Ubuntu16.04 + ROS Kinetic

ROS underlying serial communication in two ways:

  1. As ROS bottom node: https://blog.csdn.net/Kalenee/article/details/80644896
  2. Traditional serial communications style: https://blog.csdn.net/Kalenee/article/details/82422196

 

I. Introduction

Preparation of serial nodes, on completion of the message processing platform via ROS ROS standard communication formats and hair, and then delivers data through the serial port.

 

Second, placement

1, the serial-port function package

sudo apt-get install ros-kinetic-serial

api documentation: http://wjwwood.io/serial/doc/1.1.0/classserial_1_1_serial.html

2, serial installation assistant

sudo apt-get install cutecom 

 

Third, write the serial node

#include <ros/ros.h>
#include <serial/serial.h>

//串口类
serial::Serial ser;

#define sBUFFERSIZE 1000 // send buffer size串口发送缓存长度
#define rBUFFERSIZE 1000 // receive buffer size 串口接收缓存长度
unsigned char s_buffer[sBUFFERSIZE]; //发送缓存
unsigned char r_buffer[rBUFFERSIZE]; //接收缓存

//下发数据函数
void data_redownload() 
{
  ser.write(s_buffer, sBUFFERSIZE);
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "serial_node");
  ros::NodeHandle nh;

  //打开串口
  try {
    ser.setPort("/dev/ttyUSB0");
    ser.setBaudrate(115200);
    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;
  }

  // 10hz频率执行
  ros::Rate loop_rate(10);
  while (ros::ok()) {
    ros::spinOnce();
    if (ser.available()) {
      size_t bytes_read;
      bytes_read = ser.read(r_buffer, ser.available());
    //接收到数据后处理
    }
    loop_rate.sleep();
  }
}

 

Fourth, the test

1, the device connected to the serial connection state and check

dmesg | grep ttyS*

2, check the serial port by cutrcom assistant serial devices to communicate whether, if the issues can not connect permissions problem, run the following command to give the user permissions, user_name user name for the system.

sudo usermod -a -G dialout user_name

3, run node to communicate

 

reference

http://stevenshi.me/2017/10/11/stm32-serial-port-ros/

Published 53 original articles · won praise 186 · views 180 000 +

Guess you like

Origin blog.csdn.net/Kalenee/article/details/82422196