Ask Your Question

Revision history [back]

The ROS time package includes most of what you need to do this. You can get the current time in seconds since the unix epoch using:

seconds = rospy.get_time()

Your object will need to store two properties: the time of the last callback, and if there was a last callback (initially False)

Then at the start of your callback you can check if there was a previous callback and if so subtract the last time from the current time to tell you the time since the previous callback. Then at the end of the callback you can set the boolean to True and store update the previous time. This should look something like this:

self.last_callback_time = 0
self.not_first_callback = False

def callback(...):
    callback_time = rospy.get_time()
    if self.not_first_callback:
        time_since_last_callback = rospy.get_time() - self.last_callback_time

    ...

   self.not_first_callback = True
   self.last_callback_time = callback_time

Hope this helps.