time.sleep() in a node
I have a node where I need to wait for a specific amount of time. Can I just use time.sleep() in python or do I need something else? Thanks, luketheduke
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
I have a node where I need to wait for a specific amount of time. Can I just use time.sleep() in python or do I need something else? Thanks, luketheduke
You should definitely NOT use time.sleep()
The ROS way of doing this is:
rate = rospy.Rate(1) # 1 Hz
# Do stuff, maybe in a while loop
rate.sleep() # Sleeps for 1/rate sec
Or the similar:
rospy.sleep(1) # Sleeps for 1 sec
In both cases, you'll ofc need to import rospy
Here's the API documentation: http://docs.ros.org/jade/api/rospy/ht...
I only want to delay once, not repeatedly. How would I implement that?
@luketheduke, if my answer was correct / helpful, please "mark it as correct" by clicking on the checkmark button to its left. It will help people who read this question in the future. Thanks.
What is the benefit of using rospy.sleep
instead of time.sleep
? Is it just that it is interruptible?
Another option is using
ros::Duration(1.0).sleep() // Sleep for one second
while Rate
requires the sleep duration in Hz (1/s), Duration
requires the sleep time in seconds. Typically, Rate
is used inside loops, while Duration
is the better choice when the sleeping occurs just once.
Asked: 2016-04-30 05:00:21 -0600
Seen: 43,165 times
Last updated: Sep 27 '19