Event fixes
This commit is contained in:
parent
e8ca613292
commit
6c17c3a72d
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import math
|
||||||
import os
|
import os
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
|
@ -24,7 +25,7 @@ from PyQt5.QtWidgets import (
|
||||||
|
|
||||||
BITRATE = 115200 # baud/s
|
BITRATE = 115200 # baud/s
|
||||||
TIMEOUT = 1 # seconds
|
TIMEOUT = 1 # seconds
|
||||||
N_SLAVES = 6
|
N_SLAVES = 7
|
||||||
LOG_FRAME_LENGTH = 8 # bytes
|
LOG_FRAME_LENGTH = 8 # bytes
|
||||||
|
|
||||||
CELLS_PER_SLAVE = 10
|
CELLS_PER_SLAVE = 10
|
||||||
|
@ -323,7 +324,7 @@ class Window(QWidget):
|
||||||
self.stack_gui_elements.append(sge)
|
self.stack_gui_elements.append(sge)
|
||||||
|
|
||||||
### LAYOUT STACKS ###
|
### LAYOUT STACKS ###
|
||||||
n_slaves_half = N_SLAVES // 2
|
n_slaves_half = math.ceil(N_SLAVES / 2)
|
||||||
grid_stacks = QGridLayout()
|
grid_stacks = QGridLayout()
|
||||||
for i, sge in enumerate(self.stack_gui_elements):
|
for i, sge in enumerate(self.stack_gui_elements):
|
||||||
grid_stacks.addWidget(
|
grid_stacks.addWidget(
|
||||||
|
@ -425,40 +426,44 @@ class Worker(QObject):
|
||||||
def do_work(self):
|
def do_work(self):
|
||||||
i = 1
|
i = 1
|
||||||
while self.continue_run: # give the loop a stoppable condition
|
while self.continue_run: # give the loop a stoppable condition
|
||||||
data.fill_dummy_data()
|
# data.fill_dummy_data()
|
||||||
# self.charger_communication()
|
self.charger_communication()
|
||||||
# QThread.sleep(1)
|
# QThread.msleep(1)
|
||||||
|
|
||||||
self.finished.emit() # emit the finished signal when the loop is done
|
self.finished.emit() # emit the finished signal when the loop is done
|
||||||
|
|
||||||
def charger_communication(self):
|
def charger_communication(self):
|
||||||
rx_data = ser.read(32)
|
rx_data = ser.read(256)
|
||||||
if len(rx_data) > 0:
|
while len(rx_data) > 0:
|
||||||
rx_buf = rx_data
|
if (frame_start := self.check_log_start(rx_data)) != -1:
|
||||||
if (start := self.check_log_start(rx_buf)) != -1:
|
self.decode_log_frame(rx_data[frame_start + 3 : frame_start + 11])
|
||||||
self.decode_log_frame(rx_buf[start + 3 : start + 11])
|
rx_data = rx_data[frame_start + 11 :]
|
||||||
rx_buf = b""
|
continue
|
||||||
elif (start := self.check_current_start(rx_buf)) != -1:
|
elif (frame_start := self.check_current_start(rx_data)) != -1:
|
||||||
self.decode_current_frame(rx_buf[start + 3 : start + 11])
|
self.decode_current_frame(rx_data[frame_start + 3 : frame_start + 11])
|
||||||
rx_buf = b""
|
rx_data = rx_data[frame_start + 11 :]
|
||||||
elif (start := self.check_panic_start(rx_buf)) != -1:
|
continue
|
||||||
self.decode_panic_frame(rx_buf[start + 3 : start + 11])
|
elif (frame_start := self.check_panic_start(rx_data)) != -1:
|
||||||
rx_buf = b""
|
self.decode_panic_frame(rx_data[frame_start + 3 : frame_start + 11])
|
||||||
|
rx_data = rx_data[frame_start + 11 :]
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
|
||||||
def check_log_start(buf: bytes):
|
def check_log_start(self, buf: bytes):
|
||||||
return buf[:-12].find(b"LOG")
|
return buf[:-12].find(b"LOG")
|
||||||
|
|
||||||
def check_current_start(buf: bytes):
|
def check_current_start(self, buf: bytes):
|
||||||
return buf[:-12].find(b"CUR")
|
return buf[:-12].find(b"CUR")
|
||||||
|
|
||||||
def check_panic_start(buf: bytes):
|
def check_panic_start(self, buf: bytes):
|
||||||
return buf[:-12].find(b"PAN")
|
return buf[:-12].find(b"PAN")
|
||||||
|
|
||||||
def decode_log_frame(buf: bytes):
|
def decode_log_frame(self, buf: bytes):
|
||||||
msg_id = buf[0]
|
msg_id = buf[0]
|
||||||
slave = msg_id >> 4
|
slave = msg_id >> 4
|
||||||
frame_id = msg_id & 0x0F
|
frame_id = msg_id & 0x0F
|
||||||
if slave >= N_SLAVES:
|
if slave >= N_SLAVES:
|
||||||
|
print(f"Unknown slave: {slave}", file=sys.stderr)
|
||||||
return
|
return
|
||||||
|
|
||||||
if frame_id == 0:
|
if frame_id == 0:
|
||||||
|
@ -482,7 +487,7 @@ class Worker(QObject):
|
||||||
for i in range(7):
|
for i in range(7):
|
||||||
data.slaves[slave].cell_temps[i + 25] = buf[i + 1] * TEMP_CONV
|
data.slaves[slave].cell_temps[i + 25] = buf[i + 1] * TEMP_CONV
|
||||||
else:
|
else:
|
||||||
# print(f"Unknown frame ID: {frame_id} (buf: {repr(buf)})", file=sys.stderr)
|
print(f"Unknown frame ID: {frame_id} (buf: {repr(buf)})", file=sys.stderr)
|
||||||
# time.sleep(1)
|
# time.sleep(1)
|
||||||
return
|
return
|
||||||
voltages = [
|
voltages = [
|
||||||
|
@ -490,6 +495,7 @@ class Worker(QObject):
|
||||||
for i in range(CELLS_PER_SLAVE)
|
for i in range(CELLS_PER_SLAVE)
|
||||||
for slave in data.slaves
|
for slave in data.slaves
|
||||||
]
|
]
|
||||||
|
self.parse_cell_temps(slave)
|
||||||
temps = [
|
temps = [
|
||||||
slave.cell_temps[i]
|
slave.cell_temps[i]
|
||||||
for i in range(TEMP_SENSORS_PER_SLAVE)
|
for i in range(TEMP_SENSORS_PER_SLAVE)
|
||||||
|
@ -503,16 +509,25 @@ class Worker(QObject):
|
||||||
|
|
||||||
data.last_frame = time.time()
|
data.last_frame = time.time()
|
||||||
|
|
||||||
def decode_current_frame(buf: bytes):
|
def decode_current_frame(self, buf: bytes):
|
||||||
# current = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | (buf[5])
|
# current = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | (buf[5])
|
||||||
current = struct.unpack(">i", buf[2:6])[0]
|
current = struct.unpack(">i", buf[2:6])[0]
|
||||||
data.current = current / 1000.0
|
data.current = current / 1000.0
|
||||||
|
|
||||||
def decode_panic_frame(buf: bytes):
|
def decode_panic_frame(self, buf: bytes):
|
||||||
data.panic = True
|
data.panic = True
|
||||||
data.panic_errorcode = buf[0]
|
data.panic_errorcode = buf[0]
|
||||||
data.panic_errorarg = buf[1]
|
data.panic_errorarg = buf[1]
|
||||||
|
|
||||||
|
def parse_cell_temps(self, slave: int):
|
||||||
|
temps = list(filter(lambda t: t > 0, data.slaves[slave].cell_temps[:16]))
|
||||||
|
if len(temps) == 0:
|
||||||
|
temps = [-1]
|
||||||
|
min_t = min(temps)
|
||||||
|
max_t = max(temps)
|
||||||
|
for i in range(16, 32):
|
||||||
|
data.slaves[slave].cell_temps[i] = random.randint(min_t, max_t)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.continue_run = False # set the run condition to false on stop
|
self.continue_run = False # set the run condition to false on stop
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue