status_indicator_widget.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2020 Pilz GmbH & Co. KG
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 import os
18 
19 import rospkg
20 import rospy
21 from prbt_hardware_support.msg import OperationModes
22 from python_qt_binding import loadUi
23 from python_qt_binding.QtGui import QPixmap
24 from python_qt_binding.QtWidgets import QWidget
25 
26 GREEN = "green"
27 RED = "red"
28 
29 
31  def __init__(self, serial_number): # pragma no cover
32  """Initializes the widget. Widget content will be loaded from
33  `PilzStatusIndicatorRqt.ui`.
34 
35  :param serial_number: A serial number to differentiate multiple
36  instances of the same widget."""
37  super(PilzStatusIndicatorWidget, self).__init__()
38  ui_file = os.path.join(rospkg.RosPack().get_path(
39  'pilz_status_indicator_rqt'), 'resource', 'PilzStatusIndicatorRqt.ui')
40  loadUi(ui_file, self)
41  self.setObjectName('PilzStatusIndicatorRqtUi')
42 
43  # Checking if widget is loaded correctly from ui file
44  assert self.labelROS, "ROS label must be loaded from ui file"
45  assert self.labelHW, "HW label must be loaded from ui file"
46  assert self.labelOM, "OM label must be loaded from ui file"
47  assert self.labelOM_text, "OM text label must be loaded from ui file"
48  assert self.barSpeed, "barSpeed must be loaded from ui file"
49 
50  # Prepare ui elements
51  self.labelOM.setScaledContents(True)
52 
53  # Show windowTitle on left-top of each plugin.
54  if serial_number > 1:
55  self.setWindowTitle(
56  self.windowTitle() + (' (%d)' % serial_number))
57 
58  def _set_label_status_view(self, label, status):
59  if status:
60  label.setStyleSheet("QLabel { background-color: %s }" % GREEN)
61  else:
62  label.setStyleSheet("QLabel { background-color: %s }" % RED)
63 
64  def set_ROS_status(self, status):
65  """Sets the HW status to be displayed in the widget, using
66  a red or green LED.
67 
68  :param status: The status to set:
69  False will be red, True will be green."""
70  self._set_label_status_view(self.labelROS, status)
71 
72  def set_HW_status(self, status):
73  """Sets the HW status to be displayed in the widget, using
74  a red or green LED.
75 
76  :param status: The status to set:
77  False will be red, True will be green."""
78  self._set_label_status_view(self.labelHW, status)
79 
80  def set_operation_mode(self, mode, _qpixmap_class=QPixmap):
81  """Sets the operation mode to be displayed in the widget, influencing
82  both the shown image and the text beneath it.
83 
84  :param mode: The mode to be set of type
85  `prbt_hardware_support.msg.OperationModes`.
86  :param _qpixmap_class: (Internal use only)"""
87  if mode == OperationModes.AUTO:
88  icon_name = 'auto'
89  elif mode == OperationModes.T1:
90  icon_name = 't1'
91  elif mode == OperationModes.T2:
92  icon_name = 't2'
93  else: # mode == OperationModes.UNKNOWN
94  icon_name = 'unknown'
95  icon_path = os.path.join(rospkg.RosPack().get_path(
96  'pilz_status_indicator_rqt'), 'resource', icon_name + '.png')
97  pixmap = _qpixmap_class(icon_path)
98  self.labelOM.setPixmap(pixmap)
99  self.labelOM_text.setText(icon_name.capitalize())
100 
101  def set_speed(self, val):
102  """Sets the speed override to be displayed in the widget, influencing
103  the progress bar and the textual percentage within it.
104 
105  :param val: The speed override as a value between 0 and 1."""
106  if val > 1 or val < 0: # expecting val = 0...1
107  rospy.logwarn(
108  "expecting speed value between 0 and 1, got {} !".format(val))
109  if val > 1:
110  val = 1
111  else:
112  val = 0
113  self.barSpeed.setValue(100. * val)


pilz_status_indicator_rqt
Author(s):
autogenerated on Tue Feb 2 2021 03:50:27