本文介绍了ROS中spin和rate.sleep的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ROS 的新手,正在尝试了解这个强大的工具.我对 spinrate.sleep 函数感到困惑.谁能帮助我了解这两个功能之间的区别以及何时使用每个功能?

I am new to ROS and trying to understand this powerful tool. I am confused between the spin and rate.sleep functions. Could anyone help me with the difference between these two functions and when to use each one?

推荐答案

ros::spin()ros::spinOnce() 负责处理通信事件,例如到达的消息.如果您订阅消息、服务或操作,则必须调用 spin 来处理事件.

ros::spin() and ros::spinOnce() are responsible to handle communication events, e.g. arriving messages. If you are subscribing to messages, services, or actions, you must call spin to process the events.

虽然 ros::spinOnce() 处理事件并立即返回,ros::spin() 会阻塞直到 ROS 调用关闭.因此,如果需要,ros::spinOnce() 可以为您提供更多控制权.更多关于这个问题的信息:回调和旋转.

While ros::spinOnce() handles the events and returns immediately, ros::spin() blocks until ROS invokes a shutdown. So, ros::spinOnce() gives you more control if needed. More on that matter here: Callbacks and Spinning.

rate.sleep() 另一方面只是一个线程睡眠,持续时间由频率定义.这是一个例子

rate.sleep() on the other hand is merely a thread sleep with a duration defined by a frequency. Here is an example

ros::Rate rate(24.);
while(ros::ok())
{
    rate.sleep();
}

这个循环每秒执行 24 次或更少,取决于你在循环中做什么.ros::Rate 对象跟踪自上次执行 rate.sleep() 以来的时间,并在正确的时间内休眠以达到 24 Hz 标记.参见 ros::Rate::sleep() API.

This loop will be executed 24 times a second or less, depends what you do inside the loop. A ros::Rate object keeps track of how much time since the last rate.sleep() was executed and sleep for the correct amount of time to hit the 24 Hz mark. See ros::Rate::sleep() API.

时域中的等效方式是ros::Duration::sleep()

The equivalent way in the time domain is ros::Duration::sleep()

ros::Duration duration(1./24.);
while(ros::ok())
{
    duration.sleep();
}

您使用哪个只是为了方便.

Which one you use is just a matter of convenience.

这篇关于ROS中spin和rate.sleep的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 08:14