Updating Frequency of Rospy Timer Dynamically

asked 2020-06-09 14:52:10 -0600

swiz23 gravatar image

I am currently using ROS Kinetic on Ubuntu 16.04, and would like to create a ROS Timer in Python to fire a specific callback function at a specific rate. However, I'd like the rate to be configurable such that a user could change it during run time (potentially an infinite number of times). In roscpp, I was able to get this done using a oneshot timer, and the 'setPeriod', 'start', and 'stop' functions. However, rospy seems to only have a 'run' and 'shutdown' function, and I'm not sure if they can be even used for my application in conjunction with the 'oneshot' option in rospy.

That said, I was able to get this type of functionality to work in rospy for a different application by just creating an infinite loop in the main function, and changing the 'rate' object frequency that controls how long it sleeps dynamically without any issues. However, for my current application, I will not have a main function (as it will essentially be a Python Module), so I'm not sure how to go about implementing this...

Thanks in advance!

edit retag flag offensive close merge delete


I figured out a different way to make my application work, so no pressure to answer this. Essentially, I just put a loop inside the function of interest and have an argument that lets a user set the desired rate that the loop should run at. That way, every time the function is called, you can change the rate to be something different.

swiz23 gravatar image swiz23  ( 2020-06-09 17:42:16 -0600 )edit