From 9c1c9df45c8b642546cb7eef5842809b026b521c Mon Sep 17 00:00:00 2001 From: Huibean Date: Wed, 11 Sep 2024 17:30:59 +0800 Subject: [PATCH] add esc status monitor --- dronecan_gui_tool/panels/esc_panel.py | 61 +++++++++++++++++++++------ 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/dronecan_gui_tool/panels/esc_panel.py b/dronecan_gui_tool/panels/esc_panel.py index 4ba471e..562b589 100644 --- a/dronecan_gui_tool/panels/esc_panel.py +++ b/dronecan_gui_tool/panels/esc_panel.py @@ -25,9 +25,11 @@ class PercentSlider(QWidget): - def __init__(self, parent): + def __init__(self, esc_index, parent): super(PercentSlider, self).__init__(parent) + self._index = esc_index + self._slider = QSlider(Qt.Vertical, self) self._slider.setMinimum(-100) self._slider.setMaximum(100) @@ -44,23 +46,51 @@ def __init__(self, parent): self._zero_button = make_icon_button('hand-stop-o', 'Zero setpoint', self, on_clicked=self.zero) - layout = QVBoxLayout(self) - sub_layout = QHBoxLayout(self) - sub_layout.addStretch() - sub_layout.addWidget(self._slider) - sub_layout.addStretch() - layout.addLayout(sub_layout) - layout.addWidget(self._spinbox) - layout.addWidget(self._zero_button) + self._index_label = QLabel(f'Index: {self._index}', self) + self._error_count_label = QLabel('Err: NC', self) + self._voltage_label = QLabel('Volt: NC', self) + self._current_label = QLabel('Curr: NC', self) + self._temperature_label = QLabel('Temp: NC', self) + self._rpm_label = QLabel('RPM: NC', self) + self._power_rating_pct_label = QLabel('RAT: NC', self) + + layout = QHBoxLayout(self) + + status_layout = QVBoxLayout(self) + status_layout.addWidget(self._index_label) + status_layout.addWidget(self._error_count_label) + status_layout.addWidget(self._voltage_label) + status_layout.addWidget(self._current_label) + status_layout.addWidget(self._temperature_label) + status_layout.addWidget(self._rpm_label) + status_layout.addWidget(self._power_rating_pct_label) + status_layout.addStretch() + status_layout.addWidget(self._spinbox) + status_layout.addWidget(self._zero_button) + + layout.addLayout(status_layout) + layout.addWidget(self._slider) + self.setLayout(layout) - self.setMinimumHeight(400) + self.setMinimumHeight(300) def zero(self): self._slider.setValue(0) def get_value(self): return self._slider.value() + + def update_status(self, msg): + status = msg.message + if status.esc_index == self._index: + self._error_count_label.setText(f'Err: {status.error_count}') + self._voltage_label.setText(f'Volt: {status.voltage:.1f} V') + self._current_label.setText(f'Curr: {status.current:.1f} A') + temperature_celsius = status.temperature - 273.15 + self._temperature_label.setText(f'Temp: {temperature_celsius:.1f} °C') + self._rpm_label.setText(f'RPM: {status.rpm}') + self._power_rating_pct_label.setText(f'RAT: {status.power_rating_pct} %') class ESCPanel(QDialog): @@ -77,7 +107,7 @@ def __init__(self, parent, node): self._node = node - self._sliders = [PercentSlider(self) for _ in range(4)] + self._sliders = [PercentSlider(index, self) for index in range(4)] self._num_sliders = QSpinBox(self) self._num_sliders.setMinimum(len(self._sliders)) @@ -141,6 +171,13 @@ def __init__(self, parent, node): self.setLayout(layout) self.resize(self.minimumWidth(), self.minimumHeight()) + self._node.add_handler(dronecan.uavcan.equipment.esc.Status, self._on_esc_status) + + def _on_esc_status(self, msg): + if msg.message.esc_index < len(self._sliders): + sl = self._sliders[msg.message.esc_index] + sl.update_status(msg) + def _do_broadcast(self): try: if not self._pause.isChecked(): @@ -180,7 +217,7 @@ def _update_number_of_sliders(self): removee.deleteLater() while len(self._sliders) < num_sliders: - new = PercentSlider(self) + new = PercentSlider(len(self._sliders), self) self._slider_layout.addWidget(new) self._sliders.append(new)