Read current & coulomb counter

This commit is contained in:
2023-01-28 20:43:15 +01:00
parent 23f96c214b
commit 5bf6b286b6
4 changed files with 70 additions and 18 deletions

View File

@ -1,4 +1,6 @@
from abc import abstractmethod
import sys
import struct
import time
from dataclasses import dataclass
from enum import Enum
@ -12,10 +14,12 @@ from pymodbus.client import ModbusSerialClient
MODBUS_BAUDRATE = 115200
VOLTAGE_ADDRESS = 0x00
CURRENT_ADDRESS = 2048
SLAVE_UNIT_ID = 0x02
NUM_CELLS = 3
VOLTAGE_QUANT = 1 / 10000.0
CURRENT_QUANT = 1 / (500 * 72e-6)
THRESH_UV = 2.5
THRESH_OV = 4.2
@ -58,6 +62,8 @@ class BMSData:
voltages: list[float]
temperatures: list[float]
error: BMSError
current: float
current_integrated: float
class BMS(QObject):
@ -75,7 +81,13 @@ class BMS(QObject):
self._load = load
self._num_cells = num_cells
self._num_sensors = num_sensors
self._data = BMSData([0.0] * num_cells, [0.0] * num_sensors, BMSError.NONE)
self._data = BMSData(
voltages=[0.0] * num_cells,
temperatures=[0.0] * num_sensors,
error=BMSError.NONE,
current=0,
current_integrated=0,
)
def _check_for_errors(self):
error = BMSError.NONE
@ -126,19 +138,33 @@ class BMSEvalBoard(BMS):
def do_work(self):
while True:
self._check_for_errors()
time.sleep(0.1)
result = self._dev.read_holding_registers(
VOLTAGE_ADDRESS, NUM_CELLS, SLAVE_UNIT_ID
)
if result.isError():
print(f"ERROR READING VOLTAGES: {result}")
else:
self._data.voltages = list(
map(lambda v: v * VOLTAGE_QUANT, result.registers)
)
self._last_voltage_time = time.time()
self.dataUpdated.emit(self._data)
self._check_for_errors()
print(f"ERROR READING VOLTAGES: {result}", file=sys.stderr)
continue
self._data.voltages = list(
map(lambda v: v * VOLTAGE_QUANT, result.registers)
)
self._last_voltage_time = time.time()
result = self._dev.read_holding_registers(CURRENT_ADDRESS, 6, SLAVE_UNIT_ID)
if result.isError():
print(f"ERROR READING CURRENT: {result}", file=sys.stderr)
continue
assert len(result.registers) == 6
self._data.current = (
(result.registers[0] << 16) | result.registers[1]
) * CURRENT_QUANT
self._data.current_integrated = (
(result.registers[2] << 48)
| (result.registers[3] << 32)
| (result.registers[4] << 16)
| result.registers[5]
) * CURRENT_QUANT
self.dataUpdated.emit(self._data)
@Slot(list)
def _updateTemperatures(self, temps: list[float]):

View File

@ -89,3 +89,5 @@ class GUI(QObject):
self._win.setProperty("temp_min", min(data.temperatures))
self._win.setProperty("temp_max", max(data.temperatures))
self._win.setProperty("bmsError", str(data.error))
self._win.setProperty("currentActual", data.current)
self._win.setProperty("currentIntegrated", data.current_integrated)

View File

@ -51,6 +51,8 @@ class Logger(QObject):
time.time() - self._start_time,
self._data.error.value,
self._current,
self._data.current,
self._data.current_integrated,
*self._data.voltages,
*self._data.temperatures,
]