【ROS进阶】一文搞懂ROS话题通信机制与消息队列

一、话题通信机制解析

在这里插入图片描述

1、话题通信机制简介

(1)首先,发布节点把消息发布,消息进入Publisher的消息队列,同时通知订阅了该话题消息的Subscriber来取消息。
(2)其次,Subscriber来Publisher的消息队列里取消息,但取走的也是最老的消息,因为毕竟这是先入先出的队列。
(3)最后,被取走的消息存放入了Subscriber的消息队列中,等待被Callback执行。如果Callback执行很慢,消息越堆越多,最老的消息会逐渐被顶替。

2、消息队列分析

Publisher和Subscriber设置消息队列,都可以从两点进行分析:

  • 话题通信属于异步通信,队列可以存储一定量的历史数据
  • 网络传输异常的时候,队列可以预先进行缓存

(1)为什么需要设置Publisher的消息队列?

  • 话题通信属于异步通信,publisher节点发布消息,有多个Subscriber节点进行订阅,因此需要有一个地方对消息进行缓存。
  • 网络传输差、延时突然高的时候,可以把消息放在Publisher的队列中进行暂存。

(2)为什么要设置Subscriber消息队列?

  • Subscriber消息队列提供一边处理数据一边缓存新消息的机制。Publisher和Subscriber不一定在同一台主机上,但是网络的性能时好时坏,如果Subscriber没有消息队列。如果没有Subscriber消息队列,那么程序每次运行Callback函数前都要先通过网络取回消息,然后才能处理,当网络很差的时候就会造成系统的阻塞。

3、使用技巧

(1)队列长度queue_size参数选择

参考:http://wiki.ros.org/rospy/Overview/Publishers%20and%20Subscribers#Choosing_a_good_queue_size

  • 当queue_size=0,即ROS消息队列为0时,表示为无限队列长度,内存使用量可以无限增长,因此不推荐使用。
  • 当两个queue_size=1时,那么系统不会缓存数据,自然处理的就是最新的消息。
  • 当queue_size设置为10或者更多时候,用户更不容易错过发布的消息,适用于与人交互的用户界面的数据展示。

(2)ros::spinOnce机制

  • 一次 ros::spinOnce()会获取完所有Subscriber队列中的消息

二、实验验证

实验过程比较繁杂,可以直接看第三部分:总结

(一)subscriber队列长度对数据传输影响

1、实验目的

测试什么状态下队列会溢出

2、实验结论

结论一:
当回调函数处理时长小于数据发布的时间间隔,数据可以完整地传输。
结论二:
subscriber队列可以对接收数据进行缓存,spinonce每次进入回调函数,会取出Subscriber队列中最新的数据

3、详细实验过程
测试一:
1)条件设置

  • 数据发布为5Hz
  • 回调函数执行时间为0.19s

2)测试代码:

#include<ros/ros.h>
#include<ros/time.h>
#include"std_msgs/Int8.h"

int main(int argc,char **argv)
{
	ros::init(argc,argv,"test_publisher");
	ros::NodeHandle node;
	//控制队列长度
	ros::Publisher num_pub = node.advertise<std_msgs::Int8>("num",1);
	//控制Publisher发送频率
	ros::Rate loop_rate(5);
	int count=0;
	while(ros::ok())
	{
		std_msgs::Int8 msg;
		msg.data=count;
		num_pub.publish(msg);
		std::cout<<"I send:"<<count<<"   "<<"Time:"<<ros::Time::now()<<std::endl;
		count++;
		ros::spinOnce();
		loop_rate.sleep();
		
	}

}
#include"ros/ros.h"
#include<ros/time.h>
#include <ros/duration.h>
#include"std_msgs/Int8.h"

void numcallback(const std_msgs::Int8::ConstPtr& msg)
{
	ROS_INFO("I heard:[%d]",msg->data);
	std::cout<<"Time:"<<ros::Time::now()<<std::endl;
	ros::Duration(0.19).sleep();
	
		
}


int main(int argc,char **argv)
{
	ros::init(argc,argv,"test_subscriber");
	ros::NodeHandle node;
	ros::Subscriber sub=node.subscribe("num", 5, numcallback);
	
	//subscriber节点数据订阅频率
	while(ros::ok())
	{
		ros::spinOnce();
			
	}
}

3)测试结果:

I send:0   Time:1651218272.909345283
I send:1   Time:1651218273.109455274
I send:2   Time:1651218273.309404860
I send:3   Time:1651218273.509534306
I send:4   Time:1651218273.709532966
I send:5   Time:1651218273.909548481
I send:6   Time:1651218274.109474975
I send:7   Time:1651218274.309555626
I send:8   Time:1651218274.509553033
I send:9   Time:1651218274.709557666
I send:10   Time:1651218274.909548771
I send:11   Time:1651218275.109466288
I send:12   Time:1651218275.309543458
I send:13   Time:1651218275.509558336
I send:14   Time:1651218275.709546293

[ INFO] [1651218273.309568749]: I heard:[2]
Time:1651218273.310396018
[ INFO] [1651218273.509881431]: I heard:[3]
Time:1651218273.509977833
[ INFO] [1651218273.709862283]: I heard:[4]
Time:1651218273.709933310
[ INFO] [1651218273.909996336]: I heard:[5]
Time:1651218273.910075954
[ INFO] [1651218274.109691506]: I heard:[6]
Time:1651218274.109723500
[ INFO] [1651218274.309999529]: I heard:[7]
Time:1651218274.310088643
[ INFO] [1651218274.509997999]: I heard:[8]
Time:1651218274.510090090
[ INFO] [1651218274.709979046]: I heard:[9]
Time:1651218274.710068361
[ INFO] [1651218274.909994015]: I heard:[10]
Time:1651218274.910081014
[ INFO] [1651218275.109661000]: I heard:[11]
Time:1651218275.109693420
[ INFO] [1651218275.309972661]: I heard:[12]
Time:1651218275.310058529
[ INFO] [1651218275.509988978]: I heard:[13]
Time:1651218275.510075436
[ INFO] [1651218275.709965890]: I heard:[14]
Time:1651218275.710054172

测试二:
1)测试条件

  • 数据发布为5Hz
  • 回调函数执行时间为1s

2)测试代码:
同测试一代码,ros::Duration(0.19).sleep();修改为ros::Duration(1).sleep();
3)测试结果:

I send:0   Time:1651218738.926879714
I send:1   Time:1651218739.126986022
I send:2   Time:1651218739.326976590
I send:3   Time:1651218739.527334121
I send:4   Time:1651218739.727314856
I send:5   Time:1651218739.927253191
I send:6   Time:1651218740.127338630
I send:7   Time:1651218740.327119437
I send:8   Time:1651218740.527310282
I send:9   Time:1651218740.727320096
I send:10   Time:1651218740.927319061
I send:11   Time:1651218741.127077520
I send:12   Time:1651218741.327314284
I send:13   Time:1651218741.527084625
I send:14   Time:1651218741.727320603
I send:15   Time:1651218741.927310977
I send:16   Time:1651218742.127316121
I send:17   Time:1651218742.327314879
I send:18   Time:1651218742.527084705
I send:19   Time:1651218742.727317344
I send:20   Time:1651218742.927309793
I send:21   Time:1651218743.127181334
I send:22   Time:1651218743.327346438
I send:23   Time:1651218743.526975577
I send:24   Time:1651218743.727310035
I send:25   Time:1651218743.926981510
I send:26   Time:1651218744.127081416
I send:27   Time:1651218744.327057619
[ INFO] [1651218739.327222543]: I heard:[2]
Time:1651218739.328122020
[ INFO] [1651218740.328379711]: I heard:[3]
Time:1651218740.328493901
[ INFO] [1651218741.328750504]: I heard:[8]
Time:1651218741.328841823
[ INFO] [1651218742.329083810]: I heard:[13]
Time:1651218742.329179718
[ INFO] [1651218743.329413185]: I heard:[18]
Time:1651218743.329527079
[ INFO] [1651218744.329811444]: I heard:[23]
Time:1651218744.329907939
[ INFO] [1651218745.330186229]: I heard:[24]
Time:1651218745.330278204
[ INFO] [1651218746.330738718]: I heard:[25]
Time:1651218746.330852786
[ INFO] [1651218747.331349463]: I heard:[26]
Time:1651218747.331466039
[ INFO] [1651218748.331944456]: I heard:[27]
Time:1651218748.332064212

(二)数据传输时间延迟

1、实验目的

测试同一平台下,节点发布订阅之间的延迟

2、实验结论

1)同一平台上,发布与订阅节点之间存在延迟时间:大概为0.2ms
2)先运行订阅者节点,后运行发布者节点也会存在数据丢失,说明:订阅者与发布者刚建立连接时需要大致0.5s的时间

3、详细实验过程

//测试
publisher节点:5HZ发送数据
subscriber节点:循环无延迟接收
publisher队列长度:1
subscriber队列长度:1

订阅者节点程序:

#include"ros/ros.h"
#include<ros/time.h>
#include"std_msgs/Int8.h"

void numcallback(const std_msgs::Int8::ConstPtr& msg)
{
	ROS_INFO("I heard:[%d]",msg->data);
	std::cout<<"Time:"<<ros::Time::now()<<std::endl;	
}


int main(int argc,char **argv)
{
	ros::init(argc,argv,"test_subscriber");
	ros::NodeHandle node;
	ros::Subscriber sub=node.subscribe("num", 1 , numcallback);
	
	//subscriber节点数据订阅频率
	//ros::Rate loop_rate(1);
	while(ros::ok())
	{
		ros::spinOnce();
	//	loop_rate.sleep();	
	}
}
I send:0   Time:1651205837.321564955
I send:1   Time:1651205837.521682618
I send:2   Time:1651205837.721703296
I send:3   Time:1651205837.921677835
I send:4   Time:1651205838.121677621
I send:5   Time:1651205838.321678661
I send:6   Time:1651205838.521629560
I send:7   Time:1651205838.721682315
I send:8   Time:1651205838.921686516
I send:9   Time:1651205839.121680353
I send:10   Time:1651205839.321681521
I send:11   Time:1651205839.521680580
I send:12   Time:1651205839.721678035
I send:13   Time:1651205839.921678646
I send:14   Time:1651205840.121678181
I send:15   Time:1651205840.321680872
I send:16   Time:1651205840.521679562
I send:17   Time:1651205840.721690948
I send:18   Time:1651205840.921680647
I send:19   Time:1651205841.121677714
I send:20   Time:1651205841.321677517
I send:21   Time:1651205841.521679494
I send:22   Time:1651205841.721680698
I send:23   Time:1651205841.921687221
[ INFO] [1651205837.721951989]: I heard:[2]
Time:1651205837.722832444
[ INFO] [1651205837.921802016]: I heard:[3]
Time:1651205837.921839834
[ INFO] [1651205838.121838956]: I heard:[4]
Time:1651205838.121857432
[ INFO] [1651205838.321856591]: I heard:[5]
Time:1651205838.321874992
[ INFO] [1651205838.521779574]: I heard:[6]
Time:1651205838.521797385
[ INFO] [1651205838.721839387]: I heard:[7]
Time:1651205838.721876415
[ INFO] [1651205838.921855876]: I heard:[8]
Time:1651205838.921891244
[ INFO] [1651205839.121832170]: I heard:[9]
Time:1651205839.121883353
[ INFO] [1651205839.321850889]: I heard:[10]
Time:1651205839.321871226
[ INFO] [1651205839.521831303]: I heard:[11]
Time:1651205839.521865618
[ INFO] [1651205839.721848060]: I heard:[12]
Time:1651205839.721867795
[ INFO] [1651205839.921843985]: I heard:[13]
Time:1651205839.921864013
[ INFO] [1651205840.121845232]: I heard:[14]
Time:1651205840.121864557
[ INFO] [1651205840.321851113]: I heard:[15]
Time:1651205840.321871289
[ INFO] [1651205840.521843675]: I heard:[16]
Time:1651205840.521862573
[ INFO] [1651205840.721814996]: I heard:[17]
Time:1651205840.721868193
[ INFO] [1651205840.921852729]: I heard:[18]
Time:1651205840.921871924
[ INFO] [1651205841.121849661]: I heard:[19]
Time:1651205841.121867539
[ INFO] [1651205841.321848846]: I heard:[20]
Time:1651205841.321866561
[ INFO] [1651205841.521847632]: I heard:[21]
Time:1651205841.521864884
[ INFO] [1651205841.721849989]: I heard:[22]
Time:1651205841.721867769
[ INFO] [1651205841.921830872]: I heard:[23]
Time:1651205841.921847805

三、总结

1、数据发布到缓存到订阅者队列的时间是很短的,大约为0.5ms,如果对数据实时性要求比较高,发布者和订阅者的队列长度均需要设置为1;
2、回调函数处理数据时间过长,subscriber队列数据堆积,并可能导致数据丢失。每次执行回调函数时,会从subscriber队列选取最老的数据。

  • 39
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
串口通信ROS中是非常常见和重要的功能。下面是一些关于ROS串口通信进阶知识: 1. 安装serial包:首先,你需要安装ROS的serial包。在终端中运行以下命令来安装:`sudo apt-get install ros-<your_ros_version>-serial` 2. 创建ROS节点:使用ROS中的串口通信,你需要创建一个ROS节点来处理串口数据。你可以使用C++或者Python编写节点。 3. 打开串口:在ROS节点中,你需要打开串口并进行配置。你可以使用serial包提供的函数来打开和配置串口。例如,在C++中,你可以使用`serial::Serial::open()`函数来打开串口,并使用`serial::Serial::setBaudrate()`函数来设置波特率。 4. 发送和接收数据:一旦打开了串口,你就可以通过串口发送和接收数据了。你可以使用serial包提供的函数来发送和接收字节流。例如,在C++中,你可以使用`serial::Serial::write()`函数来发送数据,并使用`serial::Serial::read()`函数来接收数据。 5. ROS消息和串口数据转换:通常,你可能还需要将串口数据转换为ROS消息,以便在ROS系统中进行处理。你可以根据你的需求创建自定义的ROS消息类型,并编写转换代码将串口数据转换为ROS消息。例如,在Python中,你可以使用`rospy.Publisher`来发布ROS消息。 6. ROS参数配置:为了方便地配置串口参数,你可以使用ROS参数服务器。你可以使用`rosparam`命令或者在launch文件中设置参数。这样,你就可以在运行节点时动态地配置串口参数。 这些是ROS中串口通信的一些进阶知识。希望对你有帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后厂村路蔡徐坤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值