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 from __future__ import division, with_statement
00034 import time
00035 from StringIO import StringIO
00036 
00037 from python_qt_binding.QtCore import qWarning
00038 
00039 import roslib
00040 roslib.load_manifest('rqt_topic')
00041 import rospy
00042 from rostopic import get_topic_class, ROSTopicHz
00043 
00044 
00045 class TopicInfo(ROSTopicHz):
00046 
00047     def __init__(self, topic_name):
00048         super(TopicInfo, self).__init__(100)
00049         self._subscriber = None
00050         self.monitoring = False
00051         self._reset_data()
00052         try:
00053             self.message_class, self._topic_name, _ = get_topic_class(topic_name)
00054         except Exception:
00055             self._topic_name = None
00056             qWarning('TopicInfo.__init__(): can not get topic info for "%s"' % topic_name)
00057             return
00058 
00059     def _reset_data(self):
00060         self.last_message = None
00061         self.times = []
00062         self.timestamps = []
00063         self.sizes = []
00064 
00065     def toggle_monitoring(self):
00066         if self.monitoring:
00067             self.stop_monitoring()
00068         else:
00069             self.start_monitoring()
00070 
00071     def start_monitoring(self):
00072         self.monitoring = True
00073         if self._topic_name is not None:
00074             
00075             self._subscriber = rospy.Subscriber(self._topic_name, self.message_class, self.message_callback)
00076 
00077     def stop_monitoring(self):
00078         self.monitoring = False
00079         self._reset_data()
00080         if self._subscriber is not None:
00081             self._subscriber.unregister()
00082 
00083     def message_callback(self, message):
00084         ROSTopicHz.callback_hz(self, message)
00085         with self.lock:
00086             self.timestamps.append(time.time())
00087 
00088             
00089             
00090             
00091             buff = StringIO()
00092             message.serialize(buff)
00093             self.sizes.append(buff.len)
00094 
00095             if len(self.timestamps) > self.window_size - 1:
00096                 self.timestamps.pop(0)
00097                 self.sizes.pop(0)
00098             assert(len(self.timestamps) == len(self.sizes))
00099 
00100             self.last_message = message
00101 
00102     def get_bw(self):
00103         if len(self.timestamps) < 2:
00104             return None, None, None, None
00105         with self.lock:
00106             total = sum(self.sizes)
00107             bytes_per_s = total / (time.time() - self.timestamps[0])
00108             mean_size = total / len(self.timestamps)
00109             max_size = max(self.sizes)
00110             min_size = min(self.sizes)
00111             return bytes_per_s, mean_size, min_size, max_size
00112 
00113     def get_hz(self):
00114         if not self.times:
00115             return None, None, None, None
00116         with self.lock:
00117             n = len(self.times)
00118             mean = sum(self.times) / n
00119             rate = 1. / mean if mean > 0. else 0
00120             min_delta = min(self.times)
00121             max_delta = max(self.times)
00122         return rate, mean, min_delta, max_delta