Ask Your Question
0

How can I measure the time between callbacks?

asked 2019-01-08 04:06:44 -0500

stevemartin gravatar image

I need to measure the time between each callback in order to reset the wheel encoders back to 0. So if the time difference between callbacks is greater than 200 ms or there was no callback for 200 ms I will count back the encoders to 0.

Here is my sample encoder tick counting code:

#!/usr/bin/env python

from __future__ import print_function
import time
#import ASUS.GPIO as GPIO
import rospy
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path
from std_msgs.msg import Int16


class EncoderNodeLeft:

    def __init__(self,encoder):
        self.tick = 0
        self.encoder = encoder
        self.pub = rospy.Publisher("lwheel", Int16, queue_size=1000)
        self.rate = rospy.Rate(10)

    def tick_callback(self,channel):
        self.tick += 1
        if self.tick > 32766:
            self.tick = 0
        self.pub.publish(self.tick)

    def run(self):
        GPIO.add_event_detect(self.encoder, GPIO.RISING, callback=self.tick_callback)
        rate = rospy.Rate(10) # 10hz
        while not rospy.is_shutdown():
            self.rate.sleep()

if __name__ == "__main__":
    try:
        rospy.init_node("encoder_node_left")
        encoder = 224
        encoder_node_left = EncoderNodeLeft(encoder)
        encoder_node_left.run()
    except rospy.ROSInterruptException:
        rospy.loginfo("There was an exception in encoder_node_left")
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-01-08 04:50:36 -0500

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.

edit flag offensive delete link more

Comments

But where should I put that in a code? As if I will put that in tick_callback it is going to miss a lot of ticks as the code above will take anywhere from 100ms and above.

stevemartin gravatar image stevemartin  ( 2019-01-08 05:42:39 -0500 )edit

Yes this should be in your tick_callback. Why do you say this code will take more than 100ms to run? I would expect the code here to take less than a micro second, it's just a very simple OS call and some arithmetic.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-01-08 07:09:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-08 04:06:44 -0500

Seen: 1,079 times

Last updated: Jan 08 '19