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

Explanation of rospy.Rate()

asked 2017-06-26 18:14:20 -0600

michaelszer gravatar image

updated 2018-06-05 07:38:47 -0600

felix k gravatar image

I am looking for an explanation of rospy.rate(), I have looked it in the documentation and it says the following:

Convenience class for sleeping in a loop at a specified rate.

Can someone clarify this? What does it mean to sleep in a loop?

Documentation: http://docs.ros.org/jade/api/rospy/ht...

edit retag flag offensive close merge delete

Comments

What's the link for the documentation that you're reading?

jayess gravatar image jayess  ( 2017-06-26 18:40:59 -0600 )edit

I have added it to the question.

michaelszer gravatar image michaelszer  ( 2017-06-26 18:48:32 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
12

answered 2017-06-26 19:17:54 -0600

jayess gravatar image

updated 2020-03-31 06:01:40 -0600

From rospy Time wiki entry,

rospy.Rate(hz)

rospy provides a rospy.Rate convenience class which makes a best effort at maintaining a particular rate for a loop. For example:

r = rospy.Rate(10) # 10hz 
while not rospy.is_shutdown():
    pub.publish("hello")
    r.sleep()

In the above example, the Rate instance will attempt to keep the loop at 10hz by accounting for the time used by any operations during the loop. Rate.sleep() can throw a rospy.ROSInterruptException if the sleep is interrupted by shutdown.

Basically, it allows your loops to run at (nearly) the exact rate (in Hz) that you specify. It's a pretty standard way of looping with ROS. Check out the source code that's linked to in the documentation that you were looking at:

 87 -    def sleep(self): 
 88          """ 
 89          Attempt sleep at the specified rate. sleep() takes into 
 90          account the time elapsed since the last successful 
 91          sleep(). 
 92           
 93          @raise ROSInterruptException: if ROS shutdown occurs before 
 94          sleep completes 
 95          @raise ROSTimeMovedBackwardsException: if ROS time is set 
 96          backwards 
 97          """ 
 98          curr_time = rospy.rostime.get_rostime() 
 99          sleep(self._remaining(curr_time)) 
100          self.last_time = self.last_time + self.sleep_dur 
101   
102          # detect time jumping forwards, as well as loops that are 
103          # inherently too slow 
104          if curr_time - self.last_time > self.sleep_dur * 2: 
105              self.last_time = curr_time

Go through the rospy tutorials if you haven't already. From the writing a publisher and subscriber tutorial:

rate = rospy.Rate(10) # 10hz

This line creates a Rate object rate. With the help of its method sleep(), it offers a convenient way for looping at the desired rate. With its argument of 10, we should expect to go through the loop 10 times per second (as long as our processing time does not exceed 1/10th of a second!)

edit flag offensive delete link more

Comments

good answer.

img5d gravatar image img5d  ( 2019-07-24 19:34:07 -0600 )edit

thanks for the answer. but what happens if the process timing exceeds 1/10th of a second?

lxg gravatar image lxg  ( 2020-08-06 02:43:25 -0600 )edit

That really shouldn't change anything as long as your computer can handle it

jayess gravatar image jayess  ( 2020-08-06 23:35:51 -0600 )edit

Thanks jayes. so is it safe to conclude that all the code within the while loop above will execute 10 times a second as long as the computer can handle it? regardless of the code within the while loop?

lxg gravatar image lxg  ( 2020-08-07 02:07:16 -0600 )edit

I wouldn't want to make any sweeping generalizations, but I'd say generally yes. If you examine the code above, basically what's happening is that a call to r.sleep() sleeps for the amount of time needed to keep the loop running at the given rate as best as it can.

jayess gravatar image jayess  ( 2020-08-07 03:28:44 -0600 )edit

alright, thank you. Does r.sleep() trying to maintain the given rate have anything to do with python scripts running even after Ctr + C is pressed. I mean ROS definitely stops but the script keeps executing especially if there are loops within the while loop. especially data read from a buffer. Could it be related or is it an entirely different issue?

lxg gravatar image lxg  ( 2020-08-07 05:11:32 -0600 )edit

I'd say probably not, but if this is an issue that you're having then you should post a question about it

jayess gravatar image jayess  ( 2020-08-08 16:42:56 -0600 )edit
1

thanks. i did. and it got answered.

lxg gravatar image lxg  ( 2020-08-09 09:27:49 -0600 )edit
0

answered 2022-10-09 09:31:36 -0600

Fahima Mokhtari gravatar image

From what I understood from this edx course, it is the frequency at which a given topic is published.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2017-06-26 18:14:20 -0600

Seen: 44,216 times

Last updated: Mar 31 '20