# How do I reset a rospy Timer?

How do I reset a rospy Timer? I want to have a Python node that sends out a message like cmd_vel as a step function to test a controller, but I also want to use dynamic reconfigure to be able to change the dt for when the step function should increase. For example, I would like to do one run where my velocities increase by the step amount every 1 second, then do a second run where my velocities increase by the step amount every 5 seconds. I want to use the exact same callback function for modifying the cmd_vel output message but I want that callback function to be invoked at different times. The code I have so far that does not work is

def reset_timers(self):
try:
del self.output
except AttributeError:
self.output = rospy.Timer(rospy.Duration(self.output_dt), self.output_callback)


Note that the function reset_timers is called when the Python class is initialized and whenever self.output_dt is modified by dynamic reconfigure.

The behavior I see when using this code is that the timers are additive. Using the previous example, if I change dt from 1 second to 5 seconds, then the timer goes to the callback function every 1 second and every 5 seconds. If I modify self.output_dt often enough then each one of those times is used to invoke the callback and I end up outputting a new message with an increased velocity very rapidly.

edit retag close merge delete

Sort by » oldest newest most voted

To stop the Timer from firing, call shutdown().

I did not check if it works, so sorry if I'm wrong.

more

That was exactly the right answer and my output looks the way that I want it to now. Thanks @BennyRe! Should this be part of the documentation at: http://docs.ros.org/hydro/api/rospy/h...

( 2013-08-12 03:57:17 -0500 )edit