当前位置:网站首页>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
边栏推荐
- ClickHouse-表引擎
- Clickhouse SQL operation
- Metaprogramming, proxy and reflection
- 958. Complete binary tree test
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- Self use learning notes - connected and non connected access to database
- Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
- Tdan over half
- 超分之TDAN
- 440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
猜你喜欢
Collection of common SQL statements
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
Using quartz under. Net core - [1] quick start
ASP. Net core JWT certification
2.Electron之HelloWorld
Double pointer advanced -- leetcode title -- container with the most water
C# Task. Delay and thread The difference between sleep
If you start from zero according to the frame
Future 用法详解
Why do some people say SCM is simple and I have to learn it so hard?
随机推荐
Self use learning notes - connectingstring configuration
stm32入门开发板选野火还是正点原子呢?
[difference between Oracle and MySQL]
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
[batch change MySQL table and corresponding codes of fields in the table]
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Net standard
[related to zhengheyuan cutting tools]
41. 缺失的第一个正数
386. 字典序排数(中等)-迭代-全排列
Deep understanding of control inversion and dependency injection
In embedded system, must the program code in flash be moved to ram to run?
Learning record of uni app dark horse yougou project (Part 2)
索引:手把手教你索引从零基础到精通使用
01 - get to know the advantages of sketch sketch
1217_使用SCons生成目标文件
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
ClickHouse-表引擎
Construction of functions in C language programming
Understanding of RPC core concepts