当前位置:网站首页>ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
2022-04-23 17:36:00 【后厂村路蔡徐坤】
(一)ros::ok()
用于检查系统状态,适用于在while循环中判断状态
语法:
while(ros::ok())
{
//...
}
ros::ok()在以下几种情况下会返回false:
1、按下Ctrl-C时。
2、我们被一个同名同姓的节点从网络中踢出。
3、ros::shutdown()被应用程序的另一部分调用。
4、所有的ros::NodeHandles都被销毁了。
5、一旦ros::ok()返回false,所有的ROS调用都会失败
(二)ros::Rate
用于设置循环频率,适用于Publisher程序无限循环
ros::Rate对象可以允许你指定自循环的频率
ros::Rate loop_rate(10);//10Hz
//...
loop_rate.sleep();
(三)ros::spin()和ros::spinOnce()
用于处理ROS的回调函数,适用于Subscriber处理回调数据。
两者区别在于ros::spin()调用后不会再返回,而ros::spinOnce()在调用后还可以继续执行之后的程序。
ros::spin()用于非循环场景,ros::spinOnce()用于有循环的场景。
什么时候用ros::spin()和ros::spinOnce()呢,如果仅仅只是响应topic,就用ros::spin()。当程序中除了响应回调函数还有其他重复性工作的时候,那就在循环中做那些工作,然后调用ros::spinOnce()。
1、ros::spin()
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}
2、ros::spinOnce()
这句话的意思是监听反馈函数(callback)。只能监听反馈,不能循环。所以当你需要监听一下的时候,就调用一下这个函数。
这个函数比较灵活,尤其是我想控制接收速度的时候。配合ros::ok()效果极佳。
ros::Rate loop_rate(10);
while(ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
版权声明
本文为[后厂村路蔡徐坤]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zeye5731/article/details/124314347
边栏推荐
- For the space occupation of the software, please refer to the installation directory
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- Baidu Map Case - modify map style
- Generating access keys using JSON webtoken
- 双指针进阶--leetcode题目--盛最多水的容器
- 圆环回原点问题-字节跳动高频题
- Excel quickly and automatically fills the contents of a row on a blank cell
- 剑指 Offer 03. 数组中重复的数字
- 剑指 Offer 22. 链表中倒数第k个节点-快慢指针
- Webapi + form form upload file
猜你喜欢
Use of todesk remote control software
01 - get to know the advantages of sketch sketch
In embedded system, must the program code in flash be moved to ram to run?
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
JVM类加载机制
Using quartz under. Net core - [1] quick start
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Halo 开源项目学习(二):实体类与数据表
超分之TDAN
Signalr can actively send data from the server to the client
随机推荐
Halo 开源项目学习(二):实体类与数据表
HCIP第五次实验
開期貨,開戶雲安全還是相信期貨公司的軟件?
RPC核心概念理解
394. String decoding - auxiliary stack
470. 用 Rand7() 实现 Rand10()
古代埃及希腊,数学用的什么进制
41. 缺失的第一个正数
JVM class loading mechanism
[simple understanding of database]
198. 打家劫舍-动态规划
[C] thoroughly understand the deep copy
Seven cattle upload pictures (foreground JS + background C API get token)
[ES6] promise related (event loop, macro / micro task, promise, await / await)
[batch change MySQL table and corresponding codes of fields in the table]
Net standard
给 el-dialog 增加拖拽功能
Excel quickly and automatically fills the contents of a row on a blank cell
C# Task. Delay and thread The difference between sleep
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers