How can I measure the time between callbacks?
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")