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


jsk_topic_tools
Author(s): Kei Okada , Yusuke Furuta
autogenerated on Fri Sep 8 2017 03:38:56