Playback profiles
This commit is contained in:
parent
e485697807
commit
4a2de1959c
@ -1,9 +1,9 @@
|
|||||||
import enum
|
import enum
|
||||||
import os.path
|
|
||||||
import datetime
|
|
||||||
import csv
|
import csv
|
||||||
|
import time
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
from PyQt6.QtCore import QDir
|
from PyQt6.QtCore import QTimer
|
||||||
from PyQt6.QtWidgets import (
|
from PyQt6.QtWidgets import (
|
||||||
QWidget,
|
QWidget,
|
||||||
QGridLayout,
|
QGridLayout,
|
||||||
@ -12,36 +12,33 @@ from PyQt6.QtWidgets import (
|
|||||||
QLabel,
|
QLabel,
|
||||||
QPushButton,
|
QPushButton,
|
||||||
QFileDialog,
|
QFileDialog,
|
||||||
QMessageBox,
|
|
||||||
)
|
)
|
||||||
from PyQt6.QtCharts import QChart, QChartView, QLineSeries
|
from PyQt6.QtCharts import QChart, QChartView, QLineSeries
|
||||||
|
|
||||||
|
from .profile_handler import ProfileHandler, ProfileState
|
||||||
class ProfileState(enum.Enum):
|
|
||||||
STOPPED = 0
|
|
||||||
RUNNING = 1
|
|
||||||
PAUSED = 2
|
|
||||||
|
|
||||||
|
|
||||||
class GUI(QWidget):
|
class GUI(QWidget):
|
||||||
def __init__(self):
|
_profile_handler: ProfileHandler
|
||||||
|
_update_timer: QTimer
|
||||||
|
|
||||||
|
def __init__(self, profile_handler: ProfileHandler):
|
||||||
super().__init__(None)
|
super().__init__(None)
|
||||||
|
|
||||||
profile_label = QLabel("Profile:")
|
profile_label = QLabel("Profile:")
|
||||||
self.profile = QLabel("None")
|
self.profile = QLabel("None")
|
||||||
self.profile.setStyleSheet("font-style: italic")
|
self.profile.setStyleSheet("font-style: italic")
|
||||||
profile_chooser = QPushButton("Choose")
|
self.profile_chooser = QPushButton("Choose")
|
||||||
profile_chooser.clicked.connect(self._choose_profile)
|
self.profile_chooser.clicked.connect(self._choose_profile)
|
||||||
profile_choice = QHBoxLayout()
|
profile_choice = QHBoxLayout()
|
||||||
profile_choice.addWidget(profile_label)
|
profile_choice.addWidget(profile_label)
|
||||||
profile_choice.addWidget(self.profile)
|
profile_choice.addWidget(self.profile)
|
||||||
profile_choice.addWidget(profile_chooser)
|
profile_choice.addWidget(self.profile_chooser)
|
||||||
|
|
||||||
chart_view = QChartView()
|
chart_view = QChartView()
|
||||||
self.chart = QChart()
|
self.chart = QChart()
|
||||||
chart_view.setChart(self.chart)
|
chart_view.setChart(self.chart)
|
||||||
|
|
||||||
self.profile_state = ProfileState.STOPPED
|
|
||||||
self.start_pause = QPushButton("START")
|
self.start_pause = QPushButton("START")
|
||||||
self.start_pause.setDisabled(True)
|
self.start_pause.setDisabled(True)
|
||||||
self.start_pause.clicked.connect(self._start_pause)
|
self.start_pause.clicked.connect(self._start_pause)
|
||||||
@ -84,6 +81,14 @@ class GUI(QWidget):
|
|||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
self.setWindowTitle("Load Controller")
|
self.setWindowTitle("Load Controller")
|
||||||
|
|
||||||
|
self._profile_handler = profile_handler
|
||||||
|
self._profile_handler.current_changed.connect(self._update_current)
|
||||||
|
self._profile_handler.finished.connect(self._stop)
|
||||||
|
|
||||||
|
self._update_timer = QTimer()
|
||||||
|
self._update_timer.timeout.connect(self._update)
|
||||||
|
self._update_timer.start(100)
|
||||||
|
|
||||||
def _choose_profile(self):
|
def _choose_profile(self):
|
||||||
dlg = QFileDialog(parent=self)
|
dlg = QFileDialog(parent=self)
|
||||||
dlg.setFileMode(QFileDialog.FileMode.ExistingFile)
|
dlg.setFileMode(QFileDialog.FileMode.ExistingFile)
|
||||||
@ -97,28 +102,54 @@ class GUI(QWidget):
|
|||||||
self.profile.setStyleSheet("font-style: normal")
|
self.profile.setStyleSheet("font-style: normal")
|
||||||
|
|
||||||
series = QLineSeries()
|
series = QLineSeries()
|
||||||
with open(profile_path) as fh:
|
for t, i in self._profile_handler.load_profile(profile_path):
|
||||||
t = 0
|
series.append(t, i)
|
||||||
for l in csv.reader(fh):
|
# with open(profile_path) as fh:
|
||||||
i = float(l[0])
|
# t = 0
|
||||||
series.append(t, i)
|
# for l in csv.reader(fh):
|
||||||
t += float(l[1])
|
# i = float(l[0])
|
||||||
series.append(t, i)
|
# series.append(t, i)
|
||||||
|
# t += float(l[1])
|
||||||
|
# series.append(t, i)
|
||||||
|
# series.append()
|
||||||
self.chart.removeAllSeries()
|
self.chart.removeAllSeries()
|
||||||
self.chart.addSeries(series)
|
self.chart.addSeries(series)
|
||||||
self.chart.createDefaultAxes()
|
self.chart.createDefaultAxes()
|
||||||
|
self.start_pause.setDisabled(False)
|
||||||
|
|
||||||
|
def _update_current(self, current: float):
|
||||||
|
self.current.setText(f"{current:02.1f}")
|
||||||
|
|
||||||
def _start_pause(self):
|
def _start_pause(self):
|
||||||
if self.profile_state == ProfileState.RUNNING:
|
if self._profile_handler.state == ProfileState.STOPPED:
|
||||||
self.profile_state = ProfileState.PAUSED
|
self._profile_handler.start()
|
||||||
self.start_pause.setText("RESUME")
|
|
||||||
else:
|
|
||||||
self.profile_state = ProfileState.RUNNING
|
|
||||||
self.start_pause.setText("PAUSE")
|
self.start_pause.setText("PAUSE")
|
||||||
self.stop.setDisabled(False)
|
self.stop.setDisabled(False)
|
||||||
# TODO: Start logging if we were stopped
|
self.profile_chooser.setDisabled(True)
|
||||||
|
# TODO: Start logging
|
||||||
|
elif self._profile_handler.state == ProfileState.RUNNING:
|
||||||
|
self._profile_handler.pause()
|
||||||
|
self.start_pause.setText("RESUME")
|
||||||
|
else:
|
||||||
|
self._profile_handler.resume()
|
||||||
|
self.start_pause.setText("PAUSE")
|
||||||
|
|
||||||
def _stop(self):
|
def _stop(self):
|
||||||
self.profile_state = ProfileState.STOPPED
|
|
||||||
self.start_pause.setText("START")
|
self.start_pause.setText("START")
|
||||||
self.stop.setDisabled(True)
|
self.stop.setDisabled(True)
|
||||||
|
self.profile_chooser.setDisabled(False)
|
||||||
|
|
||||||
|
def _update(self):
|
||||||
|
if self._profile_handler.state == ProfileState.RUNNING:
|
||||||
|
dt = time.time() - self._profile_handler.profile_start
|
||||||
|
if len(self.chart.series()) == 1:
|
||||||
|
series = QLineSeries()
|
||||||
|
series.append(dt, 0)
|
||||||
|
series.append(dt, 100)
|
||||||
|
self.chart.addSeries(series)
|
||||||
|
self.chart.createDefaultAxes()
|
||||||
|
else:
|
||||||
|
series = cast(QLineSeries, self.chart.series()[-1])
|
||||||
|
series.removePoints(0, 2)
|
||||||
|
series.append(dt, 0)
|
||||||
|
series.append(dt, 100)
|
||||||
|
@ -1,19 +1,33 @@
|
|||||||
import serial
|
import serial
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from PyQt6.QtCore import QObject, pyqtSignal
|
from PyQt6.QtCore import QObject
|
||||||
|
|
||||||
|
from .profile_handler import ProfileHandler
|
||||||
|
|
||||||
|
|
||||||
class Load(QObject):
|
class Load(QObject):
|
||||||
dev: serial.Serial
|
dev: serial.Serial
|
||||||
finished: pyqtSignal
|
|
||||||
|
|
||||||
def __init__(self, uart_path: str):
|
_current: float
|
||||||
super(parent=None)
|
|
||||||
self.dev = serial.Serial(uart_path)
|
def __init__(self, uart_path: str, profile_handler: ProfileHandler):
|
||||||
self.finished = pyqtSignal()
|
super().__init__(None)
|
||||||
|
self.dev = serial.Serial(uart_path, 115200)
|
||||||
|
self._current = 0
|
||||||
|
|
||||||
|
profile_handler.current_changed.connect(self._update_current)
|
||||||
|
|
||||||
|
def _update_current(self, current):
|
||||||
|
self._current = current
|
||||||
|
|
||||||
def do_work(self):
|
def do_work(self):
|
||||||
while True:
|
while True:
|
||||||
# TODO
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
curr_quants = round(self._current / 0.1)
|
||||||
|
assert curr_quants <= 0xFFFF
|
||||||
|
|
||||||
|
msb = curr_quants >> 8
|
||||||
|
lsb = curr_quants & 0xFF
|
||||||
|
self.dev.write(bytes((msb, lsb)))
|
||||||
|
95
load_controller/profile_handler.py
Normal file
95
load_controller/profile_handler.py
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import enum
|
||||||
|
import csv
|
||||||
|
import time
|
||||||
|
|
||||||
|
from PyQt6.QtCore import QObject, pyqtSignal, QTimer
|
||||||
|
|
||||||
|
|
||||||
|
class ProfileState(enum.Enum):
|
||||||
|
STOPPED = 0
|
||||||
|
RUNNING = 1
|
||||||
|
PAUSED = 2
|
||||||
|
|
||||||
|
|
||||||
|
class ProfileHandler(QObject):
|
||||||
|
current_changed = pyqtSignal(float)
|
||||||
|
finished = pyqtSignal()
|
||||||
|
|
||||||
|
state: ProfileState
|
||||||
|
|
||||||
|
_timer: QTimer
|
||||||
|
|
||||||
|
profile_start: float
|
||||||
|
_profile: list[tuple[float, float]]
|
||||||
|
_pause_time: float
|
||||||
|
_last_current: float
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(parent=None)
|
||||||
|
self.state = ProfileState.STOPPED
|
||||||
|
self._timer = QTimer()
|
||||||
|
self._timer.timeout.connect(self._update)
|
||||||
|
self._timer.start(100)
|
||||||
|
|
||||||
|
self._profile = []
|
||||||
|
self.profile_start = 0
|
||||||
|
self._pause_time = 0
|
||||||
|
self._last_current = 0
|
||||||
|
|
||||||
|
def load_profile(self, path: str) -> list[tuple[float, float]]:
|
||||||
|
with open(path) as fh:
|
||||||
|
result = []
|
||||||
|
t = 0
|
||||||
|
for l in csv.reader(fh):
|
||||||
|
i = float(l[0])
|
||||||
|
result.append((t, i))
|
||||||
|
t += float(l[1])
|
||||||
|
result.append((t, i))
|
||||||
|
self._profile = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
assert self.state == ProfileState.STOPPED
|
||||||
|
|
||||||
|
self.profile_start = time.time()
|
||||||
|
self.state = ProfileState.RUNNING
|
||||||
|
current = self._profile[0][1]
|
||||||
|
self._last_current = current
|
||||||
|
self.current_changed.emit(current)
|
||||||
|
|
||||||
|
def pause(self):
|
||||||
|
assert self.state == ProfileState.RUNNING
|
||||||
|
|
||||||
|
self.state = ProfileState.PAUSED
|
||||||
|
self.current_changed.emit(0)
|
||||||
|
self._last_current = 0
|
||||||
|
self._pause_time = time.time()
|
||||||
|
|
||||||
|
def resume(self):
|
||||||
|
assert self.state == ProfileState.PAUSED
|
||||||
|
|
||||||
|
dt = time.time() - self._pause_time
|
||||||
|
self.profile_start += dt
|
||||||
|
self.state = ProfileState.RUNNING
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
assert self.state != ProfileState.STOPPED
|
||||||
|
|
||||||
|
self.state = ProfileState.STOPPED
|
||||||
|
self.current_changed.emit(0)
|
||||||
|
self._last_current = 0
|
||||||
|
|
||||||
|
def _update(self):
|
||||||
|
if self.state == ProfileState.RUNNING:
|
||||||
|
dt = time.time() - self.profile_start
|
||||||
|
try:
|
||||||
|
current = next(t[1] for t in self._profile if t[0] >= dt)
|
||||||
|
if current != self._last_current:
|
||||||
|
self.current_changed.emit(current)
|
||||||
|
self._last_current = current
|
||||||
|
except StopIteration:
|
||||||
|
self.state = ProfileState.STOPPED
|
||||||
|
self.finished.emit()
|
||||||
|
else:
|
||||||
|
self._last_current = 0
|
||||||
|
self.current_changed.emit(0)
|
12
main.py
12
main.py
@ -3,16 +3,26 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from PyQt6.QtCore import QThread
|
||||||
from PyQt6.QtWidgets import QApplication
|
from PyQt6.QtWidgets import QApplication
|
||||||
|
|
||||||
from load_controller.gui import GUI
|
from load_controller.gui import GUI
|
||||||
|
from load_controller.load import Load
|
||||||
|
from load_controller.profile_handler import ProfileHandler
|
||||||
|
|
||||||
|
|
||||||
def main(argv: list[str]) -> int:
|
def main(argv: list[str]) -> int:
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
gui = GUI()
|
profile_handler = ProfileHandler()
|
||||||
|
gui = GUI(profile_handler)
|
||||||
gui.show()
|
gui.show()
|
||||||
|
|
||||||
|
load = Load(argv[1], profile_handler)
|
||||||
|
load_thread = QThread()
|
||||||
|
load.moveToThread(load_thread)
|
||||||
|
load_thread.started.connect(load.do_work)
|
||||||
|
load_thread.start()
|
||||||
|
|
||||||
return app.exec()
|
return app.exec()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user