RosSerial虚拟机与单片机连接失败

文章描述了在RosSerial中,使用STM32与Ubuntu20在virtualBox中通过高速串口通信时,因数据丢失导致的连接失败问题。解决方案是将波特率降低至115200以确保稳定通信。
摘要由CSDN通过智能技术生成

项目场景: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没问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值