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