-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMSModuleManager.cpp
720 lines (640 loc) · 22 KB
/
BMSModuleManager.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
#include "config.h"
#include "BMSModuleManager.h"
extern EEPROMSettings settings;
BMSModuleManager::BMSModuleManager()
{
for (int i = 1; i <= MAX_MODULE_ADDR; i++) {
modules[i].setExists(false);
modules[i].setAddress(i);
}
lowestPackVolt = 1000.0f;
highestPackVolt = 0.0f;
lowestPackTemp = 200.0f;
highestPackTemp = -100.0f;
isFaulted = false;
}
void BMSModuleManager::balanceCells()
{
uint8_t payload[4];
uint8_t buff[30];
uint8_t balance = 0;//bit 0 - 5 are to activate cell balancing 1-6
for (int address = 1; address <= MAX_MODULE_ADDR; address++)
{
balance = 0;
for (int i = 0; i < 6; i++)
{
if (getLowCellVolt() < modules[address].getCellVoltage(i))
{
balance = balance | (1<<i);
}
}
if (balance != 0) //only send balance command when needed
{
payload[0] = address << 1;
payload[1] = REG_BAL_TIME;
payload[2] = 0x05; //5 second balance limit, if not triggered to balance it will stop after 5 seconds
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 30);
payload[0] = address << 1;
payload[1] = REG_BAL_CTRL;
payload[2] = balance; //write balance state to register
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 30);
/* if (BMSModule::isDebug()) //read registers back out to check if everthing is good
{
delay(50);
payload[0] = address << 1;
payload[1] = REG_BAL_TIME;
payload[2] = 1; //
BMSModule::sendData(payload, 3, false);
delay(2);
BMSModule::getReply(buff, 30);
payload[0] = address << 1;
payload[1] = REG_BAL_CTRL;
payload[2] = 1; //
BMSModule::sendData(payload, 3, false);
delay(2);
BMSModule::getReply(buff, 30);
} */
}
}
}
/*
* Try to set up any unitialized boards. Send a command to address 0 and see if there is a response. If there is then there is
* still at least one unitialized board. Go ahead and give it the first ID not registered as already taken.
* If we send a command to address 0 and no one responds then every board is inialized and this routine stops.
* Don't run this routine until after the boards have already been enumerated.\
* Note: The 0x80 conversion it is looking might in theory block the message from being forwarded so it might be required
* To do all of this differently. Try with multiple boards. The alternative method would be to try to set the next unused
* address and see if any boards respond back saying that they set the address.
*/
void BMSModuleManager::setupBoards()
{
uint8_t payload[3];
uint8_t buff[10];
int retLen;
payload[0] = 0;
payload[1] = 0;
payload[2] = 1;
while (1 == 1)
{
payload[0] = 0;
payload[1] = 0;
payload[2] = 1;
retLen = BMSModule::sendDataWithReply(payload, 3, false, buff, 4);
if (retLen == 4)
{
if (buff[0] == 0x80 && buff[1] == 0 && buff[2] == 1)
{
//look for a free address to use
for (int y = 1; y < 63; y++)
{
if (!modules[y].isExisting())
{
payload[0] = 0;
payload[1] = REG_ADDR_CTRL;
payload[2] = y | 0x80;
BMSModule::sendData(payload, 3, true);
delay(3);
if (BMSModule::getReply(buff, 10) > 2)
{
if (buff[0] == (0x81) && buff[1] == REG_ADDR_CTRL && buff[2] == (y + 0x80))
{
modules[y].setExists(true);
numFoundModules++;
}
}
break; //quit the for loop
}
}
}
else break; //nobody responded properly to the zero address so our work here is done.
}
else break;
}
}
/*
* Iterate through all 62 possible board addresses (1-62) to see if they respond
*/
void BMSModuleManager::findBoards()
{
uint8_t payload[3];
uint8_t buff[8];
numFoundModules = 0;
payload[0] = 0;
payload[1] = 0; //read registers starting at 0
payload[2] = 1; //read one byte
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
modules[x].setExists(false);
payload[0] = x << 1;
BMSModule::sendData(payload, 3, false);
delay(20);
if (BMSModule::getReply(buff, 8) > 4)
{
if (buff[0] == (x << 1) && buff[1] == 0 && buff[2] == 1 && buff[4] > 0) {
modules[x].setExists(true);
numFoundModules++;
}
}
delay(5);
}
}
/*
* Force all modules to reset back to address 0 then set them all up in order so that the first module
* in line from the master board is 1, the second one 2, and so on.
*/
void BMSModuleManager::renumberBoardIDs()
{
uint8_t payload[3];
uint8_t buff[8];
int attempts = 1;
for (int y = 1; y < 63; y++)
{
modules[y].setExists(false);
numFoundModules = 0;
}
while (attempts < 3)
{
payload[0] = 0x3F << 1; //broadcast the reset command
payload[1] = 0x3C;//reset
payload[2] = 0xA5;//data to cause a reset
BMSModule::sendData(payload, 3, true);
delay(100);
BMSModule::getReply(buff, 8);
if (buff[0] == 0x7F && buff[1] == 0x3C && buff[2] == 0xA5 && buff[3] == 0x57) break;
attempts++;
}
setupBoards();
}
/*
After a RESET boards have their faults written due to the hard restart or first time power up, this clears thier faults
*/
void BMSModuleManager::clearFaults()
{
uint8_t payload[3];
uint8_t buff[8];
payload[0] = 0x7F; //broadcast
payload[1] = REG_ALERT_STATUS;//Alert Status
payload[2] = 0xFF;//data to cause a reset
BMSModule::sendDataWithReply(payload, 3, true, buff, 4);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSModule::sendDataWithReply(payload, 3, true, buff, 4);
payload[0] = 0x7F; //broadcast
payload[1] = REG_FAULT_STATUS;//Fault Status
payload[2] = 0xFF;//data to cause a reset
BMSModule::sendDataWithReply(payload, 3, true, buff, 4);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSModule::sendDataWithReply(payload, 3, true, buff, 4);
isFaulted = false;
}
/*
Puts all boards on the bus into a Sleep state, very good to use when the vehicle is a rest state.
Pulling the boards out of sleep only to check voltage decay and temperature when the contactors are open.
*/
void BMSModuleManager::sleepBoards()
{
uint8_t payload[3];
uint8_t buff[8];
payload[0] = 0x7F; //broadcast
payload[1] = REG_IO_CTRL;//IO ctrl start
payload[2] = 0x04;//write sleep bit
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 8);
}
/*
Wakes all the boards up and clears thier SLEEP state bit in the Alert Status Registery
*/
void BMSModuleManager::wakeBoards()
{
uint8_t payload[3];
uint8_t buff[8];
payload[0] = 0x7F; //broadcast
payload[1] = REG_IO_CTRL;//IO ctrl start
payload[2] = 0x00;//write sleep bit
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 8);
payload[0] = 0x7F; //broadcast
payload[1] = REG_ALERT_STATUS;//Fault Status
payload[2] = 0x04;//data to cause a reset
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 8);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSModule::sendData(payload, 3, true);
delay(2);
BMSModule::getReply(buff, 8);
}
void BMSModuleManager::getAllVoltTemp()
{
packVolt = 0.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
Serial.printf("");
Serial.printf("Module %i exists. Reading voltage and temperature values", x);
modules[x].readModuleValues();
Serial.printf("Module voltage: %f", modules[x].getModuleVoltage());
Serial.printf("Lowest Cell V: %f Highest Cell V: %f", modules[x].getLowCellV(), modules[x].getHighCellV());
Serial.printf("Temp1: %f Temp2: %f", modules[x].getTemperature(0), modules[x].getTemperature(1));
packVolt += modules[x].getModuleVoltage();
if (modules[x].getLowTemp() < lowestPackTemp) lowestPackTemp = modules[x].getLowTemp();
if (modules[x].getHighTemp() > highestPackTemp) highestPackTemp = modules[x].getHighTemp();
}
}
packVolt = packVolt/Pstring;
if (packVolt > highestPackVolt) highestPackVolt = packVolt;
if (packVolt < lowestPackVolt) lowestPackVolt = packVolt;
if (digitalRead(11) == LOW) {
if (!isFaulted) Serial.println("One or more BMS modules have entered the fault state!");
isFaulted = true;
}
else
{
isFaulted = false;
}
}
float BMSModuleManager::getLowCellVolt()
{
LowCellVolt = 5.0;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
if (modules[x].getLowCellV() < LowCellVolt) LowCellVolt = modules[x].getLowCellV();
}
}
return LowCellVolt;
}
float BMSModuleManager::getHighCellVolt()
{
HighCellVolt = 5.0;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
if (modules[x].getHighCellV() < HighCellVolt) HighCellVolt = modules[x].getHighCellV();
}
}
return HighCellVolt;
}
float BMSModuleManager::getPackVoltage()
{
return packVolt;
}
float BMSModuleManager::getLowVoltage()
{
return lowestPackVolt;
}
float BMSModuleManager::getHighVoltage()
{
return highestPackVolt;
}
void BMSModuleManager::setBatteryID(int id)
{
batteryID = id;
}
void BMSModuleManager::setPstrings(int Pstrings)
{
Pstring = Pstrings;
}
void BMSModuleManager::setSensors(int sensor,float Ignore)
{
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
modules[x].settempsensor(sensor);
modules[x].setIgnoreCell(Ignore);
}
}
}
float BMSModuleManager::getAvgTemperature()
{
float avg = 0.0f;
int y = 0; //counter for modules below -70 (no sensors connected)
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
if (modules[x].getAvgTemp() > -70)
{
avg += modules[x].getAvgTemp();
}
else
{
y++;
}
}
}
avg = avg / (float)(numFoundModules-y);
return avg;
}
float BMSModuleManager::getAvgCellVolt()
{
float avg = 0.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting()) avg += modules[x].getAverageV();
}
avg = avg / (float)numFoundModules;
return avg;
}
void BMSModuleManager::printPackSummary()
{
uint8_t faults;
uint8_t alerts;
uint8_t COV;
uint8_t CUV;
Serial.printf("");
Serial.printf("");
Serial.printf("");
Serial.printf(" Pack Status:");
if (isFaulted) Serial.printf(" FAULTED!");
else Serial.printf(" All systems go!");
Serial.printf("Modules: %i Voltage: %fV Avg Cell Voltage: %fV Avg Temp: %fC ", numFoundModules,
getPackVoltage(),getAvgCellVolt(), getAvgTemperature());
Serial.printf("");
for (int y = 1; y < 63; y++)
{
if (modules[y].isExisting())
{
faults = modules[y].getFaults();
alerts = modules[y].getAlerts();
COV = modules[y].getCOVCells();
CUV = modules[y].getCUVCells();
Serial.printf(" Module #%i", y);
Serial.printf(" Voltage: %fV (%fV-%fV) Temperatures: (%fC-%fC)", modules[y].getModuleVoltage(),
modules[y].getLowCellV(), modules[y].getHighCellV(), modules[y].getLowTemp(), modules[y].getHighTemp());
if (faults > 0)
{
Serial.printf(" MODULE IS FAULTED:");
if (faults & 1)
{
Serial.print(" Overvoltage Cell Numbers (1-6): ");
for (int i = 0; i < 6; i++)
{
if (COV & (1 << i))
{
Serial.print(i+1);
Serial.print(" ");
}
}
Serial.println();
}
if (faults & 2)
{
Serial.print(" Undervoltage Cell Numbers (1-6): ");
for (int i = 0; i < 6; i++)
{
if (CUV & (1 << i))
{
Serial.print(i+1);
Serial.print(" ");
}
}
Serial.println();
}
if (faults & 4)
{
Serial.printf(" CRC error in received packet");
}
if (faults & 8)
{
Serial.printf(" Power on reset has occurred");
}
if (faults & 0x10)
{
Serial.printf(" Test fault active");
}
if (faults & 0x20)
{
Serial.printf(" Internal registers inconsistent");
}
}
if (alerts > 0)
{
Serial.printf(" MODULE HAS ALERTS:");
if (alerts & 1)
{
Serial.printf(" Over temperature on TS1");
}
if (alerts & 2)
{
Serial.printf(" Over temperature on TS2");
}
if (alerts & 4)
{
Serial.printf(" Sleep mode active");
}
if (alerts & 8)
{
Serial.printf(" Thermal shutdown active");
}
if (alerts & 0x10)
{
Serial.printf(" Test Alert");
}
if (alerts & 0x20)
{
Serial.printf(" OTP EPROM Uncorrectable Error");
}
if (alerts & 0x40)
{
Serial.printf(" GROUP3 Regs Invalid");
}
if (alerts & 0x80)
{
Serial.printf(" Address not registered");
}
}
if (faults > 0 || alerts > 0) Serial.println();
}
}
}
String BMSModuleManager::htmlPackDetails()
{
uint8_t faults;
uint8_t alerts;
uint8_t COV;
uint8_t CUV;
int cellNum = 0;
String ptr = "<!DOCTYPE html> <html>\n";
ptr += "Modules: " + String(numFoundModules);
ptr += "Voltage: " + String(getPackVoltage());
ptr += "Avg Cell Voltage: " + String( getAvgCellVolt());
ptr += "Low Cell Voltage: " + String(LowCellVolt);
ptr += "High Cell Voltage: " + String(HighCellVolt);
ptr += "Avg Temp: " + String(getAvgTemperature());
for (int y = 1; y < 63; y++)
{
if (modules[y].isExisting())
{
faults = modules[y].getFaults();
alerts = modules[y].getAlerts();
COV = modules[y].getCOVCells();
CUV = modules[y].getCUVCells();
ptr += "Module #";
ptr += String(y) + " ";
ptr += String(modules[y].getModuleVoltage()) + "V";
for (int i = 0; i < 6; i++)
{
ptr += " Cell";
ptr += String(cellNum++) + ": ";
ptr += String(modules[y].getCellVoltage(i)) + "V";
}
ptr += " Neg Term Temp: ";
ptr += String(modules[y].getTemperature(0));
ptr += "C Pos Term Temp: ";
ptr += String(modules[y].getTemperature(1)) + "C";
}
}
}
void BMSModuleManager::printPackDetails()
{
uint8_t faults;
uint8_t alerts;
uint8_t COV;
uint8_t CUV;
int cellNum = 0;
Serial.printf("Modules: %i Voltage: %fV Avg Cell Voltage: %fV Low Cell Voltage: %fV High Cell Voltage: %fV Avg Temp: %fC \n", numFoundModules,
getPackVoltage(),getAvgCellVolt(),LowCellVolt, HighCellVolt, getAvgTemperature());
for (int y = 1; y < 63; y++)
{
if (modules[y].isExisting())
{
faults = modules[y].getFaults();
alerts = modules[y].getAlerts();
COV = modules[y].getCOVCells();
CUV = modules[y].getCUVCells();
Serial.print("Module #");
Serial.print(y);
if (y < 10) Serial.print(" ");
Serial.print(" ");
Serial.print(modules[y].getModuleVoltage());
Serial.print("V");
for (int i = 0; i < 6; i++)
{
if (cellNum < 10) Serial.print(" ");
Serial.print(" Cell");
Serial.print(cellNum++);
Serial.print(": ");
Serial.print(modules[y].getCellVoltage(i));
Serial.print("V");
}
Serial.print(" Neg Term Temp: ");
Serial.print(modules[y].getTemperature(0));
Serial.print("C Pos Term Temp: ");
Serial.print(modules[y].getTemperature(1));
Serial.println("C");
}
}
}
/*
void BMSModuleManager::processCANMsg(CAN_FRAME &frame)
{
uint8_t battId = (frame.id >> 16) & 0xF;
uint8_t moduleId = (frame.id >> 8) & 0xFF;
uint8_t cellId = (frame.id) & 0xFF;
if (moduleId = 0xFF) //every module
{
if (cellId == 0xFF) sendBatterySummary();
else
{
for (int i = 1; i <= MAX_MODULE_ADDR; i++)
{
if (modules[i].isExisting())
{
sendCellDetails(i, cellId);
delayMicroseconds(500);
}
}
}
}
else //a specific module
{
if (cellId == 0xFF) sendModuleSummary(moduleId);
else sendCellDetails(moduleId, cellId);
}
}
void BMSModuleManager::sendBatterySummary()
{
CAN_FRAME outgoing;
outgoing.id = (0x1BA00000ul) + ((batteryID & 0xF) << 16) + 0xFFFF;
outgoing.rtr = 0;
outgoing.priority = 1;
outgoing.extended = true;
outgoing.length = 8;
uint16_t battV = uint16_t(getPackVoltage() * 100.0f);
outgoing.data.byte[0] = battV & 0xFF;
outgoing.data.byte[1] = battV >> 8;
outgoing.data.byte[2] = 0; //instantaneous current. Not measured at this point
outgoing.data.byte[3] = 0;
outgoing.data.byte[4] = 50; //state of charge
int avgTemp = (int)getAvgTemperature() + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[5] = avgTemp;
avgTemp = (int)lowestPackTemp + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[6] = avgTemp;
avgTemp = (int)highestPackTemp + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[7] = avgTemp;
Can0.sendFrame(outgoing);
}
void BMSModuleManager::sendModuleSummary(int module)
{
CAN_FRAME outgoing;
outgoing.id = (0x1BA00000ul) + ((batteryID & 0xF) << 16) + ((module & 0xFF) << 8) + 0xFF;
outgoing.rtr = 0;
outgoing.priority = 1;
outgoing.extended = true;
outgoing.length = 8;
uint16_t battV = uint16_t(modules[module].getModuleVoltage() * 100.0f);
outgoing.data.byte[0] = battV & 0xFF;
outgoing.data.byte[1] = battV >> 8;
outgoing.data.byte[2] = 0; //instantaneous current. Not measured at this point
outgoing.data.byte[3] = 0;
outgoing.data.byte[4] = 50; //state of charge
int avgTemp = (int)modules[module].getAvgTemp() + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[5] = avgTemp;
avgTemp = (int)modules[module].getLowestTemp() + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[6] = avgTemp;
avgTemp = (int)modules[module].getHighestTemp() + 40;
if (avgTemp < 0) avgTemp = 0;
outgoing.data.byte[7] = avgTemp;
Can0.sendFrame(outgoing);
}
void BMSModuleManager::sendCellDetails(int module, int cell)
{
CAN_FRAME outgoing;
outgoing.id = (0x1BA00000ul) + ((batteryID & 0xF) << 16) + ((module & 0xFF) << 8) + (cell & 0xFF);
outgoing.rtr = 0;
outgoing.priority = 1;
outgoing.extended = true;
outgoing.length = 8;
uint16_t battV = uint16_t(modules[module].getCellVoltage(cell) * 100.0f);
outgoing.data.byte[0] = battV & 0xFF;
outgoing.data.byte[1] = battV >> 8;
battV = uint16_t(modules[module].getHighestCellVolt(cell) * 100.0f);
outgoing.data.byte[2] = battV & 0xFF;
outgoing.data.byte[3] = battV >> 8;
battV = uint16_t(modules[module].getLowestCellVolt(cell) * 100.0f);
outgoing.data.byte[4] = battV & 0xFF;
outgoing.data.byte[5] = battV >> 8;
int instTemp = modules[module].getHighTemp() + 40;
outgoing.data.byte[6] = instTemp; // should be nearest temperature reading not highest but this works too.
outgoing.data.byte[7] = 0; //Bit encoded fault data. No definitions for this yet.
Can0.sendFrame(outgoing);
}
*/