Skip to content

actuators

ActuatorsDialog #

Bases: QDialog

ActuatorsDialog class

Build a modal for actuators remote control and monitoring.

Attributes:

Name Type Description
new_actuator_command Signal

Qt signal emitted when a actuator command is updated

closed Signal

Qt signal emitted when the window is hidden

Source code in cogip/widgets/actuators.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
class ActuatorsDialog(QtWidgets.QDialog):
    """
    ActuatorsDialog class

    Build a modal for actuators remote control and monitoring.

    Attributes:
        new_actuator_command: Qt signal emitted when a actuator command is updated
        closed: Qt signal emitted when the window is hidden
    """

    closed: qtSignal = qtSignal()
    new_actuator_command: qtSignal = qtSignal(object)

    def __init__(self, parent: QtWidgets.QWidget = None):
        """
        Class constructor.

        Arguments:
            parent: The parent widget
        """
        super().__init__(parent)
        self.servos: dict[ServoEnum, ServoControl] = {}
        self.positional_actuators: dict[PositionalActuatorEnum, PositionalActuatorControl] = {}
        self.bool_sensors: dict[BoolSensorEnum, BoolSensorControl] = {}
        self.setWindowTitle("Actuators Control")
        self.setModal(False)

        layout = QtWidgets.QGridLayout()
        self.setLayout(layout)

        for id in ServoEnum:
            self.servos[id] = ServoControl(id, layout)
            self.servos[id].command_updated.connect(self.command_updated)

        for id in PositionalActuatorEnum:
            self.positional_actuators[id] = PositionalActuatorControl(id, layout)
            self.positional_actuators[id].command_updated.connect(self.command_updated)

        for id in BoolSensorEnum:
            self.bool_sensors[id] = BoolSensorControl(id, layout)

        self.readSettings()

    def update_actuator(self, actuator_state: ActuatorState):
        """
        Update an actuator with new values.

        Arguments:
            actuator_state: current state of an actuator
        """

        match actuator_state.kind:
            case ActuatorsKindEnum.servo:
                actuator = self.servos.get(actuator_state.id)
                if actuator is None:
                    logger.warning(f"Unknown servo ID: {actuator_state.id}")
                    return
                actuator.update_value(actuator_state)
            case ActuatorsKindEnum.positional_actuator:
                actuator = self.positional_actuators.get(actuator_state.id)
                if actuator is None:
                    logger.warning(f"Unknown positional actuator ID: {actuator_state.id}")
                    return
                actuator.update_value(actuator_state)
            case ActuatorsKindEnum.bool_sensor:
                actuator = self.bool_sensors.get(actuator_state.id)
                if actuator is None:
                    logger.warning(f"Unknown bool sensor ID: {actuator_state.id}")
                    return
                actuator.update_value(actuator_state)

    def command_updated(self, command: ActuatorCommand):
        """
        Emit updated values with namespace, name and value.
        """
        self.new_actuator_command.emit(command)

    def closeEvent(self, event: QtGui.QCloseEvent):
        """
        Hide the window.

        Arguments:
            event: The close event (unused)
        """
        settings = QtCore.QSettings("COGIP", "monitor")
        settings.setValue("properties/actuators", self.saveGeometry())

        self.closed.emit()
        event.accept()
        super().closeEvent(event)

    def readSettings(self):
        settings = QtCore.QSettings("COGIP", "monitor")
        self.restoreGeometry(settings.value("properties/actuators"))

__init__(parent=None) #

Class constructor.

Parameters:

Name Type Description Default
parent QWidget

The parent widget

None
Source code in cogip/widgets/actuators.py
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def __init__(self, parent: QtWidgets.QWidget = None):
    """
    Class constructor.

    Arguments:
        parent: The parent widget
    """
    super().__init__(parent)
    self.servos: dict[ServoEnum, ServoControl] = {}
    self.positional_actuators: dict[PositionalActuatorEnum, PositionalActuatorControl] = {}
    self.bool_sensors: dict[BoolSensorEnum, BoolSensorControl] = {}
    self.setWindowTitle("Actuators Control")
    self.setModal(False)

    layout = QtWidgets.QGridLayout()
    self.setLayout(layout)

    for id in ServoEnum:
        self.servos[id] = ServoControl(id, layout)
        self.servos[id].command_updated.connect(self.command_updated)

    for id in PositionalActuatorEnum:
        self.positional_actuators[id] = PositionalActuatorControl(id, layout)
        self.positional_actuators[id].command_updated.connect(self.command_updated)

    for id in BoolSensorEnum:
        self.bool_sensors[id] = BoolSensorControl(id, layout)

    self.readSettings()

closeEvent(event) #

Hide the window.

Parameters:

Name Type Description Default
event QCloseEvent

The close event (unused)

required
Source code in cogip/widgets/actuators.py
307
308
309
310
311
312
313
314
315
316
317
318
319
def closeEvent(self, event: QtGui.QCloseEvent):
    """
    Hide the window.

    Arguments:
        event: The close event (unused)
    """
    settings = QtCore.QSettings("COGIP", "monitor")
    settings.setValue("properties/actuators", self.saveGeometry())

    self.closed.emit()
    event.accept()
    super().closeEvent(event)

command_updated(command) #

Emit updated values with namespace, name and value.

Source code in cogip/widgets/actuators.py
301
302
303
304
305
def command_updated(self, command: ActuatorCommand):
    """
    Emit updated values with namespace, name and value.
    """
    self.new_actuator_command.emit(command)

update_actuator(actuator_state) #

Update an actuator with new values.

Parameters:

Name Type Description Default
actuator_state ActuatorState

current state of an actuator

required
Source code in cogip/widgets/actuators.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def update_actuator(self, actuator_state: ActuatorState):
    """
    Update an actuator with new values.

    Arguments:
        actuator_state: current state of an actuator
    """

    match actuator_state.kind:
        case ActuatorsKindEnum.servo:
            actuator = self.servos.get(actuator_state.id)
            if actuator is None:
                logger.warning(f"Unknown servo ID: {actuator_state.id}")
                return
            actuator.update_value(actuator_state)
        case ActuatorsKindEnum.positional_actuator:
            actuator = self.positional_actuators.get(actuator_state.id)
            if actuator is None:
                logger.warning(f"Unknown positional actuator ID: {actuator_state.id}")
                return
            actuator.update_value(actuator_state)
        case ActuatorsKindEnum.bool_sensor:
            actuator = self.bool_sensors.get(actuator_state.id)
            if actuator is None:
                logger.warning(f"Unknown bool sensor ID: {actuator_state.id}")
                return
            actuator.update_value(actuator_state)

BoolSensorControl #

Bases: QObject

BoolSensorControl class.

Build a widget to show the state of a bool sensor.

Source code in cogip/widgets/actuators.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
class BoolSensorControl(QtCore.QObject):
    """
    BoolSensorControl class.

    Build a widget to show the state of a bool  sensor.
    """

    def __init__(self, id: BoolSensorEnum, layout: QtWidgets.QGridLayout):
        """
        Class constructor.

        Arguments:
            id: ID of bool sensor to display
            layout: The parent layout
        """
        super().__init__()
        self.enabled = False
        self.id = id

        row = layout.rowCount()

        self.label = QtWidgets.QLabel(self.id.name)
        layout.addWidget(self.label, row, 0)

        self.kind = QtWidgets.QLabel("Bool Sensor")
        layout.addWidget(self.kind, row, 1)

        self.state = QtWidgets.QCheckBox()
        self.state.setToolTip("State")
        self.state.setAttribute(QtCore.Qt.WidgetAttribute.WA_TransparentForMouseEvents)
        self.state.setFocusPolicy(QtCore.Qt.NoFocus)
        self.state.setChecked(False)
        layout.addWidget(self.state, row, 2)

        self.label.setEnabled(False)
        self.kind.setEnabled(False)
        self.state.setEnabled(False)

    def update_value(self, actuator: BoolSensor):
        if not self.enabled:
            self.enabled = True
            self.label.setEnabled(True)
            self.kind.setEnabled(True)
            self.state.setEnabled(True)

        self.state.setChecked(actuator.state)

__init__(id, layout) #

Class constructor.

Parameters:

Name Type Description Default
id BoolSensorEnum

ID of bool sensor to display

required
layout QGridLayout

The parent layout

required
Source code in cogip/widgets/actuators.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def __init__(self, id: BoolSensorEnum, layout: QtWidgets.QGridLayout):
    """
    Class constructor.

    Arguments:
        id: ID of bool sensor to display
        layout: The parent layout
    """
    super().__init__()
    self.enabled = False
    self.id = id

    row = layout.rowCount()

    self.label = QtWidgets.QLabel(self.id.name)
    layout.addWidget(self.label, row, 0)

    self.kind = QtWidgets.QLabel("Bool Sensor")
    layout.addWidget(self.kind, row, 1)

    self.state = QtWidgets.QCheckBox()
    self.state.setToolTip("State")
    self.state.setAttribute(QtCore.Qt.WidgetAttribute.WA_TransparentForMouseEvents)
    self.state.setFocusPolicy(QtCore.Qt.NoFocus)
    self.state.setChecked(False)
    layout.addWidget(self.state, row, 2)

    self.label.setEnabled(False)
    self.kind.setEnabled(False)
    self.state.setEnabled(False)

PositionalActuatorControl #

Bases: QObject

PositionalControl class.

Build a widget to control a positional actuator.

Source code in cogip/widgets/actuators.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class PositionalActuatorControl(QtCore.QObject):
    """
    PositionalControl class.

    Build a widget to control a positional actuator.
    """

    command_updated: qtSignal = qtSignal(object)

    def __init__(self, id: PositionalActuatorEnum, layout: QtWidgets.QGridLayout):
        """
        Class constructor.

        Arguments:
            id: ID of positional actuator to control
            layout: The parent layout
        """
        super().__init__()
        self.enabled = False
        command_schema = Servo.model_json_schema()["properties"]["command"]
        self.id = id

        row = layout.rowCount()
        minimum, maximum = actuator_limits.get(id, (command_schema.get("minimum"), command_schema.get("maximum")))

        self.label = QtWidgets.QLabel(self.id.name)
        layout.addWidget(self.label, row, 0)

        self.kind = QtWidgets.QLabel("Positional")
        layout.addWidget(self.kind, row, 1)

        self.command = QtWidgets.QSpinBox()
        self.command.setToolTip("Position command")
        self.command.setMinimum(minimum)
        self.command.setMaximum(maximum)
        self.command.setSingleStep(1)
        self.command.valueChanged.connect(self.command_changed)
        layout.addWidget(self.command, row, 2)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.slider.setToolTip("Position command")
        self.slider.setMinimum(minimum)
        self.slider.setMaximum(maximum)
        self.slider.setSingleStep(1)
        self.slider.valueChanged.connect(self.command.setValue)
        layout.addWidget(self.slider, row, 3)

        self.position = QtWidgets.QLabel()
        self.position.setToolTip("Current command")
        layout.addWidget(self.position, row, 4)

        self.label.setEnabled(False)
        self.kind.setEnabled(False)
        self.command.setEnabled(False)
        self.slider.setEnabled(False)
        self.position.setEnabled(False)

    def command_changed(self, value):
        self.slider.setValue(value)
        command = PositionalActuatorCommand(id=self.id, command=value)
        self.command_updated.emit(command)

    def update_value(self, actuator: PositionalActuator):
        if not self.enabled:
            self.enabled = True
            self.label.setEnabled(True)
            self.kind.setEnabled(True)
            self.command.setEnabled(True)
            self.slider.setEnabled(True)
            self.position.setEnabled(True)

        self.command.blockSignals(True)
        self.command.setValue(actuator.command)
        self.command.blockSignals(False)
        self.slider.blockSignals(True)
        self.slider.setValue(actuator.command)
        self.slider.blockSignals(False)
        self.position.setText(str(actuator.command))

__init__(id, layout) #

Class constructor.

Parameters:

Name Type Description Default
id PositionalActuatorEnum

ID of positional actuator to control

required
layout QGridLayout

The parent layout

required
Source code in cogip/widgets/actuators.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def __init__(self, id: PositionalActuatorEnum, layout: QtWidgets.QGridLayout):
    """
    Class constructor.

    Arguments:
        id: ID of positional actuator to control
        layout: The parent layout
    """
    super().__init__()
    self.enabled = False
    command_schema = Servo.model_json_schema()["properties"]["command"]
    self.id = id

    row = layout.rowCount()
    minimum, maximum = actuator_limits.get(id, (command_schema.get("minimum"), command_schema.get("maximum")))

    self.label = QtWidgets.QLabel(self.id.name)
    layout.addWidget(self.label, row, 0)

    self.kind = QtWidgets.QLabel("Positional")
    layout.addWidget(self.kind, row, 1)

    self.command = QtWidgets.QSpinBox()
    self.command.setToolTip("Position command")
    self.command.setMinimum(minimum)
    self.command.setMaximum(maximum)
    self.command.setSingleStep(1)
    self.command.valueChanged.connect(self.command_changed)
    layout.addWidget(self.command, row, 2)

    self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
    self.slider.setToolTip("Position command")
    self.slider.setMinimum(minimum)
    self.slider.setMaximum(maximum)
    self.slider.setSingleStep(1)
    self.slider.valueChanged.connect(self.command.setValue)
    layout.addWidget(self.slider, row, 3)

    self.position = QtWidgets.QLabel()
    self.position.setToolTip("Current command")
    layout.addWidget(self.position, row, 4)

    self.label.setEnabled(False)
    self.kind.setEnabled(False)
    self.command.setEnabled(False)
    self.slider.setEnabled(False)
    self.position.setEnabled(False)

ServoControl #

Bases: QObject

ServoControl class.

Build a widget to control a servo.

Source code in cogip/widgets/actuators.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class ServoControl(QtCore.QObject):
    """
    ServoControl class.

    Build a widget to control a servo.
    """

    command_updated: qtSignal = qtSignal(object)

    def __init__(self, id: ServoEnum, layout: QtWidgets.QGridLayout):
        """
        Class constructor.

        Arguments:
            id: ID of servo to control
            layout: The parent layout
        """
        super().__init__()
        self.enabled = False
        position_schema = Servo.model_json_schema()["properties"]["position"]
        self.id = id

        row = layout.rowCount()
        minimum, maximum = actuator_limits.get(id, (position_schema.get("minimum"), position_schema.get("maximum")))

        self.label = QtWidgets.QLabel(self.id.name)
        layout.addWidget(self.label, row, 0)

        self.kind = QtWidgets.QLabel("Servo")
        layout.addWidget(self.kind, row, 1)

        self.command = QtWidgets.QSpinBox()
        self.command.setToolTip("Position command")
        self.command.setMinimum(minimum)
        self.command.setMaximum(maximum)
        self.command.setSingleStep(1)
        self.command.valueChanged.connect(self.command_changed)
        layout.addWidget(self.command, row, 2)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.slider.setToolTip("Position command")
        self.slider.setMinimum(minimum)
        self.slider.setMaximum(maximum)
        self.slider.setSingleStep(1)
        self.slider.valueChanged.connect(self.command.setValue)
        layout.addWidget(self.slider, row, 3)

        self.position = QtWidgets.QLabel()
        self.position.setToolTip("Current position")
        layout.addWidget(self.position, row, 4)

        self.label.setEnabled(False)
        self.kind.setEnabled(False)
        self.command.setEnabled(False)
        self.slider.setEnabled(False)
        self.position.setEnabled(False)

    def command_changed(self, value):
        self.slider.setValue(value)
        command = ServoCommand(id=self.id, command=value)
        self.command_updated.emit(command)

    def update_value(self, actuator: Servo):
        if not self.enabled:
            self.enabled = True
            self.label.setEnabled(True)
            self.kind.setEnabled(True)
            self.command.setEnabled(True)
            self.slider.setEnabled(True)
            self.position.setEnabled(True)

        self.command.blockSignals(True)
        self.command.setValue(actuator.position)
        self.command.blockSignals(True)
        self.slider.blockSignals(False)
        self.slider.setValue(actuator.command)
        self.slider.blockSignals(False)
        self.position.setText(str(actuator.position))

__init__(id, layout) #

Class constructor.

Parameters:

Name Type Description Default
id ServoEnum

ID of servo to control

required
layout QGridLayout

The parent layout

required
Source code in cogip/widgets/actuators.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(self, id: ServoEnum, layout: QtWidgets.QGridLayout):
    """
    Class constructor.

    Arguments:
        id: ID of servo to control
        layout: The parent layout
    """
    super().__init__()
    self.enabled = False
    position_schema = Servo.model_json_schema()["properties"]["position"]
    self.id = id

    row = layout.rowCount()
    minimum, maximum = actuator_limits.get(id, (position_schema.get("minimum"), position_schema.get("maximum")))

    self.label = QtWidgets.QLabel(self.id.name)
    layout.addWidget(self.label, row, 0)

    self.kind = QtWidgets.QLabel("Servo")
    layout.addWidget(self.kind, row, 1)

    self.command = QtWidgets.QSpinBox()
    self.command.setToolTip("Position command")
    self.command.setMinimum(minimum)
    self.command.setMaximum(maximum)
    self.command.setSingleStep(1)
    self.command.valueChanged.connect(self.command_changed)
    layout.addWidget(self.command, row, 2)

    self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
    self.slider.setToolTip("Position command")
    self.slider.setMinimum(minimum)
    self.slider.setMaximum(maximum)
    self.slider.setSingleStep(1)
    self.slider.valueChanged.connect(self.command.setValue)
    layout.addWidget(self.slider, row, 3)

    self.position = QtWidgets.QLabel()
    self.position.setToolTip("Current position")
    layout.addWidget(self.position, row, 4)

    self.label.setEnabled(False)
    self.kind.setEnabled(False)
    self.command.setEnabled(False)
    self.slider.setEnabled(False)
    self.position.setEnabled(False)