From 2385e49e5dc1be01374fc1f29f779373f4f91ff2 Mon Sep 17 00:00:00 2001 From: benjaminwp18 <90342856+benjaminwp18@users.noreply.github.com> Date: Sat, 8 Jun 2024 14:40:05 -0400 Subject: [PATCH] Display and offset temp --- src/surface/gui/gui/operator_app.py | 8 ++- src/surface/gui/gui/widgets/temperature.py | 59 ++++++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/surface/gui/gui/operator_app.py b/src/surface/gui/gui/operator_app.py index a8e2e73a..f1c91d3f 100644 --- a/src/surface/gui/gui/operator_app.py +++ b/src/surface/gui/gui/operator_app.py @@ -4,6 +4,7 @@ from gui.widgets.float_comm import FloatComm from gui.widgets.timer import InteractiveTimer from gui.widgets.task_selector import TaskSelector +from gui.widgets.temperature import TemperatureSensor from PyQt6.QtWidgets import QGridLayout, QTabWidget, QWidget, QVBoxLayout @@ -21,8 +22,11 @@ def __init__(self) -> None: timer = InteractiveTimer() main_layout.addWidget(timer, 0, 1) - task_selector = TaskSelector() - main_layout.addWidget(task_selector, 1, 1) + # task_selector = TaskSelector() + # main_layout.addWidget(task_selector, 1, 1) + + temp_sensor = TemperatureSensor() + main_layout.addWidget(temp_sensor, 1, 1) self.float_comm: FloatComm = FloatComm() main_layout.addWidget(self.float_comm, 0, 0) diff --git a/src/surface/gui/gui/widgets/temperature.py b/src/surface/gui/gui/widgets/temperature.py index c5350b8c..353a8f14 100644 --- a/src/surface/gui/gui/widgets/temperature.py +++ b/src/surface/gui/gui/widgets/temperature.py @@ -1,17 +1,68 @@ +from collections import deque + from gui.gui_nodes.event_nodes.subscriber import GUIEventSubscriber from rov_msgs.msg import Temperature from PyQt6.QtCore import pyqtSignal, pyqtSlot from PyQt6.QtWidgets import (QGridLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget) -class ThrusterTester(QWidget): +MIN_TEMP_C = 10 +MAX_TEMP_C = 30 +QUEUE_LEN = 5 - temperature_reading_signal: pyqtSignal = pyqtSignal() +class TemperatureSensor(QWidget): + temperature_reading_signal: pyqtSignal = pyqtSignal(Temperature) def __init__(self) -> None: - self.cmd_client: GUIEventSubscriber = GUIEventSubscriber( + super().__init__() + + self.temperature_reading_signal.connect(self.temperature_received) + self.temp_subscriber: GUIEventSubscriber = GUIEventSubscriber( Temperature, - "temperature", + "/tether/temperature", self.temperature_reading_signal ) + + self.temps: deque[float] = deque(maxlen=QUEUE_LEN) + self.offset = 0.0 + + root_layout = QVBoxLayout() + self.setLayout(root_layout) + + self.ave_temp_label = QLabel() + self.ave_temp_label.setText('Waiting for temp...') + + self.offset_field = QLineEdit() + self.offset_field.setPlaceholderText('Enter offset (C)') + + self.offset_button = QPushButton('Set offset') + self.offset_button.clicked.connect(self.set_offset) + + root_layout.addWidget(self.ave_temp_label) + root_layout.addWidget(self.offset_field) + root_layout.addWidget(self.offset_button) + + def set_offset(self) -> None: + offset_text = self.offset_field.text() + + if offset_text == '': + offset_text = '0' + + try: + self.offset = float(offset_text) + self.offset_button.setText('Offset applied') + except ValueError: + self.offset_button.setText('Illegal') + return + + @pyqtSlot(Temperature) + def temperature_received(self, msg: Temperature) -> None: + if MIN_TEMP_C <= msg.reading <= MAX_TEMP_C: + self.temps.append(msg.reading) + + ave = sum(self.temps) / QUEUE_LEN + offset_ave = ave + self.offset + self.ave_temp_label.setText(f'{round(offset_ave, 4)}\tC') + + self.offset_button.setText('Set offset')