项目场景:RosSerial虚拟机与单片机连接失败
在virtualBox中的ubantu20和stm32使用rosserial连接时使用过高的波特率会丢失数据导致报错,表现为只能注册一个发布者或订阅者,第二个一定会报错:
Tried to publish before configured, topic id 125
问题描述
stm32代码如下,只是注册了两个发布者,开始发布数据
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int32.h>
ros::NodeHandle nh;
std_msgs::String stm32_to_pc_word;
std_msgs::Int32 intdata;
ros::Publisher publisher("stm32_to_pc", &stm32_to_pc_word);
ros::Publisher int32_pub("stm32_int32",&intdata);
char hello[17] = "hello world! soc";
void RosserialSetup(void)
{
nh.initNode();
nh.advertise(publisher);
nh.advertise(int32_pub);
}
void RosserialLoop(void)
{
static int i = 0;
i++;
stm32_to_pc_word.data = hello;
publisher.publish(&stm32_to_pc_word);
intdata.data = i;
int32_pub.publish(&intdata);
nh.spinOnce();
vTaskDelay(1000);
}
笔者在使用1152000的波特率通信时会出现一下情况,只能注册一个发布,
isky@ROS1:~$ rosrun rosserial_python serial_node.py _port:=/dev/ttyUSBMCU _baud:=1152000
[INFO] [1705456160.076993]: ROS Serial Python Node
[INFO] [1705456160.082020]: Connecting to /dev/ttyUSBMCU at 1152000 baud
[INFO] [1705456162.266092]: Requesting topics...
[INFO] [1705456162.860659]: Note: publish buffer size is 2048 bytes
[INFO] [1705456162.861860]: Setup publisher on stm32_to_pc [std_msgs/String]
[INFO] [1705456164.840994]: wrong checksum for topic id and msg
[ERROR] [1705456164.844859]: Tried to publish before configured, topic id 126
[INFO] [1705456164.846802]: Requesting topics...
[ERROR] [1705456165.843193]: Tried to publish before configured, topic id 126
[INFO] [1705456165.844716]: Requesting topics...
将波特率降低后正常
isky@ROS1:~$ rosrun rosserial_python serial_node.py _port:=/dev/ttyUSBMCU _baud:=115200
[INFO] [1705456272.556020]: ROS Serial Python Node
[INFO] [1705456272.561174]: Connecting to /dev/ttyUSBMCU at 115200 baud
[INFO] [1705456274.740733]: Requesting topics...
[INFO] [1705456275.295953]: Note: publish buffer size is 2048 bytes
[INFO] [1705456275.308129]: Setup publisher on stm32_to_pc [std_msgs/String]
[INFO] [1705456275.318081]: Setup publisher on stm32_int32 [std_msgs/Int32]
原因分析:
应该是在虚拟机内高速串口丢数据导致的问题,串口使用的是ch340转usb的,暂时未知该问题是否会在实体机发生。
解决方案:
将波特率降低,目前实测115200没问题