Display TS state

This commit is contained in:
jazzpi 2022-08-12 01:25:58 +02:00
parent 18d9517fcc
commit 857ca68d83
1 changed files with 38 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from enum import Enum
import math import math
import os import os
import struct import struct
@ -53,8 +54,6 @@ MASTER_THRESH_OT = 1
MASTER_THRESH_UV = 2 MASTER_THRESH_UV = 2
MASTER_THRESH_OV = 3 MASTER_THRESH_OV = 3
TS_ERROR = 4
class SlaveData: class SlaveData:
cell_voltages: list[float] cell_voltages: list[float]
@ -67,6 +66,17 @@ class SlaveData:
) )
class TSState(Enum):
TS_INACTIVE = 0
TS_ACTIVE = 1
TS_PRECHARGE = 2
TS_DISCHARGE = 3
TS_ERROR = 4
TS_CHARGING_CHECK = 5
TS_CHARGING = 6
TS_BALANCING = 7
class AccumulatorData: class AccumulatorData:
slaves: list[SlaveData] slaves: list[SlaveData]
min_voltage: float min_voltage: float
@ -81,6 +91,7 @@ class AccumulatorData:
panic: bool panic: bool
panic_errorcode: int panic_errorcode: int
panic_errorarg: int panic_errorarg: int
ts_state: TSState
def __init__(self) -> None: def __init__(self) -> None:
self.slaves = [SlaveData() for _ in range(N_SLAVES)] self.slaves = [SlaveData() for _ in range(N_SLAVES)]
@ -94,6 +105,7 @@ class AccumulatorData:
self.panic = False self.panic = False
self.panic_errorcode = self.panic_errorarg = 0 self.panic_errorcode = self.panic_errorarg = 0
self.time_since_last_frame = 0 self.time_since_last_frame = 0
self.ts_state = TSState.TS_INACTIVE
def fill_dummy_data(self): def fill_dummy_data(self):
self.min_voltage = random.uniform(1, 3) self.min_voltage = random.uniform(1, 3)
@ -317,15 +329,15 @@ class Window(QWidget):
self.l_current.setNum(data.current) self.l_current.setNum(data.current)
self.l_current.setAlignment(Qt.AlignLeft) self.l_current.setAlignment(Qt.AlignLeft)
self.l8 = QLabel("Error") self.l8 = QLabel("State")
self.l_error = QLabel() self.l_state = QLabel()
self.l_error.setText(str(data.panic)) self.l_state.setText(data.ts_state.name)
self.l_error.setAlignment(Qt.AlignLeft) self.l_state.setAlignment(Qt.AlignLeft)
self.l_errorcode = QLabel() self.l_errorcode = QLabel()
self.l_errorcode.setText(str(data.panic_errorcode)) self.l_errorcode.setText("")
self.l_errorcode.setAlignment(Qt.AlignLeft) self.l_errorcode.setAlignment(Qt.AlignLeft)
self.l_errorarg = QLabel() self.l_errorarg = QLabel()
self.l_errorarg.setText(str(data.panic_errorarg)) self.l_errorarg.setText("")
self.l_errorcode.setAlignment(Qt.AlignLeft) self.l_errorcode.setAlignment(Qt.AlignLeft)
self.l9 = QLabel("Time Since Last Dataframe") self.l9 = QLabel("Time Since Last Dataframe")
@ -352,7 +364,7 @@ class Window(QWidget):
grid_accumulator.addWidget(self.l_min_temp, 0, 3) grid_accumulator.addWidget(self.l_min_temp, 0, 3)
grid_accumulator.addWidget(self.l_max_temp, 1, 3) grid_accumulator.addWidget(self.l_max_temp, 1, 3)
grid_accumulator.addWidget(self.l_current, 2, 3) grid_accumulator.addWidget(self.l_current, 2, 3)
grid_accumulator.addWidget(self.l_error, 5, 1) grid_accumulator.addWidget(self.l_state, 5, 1)
grid_accumulator.addWidget(self.l_errorcode, 5, 2) grid_accumulator.addWidget(self.l_errorcode, 5, 2)
grid_accumulator.addWidget(self.l_errorarg, 5, 3) grid_accumulator.addWidget(self.l_errorarg, 5, 3)
grid_accumulator.addWidget(self.l_time_since_last_frame, 3, 3) grid_accumulator.addWidget(self.l_time_since_last_frame, 3, 3)
@ -463,9 +475,20 @@ class Window(QWidget):
self.l_min_temp.setText(f"{data.min_temp:.02f}") self.l_min_temp.setText(f"{data.min_temp:.02f}")
self.l_max_temp.setText(f"{data.max_temp:.02f}") self.l_max_temp.setText(f"{data.max_temp:.02f}")
self.l_current.setText(f"{data.current:.02f}") self.l_current.setText(f"{data.current:.02f}")
self.l_error.setText(str(data.panic)) self.l_state.setText(data.ts_state.name)
self.l_errorcode.setText(str(data.panic_errorcode)) if data.panic:
self.l_errorarg.setText(str(data.panic_errorarg)) self.l_errorcode.setText(str(data.panic_errorcode))
self.l_errorarg.setText(str(data.panic_errorarg))
for l in (self.l_state, self.l_errorcode, self.l_errorarg):
l.setStyleSheet("color: red; font-weight: bold;")
elif data.ts_state == TSState.TS_CHARGING:
for l in (self.l_state, self.l_errorcode, self.l_errorarg):
l.setStyleSheet("color: green; font-weight: bold;")
else:
self.l_errorcode.setText("")
self.l_errorarg.setText("")
for l in (self.l_state, self.l_errorcode, self.l_errorarg):
l.setStyleSheet("color: black; font-weight: normal;")
last_time = time.time() - data.last_frame last_time = time.time() - data.last_frame
self.l_time_since_last_frame.setText(f"{last_time:.03f}") self.l_time_since_last_frame.setText(f"{last_time:.03f}")
@ -651,8 +674,9 @@ class Worker(QObject):
data.panic_errorarg = buf[1] data.panic_errorarg = buf[1]
def decode_status_frame(self, buf: bytes): def decode_status_frame(self, buf: bytes):
ts_state = buf[1] & 0x7F state = buf[0] & 0x7F
data.panic = ts_state == TS_ERROR data.ts_state = TSState(state)
data.panic = data.ts_state == TSState.TS_ERROR
INTERNAL_RESISTANCE_CURVE_X = [2.0, 4.12] INTERNAL_RESISTANCE_CURVE_X = [2.0, 4.12]
INTERNAL_RESISTANCE_CURVE_Y = [0.0528, 0.0294] INTERNAL_RESISTANCE_CURVE_Y = [0.0528, 0.0294]