目录
写在前面
ROS2有spin_some, spin,而ROS有spinOnce,spin,他们有什么区别和联系呢?
如果你学过ROS,那么只用看第一部分。
如果你直接学ROS2,也建议按顺序看,加深理解。
1. ROS里的spin和spinOnce
如果你刚接触ROS,很可能看过这份很详细的ROS官方教程,它提到spin和spinOnce的基本用法。但是,我估计,极大可能,看完你还是不明白两者有什么区别,又该如何去用。
ROS/Tutorials/WritingPublisherSubscriber(c++) - ROS Wikihttp://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c++%29别着急,我们把相关内容提取出来,品一品。
ros::spinOnce()
Calling ros::spinOnce() here is not necessary for this simple program, because we are not receiving any callbacks. However, if you were to add a subscription into this application, and did not have ros::spinOnce() here, your callbacks would never get called. So, add it for good measure.
上面这段话,摘自publisher的节点代码注释。
大意是,作为一个纯粹的、简单的publisher程序,不需要使用spinOnce(),因为它不执行任何回调。但是,如果想在这个程序里增加订阅功能,而不使用spinOnce(), 回调将不会产生。
ros::spin()
ros::spin() enters a loop, calling message callbacks as fast as possible. Don't worry though, if there's nothing for it to do it won't use much CPU. ros::spin() will exit once ros::ok() returns false, which means ros::shutdown() has been called, eit