forked from Tom-evnut/TeslaBMS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBMSModuleManager.cpp
More file actions
531 lines (486 loc) · 15.5 KB
/
Copy pathBMSModuleManager.cpp
File metadata and controls
531 lines (486 loc) · 15.5 KB
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
#include "config.h"
#include "BMSModuleManager.h"
#include "BMSUtil.h"
#include "Logger.h"
BMSModuleManager::BMSModuleManager()
{
for (int i = 1; i <= MAX_MODULE_ADDR; i++)
{
modules[i].setExists(false);
modules[i].setAddress(i);
}
isFaulted = false;
}
bool BMSModuleManager::balanceCells(float avgCellV, float triggerDiffCellV)
{
bool out = false;
for (int x = 1; x <= MAX_MODULE_ADDR; x+=2)
{
if (modules[x].isExisting() && modules[x+1].isExisting() && (modules[x].needsBalancing(avgCellV + triggerDiffCellV) || modules[x+1].needsBalancing(avgCellV + triggerDiffCellV)))
{
out = true;
modules[x].balanceCells(avgCellV);
modules[x+1].balanceCells(avgCellV);
}
}
return out;
}
void BMSModuleManager::balanceInfo()
{
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting() && modules[x].isBalancing())
{
modules[x].print();
}
}
}
/*
* 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()
{
Logger::debug("setupBoards starting");
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++)
{
Logger::debug("Addresstest %X", 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)
{
Logger::debug("got reply %X", y);
if (buff[0] == (0x81) && buff[1] == REG_ADDR_CTRL && buff[2] == (y + 0x80))
{
modules[y].setExists(true);
numFoundModules++;
Logger::debug("Address assigned %X", y);
}
else
{
Logger::debug("Address no response %X", y);
}
}
break; //quit the for loop
}
}
}
else
{
Logger::debug("no response from bms 1");
break; //nobody responded properly to the zero address so our work here is done.
}
}
else
{
Logger::debug("no response from bms 2");
break; //nobody responded properly to the zero address so our work here is done.
}
}
}
/*
* 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()
{
Logger::debug("renumberBoardIDs starting");
pinMode(INBMBFAULT, INPUT);
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;
communicationErrors = 0;
}
/*
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; //Alert 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()
{
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));
}
}
if (digitalRead(INBMBFAULT) == 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::getLowCellVolt()
{
float 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()
{
float HighCellVolt = 1.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()
{
float packVolt = 0.0;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
packVolt += modules[x].getModuleVoltage();
}
}
return packVolt;
}
void BMSModuleManager::setBatteryID(int id)
{
batteryID = id;
}
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::getLowTemperature()
{
float Low = 100.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
if (modules[x].getLowTemp() < Low)
{
Low = modules[x].getLowTemp();
}
}
}
return Low;
}
float BMSModuleManager::getHighTemperature()
{
float High = 0.0f;
for (int x = 1; x <= MAX_MODULE_ADDR; x++)
{
if (modules[x].isExisting())
{
if (modules[x].getHighTemp() > High)
{
High = modules[x].getHighTemp();
}
}
}
return High;
}
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::printPackDetails()
{
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());
if (faults > 0)
{
Logger::console(" MODULE IS FAULTED:");
if (faults & 1)
{
for (int i = 0; i < 6; i++)
{
if (COV & (1 << i))
{
Logger::console(" Overvoltage Cell %i ", i+1);
}
}
}
if (faults & 2)
{
for (int i = 0; i < 6; i++)
{
if (CUV & (1 << i))
{
Logger::console(" Undervoltage Cell %i ", i+1);
}
}
}
if (faults & 4)
{
Logger::console(" CRC error in received packet");
communicationErrors += 1;
}
if (faults & 8)
{
Logger::console(" Power on reset has occurred");
communicationErrors += 1;
}
if (faults & 0x10)
{
Logger::console(" Test fault active");
communicationErrors += 1;
}
if (faults & 0x20)
{
Logger::console(" Internal registers inconsistent");
communicationErrors += 1;
}
}
if (alerts > 0)
{
Logger::console(" MODULE HAS ALERTS:");
if (alerts & 1)
{
Logger::console(" Over temperature on TS1");
communicationErrors += 1;
}
if (alerts & 2)
{
Logger::console(" Over temperature on TS2");
communicationErrors += 1;
}
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");
communicationErrors += 1;
}
if (alerts & 0x40)
{
Logger::console(" GROUP3 Regs Invalid");
communicationErrors += 1;
}
if (alerts & 0x80)
{
Logger::console(" Address not registered");
communicationErrors += 1;
}
}
}
}
}
long BMSModuleManager::getCommunicationErrors()
{
return communicationErrors;
}
void BMSModuleManager::resetCommunicationErrors()
{
communicationErrors = 0;
}