(1)首先,发布节点把消息发布,消息进入Publisher的消息队列,同时通知订阅了该话题消息的Subscriber来取消息。
(2)其次,Subscriber来Publisher的消息队列里取消息,但取走的也是最老的消息,因为毕竟这是先入先出的队列。
(3)最后,被取走的消息存放入了Subscriber的消息队列中,等待被Callback执行。如果Callback执行很慢,消息越堆越多,最老的消息会逐渐被顶替。
Publisher和Subscriber设置消息队列,都可以从两点进行分析:
(1)为什么需要设置Publisher的消息队列?
(2)为什么要设置Subscriber消息队列?
(1)队列长度queue_size参数选择
参考:http://wiki.ros.org/rospy/Overview/Publishers%20and%20Subscribers#Choosing_a_good_queue_size
(2)ros::spinOnce机制
实验过程比较繁杂,可以直接看第三部分:总结
1、实验目的
测试什么状态下队列会溢出
2、实验结论
结论一:
当回调函数处理时长小于数据发布的时间间隔,数据可以完整地传输。
结论二:
subscriber队列可以对接收数据进行缓存,spinonce每次进入回调函数,会取出Subscriber队列中最新的数据
3、详细实验过程
测试一:
1)条件设置
2)测试代码:
#include
#include
#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("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:"<
#include"ros/ros.h"
#include
#include
#include"std_msgs/Int8.h"
void numcallback(const std_msgs::Int8::ConstPtr& msg)
{
ROS_INFO("I heard:[%d]",msg->data);
std::cout<<"Time:"<
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)测试条件
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
#include"std_msgs/Int8.h"
void numcallback(const std_msgs::Int8::ConstPtr& msg)
{
ROS_INFO("I heard:[%d]",msg->data);
std::cout<<"Time:"<
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队列选取最老的数据。