Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 import roslib; roslib.load_manifest('rospy')
00037 import rospy, threading, time
00038
00039 class TimerEvent(object):
00040 def __init__(self, last_expected, last_real, current_expected, current_real, last_duration):
00041 self.last_expected = last_expected
00042 self.last_real = last_real
00043 self.current_expected = current_expected
00044 self.current_real = current_real
00045 self.last_duration = last_duration
00046
00047 class Timer(threading.Thread):
00048 def __init__(self, duration, callback, oneshot=False):
00049 threading.Thread.__init__(self)
00050 self._duration = duration
00051 self._callback = callback
00052 self._oneshot = oneshot
00053 self.setDaemon(True)
00054 self.start()
00055
00056 def run(self):
00057 r = rospy.Rate(1.0 / self._duration.to_sec())
00058 current_expected = rospy.Time.now() + self._duration
00059 last_expected, last_real, last_duration = None, None, rospy.Duration(0)
00060 while not rospy.is_shutdown():
00061 r.sleep()
00062 current_real = rospy.Time.now()
00063
00064 start = rospy.Time.now()
00065 self._callback(TimerEvent(last_expected, last_real, current_expected, current_real, last_duration))
00066 if self._oneshot:
00067 break
00068
00069 last_duration = rospy.Time.now() - start
00070 last_expected, last_real = current_expected, current_real
00071 current_expected += self._duration
00072
00073 if __name__ == '__main__':
00074 import time
00075 rospy.init_node('test')
00076 def callback(event):
00077 print 'last_expected: ', event.last_expected
00078 print 'last_real: ', event.last_real
00079 print 'current_expected: ', event.current_expected
00080 print 'current_real: ', event.current_real
00081 print 'current_error: ', (event.current_real - event.current_expected).to_sec()
00082 print 'profile.last_duration:', event.last_duration.to_sec()
00083 if event.last_real:
00084 print 'last_error: ', (event.last_real - event.last_expected).to_sec(), 'secs'
00085 print
00086
00087 Timer(rospy.Duration(0.05), callback)
00088 time.sleep(15)