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 import os
00035 import sys
00036 import unittest
00037 import time
00038
00039 import rosunit
00040
00041 from threading import Thread
00042
00043 class TestRospyTimerOnline(unittest.TestCase):
00044
00045 def __init__(self, *args):
00046 unittest.TestCase.__init__(self, *args)
00047 import rospy
00048 rospy.init_node('test_rospy_timer_online')
00049 self.timer_callbacks = 0
00050 self.timer_event = None
00051
00052 def test_sleep(self):
00053 import rospy
00054 import time
00055 t = time.time()
00056 rospy.sleep(0.1)
00057 dur = time.time() - t
00058
00059
00060
00061 self.assert_(abs(dur - 0.1) < 0.03, dur)
00062
00063 t = time.time()
00064 rospy.sleep(rospy.Duration.from_sec(0.1))
00065 dur = time.time() - t
00066
00067 self.assert_(abs(dur - 0.1) < 0.03, dur)
00068
00069
00070 t = time.time()
00071 rospy.sleep(rospy.Duration.from_sec(-10.))
00072 dur = time.time() - t
00073
00074 self.assert_(abs(dur) < 0.1, dur)
00075
00076 def test_Rate(self):
00077 import rospy
00078 import time
00079 t = time.time()
00080 count = 0
00081 r = rospy.Rate(10)
00082 for x in xrange(10):
00083 r.sleep()
00084 dur = time.time() - t
00085
00086 self.assert_(abs(dur - 1.0) < 0.5, dur)
00087
00088 def _Timer_callback(self, event):
00089 self.timer_callbacks += 1
00090 self.timer_event = event
00091
00092 def callback(event):
00093 print 'last_expected: ', event.last_expected
00094 print 'last_real: ', event.last_real
00095 print 'current_expected: ', event.current_expected
00096 print 'current_real: ', event.current_real
00097 print 'current_error: ', (event.current_real - event.current_expected).to_sec()
00098 print 'profile.last_duration:', event.last_duration
00099 if event.last_real:
00100 print 'last_error: ', (event.last_real - event.last_expected).to_sec(), 'secs'
00101
00102 def test_Timer(self):
00103 import rospy
00104 timer = rospy.Timer(rospy.Duration(1), self._Timer_callback)
00105 time.sleep(10)
00106 timer.shutdown()
00107
00108
00109 self.assert_(abs(self.timer_callbacks - 10) < 2)
00110
00111
00112 ev = self.timer_event
00113 self.assert_(ev is not None)
00114 self.assert_(abs((ev.current_real - ev.current_expected).to_sec()) < 2.)
00115
00116 if __name__ == '__main__':
00117 rosunit.unitrun('test_rospy', sys.argv[0], TestRospyTimerOnline, coverage_packages=['rospy.timer'])