topic_info.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from __future__ import division, with_statement
34 try:
35  from cStringIO import StringIO as BufferType
36 except ImportError:
37  from io import BytesIO as BufferType
38 
39 from python_qt_binding.QtCore import qWarning
40 
41 import roslib
42 import rospy
43 from rostopic import ROSTopicHz
44 
45 
46 class TopicInfo(ROSTopicHz):
47 
48  def __init__(self, topic_name, topic_type):
49  super(TopicInfo, self).__init__(100)
50  self._topic_name = topic_name
51  self.error = None
52  self._subscriber = None
53  self.monitoring = False
54  self._reset_data()
55  self.message_class = None
56  try:
57  self.message_class = roslib.message.get_message_class(topic_type)
58  except Exception as e:
59  self.message_class = None
60  qWarning('TopicInfo.__init__(): %s' % (e))
61 
62  if self.message_class is None:
63  self.error = 'can not get message class for type "%s"' % topic_type
64  qWarning('TopicInfo.__init__(): topic "%s": %s' % (topic_name, self.error))
65 
66  def _reset_data(self):
67  self.last_message = None
68  self.times = []
69  self.timestamps = []
70  self.sizes = []
71 
72  def toggle_monitoring(self):
73  if self.monitoring:
74  self.stop_monitoring()
75  else:
76  self.start_monitoring()
77 
78  def start_monitoring(self):
79  if self.message_class is not None:
80  self.monitoring = True
81  # FIXME: subscribing to class AnyMsg breaks other subscribers on same node
82  self._subscriber = rospy.Subscriber(
84 
85  def stop_monitoring(self):
86  self.monitoring = False
87  self._reset_data()
88  if self._subscriber is not None:
89  self._subscriber.unregister()
90 
91  def message_callback(self, message):
92  ROSTopicHz.callback_hz(self, message)
93  with self.lock:
94  self.timestamps.append(rospy.get_time())
95 
96  # FIXME: this only works for message of class AnyMsg
97  # self.sizes.append(len(message._buff))
98  # time consuming workaround...
99  buff = BufferType()
100  message.serialize(buff)
101  self.sizes.append(len(buff.getvalue()))
102 
103  if len(self.timestamps) > self.window_size - 1:
104  self.timestamps.pop(0)
105  self.sizes.pop(0)
106  assert(len(self.timestamps) == len(self.sizes))
107 
108  self.last_message = message
109 
110  def get_bw(self):
111  if len(self.timestamps) < 2:
112  return None, None, None, None
113  current_time = rospy.get_time()
114  if current_time <= self.timestamps[0]:
115  return None, None, None, None
116 
117  with self.lock:
118  total = sum(self.sizes)
119  bytes_per_s = total / (current_time - self.timestamps[0])
120  mean_size = total / len(self.timestamps)
121  max_size = max(self.sizes)
122  min_size = min(self.sizes)
123  return bytes_per_s, mean_size, min_size, max_size
124 
125  def get_hz(self):
126  if not self.times:
127  return None, None, None, None
128  with self.lock:
129  n = len(self.times)
130  mean = sum(self.times) / n
131  rate = 1. / mean if mean > 0. else 0
132  min_delta = min(self.times)
133  max_delta = max(self.times)
134  return rate, mean, min_delta, max_delta
def __init__(self, topic_name, topic_type)
Definition: topic_info.py:48
def message_callback(self, message)
Definition: topic_info.py:91


rqt_topic
Author(s): Dirk Thomas, Dorian Scholz
autogenerated on Sat Mar 20 2021 02:41:10