forked from collin80/TeslaBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMSModuleManager.cpp
581 lines (529 loc) · 18.9 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
#include "config.h"
#include "BMSModuleManager.h"
#include "BMSUtil.h"
#include "Logger.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()
{
for (int address = 1; address <= MAX_MODULE_ADDR; address++)
{
if (modules[address].isExisting()) modules[address].balanceCells();
}
}
/*
* 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 = BMSUtil::sendDataWithReply(payload, 3, false, buff, 4);
if (retLen == 4)
{
if (buff[0] == 0x80 && buff[1] == 0 && buff[2] == 1)
{
Logger::debug("00 found");
//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;
BMSUtil::sendData(payload, 3, true);
delay(3);
if (BMSUtil::getReply(buff, 10) > 2)
{
if (buff[0] == (0x81) && buff[1] == REG_ADDR_CTRL && buff[2] == (y + 0x80))
{
modules[y].setExists(true);
numFoundModules++;
Logger::debug("Address assigned");
}
}
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;
BMSUtil::sendData(payload, 3, false);
delay(20);
if (BMSUtil::getReply(buff, 8) > 4)
{
if (buff[0] == (x << 1) && buff[1] == 0 && buff[2] == 1 && buff[4] > 0) {
modules[x].setExists(true);
numFoundModules++;
Logger::debug("Found module with address: %X", x);
}
}
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
BMSUtil::sendData(payload, 3, true);
delay(100);
BMSUtil::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
BMSUtil::sendDataWithReply(payload, 3, true, buff, 4);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSUtil::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
BMSUtil::sendDataWithReply(payload, 3, true, buff, 4);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSUtil::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
BMSUtil::sendData(payload, 3, true);
delay(2);
BMSUtil::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
BMSUtil::sendData(payload, 3, true);
delay(2);
BMSUtil::getReply(buff, 8);
payload[0] = 0x7F; //broadcast
payload[1] = REG_ALERT_STATUS;//Fault Status
payload[2] = 0x04;//data to cause a reset
BMSUtil::sendData(payload, 3, true);
delay(2);
BMSUtil::getReply(buff, 8);
payload[0] = 0x7F; //broadcast
payload[2] = 0x00;//data to clear
BMSUtil::sendData(payload, 3, true);
delay(2);
BMSUtil::getReply(buff, 8);
}
void BMSModuleManager::getAllVoltTemp()
{
packVolt = 0.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
Logger::debug("");
Logger::debug("Module %i exists. Reading voltage and temperature values", x);
modules[x].readModuleValues();
Logger::debug("Module voltage: %f", modules[x].getModuleVoltage());
Logger::debug("Lowest Cell V: %f Highest Cell V: %f", modules[x].getLowCellV(), modules[x].getHighCellV());
Logger::debug("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();
}
}
if (packVolt > highestPackVolt) highestPackVolt = packVolt;
if (packVolt < lowestPackVolt) lowestPackVolt = packVolt;
if (digitalRead(13) == LOW) {
if (!isFaulted) Logger::error("One or more BMS modules have entered the fault state!");
isFaulted = true;
}
else
{
if (isFaulted) Logger::info("All modules have exited a faulted state");
isFaulted = false;
}
}
float BMSModuleManager::getPackVoltage()
{
return packVolt;
}
float BMSModuleManager::getAvgTemperature()
{
float avg = 0.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting()) avg += modules[x].getAvgTemp();
}
avg = avg / (float)numFoundModules;
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;
Logger::console("");
Logger::console("");
Logger::console("");
Logger::console(" Pack Status:");
if (isFaulted) Logger::console(" FAULTED!");
else Logger::console(" All systems go!");
Logger::console("Modules: %i Voltage: %fV Avg Cell Voltage: %fV Avg Temp: %fC ", numFoundModules,
getPackVoltage(),getAvgCellVolt(), getAvgTemperature());
Logger::console("");
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();
Logger::console(" Module #%i", y);
Logger::console(" Voltage: %fV (%fV-%fV) Temperatures: (%fC-%fC)", modules[y].getModuleVoltage(),
modules[y].getLowCellV(), modules[y].getHighCellV(), modules[y].getLowTemp(), modules[y].getHighTemp());
SerialUSB.print(" Currently balancing cells: ");
for (int i = 0; i < 6; i++)
{
if (modules[y].getBalancingState(i) == 1)
{
SerialUSB.print(i);
SerialUSB.print(" ");
}
}
SerialUSB.println();
if (faults > 0)
{
Logger::console(" MODULE IS FAULTED:");
if (faults & 1)
{
SerialUSB.print(" Overvoltage Cell Numbers (1-6): ");
for (int i = 0; i < 6; i++)
{
if (COV & (1 << i))
{
SerialUSB.print(i+1);
SerialUSB.print(" ");
}
}
SerialUSB.println();
}
if (faults & 2)
{
SerialUSB.print(" Undervoltage Cell Numbers (1-6): ");
for (int i = 0; i < 6; i++)
{
if (CUV & (1 << i))
{
SerialUSB.print(i+1);
SerialUSB.print(" ");
}
}
SerialUSB.println();
}
if (faults & 4)
{
Logger::console(" CRC error in received packet");
}
if (faults & 8)
{
Logger::console(" Power on reset has occurred");
}
if (faults & 0x10)
{
Logger::console(" Test fault active");
}
if (faults & 0x20)
{
Logger::console(" Internal registers inconsistent");
}
}
if (alerts > 0)
{
Logger::console(" MODULE HAS ALERTS:");
if (alerts & 1)
{
Logger::console(" Over temperature on TS1");
}
if (alerts & 2)
{
Logger::console(" Over temperature on TS2");
}
if (alerts & 4)
{
Logger::console(" Sleep mode active");
}
if (alerts & 8)
{
Logger::console(" Thermal shutdown active");
}
if (alerts & 0x10)
{
Logger::console(" Test Alert");
}
if (alerts & 0x20)
{
Logger::console(" OTP EPROM Uncorrectable Error");
}
if (alerts & 0x40)
{
Logger::console(" GROUP3 Regs Invalid");
}
if (alerts & 0x80)
{
Logger::console(" Address not registered");
}
}
if (faults > 0 || alerts > 0) SerialUSB.println();
}
}
}
void BMSModuleManager::printPackDetails()
{
uint8_t faults;
uint8_t alerts;
uint8_t COV;
uint8_t CUV;
int cellNum = 0;
Logger::console("");
Logger::console("");
Logger::console("");
Logger::console(" Pack Status:");
if (isFaulted) Logger::console(" FAULTED!");
else Logger::console(" All systems go!");
Logger::console("Modules: %i Voltage: %fV Avg Cell Voltage: %fV Avg Temp: %fC ", numFoundModules,
getPackVoltage(),getAvgCellVolt(), getAvgTemperature());
Logger::console("");
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();
SerialUSB.print("Module #");
SerialUSB.print(y);
if (y < 10) SerialUSB.print(" ");
SerialUSB.print(" ");
SerialUSB.print(modules[y].getModuleVoltage());
SerialUSB.print("V");
for (int i = 0; i < 6; i++)
{
if (cellNum < 10) SerialUSB.print(" ");
SerialUSB.print(" Cell");
SerialUSB.print(cellNum++);
SerialUSB.print(": ");
SerialUSB.print(modules[y].getCellVoltage(i));
SerialUSB.print("V");
if (modules[y].getBalancingState(i) == 1) SerialUSB.print("*");
else SerialUSB.print(" ");
}
SerialUSB.print(" Neg Term Temp: ");
SerialUSB.print(modules[y].getTemperature(0));
SerialUSB.print("C Pos Term Temp: ");
SerialUSB.print(modules[y].getTemperature(1));
SerialUSB.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) + ((settings.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) + ((settings.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) + ((settings.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);
}
//The SerialConsole actually sets the battery ID to a specific value. We just have to set up the CAN filter here to
//match.
void BMSModuleManager::setBatteryID()
{
//Setup filter for direct access to our registered battery ID
uint32_t canID = (0xBAul << 20) + (((uint32_t)settings.batteryID & 0xF) << 16);
Can0.setRXFilter(0, canID, 0x1FFF0000ul, true);
}