ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Ignoring the "rospy.sleep(..)
gives back control to ROS" comment, there is a general answer to your question:
does it make sense, and is it allowed, to call rospy.sleep() or rospy.spin() or rospy.spinOnce() from inside a callback?
No, I cannot think of any reason to call rospy.spin()
in a callback. It would completely block the execution of the callback, and that part of your node would stop working (ie: no further messages processed, no other timer events handled, etc).
You could theoretically call rospy.sleep(..)
in a callback. It would just introduce a delay, similar to what time.sleep(..)
would do (but taking use_sim_time
into account). Would that be useful? Depends on the rest of the code in the callback.
Would I recommend doing it? Probably not. If, in an event based system, you need to introduce an explicit sleep(..)
somewhere, it's likely you should be using callbacks more/differently (ie: instead of sleep(..)
ing to wait for something, register your interest in whatever it is you're waiting for and register a callback to be notified whenever what you're interested in happens).
(Note that rospy.spinOnce()
does not exist)
Are you allowed to call those functions? Of course. In the sense that no one will prevent you from calling them, nor will rospy
"complain".