网站pr查询,老年大学网站开发,百度做网站要多长时间,wordpress安装好后怎么使用文章目录 一、话题通信机制解析1、话题通信机制简介2、消息队列分析3、使用技巧 二、实验验证#xff08;一#xff09;subscriber队列长度对数据传输影响#xff08;二#xff09;数据传输时间延迟 三、总结 一、话题通信机制解析 1、话题通信机制简介
#xff08;1一subscriber队列长度对数据传输影响二数据传输时间延迟 三、总结 一、话题通信机制解析 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_size0即ROS消息队列为0时表示为无限队列长度内存使用量可以无限增长因此不推荐使用。当两个queue_size1时那么系统不会缓存数据自然处理的就是最新的消息。当queue_size设置为10或者更多时候用户更不容易错过发布的消息适用于与人交互的用户界面的数据展示。
2ros::spinOnce机制
一次 ros::spinOnce()会获取完所有Subscriber队列中的消息
二、实验验证
实验过程比较繁杂可以直接看第三部分总结
一subscriber队列长度对数据传输影响
1、实验目的
测试什么状态下队列会溢出2、实验结论 结论一 当回调函数处理时长小于数据发布的时间间隔数据可以完整地传输。 结论二 subscriber队列可以对接收数据进行缓存spinonce每次进入回调函数会取出Subscriber队列中最新的数据 3、详细实验过程 测试一 1条件设置
数据发布为5Hz回调函数执行时间为0.19s
2测试代码
#includeros/ros.h
#includeros/time.h
#includestd_msgs/Int8.hint main(int argc,char **argv)
{ros::init(argc,argv,test_publisher);ros::NodeHandle node;//控制队列长度ros::Publisher num_pub node.advertisestd_msgs::Int8(num,1);//控制Publisher发送频率ros::Rate loop_rate(5);int count0;while(ros::ok()){std_msgs::Int8 msg;msg.datacount;num_pub.publish(msg);std::coutI send:count Time:ros::Time::now()std::endl;count;ros::spinOnce();loop_rate.sleep();}}#includeros/ros.h
#includeros/time.h
#include ros/duration.h
#includestd_msgs/Int8.hvoid numcallback(const std_msgs::Int8::ConstPtr msg)
{ROS_INFO(I heard:[%d],msg-data);std::coutTime: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 subnode.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订阅者节点程序
#includeros/ros.h
#includeros/time.h
#includestd_msgs/Int8.hvoid numcallback(const std_msgs::Int8::ConstPtr msg)
{ROS_INFO(I heard:[%d],msg-data);std::coutTime:ros::Time::now()std::endl;
}int main(int argc,char **argv)
{ros::init(argc,argv,test_subscriber);ros::NodeHandle node;ros::Subscriber subnode.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队列选取最老的数据。