ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
4

time.sleep() in a node

asked 2016-04-30 05:00:21 -0600

luketheduke gravatar image

updated 2022-02-22 11:23:15 -0600

lucasw gravatar image

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
7

answered 2016-04-30 11:45:42 -0600

spmaniato gravatar image

updated 2016-04-30 16:52:36 -0600

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...

edit flag offensive delete link more

Comments

I only want to delay once, not repeatedly. How would I implement that?

luketheduke gravatar image luketheduke  ( 2016-04-30 16:21:39 -0600 )edit

It's the second code snippet above: rospy.sleep(1) # Sleeps for 1 sec Adapt it to how many seconds you want to delay.

spmaniato gravatar image spmaniato  ( 2016-04-30 16:52:02 -0600 )edit

@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.

spmaniato gravatar image spmaniato  ( 2016-05-01 14:10:57 -0600 )edit
3

What is the benefit of using rospy.sleep instead of time.sleep? Is it just that it is interruptible?

Rufus gravatar image Rufus  ( 2021-05-30 20:39:37 -0600 )edit
1

+1 on @Rufus question comment. It's a very strong NOT, without any explanation.

marko525 gravatar image marko525  ( 2022-04-29 14:22:28 -0600 )edit
2

answered 2019-09-27 04:39:37 -0600

Tones gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-30 05:00:21 -0600

Seen: 40,884 times

Last updated: Sep 27 '19