-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeviceBMS.cpp
508 lines (459 loc) · 13.1 KB
/
DeviceBMS.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
/*
* Copyright (C) 2015 Michal Podhradsky
*
* This file is part of Viking Motorsports Arduino_eVCU.
*
* Viking Motorsports Arduino_eVCU is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Viking Motorsports Arduino_eVCU is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Viking Motorsports Arduino_eVCU; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* @file DeviceBMS.cpp
*
* Battery Management System
*/
#include "DeviceBMS.h"
DeviceBMS::DeviceBMS() {
bus = &CAN;//because we want CAN0 now
can_init_rlecs();
max_cell_temp = 0;
min_cell_temp = 100;
max_cell_volt = 0;
min_cell_volt = -1;
enum VSMstate vsm_state = VSM_start;// u
}
/**
* Initialize RLECs
*/
void DeviceBMS::can_init_rlecs() {
// MLEC setup
status = MLECUninit;
memset(rlecs, 0, sizeof(rlecs));
rlec_idx = 0;
rlec_txoffset = 0x40;
mlec_init_broadcast();
mlec_init_msgs();
mlec_update_msg_ids(rlec_txoffset);
request_sent = 0;
}
/**
* Periodic CAN function
*
* Scan and ask for data
*/
void DeviceBMS::can_periodic(){
switch(status){
case MLECUninit:
#if PRINT_DEBUG
SerialUSB.print("MLEC Uninit \r\n");
#endif
if (mlec_broadcast() && mlec_send_request()) {
#if PRINT_DEBUG
SerialUSB.print("Request sent");
#endif
request_sent = 1;
status = MLECInit;
}
break;
case MLECInit:
#if PRINT_DEBUG
SerialUSB.print("Scanned RLEC: " + String(rlec_idx) + "\r\n");
SerialUSB.print("Found: " + String(rlecsX[rlec_idx].status) + "\r\n");
#endif
if (rlec_idx < (NUM_RLECS-1)) {
// Scanning
rlec_idx++;
rlec_txoffset = rlec_txoffset+0x2;
mlec_update_msg_ids(rlec_txoffset);
mlec_send_request();
request_sent = 1;
}
else {
rlec_idx = 0;
rlec_txoffset = 0x40;
status = MLECRunning; // Scan complete
}
break;
case MLECRunning:
// Good for now, although it looses time if waiting for rlecs that arent
// present, maybe some resolution to move up idx to actual values of RLECS
// (only 1 in the array)
if (rlecsX[rlec_idx].status == Active) {
// RLEC idx present
mlec_update_msg_ids(rlec_txoffset+0x2*rlec_idx);
// Modify messages for charger (TODO)
mlec_charger_periodic(&rlecsX[rlec_idx]);
mlec_send_request();
//Telemetry (OPTIONAL)
#if PRINT_DEBUG
//SerialUSB.print("Min voltage: " + String((float)min_cell_volt*0.00244) + "\r\n");
//SerialUSB.print("Minimal voltage value: " +String((float)min_cell_volt*RLEC_CAN_VOLTAGE_MULT) "\r\n");
send_rlec_info(&(rlecsX[rlec_idx]));
#endif
}
//set messages to default
mlec_init_msgs();
rlec_idx++;
rlec_idx = rlec_idx % NUM_RLECS;
break;
default:
break;
}
}
/**
* Charger function
*/
void DeviceBMS::mlec_charger_periodic(RLECModule* module) {
static uint16_t balance_resistors;
balance_resistors = 0;
// balance only when not running...
if (vsm_state != VSM_running) {
for (uint8_t j = 0; j<RLEC_CELLS;j++) {
if (module->cell_voltage[j] > (min_cell_volt + BAT_DELTA_BALANCE)) {
balance_resistors = balance_resistors + (0x1 << j);
}
}
}
else {
balance_resistors = 0;
}
// Configure message to given RLEC
tMsg0.data.bytes[3] = (uint8_t)(balance_resistors >> 8);
tMsg0.data.bytes[4] = (uint8_t)(balance_resistors & 0xFF);
}
/**
* Print info - debug only
*/
void DeviceBMS::send_rlec_info(RLECModule* rlec) {
SerialUSB.print(">>> RLEC_" + String(rlec_idx) + "\r\n");
SerialUSB.print("max_cell_volt: " + String((float)(rlec->max_cell_volt*RLEC_CAN_VOLTAGE_MULT)) + "[V]\r\n");
SerialUSB.print("min_cell_volt: " + String((float)(rlec->min_cell_volt*RLEC_CAN_VOLTAGE_MULT)) + "[V]\r\n");
SerialUSB.print("rlec_temp: " + String(rlec->rlec_temp) + "[C]\r\n");
SerialUSB.print("balance_resistors: ");
SerialUSB.print(rlec->balance_resistors, HEX);
SerialUSB.print("\r\n");
SerialUSB.print("faults: " + String(rlec->faults) + "\r\n");
SerialUSB.print("rlec_volt: " + String((float)(rlec->rlec_volt*RLEC_CAN_MODULE_MULT)) + "[V]\r\n");
SerialUSB.print("max_cell_temp: " + String(rlec->max_cell_temp) + "[C]\r\n");
SerialUSB.print("min_cell_temp: " + String(rlec->min_cell_temp) + "[C]\r\n");
SerialUSB.print("\r\n");
}
/**
* Check min and max cell voltages/temperatures
*/
void DeviceBMS::update_battery_data(){
max_cell_temp = 0;
min_cell_temp = 100;
max_cell_volt = 0;
min_cell_volt = -1;
for (int i=0;i<NUM_RLECS;i++){
if (rlecsX[i].status == Active) {
// check max cell temp
if (rlecsX[i].max_cell_temp > max_cell_temp) {
max_cell_temp = rlecsX[i].max_cell_temp;
}
// check min cell temp
if (rlecsX[i].min_cell_temp < min_cell_temp) {
min_cell_temp = rlecsX[i].min_cell_temp;
}
// check max cell voltage
if (rlecsX[i].max_cell_volt > max_cell_volt) {
max_cell_volt = rlecsX[i].max_cell_volt;
}
// check min cell voltage
if (rlecsX[i].min_cell_volt < min_cell_volt) {
min_cell_volt = rlecsX[i].min_cell_volt;
}
}
}
}
/**
* Check if we received messages
*/
void DeviceBMS::process() {
if (bus->rx_avail()) {
bus->get_rx_buff(rx_msg);
//SerialUSB.print("Msg id is " + String(rx_msg.id) + "\r\n");
if (status == MLECRunning) {
// Normal operation
rlec_parse_msg();
// handleCanFrame(rx_msg);
}
else {
// Scan operation
rlec_parse_scan(rx_msg.id);
}
}
}
/**
* Parse message during normal operation
*/
void DeviceBMS::rlec_parse_msg(void) {
static uint8_t msg_offset, msg_id, idx;
msg_offset = (uint8_t)(rx_msg.id >> 4);
msg_id = (uint8_t)(rx_msg.id & 0xF);
idx = msg_offset/2;
if (idx >= NUM_RLECS) idx = 0;
switch(msg_id) {
case 0x1: // Cell Voltage 1-4
rlecsX[idx].cell_voltage[0] = (uint16_t)(rx_msg.data.bytes[0] << 8 | rx_msg.data.bytes[1]);
rlecsX[idx].cell_voltage[1] = (uint16_t)(rx_msg.data.bytes[2] << 8 | rx_msg.data.bytes[3]);
rlecsX[idx].cell_voltage[2] = (uint16_t)(rx_msg.data.bytes[4] << 8 | rx_msg.data.bytes[5]);
rlecsX[idx].cell_voltage[3] = (uint16_t)(rx_msg.data.bytes[6] << 8 | rx_msg.data.bytes[7]);
break;
case 0x2: // Cell Voltage 5-8
rlecsX[idx].cell_voltage[4] = (uint16_t)(rx_msg.data.bytes[0] << 8 | rx_msg.data.bytes[1]);
rlecsX[idx].cell_voltage[5] = (uint16_t)(rx_msg.data.bytes[2] << 8 | rx_msg.data.bytes[3]);
rlecsX[idx].cell_voltage[6] = (uint16_t)(rx_msg.data.bytes[4] << 8 | rx_msg.data.bytes[5]);
rlecsX[idx].cell_voltage[7] = (uint16_t)(rx_msg.data.bytes[6] << 8 | rx_msg.data.bytes[7]);
break;
case 0x3: // Cell Voltage 9-12
rlecsX[idx].cell_voltage[8] = (uint16_t)(rx_msg.data.bytes[0] << 8 | rx_msg.data.bytes[1]);
rlecsX[idx].cell_voltage[9] = (uint16_t)(rx_msg.data.bytes[2] << 8 | rx_msg.data.bytes[3]);
rlecsX[idx].cell_voltage[10] = (uint16_t)(rx_msg.data.bytes[4] << 8 | rx_msg.data.bytes[5]);
rlecsX[idx].cell_voltage[11] = (uint16_t)(rx_msg.data.bytes[6] << 8 | rx_msg.data.bytes[7]);
break;
case 0x4: // Max/Min cell voltage, RLEC temp, balance, faults
rlecsX[idx].max_cell_volt = (uint16_t)(rx_msg.data.bytes[0] << 8 | rx_msg.data.bytes[1]);
rlecsX[idx].min_cell_volt = (uint16_t)(rx_msg.data.bytes[2] << 8 | rx_msg.data.bytes[3]);
rlecsX[idx].rlec_temp = rx_msg.data.bytes[4];
rlecsX[idx].balance_resistors = (uint16_t)( (rx_msg.data.bytes[5] << 8 | rx_msg.data.bytes[6]) & 0xFFF);
rlecsX[idx].faults = rx_msg.data.bytes[7];
break;
case 0x5: // Unfiltered Cell Voltage 1-4
// Not used
break;
case 0x6: // Unfiltered Cell Voltage 5-8
// Not used
break;
case 0x7: // Unfiltered Cell Voltage 9-12
// Not used
break;
case 0x8: // Raw Cell Voltage 1-4
// Not used
break;
case 0x9: // Raw Cell Voltage 5-8
// Not used
break;
case 0xA: // Raw Cell Voltage 9-12
// Not used
break;
case 0xB: // RLEC module voltage,
rlecsX[idx].rlec_volt = (uint16_t)(rx_msg.data.bytes[6] << 8 | rx_msg.data.bytes[7]);
break;
case 0xC: // Cell temp 1-8
rlecsX[idx].cell_temp[0] = rx_msg.data.bytes[0];
rlecsX[idx].cell_temp[1] = rx_msg.data.bytes[1];
rlecsX[idx].cell_temp[2] = rx_msg.data.bytes[2];
rlecsX[idx].cell_temp[3] = rx_msg.data.bytes[3];
rlecsX[idx].cell_temp[4] = rx_msg.data.bytes[4];
rlecsX[idx].cell_temp[5] = rx_msg.data.bytes[5];
rlecsX[idx].cell_temp[6] = rx_msg.data.bytes[6];
rlecsX[idx].cell_temp[7] = rx_msg.data.bytes[7];
break;
case 0xD: // Cell temp 9-12, min/max cell temp, SW id
rlecsX[idx].cell_temp[8] = rx_msg.data.bytes[0];
rlecsX[idx].cell_temp[9] = rx_msg.data.bytes[1];
rlecsX[idx].cell_temp[10] = rx_msg.data.bytes[2];
rlecsX[idx].cell_temp[11] = rx_msg.data.bytes[3];
rlecsX[idx].max_cell_temp = rx_msg.data.bytes[4];
rlecsX[idx].min_cell_temp = rx_msg.data.bytes[5];
rlecsX[idx].sw_id = rx_msg.data.bytes[7];
break;
default:
break;
}
}
/**
* Parse message during scan
*/
void DeviceBMS::rlec_parse_scan(int msg_id) {
if ((msg_id >> 4) == (rlec_txoffset-0x40)) {//offset between Tx and Rx msgs for a particular RLEC
// expected value arrived, notify periodic
request_sent = 0;
// write in buffer
rlecsX[rlec_idx].status = Active;
}
}
/**
* mlec_broadcast
*
* Send broadcast messages
*/
uint8_t DeviceBMS::mlec_broadcast(void){
bus->sendFrame(bdc0);
delay(7);
bus->sendFrame(bdc1);
delay(7);
bus->sendFrame(bdc2);
delay(7);
bus->sendFrame(bdc3);
delay(7);
bus->sendFrame(bdc4);
delay(7);
bus->sendFrame(bdc5);
delay(7);
return 1;
}
/**
* mlec_send_request
*
* Send data request for a specific RLEC.
* Call this function after setting the right msg ids
*/
uint8_t DeviceBMS::mlec_send_request(void){
bus->sendFrame(tMsg0);
delay(7);
bus->sendFrame(tMsg1);
delay(7);
bus->sendFrame(tMsg2);
delay(7);
bus->sendFrame(tMsg3);
delay(7);
return 1;
}
/**
* mlec_update_msg_id
*
* Updates msg ids
*/
void DeviceBMS::mlec_update_msg_ids(uint16_t offset){
offset = offset << 4;
tMsg0.id = offset | 0x6;
tMsg1.id = offset | 0xA;
tMsg2.id = offset | 0xB;
tMsg3.id = offset | 0xC;
}
/**
* mlec_init_msgs
*
* Fills in data request messages
*/
void DeviceBMS::mlec_init_msgs(void){
// from the TH!NK datasheet
// MSG 0, id = 0xXX6
tMsg0.length = 8;
tMsg0.data.bytes[0] = 0x0F;
tMsg0.data.bytes[1] = 0x0F;
tMsg0.data.bytes[2] = 0xFF;
tMsg0.data.bytes[3] = 0x00;
tMsg0.data.bytes[4] = 0x00;
tMsg0.data.bytes[5] = 0x00;
tMsg0.data.bytes[6] = 0x00;
tMsg0.data.bytes[7] = 0x00;
tMsg0.extended = false;
// MSG 1, id = 0xXXA
tMsg1.length = 8;
tMsg1.data.bytes[0] = 0x00;
tMsg1.data.bytes[1] = 0x00;
tMsg1.data.bytes[2] = 0x00;
tMsg1.data.bytes[3] = 0x00;
tMsg1.data.bytes[4] = 0x00;
tMsg1.data.bytes[5] = 0x00;
tMsg1.data.bytes[6] = 0x00;
tMsg1.data.bytes[7] = 0x00;
tMsg1.extended = false;
// MSG 2, id = 0xXXB
tMsg2.length = 8;
tMsg2.data.bytes[0] = 0x00;
tMsg2.data.bytes[1] = 0x00;
tMsg2.data.bytes[2] = 0x00;
tMsg2.data.bytes[3] = 0x00;
tMsg2.data.bytes[4] = 0x00;
tMsg2.data.bytes[5] = 0x00;
tMsg2.data.bytes[6] = 0x00;
tMsg2.data.bytes[7] = 0x00;
tMsg2.extended = false;
// MSG 3, id = 0xXXC
tMsg3.length = 8;
tMsg3.data.bytes[0] = 0x00;
tMsg3.data.bytes[1] = 0x00;
tMsg3.data.bytes[2] = 0x00;
tMsg3.data.bytes[3] = 0x00;
tMsg3.data.bytes[4] = 0x00;
tMsg3.data.bytes[5] = 0x00;
tMsg3.data.bytes[6] = 0x0C;
tMsg3.data.bytes[7] = 0x0C;
tMsg3.extended = false;
}
/**
* mlec_init_broadcast
*
* Fills in universal broadcast messages
*/
void DeviceBMS::mlec_init_broadcast(void){
// from the TH!NK datasheet
bdc0.id = 0x7e1;
bdc0.length = 8;
bdc0.data.bytes[0] = 0x01;
bdc0.data.bytes[1] = 0x0C;
bdc0.data.bytes[2] = 0x0C;
bdc0.data.bytes[3] = 0x01;
bdc0.data.bytes[4] = 0x01;
bdc0.data.bytes[5] = 0x00;
bdc0.data.bytes[6] = 0x05;
bdc0.data.bytes[7] = 0xBD;
bdc0.extended = false;;
bdc1.id = 0x7e2;
bdc1.length = 8;
bdc1.data.bytes[0] = 0x00;
bdc1.data.bytes[1] = 0x0A;
bdc1.data.bytes[2] = 0x00;
bdc1.data.bytes[3] = 0x0A;
bdc1.data.bytes[4] = 0x00;
bdc1.data.bytes[5] = 0x00;
bdc1.data.bytes[6] = 0x01;
bdc1.data.bytes[7] = 0x00;
bdc1.extended = false;;
bdc2.id = 0x7e3;
bdc2.length = 8;
bdc2.data.bytes[0] = 0x06;
bdc2.data.bytes[1] = 0xB9;
bdc2.data.bytes[2] = 0x06;
bdc2.data.bytes[3] = 0xB9;
bdc2.data.bytes[4] = 0x00;
bdc2.data.bytes[5] = 0x09;
bdc2.data.bytes[6] = 0x00;
bdc2.data.bytes[7] = 0x03;
bdc2.extended = false;;
bdc3.id = 0x7e4;
bdc3.length = 8;
bdc3.data.bytes[0] = 0x00;
bdc3.data.bytes[1] = 0x09;
bdc3.data.bytes[2] = 0x46;
bdc3.data.bytes[3] = 0x2D;
bdc3.data.bytes[4] = 0x0A;
bdc3.data.bytes[5] = 0x02;
bdc3.data.bytes[6] = 0x00;
bdc3.data.bytes[7] = 0x4B;
bdc3.extended = false;;
bdc4.id = 0x7e5;
bdc4.length = 8;
bdc4.data.bytes[0] = 0x05;
bdc4.data.bytes[1] = 0x02;
bdc4.data.bytes[2] = 0x03;
bdc4.data.bytes[3] = 0x51;
bdc4.data.bytes[4] = 0x03;
bdc4.data.bytes[5] = 0xAE;
bdc4.data.bytes[6] = 0x05;
bdc4.data.bytes[7] = 0xCA;
bdc4.extended = false;;
bdc5.id = 0x7e6;
bdc5.length = 2;
bdc5.data.bytes[0] = 0x23;
bdc5.data.bytes[1] = 0x1E;
bdc5.extended = false;;
}