0
$\begingroup$

Rosanswers logo

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

$\endgroup$

1 Answer 1

1
$\begingroup$

Rosanswers logo

In http://www.ros.org/wiki/rospy/Overview/Time it says

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

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


Originally posted by BennyRe with karma: 2949 on 2013-08-10

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Thomas D on 2013-08-12:
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/html/rospy.timer-module.html?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.