Ask Your Question

How do I reset a rospy Timer?

asked 2013-08-08 14:04:13 -0600

Thomas D gravatar image

updated 2014-01-28 17:17:37 -0600

ngrennan gravatar image

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):
        del self.output
    except AttributeError:
        rospy.loginfo("self.output exception caught")
    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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2013-08-10 21:50:14 -0600

BennyRe gravatar image

In it says

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

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

edit flag offensive delete link 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:

Thomas D gravatar image Thomas D  ( 2013-08-12 03:57:17 -0600 )edit

Your Answer


Add Answer

Question Tools



Asked: 2013-08-08 14:04:13 -0600

Seen: 4,524 times

Last updated: Aug 10 '13