-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbms.cpp
822 lines (716 loc) · 29.4 KB
/
bms.cpp
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/* Copyright 2020 Neil Jansen ([email protected])
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "bms.h"
// constructor
OverkillSolarBms::OverkillSolarBms() {
is_initialized = false;
voltage = 0;
current = 0;
balance_capacity = 0;
rate_capacity = 0;
cycle_count = 0;
#ifdef BMS_OPTION_PRODUCTION_DATE
production_date = 0;
#endif
balance_status = 0;
protection_status = 0;
#ifdef BMS_OPTION_SW_VERSION
software_version = 0;
#endif
remaining_soc = 0;
mosfet_status = 0;
num_cells = 0;
num_ntcs = 0;
for (uint8_t i; i<BMS_MAX_NTCs; i++) {
ntc_temps[i] = 0;
}
for (uint8_t i; i<BMS_MAX_CELLS; i++) {
cell_voltages[i] = 0;
}
#ifdef BMS_OPTION_NAME
bms_name = String("");
#endif
}
// ###########################################################################
// Start processing
void OverkillSolarBms::begin(Stream *port) {
#ifdef BMS_OPTION_DEBUG
Serial.println("OverkillSolarBMS Begin!");
#endif
serial = port;
is_initialized = true;
rx_state = BMS_STATE_WAIT_FOR_START_BYTE;
uint32_t now = millis();
last_query_time = now;
last_10ms_time = now;
last_0x03_timestamp = now;
last_0x04_timestamp = now;
tx_query_rate = 1000;
}
// End processing
void OverkillSolarBms::end() {
is_initialized = false;
}
// Call this as fast as possible within the sketch's loop() function
void OverkillSolarBms::main_task() {
if (is_initialized) {
uint32_t now = millis();
// 10 millisecond task (serial data task)
if (now - last_10ms_time >= 10) {
serial_rx_task();
last_10ms_time = now;
}
// query task
if (now - last_query_time >= tx_query_rate) {
query_basic_info();
query_cell_voltages();
last_query_time = now;
}
}
}
bool OverkillSolarBms::get_comm_error_state() {
uint32_t now = millis();
bool err_state = false;
if (now - last_0x03_timestamp >= (tx_query_rate * 2)) {
err_state = true;
}
if (now - last_0x04_timestamp >= (tx_query_rate * 2)) {
err_state = true;
}
return err_state;
}
void OverkillSolarBms::set_query_rate(uint16_t rate) {
tx_query_rate = rate;
}
// ###########################################################################
// 0x03 Basic Info & Status
// ###########################################################################
float OverkillSolarBms::get_voltage() {
// Voltage is stored internally as a uint16_t in units of 10mV
return voltage * 0.01;
}
float OverkillSolarBms::get_current() {
// Current is stored internally as a uint16_t in units of 10mA
// NOTE: There is an error in the API documentation:
// When charging, the units are 10 mA.
// When discharging, the units are
return current * 0.01;
}
float OverkillSolarBms::get_balance_capacity() {
// Capacity is stored internally as a uint16_t in units of 10mAh
return balance_capacity * 0.01;
}
float OverkillSolarBms::get_rate_capacity() {
// Capacity is stored internally as a uint16_t in units of 10mAh
return rate_capacity * 0.01;
}
uint16_t OverkillSolarBms::get_cycle_count() {
return cycle_count;
}
#ifdef BMS_OPTION_PRODUCTION_DATE
ProductionDate OverkillSolarBms::get_production_date() {
// Production date is stored internally as a uint16_t, bit-packed as follows:
// 1111110000000000
// Field 5432109876543210 # bits offset
// ====== ================ ====== ======
// Day: xxxxx 5 0
// Month: xxxx 4 5
// Year: xxxxxxx 7 9
ProductionDate date;
date.year = 2000 + ((production_date >> 9) & 0b1111111);
date.month = (production_date >> 5) & 0b1111;
date.day = production_date & 0b11111;
return date;
}
#endif
bool OverkillSolarBms::get_balance_status(uint8_t cell_index) {
if (cell_index <= 31) {
return (balance_status >> cell_index) & 1;
}
else {
return false;
}
}
#ifdef BMS_OPTION_FAULT_COUNTS
bool OverkillSolarBms::has_new_fault_occured(uint8_t index) {
return (bool)((((last_protection_status ^ protection_status) & protection_status) >> index) & 1);
}
#endif
ProtectionStatus OverkillSolarBms::get_protection_status() {
ProtectionStatus status;
status.single_cell_overvoltage_protection = (bool)((protection_status) & 1);
status.single_cell_undervoltage_protection = (bool)((protection_status >> 1) & 1);
status.whole_pack_overvoltage_protection = (bool)((protection_status >> 2) & 1);
status.whole_pack_undervoltage_protection = (bool)((protection_status >> 3) & 1);
status.charging_over_temperature_protection = (bool)((protection_status >> 4) & 1);
status.charging_low_temperature_protection = (bool)((protection_status >> 5) & 1);
status.discharge_over_temperature_protection= (bool)((protection_status >> 6) & 1);
status.discharge_low_temperature_protection = (bool)((protection_status >> 7) & 1);
status.charging_overcurrent_protection = (bool)((protection_status >> 8) & 1);
status.discharge_overcurrent_protection = (bool)((protection_status >> 9) & 1);
status.short_circuit_protection = (bool)((protection_status >> 10) & 1);
status.front_end_detection_ic_error = (bool)((protection_status >> 11) & 1);
status.software_lock_mos = (bool)((protection_status >> 12) & 1);
return status;
}
#ifdef BMS_OPTION_FAULT_COUNTS
FaultCount OverkillSolarBms::get_fault_counts() {
return fault_count;
}
void OverkillSolarBms::clear_fault_counts() {
fault_count.single_cell_overvoltage_protection = 0;
fault_count.single_cell_undervoltage_protection = 0;
fault_count.whole_pack_overvoltage_protection = 0;
fault_count.whole_pack_undervoltage_protection = 0;
fault_count.charging_over_temperature_protection = 0;
fault_count.charging_low_temperature_protection = 0;
fault_count.discharge_over_temperature_protection = 0;
fault_count.discharge_low_temperature_protection = 0;
fault_count.charging_overcurrent_protection = 0;
fault_count.discharge_overcurrent_protection = 0;
fault_count.short_circuit_protection = 0;
fault_count.front_end_detection_ic_error = 0;
fault_count.software_lock_mos = 0;
}
#endif
bool OverkillSolarBms::get_protection_status_summary() {
return (bool)protection_status & 0b1111111111111;
}
#ifdef BMS_OPTION_SW_VERSION
SoftwareVersion OverkillSolarBms::get_software_version() {
SoftwareVersion version;
version.major = (software_version >> 4) & 0b1111;
version.minor = software_version & 0b1111;
return version;
}
#endif
uint8_t OverkillSolarBms::get_state_of_charge() {
// note: the result is not bounds-checked, I assume that the BMS could
// return higher than 100%, but rather than hiding that here and
// bounding it to 100, it is returned as-is.
return remaining_soc;
}
bool OverkillSolarBms::get_discharge_mosfet_status() {
return (mosfet_status >> 1) & 1;
}
bool OverkillSolarBms::get_charge_mosfet_status() {
return mosfet_status & 1;
}
uint8_t OverkillSolarBms::get_num_cells() {
return num_cells;
}
uint8_t OverkillSolarBms::get_num_ntcs() {
return num_ntcs;
}
// Returns the temperature, in celsius
float OverkillSolarBms::get_ntc_temperature(uint8_t ntc_index) {
if (ntc_index + 1 <= BMS_MAX_NTCs) {
float temp = ntc_temps[ntc_index];
temp *= 0.1; // Convert fixed-precision int 0.1 degrees K per LSB to float degrees K
temp -= 273.15; // Convert Kelvin to Celsius
return temp;
}
else {
return 1.0 / 0.0; // NaN
}
}
// ###########################################################################
// 0x04 Cell Voltages
// ###########################################################################
// Returns the cell voltage, in volts
float OverkillSolarBms::get_cell_voltage(uint8_t cell_index) {
if (cell_index + 1 <= BMS_MAX_CELLS) {
float voltage = cell_voltages[cell_index];
voltage *= 0.001; // Convert millivolts to volts
return voltage;
}
else {
return 1.0 / 0.0; // NaN
}
}
// ###########################################################################
// 0x05 BMS Name
// ###########################################################################
#ifdef BMS_OPTION_NAME
String OverkillSolarBms::get_bms_name() {
// Clear the bms_name string
bms_name = String("");
#ifdef BMS_OPTION_DEBUG
Serial.println("Query 0x05 BMS Name");
#endif
uint8_t length = 0;
uint8_t *data = NULL;
write(true, BMS_REG_NAME, length, data);
uint32_t t0 = millis();
// Handle the incoming serial data until the BMS name is received
while(1) {
serial_rx_task();
delay(10);
// Wait for the RX task to receive the message, parse it and store
// it in the bms_name
if (bms_name.length() > 0) {
break;
}
// Timeout if it took too long
if (millis() - t0 >= BMS_TIMEOUT ) {
break;
}
}
return bms_name;
}
#endif
// ###########################################################################
// 0xE1 Set MOSFET
// ###########################################################################
void OverkillSolarBms::set_mosfet_control(bool charge, bool discharge) {
#ifdef BMS_OPTION_DEBUG
Serial.println("Query 0xE1 MOSFET Control");
#endif
uint8_t length = 2;
uint8_t data[2];
data[0] = 0b11;
data[1] = 0;
if (charge) {
data[1] &= 0b10;
}
if (discharge) {
data[1] &= 0b01;
}
write(true, BMS_REG_CTL_MOSFET, length, data);
}
#ifdef BMS_OPTION_DEBUG
void OverkillSolarBms::debug() {
Serial.println("==============================================");
Serial.print("Voltage: ");
Serial.print(get_voltage(), 3);
Serial.println(" V");
Serial.print("Current: ");
Serial.print(get_current(), 3);
Serial.println(" A");
Serial.print("Balance capacity: ");
Serial.print(get_balance_capacity(), 3);
Serial.println(" Ah");
Serial.print("Rate capacity: ");
Serial.print(get_rate_capacity(), 3);
Serial.println(" Ah");
Serial.print("Cycle count: ");
Serial.println(get_cycle_count() , DEC);
#ifdef BMS_OPTION_PRODUCTION_DATE
Serial.print("Production Date: ");
ProductionDate date = get_production_date();
Serial.print(date.day, DEC);
Serial.print("/");
Serial.print(date.month, DEC);
Serial.print("/");
Serial.println(date.year, DEC);
#endif
Serial.println("Protection Status: ");
ProtectionStatus prot_status = get_protection_status();
Serial.print(" software_lock_mos: ");
Serial.println(prot_status.software_lock_mos, DEC);
Serial.print(" front_end_detection_ic_error: ");
Serial.println(prot_status.front_end_detection_ic_error, DEC);
Serial.print(" short_circuit_protection: ");
Serial.println(prot_status.short_circuit_protection, DEC);
Serial.print(" discharge_overcurrent_protection: ");
Serial.println(prot_status.discharge_overcurrent_protection, DEC);
Serial.print(" charging_overcurrent_protection: ");
Serial.println(prot_status.charging_overcurrent_protection, DEC);
Serial.print(" discharge_low_temperature_protection: ");
Serial.println(prot_status.discharge_low_temperature_protection, DEC);
Serial.print(" discharge_over_temperature_protection:");
Serial.println(prot_status.discharge_over_temperature_protection, DEC);
Serial.print(" charging_low_temperature_protection: ");
Serial.println(prot_status.charging_low_temperature_protection, DEC);
Serial.print(" charging_over_temperature_protection: ");
Serial.println(prot_status.charging_over_temperature_protection, DEC);
Serial.print(" whole_pack_undervoltage_protection: ");
Serial.println(prot_status.whole_pack_undervoltage_protection, DEC);
Serial.print(" whole_pack_overvoltage_protection: ");
Serial.println(prot_status.whole_pack_overvoltage_protection, DEC);
Serial.print(" single_cell_undervoltage_protection: ");
Serial.println(prot_status.single_cell_undervoltage_protection, DEC);
Serial.print(" single_cell_overvoltage_protection: ");
Serial.println(prot_status.single_cell_overvoltage_protection, DEC);
#ifdef BMS_OPTION_SW_VERSION
Serial.print("Software version: ");
SoftwareVersion version = get_software_version();
Serial.print(version.major, DEC);
Serial.print(".");
Serial.println(version.minor, DEC);
#endif
Serial.print("State of Charge: ");
Serial.print(get_state_of_charge(), DEC);
Serial.println("%");
Serial.print("Discharge MOSFET: ");
Serial.println(get_discharge_mosfet_status()?"ON":"OFF");
Serial.print("Charge MOSFET: ");
Serial.println(get_charge_mosfet_status()?"ON":"OFF");
Serial.print("# of cells: ");
Serial.println(get_num_cells(), DEC);
Serial.print("# of temp sensors: ");
Serial.println(get_num_ntcs(), DEC);
Serial.println("Temperatures:");
for (uint8_t i=0; i < min(BMS_MAX_NTCs, get_num_ntcs()); i++) {
Serial.print(" ");
Serial.print(get_ntc_temperature(i), 1);
Serial.println(" deg C");
}
Serial.println("Cell Voltages & Balance Status: ");
for (uint8_t i=0; i < min(BMS_MAX_CELLS, get_num_cells()); i++) {
Serial.print(" ");
Serial.print(get_cell_voltage(i), 3); // Returns the cell voltage, in volts
Serial.print("V ");
Serial.println(get_balance_status(i)?"(balancing)":"(not balancing)");
}
#ifdef BMS_OPTION_NAME
Serial.print("BMS Name: ");
Serial.println(get_bms_name());
#endif
// Serial.println("==============================================");
Serial.println();
}
#endif
// ###########################################################################
// Messages to and from the BMS
void OverkillSolarBms::query_basic_info() {
#ifdef BMS_OPTION_DEBUG
Serial.println("Query 0x03 Basic Info");
#endif
uint8_t length = 0;
uint8_t *data = NULL;
write(true, BMS_REG_BASIC_SYSTEM_INFO, length, data);
uint32_t t0 = millis();
// Handle the incoming serial data and block until the basic info is received
while(1) {
serial_rx_task();
delay(10);
if (last_0x03_timestamp > t0) {
break;
}
// Timeout if it took too long
if (millis() - t0 >= BMS_TIMEOUT ) {
break;
}
}
}
void OverkillSolarBms::query_cell_voltages() {
#ifdef BMS_OPTION_DEBUG
Serial.println("Query 0x04 Cell Voltages");
#endif
uint8_t length = 0;
uint8_t *data = NULL;
write(true, BMS_REG_CELL_VOLTAGES, length, data);
uint32_t t0 = millis();
// Handle the incoming serial data and block until the voltages are received
while(1) {
serial_rx_task();
delay(10);
if (last_0x04_timestamp > t0) {
break;
}
// Timeout if it took too long
if (millis() - t0 >= BMS_TIMEOUT ) {
break;
}
}
}
// ###########################################################################
// Low-level read/write methods
// Write to BMS
void OverkillSolarBms::write(bool read, uint8_t command_code, uint8_t* data, uint8_t length) {
uint16_t checksum = 0;
// Write the start byte, 0xDD
serial->write(BMS_STARTBYTE);
// Write the status byte, which indicates whether a read or write is being requested
if (read) {
serial->write(BMS_READ); // 0xA5
}
else {
serial->write(BMS_WRITE); // 0x5A
}
// Write the command code (the register address)
serial->write(command_code);
checksum += command_code;
// Write the length
serial->write(length);
checksum += length;
// Write the data
for (uint8_t i=0; i< length; i++) {
serial->write(data[i]);
checksum += data[i];
}
// Write the checksum
checksum = (uint16_t)((0x10000UL) - (uint32_t)checksum);
uint8_t checksum_msb = (uint8_t)((checksum >> 8) & 0xFF);
serial->write(checksum_msb);
uint8_t checksum_lsb = (uint8_t)(checksum & 0xFF);
serial->write(checksum_lsb);
// Write the stop byte, 0x77
serial->write(BMS_STOPBYTE);
}
void OverkillSolarBms::serial_rx_task() {
int bytes_available = serial->available();
if (bytes_available > 0) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print(bytes_available, DEC);
Serial.println(" bytes available to read");
#endif
for (int i=0; i < bytes_available; i++) {
int c = serial->read();
if (c == -1) {
continue;
}
if (rx_state == BMS_STATE_WAIT_FOR_START_BYTE) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_START_BYTE]: ");
Serial.println(c, HEX);
#endif
if (c == BMS_STARTBYTE) {
rx_cmd_code = 0;
rx_status = 0;
rx_length = 0;
rx_data_index = 0;
rx_checksum = 0;
// Reset all of the state variables
rx_state = BMS_STATE_WAIT_FOR_CMD_CODE;
}
else {
// Error
// Serial.println("");
// Serial.println("Framing error! Not a start byte!");
num_rx_errors += 1;
rx_state = BMS_STATE_WAIT_FOR_START_BYTE;
}
}
else if (rx_state == BMS_STATE_WAIT_FOR_CMD_CODE) {
rx_cmd_code = c;
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_CMD_CODE]: ");
Serial.println(c, HEX);
#endif
rx_state = BMS_STATE_WAIT_FOR_STATUS_BYTE;
}
else if (rx_state == BMS_STATE_WAIT_FOR_STATUS_BYTE) {
rx_status = c;
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_STATUS_BYTE]: ");
Serial.println(c, HEX);
#endif
if (rx_status == 0x00) {
// The BMS should set the status to 0x00 if it is OK.
}
else if (rx_status == 0x80) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.println("");
Serial.println("RX error! Status byte should have been 0x00, but got 0x80!");
#endif
// An error occured
num_rx_errors += 1;
}
else {
// Any code other to us is still an error
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.println("");
Serial.println("RX error! Status byte should have been 0x00 or 0x80!");
#endif
num_rx_errors += 1;
}
rx_state = BMS_STATE_WAIT_FOR_LENGTH;
}
else if (rx_state == BMS_STATE_WAIT_FOR_LENGTH) {
rx_length = c;
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_LENGTH]: ");
Serial.println(c, DEC);
#endif
if (rx_length > BMS_MAX_RX_DATA_LEN) {
// Error
// Serial.println("");
num_rx_errors += 1;
}
rx_state = BMS_STATE_WAIT_FOR_DATA;
}
else if (rx_state == BMS_STATE_WAIT_FOR_DATA) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_DATA]: ");
Serial.print(c, HEX);
#endif
if (rx_data_index + 1 <= BMS_MAX_RX_DATA_LEN) {
rx_data[rx_data_index] = c;
}
if (rx_data_index + 1 >= rx_length) {
rx_state = BMS_STATE_WAIT_FOR_CHECKSUM_MSB;
}
else {
// Keep the rx_state in BMS_STATE_WAIT_FOR_DATA,
// until all data bytes have been received
rx_data_index += 1;
}
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print(", rx_data_index=");
Serial.println(rx_data_index, DEC);
#endif
}
else if (rx_state == BMS_STATE_WAIT_FOR_CHECKSUM_MSB) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_CHECKSUM_MSB]: ");
Serial.println(c, HEX);
#endif
rx_checksum = (c << 8) & 0xFF00;
rx_state = BMS_STATE_WAIT_FOR_CHECKSUM_LSB;
}
else if (rx_state == BMS_STATE_WAIT_FOR_CHECKSUM_LSB) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_CHECKSUM_LSB]: ");
Serial.println(c, HEX);
#endif
rx_checksum |= c;
rx_state = BMS_STATE_WAIT_FOR_STOP_BYTE;
}
else if (rx_state == BMS_STATE_WAIT_FOR_STOP_BYTE) {
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("[WAIT_FOR_STOP_BYTE]: ");
Serial.println(c, HEX);
#endif
// Calculate the checksum of the length and data bytes.
// If it matches the received checksum, then continue.
// Otherwise, flag it as an error and stop.
uint16_t calc_checksum = 0;
calc_checksum += rx_status;
calc_checksum += rx_length;
for (uint8_t i=0; i < rx_length; i++) {
calc_checksum += rx_data[i];
}
calc_checksum = (uint16_t)((0x10000UL) - (uint32_t)calc_checksum);
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
if (rx_checksum != calc_checksum) {
Serial.println("");
Serial.println("Checksum did not calculate!");
Serial.print(" Calculated: ");
Serial.println(calc_checksum, HEX);
Serial.print(" Received: ");
Serial.println(rx_checksum, HEX);
}
#endif
if (rx_checksum == calc_checksum && rx_status == 0x00) {
if (c == BMS_STOPBYTE) {
// Everything looks OK, handle the data
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.println("Got a complete msg");
#endif
if (rx_cmd_code == BMS_REG_BASIC_SYSTEM_INFO) { // 0x03
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.println("Got an 0x03 Basic Info msg");
#endif
voltage = (uint16_t)(rx_data[0] << 8) | (uint16_t)(rx_data[1]); // 0-1 Total voltage
current = (uint16_t)(rx_data[2] << 8) | (uint16_t)(rx_data[3]); // 2-3 Current
balance_capacity = (uint16_t)(rx_data[4] << 8) | (uint16_t)(rx_data[5]); // 4-5 Balance capacity
rate_capacity = (uint16_t)(rx_data[6] << 8) | (uint16_t)(rx_data[7]); // 6-7 Rate capacity
cycle_count = (uint16_t)(rx_data[8] << 8) | (uint16_t)(rx_data[9]); // 8-9 Cycle count
#ifdef BMS_OPTION_PRODUCTION_DATE
production_date = (uint16_t)(rx_data[10] << 8) | (uint16_t)(rx_data[11]); // 10-11 Production Date
#endif
balance_status = (uint16_t)(rx_data[12] << 8) | (uint16_t)(rx_data[13]); // 12-13, 14-15 Balance Status
protection_status = (uint16_t)(rx_data[16] << 8) | (uint16_t)(rx_data[17]); // 16-17 Protection status
#ifdef BMS_OPTION_FAULT_COUNTS
// See if there are any new faults. If so, then increment the count.
if (has_new_fault_occured(0)) { fault_count.single_cell_overvoltage_protection += 1; }
if (has_new_fault_occured(1)) { fault_count.single_cell_undervoltage_protection += 1; }
if (has_new_fault_occured(2)) { fault_count.whole_pack_undervoltage_protection += 1; }
if (has_new_fault_occured(3)) { fault_count.single_cell_overvoltage_protection += 1; }
if (has_new_fault_occured(4)) { fault_count.charging_over_temperature_protection += 1; }
if (has_new_fault_occured(5)) { fault_count.charging_low_temperature_protection += 1; }
if (has_new_fault_occured(6)) { fault_count.discharge_over_temperature_protection += 1; }
if (has_new_fault_occured(7)) { fault_count.discharge_low_temperature_protection += 1; }
if (has_new_fault_occured(8)) { fault_count.charging_overcurrent_protection += 1; }
if (has_new_fault_occured(9)) { fault_count.discharge_overcurrent_protection += 1; }
if (has_new_fault_occured(10)) { fault_count.short_circuit_protection += 1; }
if (has_new_fault_occured(11)) { fault_count.front_end_detection_ic_error += 1; }
if (has_new_fault_occured(12)) { fault_count.software_lock_mos += 1; }
#endif
#ifdef BMS_OPTION_SW_VERSION
software_version = rx_data[18]; // 18 Software version
#endif
remaining_soc = rx_data[19]; // 19 Remaining state of charge
mosfet_status = rx_data[20]; // 20 MOSFET status
num_cells = rx_data[21]; // 21 # of batteries in series
num_ntcs = rx_data[22]; // 22 # of NTCs
for (uint8_t i=0; i < min(BMS_MAX_NTCs, num_ntcs); i++) {
uint8_t ntc_index = 23 + (i * 2);
ntc_temps[i] = (uint16_t)(rx_data[ntc_index] << 8) | (uint16_t)(rx_data[ntc_index + 1]);
}
last_0x03_timestamp = millis();
}
else if (rx_cmd_code == BMS_REG_CELL_VOLTAGES) { // 0x04
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.println("Got an 0x04 Cell Voltage msg");
#endif
for (uint8_t i=0; i < min(BMS_MAX_CELLS, num_cells); i++) {
cell_voltages[i] = (uint16_t)(rx_data[i * 2] << 8) | (uint16_t)(rx_data[(i * 2) + 1]);
}
last_0x04_timestamp = millis();
}
#ifdef BMS_OPTION_NAME
else if (rx_cmd_code == BMS_REG_NAME) { // 0x05
bms_name = String("");
for (uint8_t i=0; i < min(BMS_MAX_RX_DATA_LEN, rx_length); i++) {
bms_name += (char)rx_data[i];
}
}
#endif
#ifdef BMS_OPTION_DEBUG
else {
Serial.print("Skipping unknown register: ");
Serial.println(rx_cmd_code, HEX);
}
#endif
}
#ifdef BMS_OPTION_DEBUG
else {
Serial.println("");
Serial.println("Framing error! Expected 0x77 stop byte!");
num_rx_errors += 1;
}
#endif
}
else {
num_rx_errors += 1;
}
rx_state = BMS_STATE_WAIT_FOR_START_BYTE;
}
#ifdef BMS_OPTION_DEBUG
else {
Serial.print("THIS SHOULD NEVER HAPPEN! The rx_state was: ");
Serial.println(rx_state, DEC);
} // rx_state
#endif
} // for i in bytes_available
#ifdef BMS_OPTION_DEBUG_STATE_MACHINE
Serial.print("The rx_state is: ");
Serial.print(rx_state, DEC);
Serial.println("...");
#endif
} // if bytes_available > 0
}