Ask Your Question
0

time.sleep() in a node

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

luketheduke 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
1

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

spmaniato gravatar image

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

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 imageluketheduke ( 2016-04-30 16:21:39 -0500 )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 imagespmaniato ( 2016-04-30 16:52:02 -0500 )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 imagespmaniato ( 2016-05-01 14:10:57 -0500 )edit
0

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 10,307 times

Last updated: Sep 27