monitor_dash_widget.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import rospy
00034 from diagnostic_msgs.msg import DiagnosticStatus
00035 from python_qt_binding.QtCore import QMutex, QMutexLocker, QSize, QTimer
00036 from rqt_robot_monitor.robot_monitor import RobotMonitorWidget
00037 from .icon_tool_button import IconToolButton
00038 
00039 
00040 class MonitorDashWidget(IconToolButton):
00041     """
00042     A widget which brings up the rqt_robot_monitor.
00043 
00044     Times out after certain period of time (set as 5 sec as of Apr 2013)
00045     without receiving diagnostics msg ('/diagnostics_toplevel_state' of
00046     DiagnosticStatus type), status becomes as 'stale'.
00047 
00048     :param context: The plugin context to create the monitor in.
00049     :type context: qt_gui.plugin_context.PluginContext
00050     """
00051     def __init__(self, context, icon_paths=[]):
00052         self._graveyard = []
00053         ok_icon = ['bg-green.svg', 'ic-diagnostics.svg']
00054         warn_icon = ['bg-yellow.svg', 'ic-diagnostics.svg',
00055                      'ol-warn-badge.svg']
00056         err_icon = ['bg-red.svg', 'ic-diagnostics.svg', 'ol-err-badge.svg']
00057         stale_icon = ['bg-grey.svg', 'ic-diagnostics.svg',
00058                       'ol-stale-badge.svg']
00059 
00060         icons = [ok_icon, warn_icon, err_icon, stale_icon]
00061 
00062         super(MonitorDashWidget, self).__init__('MonitorWidget', icons,
00063                                                 icon_paths=icon_paths)
00064 
00065         self.setFixedSize(self._icons[0].actualSize(QSize(50, 30)))
00066 
00067         self._monitor = None
00068         self._close_mutex = QMutex()
00069         self._show_mutex = QMutex()
00070 
00071         self._last_update = rospy.Time.now()
00072 
00073         self.context = context
00074         self.clicked.connect(self._show_monitor)
00075 
00076         self._monitor_shown = False
00077         self.setToolTip('Diagnostics')
00078 
00079         self._diagnostics_toplevel_state_sub = rospy.Subscriber(
00080                                 'diagnostics_toplevel_state',
00081                                 DiagnosticStatus, self.toplevel_state_callback)
00082         self._top_level_state = -1
00083         self._stall_timer = QTimer()
00084         self._stall_timer.timeout.connect(self._stalled)
00085         self._stalled()
00086         self._plugin_settings = None
00087         self._instance_settings = None
00088 
00089     def toplevel_state_callback(self, msg):
00090         self._is_stale = False
00091         self._stall_timer.start(5000)
00092 
00093         if self._top_level_state != msg.level:
00094             if (msg.level >= 2):
00095                 self.update_state(2)
00096                 self.setToolTip("Diagnostics: Error")
00097             elif (msg.level == 1):
00098                 self.update_state(1)
00099                 self.setToolTip("Diagnostics: Warning")
00100             else:
00101                 self.update_state(0)
00102                 self.setToolTip("Diagnostics: OK")
00103             self._top_level_state = msg.level
00104 
00105     def _stalled(self):
00106         self._stall_timer.stop()
00107         self._is_stale = True
00108         self.update_state(3)
00109         self._top_level_state = 3
00110         self.setToolTip("Diagnostics: Stale\nNo message received on " +
00111                         "dashboard_agg in the last 5 seconds")
00112 
00113     def _show_monitor(self):
00114         with QMutexLocker(self._show_mutex):
00115             try:
00116                 if self._monitor_shown:
00117                     self.context.remove_widget(self._monitor)
00118                     self._monitor_close()
00119                     self._monitor_shown = False
00120                 else:
00121                     self._monitor = RobotMonitorWidget(self.context,
00122                                                        'diagnostics_agg')
00123                     if self._plugin_settings:
00124                         self._monitor.restore_settings(self._plugin_settings,
00125                                                        self._instance_settings)
00126                     self.context.add_widget(self._monitor)
00127                     self._monitor_shown = True
00128             except Exception:
00129                 if self._monitor_shown == False:
00130                     raise
00131                 #TODO: when closeEvents is available fix this hack
00132                 # (It ensures the button will toggle correctly)
00133                 self._monitor_shown = False
00134                 self._show_monitor()
00135 
00136     def _monitor_close(self):
00137         if self._monitor_shown:
00138             with QMutexLocker(self._close_mutex):
00139                 if self._plugin_settings:
00140                     self._monitor.save_settings(self._plugin_settings,
00141                                                 self._instance_settings)
00142                 self._monitor.shutdown()
00143                 self._monitor.close()
00144                 self._graveyard.append(self._monitor)
00145                 self._monitor = None
00146 
00147     def shutdown_widget(self):
00148         self._stall_timer.stop()
00149         if self._monitor:
00150             self._monitor.shutdown()
00151         self._diagnostics_toplevel_state_sub.unregister()
00152 
00153     def save_settings(self, plugin_settings, instance_settings):
00154         if self._monitor_shown:
00155             self._monitor.save_settings(self._plugin_settings,
00156                                         self._instance_settings)
00157 
00158     def restore_settings(self, plugin_settings, instance_settings):
00159         self._plugin_settings = plugin_settings
00160         self._instance_settings = instance_settings


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Fri Aug 28 2015 12:50:54