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:
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.
Originally posted by Thomas D on ROS Answers with karma: 4347 on 2013-08-08
Post score: 4