status_light.py
Go to the documentation of this file.
1 from distutils.version import LooseVersion
2 import math
3 import os
4 import sys
5 from threading import Lock
6 
7 import python_qt_binding
8 import python_qt_binding.QtCore as QtCore
9 from python_qt_binding.QtCore import QEvent
10 from python_qt_binding.QtCore import QSize
11 from python_qt_binding.QtCore import Qt
12 from python_qt_binding.QtCore import QTimer
13 from python_qt_binding.QtCore import qWarning
14 from python_qt_binding.QtCore import Slot
15 import python_qt_binding.QtGui as QtGui
16 from python_qt_binding.QtGui import QBrush
17 from python_qt_binding.QtGui import QColor
18 from python_qt_binding.QtGui import QFont
19 from python_qt_binding.QtGui import QIcon
20 from python_qt_binding.QtGui import QPainter
21 from python_qt_binding.QtGui import QPen
22 import yaml
23 
24 from resource_retriever import get_filename
25 import rospy
26 from rqt_gui_py.plugin import Plugin
27 from std_msgs.msg import Bool
28 from std_msgs.msg import Time
29 from std_msgs.msg import UInt8
30 
31 from image_view2_wrapper import ComboBoxDialog
32 
33 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
34  from python_qt_binding.QtWidgets import QAction
35  from python_qt_binding.QtWidgets import QComboBox
36  from python_qt_binding.QtWidgets import QLabel
37  from python_qt_binding.QtWidgets import QMenu
38  from python_qt_binding.QtWidgets import QMessageBox
39  from python_qt_binding.QtWidgets import QSizePolicy
40  from python_qt_binding.QtWidgets import QWidget
41 
42 else:
43  from python_qt_binding.QtGui import QAction
44  from python_qt_binding.QtGui import QComboBox
45  from python_qt_binding.QtGui import QLabel
46  from python_qt_binding.QtGui import QMenu
47  from python_qt_binding.QtGui import QMessageBox
48  from python_qt_binding.QtGui import QSizePolicy
49  from python_qt_binding.QtGui import QWidget
50 
51 
53  """
54  rqt plugin to show light like ultra-man's light.
55  It subscribes std_msgs/UInt8 topic and the value means:
56  0 == Unknown (gray)
57  1 == Success (green)
58  2 == Warn (yellow)
59  """
60  def __init__(self, context):
61  super(StatusLight, self).__init__(context)
62  self.setObjectName("StatusLight")
64  context.add_widget(self._widget)
65 
66  def save_settings(self, plugin_settings, instance_settings):
67  self._widget.save_settings(plugin_settings, instance_settings)
68 
69  def restore_settings(self, plugin_settings, instance_settings):
70  self._widget.restore_settings(plugin_settings, instance_settings)
71 
73  self._widget.trigger_configuration()
74 
75 
76 class StatusLightWidget(QWidget):
77  _UNKNOWN_COLOR = QColor("#dddddd")
78  _SUCCESS_COLOR = QColor("#18FFFF")
79  _WARN_COLOR = QColor("#FFCA00")
80  _ERROR_COLOR = QColor("#F44336")
81 
82  def __init__(self):
83  super(StatusLightWidget, self).__init__()
84  self.lock = Lock()
85  self.status_sub = None
86  self.status = 0
87  self._status_topics = []
88  self._update_topic_timer = QTimer(self)
89  self._update_topic_timer.timeout.connect(self.updateTopics)
90  self._update_topic_timer.start(1000)
91  self._active_topic = None
92  self._dialog = ComboBoxDialog()
93  self._update_plot_timer = QTimer(self)
94  self._update_plot_timer.timeout.connect(self.redraw)
95  self._update_plot_timer.start(1000 / 15)
96 
97  def redraw(self):
98  self.update()
99 
100  def paintEvent(self, event):
101  with self.lock:
102  if self.status == 1:
103  color = self._SUCCESS_COLOR
104  elif self.status == 2:
105  color = self._WARN_COLOR
106  else:
107  color = self._UNKNOWN_COLOR
108  rect = event.rect()
109  qp = QPainter()
110  qp.begin(self)
111  radius = min(rect.width(), rect.height()) - 100
112  qp.setFont(QFont('Helvetica', 100))
113  qp.setPen(QPen(QBrush(color), 50))
114  qp.setBrush(color)
115  qp.drawEllipse(
116  (rect.width() - radius) / 2, (rect.height() - radius) / 2,
117  radius, radius)
118  qp.end()
119  return
120 
122  self._dialog.exec_()
123  self.setupSubscriber(self._status_topics[self._dialog.number])
124 
125  def updateTopics(self):
126  need_to_update = False
127  for (topic, topic_type) in rospy.get_published_topics():
128  if topic_type == "std_msgs/UInt8":
129  if not topic in self._status_topics:
130  self._status_topics.append(topic)
131  need_to_update = True
132  if need_to_update:
133  self._status_topics = sorted(self._status_topics)
134  self._dialog.combo_box.clear()
135  for topic in self._status_topics:
136  self._dialog.combo_box.addItem(topic)
137  if self._active_topic:
138  if self._active_topic not in self._status_topics:
139  self._status_topics.append(self._active_topic)
140  self._dialog.combo_box.addItem(self._active_topic)
141  self._dialog.combo_box.setCurrentIndex(
142  self._status_topics.index(self._active_topic))
143 
144  def setupSubscriber(self, topic):
145  if self.status_sub:
146  self.status_sub.unregister()
147  self.status_sub = rospy.Subscriber(topic, UInt8,
148  self.statusCallback)
149  self._active_topic = topic
150 
151  def onActivated(self, number):
152  self.setupSubscriber(self._status_topics[number])
153 
154  def statusCallback(self, msg):
155  self.status = msg.data
156 
157  def save_settings(self, plugin_settings, instance_settings):
158  if self._active_topic:
159  instance_settings.set_value("active_topic", self._active_topic)
160 
161  def restore_settings(self, plugin_settings, instance_settings):
162  if instance_settings.value("active_topic"):
163  topic = instance_settings.value("active_topic")
164  self._dialog.combo_box.addItem(topic)
165  self.setupSubscriber(topic)
def save_settings(self, plugin_settings, instance_settings)
def restore_settings(self, plugin_settings, instance_settings)
Definition: status_light.py:69
def save_settings(self, plugin_settings, instance_settings)
Definition: status_light.py:66
def restore_settings(self, plugin_settings, instance_settings)


jsk_rqt_plugins
Author(s):
autogenerated on Sat Mar 20 2021 03:03:13