monitor_dash_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
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 Willow Garage, Inc. 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 OWNER 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 import rospy
34 from diagnostic_msgs.msg import DiagnosticStatus
35 from python_qt_binding.QtCore import QMutex, QMutexLocker, QSize, QTimer, Signal
36 from rqt_robot_monitor.robot_monitor import RobotMonitorWidget
37 from .icon_tool_button import IconToolButton
38 
39 
41  """
42  A widget which brings up the rqt_robot_monitor.
43 
44  Times out after certain period of time (set as 5 sec as of Apr 2013)
45  without receiving diagnostics msg ('/diagnostics_toplevel_state' of
46  DiagnosticStatus type), status becomes as 'stale'.
47 
48  :param context: The plugin context to create the monitor in.
49  :type context: qt_gui.plugin_context.PluginContext
50  """
51  _msg_trigger = Signal()
52 
53  def __init__(self, context, icon_paths=[]):
54  self._graveyard = []
55  ok_icon = ['bg-green.svg', 'ic-diagnostics.svg']
56  warn_icon = ['bg-yellow.svg', 'ic-diagnostics.svg',
57  'ol-warn-badge.svg']
58  err_icon = ['bg-red.svg', 'ic-diagnostics.svg', 'ol-err-badge.svg']
59  stale_icon = ['bg-grey.svg', 'ic-diagnostics.svg',
60  'ol-stale-badge.svg']
61 
62  icons = [ok_icon, warn_icon, err_icon, stale_icon]
63 
64  super(MonitorDashWidget, self).__init__('MonitorWidget', icons,
65  icon_paths=icon_paths)
66 
67  self.setFixedSize(self._icons[0].actualSize(QSize(50, 30)))
68 
69  self._monitor = None
70  self._close_mutex = QMutex()
71  self._show_mutex = QMutex()
72 
73  self._last_update = rospy.Time.now()
74 
75  self.context = context
76  self.clicked.connect(self._show_monitor)
77 
78  self._monitor_shown = False
79  self.setToolTip('Diagnostics')
80 
81  self._diagnostics_toplevel_state_sub = rospy.Subscriber(
82  'diagnostics_toplevel_state',
83  DiagnosticStatus, self.toplevel_state_callback)
84  self._top_level_state = -1
85  self._stall_timer = QTimer()
86  self._stall_timer.timeout.connect(self._stalled)
87  self._stalled()
88  self._plugin_settings = None
89  self._instance_settings = None
90  self._msg_trigger.connect(self._handle_msg_trigger)
91 
92  def toplevel_state_callback(self, msg):
93  self._is_stale = False
94  self._msg_trigger.emit()
95 
96  if self._top_level_state != msg.level:
97  if (msg.level >= 2):
98  self.update_state(2)
99  self.setToolTip("Diagnostics: Error")
100  elif (msg.level == 1):
101  self.update_state(1)
102  self.setToolTip("Diagnostics: Warning")
103  else:
104  self.update_state(0)
105  self.setToolTip("Diagnostics: OK")
106  self._top_level_state = msg.level
107 
109  self._stall_timer.start(5000)
110 
111  def _stalled(self):
112  self._stall_timer.stop()
113  self._is_stale = True
114  self.update_state(3)
115  self._top_level_state = 3
116  self.setToolTip("Diagnostics: Stale\nNo message received on "
117  "/diagnostics_agg in the last 5 seconds")
118 
119  def _show_monitor(self):
120  with QMutexLocker(self._show_mutex):
121  try:
122  if self._monitor_shown:
123  self.context.remove_widget(self._monitor)
124  self._monitor_close()
125  self._monitor_shown = False
126  else:
127  self._monitor = RobotMonitorWidget(self.context,
128  '/diagnostics_agg')
129  if self._plugin_settings:
131  self._instance_settings)
132  self.context.add_widget(self._monitor)
133  self._monitor_shown = True
134  except Exception:
135  if self._monitor_shown == False:
136  raise
137  #TODO: when closeEvents is available fix this hack
138  # (It ensures the button will toggle correctly)
139  self._monitor_shown = False
140  self._show_monitor()
141 
142  def _monitor_close(self):
143  if self._monitor_shown:
144  with QMutexLocker(self._close_mutex):
145  if self._plugin_settings:
147  self._instance_settings)
148  self._monitor.shutdown()
149  self._monitor.close()
150  self._graveyard.append(self._monitor)
151  self._monitor = None
152 
153  def shutdown_widget(self):
154  self._stall_timer.stop()
155  if self._monitor:
156  self._monitor.shutdown()
157  self._diagnostics_toplevel_state_sub.unregister()
158 
159  def save_settings(self, plugin_settings, instance_settings):
160  if self._monitor_shown:
162  self._instance_settings)
163 
164  def restore_settings(self, plugin_settings, instance_settings):
165  self._plugin_settings = plugin_settings
166  self._instance_settings = instance_settings
def save_settings(self, plugin_settings, instance_settings)
def restore_settings(self, plugin_settings, instance_settings)


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Mon Feb 28 2022 23:38:55