test_hz_measure.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import os
00003 import sys
00004 
00005 import unittest
00006 
00007 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", 
00008                                                 "scripts")))
00009 from topic_compare import ROSTopicCompare
00010 
00011 import rospy
00012 import time
00013 
00014 import numpy as np
00015 
00016 try:
00017     from std_msgs.msg import Float32
00018 except:
00019     import roslib; roslib.load_manifest("jsk_topic_tools")
00020     from std_msgs.msg import Float32
00021 
00022 def eps_equal(a, b, err=0.001):
00023     return abs(a - b) < err
00024 
00025 hz_msg = None
00026 
00027 def topic_cb(msg):
00028     global hz_msg
00029     hz_msg = msg
00030 
00031 
00032 class TestHzMeasure(unittest.TestCase):
00033     def test_hz(self):
00034         global hz_msg
00035         while hz_msg == None:
00036             rospy.loginfo('wait for msg...')
00037             if not rospy.is_shutdown():
00038                 rospy.sleep(1.0)          #wait 1 sec
00039         # should be 30Hz
00040         msgs = []
00041         while len(msgs) < 30:
00042             msgs.append(hz_msg.data)
00043             rospy.loginfo('hz of msg %s'%hz_msg.data)
00044             rospy.sleep(0.1)
00045         hz = np.median(msgs)
00046         rospy.loginfo('average hz of msg %s'%np.mean(msgs))
00047         rospy.loginfo('median  hz of msg %s'%hz)
00048         self.assertTrue(eps_equal(hz,
00049                                   30,
00050                                   1))
00051 
00052 if __name__ == "__main__":
00053     import rostest
00054     rospy.init_node("test_hz_measure")
00055     s = rospy.Subscriber("/hz/output", Float32, topic_cb)
00056     rostest.rosrun("jsk_topic_tools", "test_hz_measure", TestHzMeasure)


jsk_topic_tools
Author(s): Kei Okada , Yusuke Furuta
autogenerated on Sun Jan 25 2015 00:30:41