-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
611 lines (525 loc) · 19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
""" This is the main file for the BMS HV Utility. It is used to display the data from the BMS HV and to change its settings"""
import json
from types import SimpleNamespace
from dataclasses import dataclass
import queue
import threading
import sys
import time
from statistics import median
import PySimpleGUI as sg
import serial
from colorama import Fore, Style
import numpy as np
sg.theme("Material2")
sg.set_options(font=("Helvetica", 11))
IMAGE_PATH = "putm_logo.png"
SERIAL_DATA_IN_FREQ_SEC = 0.250
STANDARD_TEXT_WIDTH = 9
CELL_VOLTAGE_TABLE_COLUMNS = 15
CELL_VOLTAGE_TABLE_ROWS = 9
TEMPERATURE_TABLE_COLUMNS = 15
TEMPERATURE_TABLE_ROWS = 3
ERROR_TABLE_COLUMNS = 2
ERROR_TABLE_ROWS = 6
SOC_TABLE_COLUMNS = 4
SOC_TABLE_ROWS = 1
FLOAT_PRECISION = 4
KEY_CONNECTION_STATUS = "-CONNECTION-STATUS-"
KEY_TIMESTAMP = "-TIMESTAMP-"
KEY_CELL_MAX_VOLTAGE = "-MAX-VOLTAGE-"
KEY_CELL_MAX_VOLTAGE_LTC = "-MAX-VOLTAGE-LTC-"
KEY_CELL_MAX_VOLTAGE_CELL = "-MAX-VOLTAGE-CELL-"
KEY_CELL_MIN_VOLTAGE = "-MIN-VOLTAGE-"
KEY_CELL_MIN_VOLTAGE_LTC = "-MIN-VOLTAGE-LTC-"
KEY_CELL_MIN_VOLTAGE_CELL = "-MIN-VOLTAGE-CELL-"
KEY_MAX_TEMPERATURE = "-MAX-TEMPERATURE-"
KEY_CURRENT = "-CURRENT-"
KEY_ACC_VOLTAGE = "-ACC-VOLTAGE-"
KEY_CAR_VOLTAGE = "-CAR-VOLTAGE-"
KEY_SOC = "-SOC-"
KEY_CELL_VOLTAGE = "-CELL-VOLTAGE-"
KEY_TEMPERATURE = "-TEMPERATURE-"
KEY_ERROR = "-CELL-ERRORS-"
KEY_CHARGING_STATUS = "-CHARGING-STATUS-"
KEY_BALANCE_STATUS = "-BALANCE-STATUS-"
@dataclass
class BmsHvData:
"""Dataclass for BMS HV data"""
current: float
acc_voltage: float
car_voltage: float
soc: list[float]
cell_voltage: list[float]
temperature: list[float]
discharge: list[int]
balance: int
charging: int
under_voltage: list[int]
over_voltage: list[int]
under_temperature: list[int]
over_temperature: list[int]
over_current: list[int]
current_sensor_disconnected: list[int]
timestamp: float
basic_info = [
[
sg.Text("Connection Status: "),
sg.Text("-", size=(12, 1), key=KEY_CONNECTION_STATUS, justification="l"),
],
[
sg.Text("Timestamp:"),
sg.Text("-", auto_size_text=True, key=KEY_TIMESTAMP),
sg.Text("s"),
],
[
sg.Text("Current:"),
sg.Text("-", auto_size_text=True, key=KEY_CURRENT),
sg.Text("A"),
],
# [
# sg.Text("Acc Voltage:"),
# sg.Text("-", auto_size_text=True, key=KEY_ACC_VOLTAGE),
# sg.Text("V"),
# ],
# [
# sg.Text("Car Voltage:"),
# sg.Text("-", auto_size_text=True, key=KEY_CAR_VOLTAGE),
# sg.Text("V"),
# ],
[
sg.Text("Charging Status:"),
sg.Text("-", auto_size_text=True, key=KEY_CHARGING_STATUS),
],
[
sg.Text("Balance Status:"),
sg.Text("-", auto_size_text=True, key=KEY_BALANCE_STATUS),
],
]
cell_voltage = [
[
sg.Table(
values=[
["-" for i in range(CELL_VOLTAGE_TABLE_COLUMNS)]
for j in range(CELL_VOLTAGE_TABLE_ROWS)
],
headings=[f"LTC {j}" for j in range(CELL_VOLTAGE_TABLE_COLUMNS)],
select_mode=sg.TABLE_SELECT_MODE_NONE,
display_row_numbers=True,
auto_size_columns=False,
justification="center",
num_rows=CELL_VOLTAGE_TABLE_ROWS,
enable_events=False,
hide_vertical_scroll=True,
key=KEY_CELL_VOLTAGE,
def_col_width=STANDARD_TEXT_WIDTH,
)
],
[
sg.Text("Max Voltage:"),
sg.Text("-", key=KEY_CELL_MAX_VOLTAGE, justification="l"),
sg.Text("V"),
sg.Text("LTC:"),
sg.Text("-", key=KEY_CELL_MAX_VOLTAGE_LTC, justification="l"),
sg.Text("Cell:"),
sg.Text("-", key=KEY_CELL_MAX_VOLTAGE_CELL, justification="l"),
],
[
sg.Text("Min Voltage:"),
sg.Text("-", key=KEY_CELL_MIN_VOLTAGE, justification="l"),
sg.Text("V"),
sg.Text("LTC:"),
sg.Text("-", key=KEY_CELL_MIN_VOLTAGE_LTC, justification="l"),
sg.Text("Cell:"),
sg.Text("-", key=KEY_CELL_MIN_VOLTAGE_CELL, justification="l"),
],
]
temperature = [
[
sg.Table(
values=[
["-" for i in range(TEMPERATURE_TABLE_COLUMNS)]
for j in range(TEMPERATURE_TABLE_ROWS)
],
headings=[f"Col {j+1}" for j in range(TEMPERATURE_TABLE_COLUMNS)],
select_mode=sg.TABLE_SELECT_MODE_NONE,
display_row_numbers=True,
auto_size_columns=False,
justification="center",
num_rows=TEMPERATURE_TABLE_ROWS,
enable_events=False,
hide_vertical_scroll=True,
key=KEY_TEMPERATURE,
def_col_width=STANDARD_TEXT_WIDTH,
)
],
[
sg.Text("Max Temp:"),
sg.Text("-", key=KEY_MAX_TEMPERATURE, justification="L"),
sg.Text("°C"),
],
]
error = [
[
sg.Table(
values=[
["-" for i in range(ERROR_TABLE_COLUMNS)]
for j in range(ERROR_TABLE_ROWS)
],
headings=["Error", "Value"],
select_mode=sg.TABLE_SELECT_MODE_NONE,
display_row_numbers=False,
auto_size_columns=False,
justification="r",
num_rows=ERROR_TABLE_ROWS,
enable_events=False,
hide_vertical_scroll=True,
key=KEY_ERROR,
def_col_width=14,
)
]
]
soc = [
[
sg.Table(
values=[
["-" for i in range(SOC_TABLE_COLUMNS)] for j in range(SOC_TABLE_ROWS)
],
headings=[
"Min",
"Max",
"Avg",
"Median",
],
select_mode=sg.TABLE_SELECT_MODE_NONE,
display_row_numbers=False,
auto_size_columns=False,
justification="c",
num_rows=SOC_TABLE_ROWS,
enable_events=False,
hide_vertical_scroll=True,
key=KEY_SOC,
def_col_width=STANDARD_TEXT_WIDTH,
)
]
]
charge_control = [
[sg.Button("Full Battery Soc")],
[sg.Button("Start Charging")],
[sg.Button("Stop Charging")],
[sg.Button("Start Balance")],
[sg.Button("Stop Balance")],
[sg.Button("Set Charge Current to 1A")],
[sg.Button("Set Charge Current to 2A")],
[sg.Button("Set Charge Current to 4A")],
[sg.Button("Set Charge Current to 8A")],
[sg.Button("Set Charge Current to 12A")],
]
exit_button = [[sg.Button("Exit")]]
image = sg.Image(IMAGE_PATH)
frame_basic_info = sg.Frame("Basic Info", basic_info)
frame_charge_control = sg.Frame("Charge control", charge_control)
frame_exit_button = sg.Frame("Exit", exit_button)
frame_cell_voltage = sg.Frame("Cell Voltages", cell_voltage)
frame_temperature = sg.Frame("Temperatures", temperature)
frame_error = sg.Frame("Errors", error)
frame_soc = sg.Frame("Soc", soc)
column_left = sg.Column(
[[frame_basic_info], [frame_charge_control], [frame_exit_button]],
element_justification="l",
vertical_alignment="top",
)
column_right = sg.Column(
[[frame_cell_voltage], [frame_temperature], [frame_soc], [frame_error]],
element_justification="l",
vertical_alignment="top",
)
layout = [
[image],
[column_left, sg.VerticalSeparator(pad=None), column_right],
]
window = sg.Window("BMS HV Utility", layout, element_justification="c")
def float_to_string_with_precision(value, precision):
"""Converts a float to a string with the specified precision"""
return f"{value:.{precision}f}"
def mark_cell_if_discharge(value, is_discharging):
"""Marks a cell if it is discharging"""
return (f"#{value}#") if is_discharging else value
def print_ok(msg):
"""Prints an ok message"""
print(f"{Fore.GREEN}{msg}{Style.RESET_ALL}")
def print_error(msg):
"""Prints an error message"""
print(f"{Fore.RED}{msg}{Style.RESET_ALL}")
def print_warning(msg):
"""Prints a warning message"""
print(f"{Fore.YELLOW}{msg}{Style.RESET_ALL}")
def to_matrix(l, columns):
"""Converts a list to a matrix with the specified number of columns"""
matrix = np.reshape(np.array(l), (columns, -1)).T
return matrix
def send_message_to_write_queue(write_queue, message):
"""This function is used to send a message to the write queue"""
try:
write_queue.put_nowait(message)
except queue.Full:
print_error("The write queue is full, the message will be discarded")
def serial_task(port, read_queue, write_queue, connected_event, exit_event):
"""This function is used to read data from and to write data to the serial port"""
serial_task_prefix = "SERIAL TASK: "
write_prefix = "WRITE: "
read_prefix = "READ: "
keep_alive_prefix = "KEEP_ALIVE: "
keep_alive_message = "!C-CC@"
ser = serial.Serial()
ser.port = port
# this value has to be bigger than frequency of sending data from BMS HV
ser.timeout = SERIAL_DATA_IN_FREQ_SEC + 0.2
while not ser.is_open:
if exit_event.is_set():
return
try:
ser.open()
connected_event.set()
print_ok(f"{serial_task_prefix} Serial port: {port} opened")
except serial.serialutil.SerialException:
print_error(f"{serial_task_prefix} Serial port: {port} not available")
time.sleep(1)
continue
while True:
if exit_event.is_set():
ser.close()
return
try:
print_ok("-------------------------------------------------------")
# Keep alive
ser.write(keep_alive_message.encode("utf-8"))
print_ok(f"{keep_alive_prefix} Keep alive message sent to the serial port")
# Write data
try:
data = write_queue.get_nowait()
ser.write(data.encode("utf-8"))
print_ok(f"{write_prefix} New data sent to the serial port: {data}")
except queue.Empty:
print_warning(f"{write_prefix} Nothing to send to the serial port")
# Read data
try:
ser.reset_input_buffer()
ser.readline()
line = ser.readline().decode("utf-8")
if line == "":
print_error(f"{read_prefix} Nothing received from the serial port")
continue
read_queue.put_nowait(line)
print_ok(f"{read_prefix} New data received from the serial port")
except queue.Full:
print_warning(f"{read_prefix} Read queue is full")
except serial.serialutil.SerialException:
connected_event.clear()
print_error(f"{serial_task_prefix} Serial port: {port} disconnected")
while True:
if exit_event.is_set():
return
try:
ser.open()
connected_event.set()
break
except serial.serialutil.SerialException:
print_error(
f"{serial_task_prefix} Failed to reopen serial port: {port}"
)
time.sleep(1)
continue
def main():
"""Main function"""
if len(sys.argv) != 2:
print_error("Usage: python main.py <serial_port>")
sys.exit(1)
print_ok("Starting...")
serial_task_connected_event = threading.Event()
main_exit_event = threading.Event()
bms_hv_read_queue = queue.Queue(maxsize=1)
bms_hv_write_queue = queue.Queue(maxsize=1)
serial_task_thread = threading.Thread(
target=serial_task,
args=(
sys.argv[1],
bms_hv_read_queue,
bms_hv_write_queue,
serial_task_connected_event,
main_exit_event,
),
daemon=True,
)
serial_task_thread.start()
while True:
event, values = window.read(timeout=500)
if serial_task_connected_event.is_set():
window[KEY_CONNECTION_STATUS].update("Connected")
else:
window[KEY_CONNECTION_STATUS].update("Disconnected")
if event == sg.WINDOW_CLOSED or event == "Exit":
break
elif event == "Full Battery Soc":
send_message_to_write_queue(bms_hv_write_queue, "!B-FC@")
elif event == "Start Charging":
send_message_to_write_queue(bms_hv_write_queue, "!C-ON@")
elif event == "Stop Charging":
send_message_to_write_queue(bms_hv_write_queue, "!C-OF@")
elif event == "Start Balance":
send_message_to_write_queue(bms_hv_write_queue, "!B-ON@")
elif event == "Stop Balance":
send_message_to_write_queue(bms_hv_write_queue, "!B-OF@")
elif event == "Set Charge Current to 1A":
send_message_to_write_queue(bms_hv_write_queue, "!I-1A@")
elif event == "Set Charge Current to 2A":
send_message_to_write_queue(bms_hv_write_queue, "!I-2A@")
elif event == "Set Charge Current to 4A":
send_message_to_write_queue(bms_hv_write_queue, "!I-4A@")
elif event == "Set Charge Current to 8A":
send_message_to_write_queue(bms_hv_write_queue, "!I-8A@")
elif event == "Set Charge Current to 12A":
send_message_to_write_queue(bms_hv_write_queue, "!I-12@")
if not bms_hv_read_queue.empty():
bms_hv_data_json = bms_hv_read_queue.get()
try:
bms_hv_data = json.loads(
bms_hv_data_json, object_hook=lambda d: SimpleNamespace(**d)
)
bms_hv_data = BmsHvData(**bms_hv_data.__dict__)
except json.decoder.JSONDecodeError:
print_error(f"Invalid JSON: {bms_hv_data_json}")
continue
except TypeError:
print_error(
f"Received JSON is not of type BmsHvData: {bms_hv_data_json}"
)
continue
# BASIC INFO
window[KEY_TIMESTAMP].update(
float_to_string_with_precision((bms_hv_data.timestamp / 1000), 3)
)
window[KEY_MAX_TEMPERATURE].update(
float_to_string_with_precision(
max(bms_hv_data.temperature), FLOAT_PRECISION
)
)
window[KEY_CURRENT].update(
float_to_string_with_precision(bms_hv_data.current, FLOAT_PRECISION)
)
# window[KEY_ACC_VOLTAGE].update(
# float_to_string_with_precision(bms_hv_data.acc_voltage, FLOAT_PRECISION)
# )
# window[KEY_CAR_VOLTAGE].update(
# float_to_string_with_precision(bms_hv_data.car_voltage, FLOAT_PRECISION)
# )
window[KEY_CHARGING_STATUS].update("On" if bms_hv_data.charging else "Off")
window[KEY_BALANCE_STATUS].update("On" if bms_hv_data.balance else "Off")
# CELL VOLTAGE TABLE
window[KEY_CELL_VOLTAGE].update(
values=to_matrix(
[
[
mark_cell_if_discharge(
float_to_string_with_precision(v, FLOAT_PRECISION),
bms_hv_data.discharge[i],
)
for i, v in enumerate(bms_hv_data.cell_voltage)
]
],
CELL_VOLTAGE_TABLE_COLUMNS,
).tolist()
)
window[KEY_CELL_MAX_VOLTAGE].update(
float_to_string_with_precision(
max(bms_hv_data.cell_voltage), FLOAT_PRECISION
)
)
max_cell_num, max_ltc_num = np.where(
to_matrix(bms_hv_data.cell_voltage, CELL_VOLTAGE_TABLE_COLUMNS)
== max(bms_hv_data.cell_voltage)
)
window[KEY_CELL_MAX_VOLTAGE_LTC].update(max_ltc_num[0])
window[KEY_CELL_MAX_VOLTAGE_CELL].update(max_cell_num[0])
window[KEY_CELL_MIN_VOLTAGE].update(
float_to_string_with_precision(
min(bms_hv_data.cell_voltage), FLOAT_PRECISION
)
)
min_cell_num, min_ltc_num = np.where(
to_matrix(bms_hv_data.cell_voltage, CELL_VOLTAGE_TABLE_COLUMNS)
== min(bms_hv_data.cell_voltage)
)
window[KEY_CELL_MIN_VOLTAGE_LTC].update(min_ltc_num[0])
window[KEY_CELL_MIN_VOLTAGE_CELL].update(min_cell_num[0])
# TEMPERATURE TABLE
window[KEY_TEMPERATURE].update(
values=to_matrix(
[
float_to_string_with_precision(v, FLOAT_PRECISION)
for v in bms_hv_data.temperature
],
TEMPERATURE_TABLE_COLUMNS,
).tolist()
)
# SOC TABLE
window[KEY_SOC].update(
values=to_matrix(
[
[
float_to_string_with_precision(v * 100, FLOAT_PRECISION)
for v in [
min(bms_hv_data.soc),
max(bms_hv_data.soc),
sum(bms_hv_data.soc) / len(bms_hv_data.soc),
median(bms_hv_data.soc),
]
]
],
SOC_TABLE_COLUMNS,
).tolist()
)
errors = [
([e[0], e[2]] if e[1] == 1 else ["-", "-"])
for e in [
[
"Under Voltage",
bms_hv_data.under_voltage[0],
bms_hv_data.under_voltage[1],
],
[
"Over Voltage",
bms_hv_data.over_voltage[0],
bms_hv_data.over_voltage[1],
],
[
"Under Temperature",
bms_hv_data.under_temperature[0],
bms_hv_data.under_temperature[1],
],
[
"Over Temperature",
bms_hv_data.over_temperature[0],
bms_hv_data.over_temperature[1],
],
[
"Over Current",
bms_hv_data.over_current[0],
bms_hv_data.over_current[1],
],
[
"Current Sensor",
bms_hv_data.current_sensor_disconnected[0],
"Disconnected",
],
]
]
# ERROR TABLE
window[KEY_ERROR].update(values=errors)
main_exit_event.set()
serial_task_thread.join()
window.close()
print_ok("Exiting...")
return 0
if __name__ == "__main__":
main()