forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bms.c
378 lines (310 loc) · 10.9 KB
/
bms.c
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
/*
Copyright 2020 Benjamin Vedder [email protected]
This file is part of the VESC firmware.
The VESC firmware 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 3 of the License, or
(at your option) any later version.
The VESC firmware 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This is the BMS module of the VESC firmware. It mainly supports the VESC BMS, but
* the intention is to have it extendible to other BMSs too. The first step is
* to add the BMS you want to support to the BMS_TYPE enum, and then you need to update
* this module to interpret CAN-messages from it properly.
*/
#include "bms.h"
#include "buffer.h"
#include "utils.h"
#include "datatypes.h"
#include "comm_can.h"
#include <string.h>
#include <math.h>
// Settings
#define MAX_CAN_AGE_SEC 2.0
// Private variables
static volatile bms_config m_conf;
static volatile bms_values m_values;
static volatile bms_soc_soh_temp_stat m_stat_temp_max;
static volatile bms_soc_soh_temp_stat m_stat_soc_min;
static volatile bms_soc_soh_temp_stat m_stat_soc_max;
void bms_init(bms_config *conf) {
m_conf = *conf;
memset((void*)&m_values, 0, sizeof(m_values));
memset((void*)&m_stat_temp_max, 0, sizeof(m_stat_temp_max));
memset((void*)&m_stat_soc_min, 0, sizeof(m_stat_soc_min));
memset((void*)&m_stat_soc_max, 0, sizeof(m_stat_soc_max));
memset((void*)&m_values, 0, sizeof(m_values));
m_values.can_id = -1;
m_stat_temp_max.id = -1;
m_stat_soc_min.id = -1;
m_stat_soc_max.id = -1;
}
bool bms_process_can_frame(uint32_t can_id, uint8_t *data8, int len, bool is_ext) {
bool used_data = false;
if (m_conf.type == BMS_TYPE_VESC) {
if (is_ext) {
uint8_t id = can_id & 0xFF;
CAN_PACKET_ID cmd = can_id >> 8;
switch (cmd) {
case CAN_PACKET_BMS_SOC_SOH_TEMP_STAT: {
used_data = true;
int32_t ind = 0;
bms_soc_soh_temp_stat msg;
msg.id = id;
msg.rx_time = chVTGetSystemTime();
msg.v_cell_min = buffer_get_float16(data8, 1e3, &ind);
msg.v_cell_max = buffer_get_float16(data8, 1e3, &ind);
msg.soc = ((float)((uint8_t)data8[ind++])) / 255.0;
msg.soh = ((float)((uint8_t)data8[ind++])) / 255.0;
msg.t_cell_max = (float)((int8_t)data8[ind++]);
uint8_t stat = data8[ind++];
msg.is_charging = (stat >> 0) & 1;
msg.is_balancing = (stat >> 1) & 1;
msg.is_charge_allowed = (stat >> 2) & 1;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
m_values.soc = msg.soc;
m_values.soh = msg.soh;
m_values.temp_max_cell = msg.t_cell_max;
}
// In case there is more than one BMS, keep track of the limiting
// values for all of them.
if (m_stat_temp_max.id < 0 ||
UTILS_AGE_S(m_stat_temp_max.rx_time) > MAX_CAN_AGE_SEC ||
m_stat_temp_max.t_cell_max < msg.t_cell_max) {
m_stat_temp_max = msg;
} else if (m_stat_temp_max.id == msg.id) {
m_stat_temp_max = msg;
}
if (m_stat_soc_min.id < 0 ||
UTILS_AGE_S(m_stat_soc_min.rx_time) > MAX_CAN_AGE_SEC ||
m_stat_soc_min.soc > msg.soc) {
m_stat_soc_min = msg;
} else if (m_stat_soc_min.id == msg.id) {
m_stat_soc_min = msg;
}
if (m_stat_soc_max.id < 0 ||
UTILS_AGE_S(m_stat_soc_max.rx_time) > MAX_CAN_AGE_SEC ||
m_stat_soc_max.soc < msg.soc) {
m_stat_soc_max = msg;
} else if (m_stat_soc_max.id == msg.id) {
m_stat_soc_max = msg;
}
} break;
case CAN_PACKET_BMS_V_TOT: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
m_values.v_tot = buffer_get_float32_auto(data8, &ind);
m_values.v_charge = buffer_get_float32_auto(data8, &ind);
}
} break;
case CAN_PACKET_BMS_I: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
m_values.i_in = buffer_get_float32_auto(data8, &ind);
m_values.i_in_ic = buffer_get_float32_auto(data8, &ind);
}
} break;
case CAN_PACKET_BMS_AH_WH: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
m_values.ah_cnt = buffer_get_float32_auto(data8, &ind);
m_values.wh_cnt = buffer_get_float32_auto(data8, &ind);
}
} break;
case CAN_PACKET_BMS_V_CELL: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
unsigned int ofs = data8[ind++];
m_values.cell_num = data8[ind++];
while(ind < len) {
if (ofs >= (sizeof(m_values.v_cell) / sizeof(float))) {
// Out of buffer space
break;
}
m_values.v_cell[ofs++] = buffer_get_float16(data8, 1e3, &ind);
}
}
} break;
case CAN_PACKET_BMS_BAL: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
int cell_num = data8[0];
uint64_t bal_state_0 = buffer_get_uint32(data8, &ind);
bal_state_0 &= 0x00FFFFFF;
uint64_t bal_state_1 = buffer_get_uint32(data8, &ind);
uint64_t bal_state = bal_state_0 << 32 | bal_state_1;
ind = 0;
while (ind < (int)(sizeof(m_values.bal_state) / sizeof(bool)) && ind < cell_num) {
m_values.bal_state[ind] = (bal_state >> ind) & 1;
ind++;
}
}
} break;
case CAN_PACKET_BMS_TEMPS: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
unsigned int ofs = data8[ind++];
m_values.temp_adc_num = data8[ind++];
while(ind < len) {
if (ofs >= (sizeof(m_values.temps_adc) / sizeof(float))) {
// Out of buffer space
break;
}
m_values.temps_adc[ofs++] = buffer_get_float16(data8, 1e2, &ind);
}
}
} break;
case CAN_PACKET_BMS_HUM: {
used_data = true;
if (id == m_values.can_id || UTILS_AGE_S(m_values.update_time) > MAX_CAN_AGE_SEC) {
int32_t ind = 0;
m_values.can_id = id;
m_values.update_time = chVTGetSystemTimeX();
m_values.temp_hum = buffer_get_float16(data8, 1e2, &ind);
m_values.hum = buffer_get_float16(data8, 1e2, &ind);
m_values.temp_ic = buffer_get_float16(data8, 1e2, &ind);
}
} break;
default:
break;
}
}
}
return used_data;
}
void bms_update_limits(float *i_in_min, float *i_in_max,
float i_in_min_conf, float i_in_max_conf) {
float i_in_min_bms = i_in_min_conf;
float i_in_max_bms = i_in_max_conf;
// Temperature
if (UTILS_AGE_S(m_stat_temp_max.rx_time) < MAX_CAN_AGE_SEC) {
float temp = m_stat_temp_max.t_cell_max;
if (temp < m_conf.t_limit_start) {
// OK
} else if (temp > m_conf.t_limit_end) {
i_in_min_bms = 0.0;
i_in_max_bms = 0.0;
// Maybe add fault code?
// mc_interface_fault_stop(FAULT_CODE_OVER_TEMP_FET, false, false);
} else {
float maxc = fabsf(i_in_max_conf);
if (fabsf(i_in_min_conf) > maxc) {
maxc = fabsf(i_in_min_conf);
}
maxc = utils_map(temp, m_conf.t_limit_start, m_conf.t_limit_end, maxc, 0.0);
if (fabsf(i_in_min_bms) > maxc) {
i_in_min_bms = SIGN(i_in_min_bms) * maxc;
}
if (fabsf(i_in_max_bms) > maxc) {
i_in_max_bms = SIGN(i_in_max_bms) * maxc;
}
}
}
// TODO: add support for conf->l_temp_accel_dec to still have braking.
// SOC
if (UTILS_AGE_S(m_stat_soc_min.rx_time) < MAX_CAN_AGE_SEC) {
float soc = m_stat_soc_min.soc;
if (soc > m_conf.soc_limit_start) {
// OK
} else if (soc < m_conf.soc_limit_end) {
i_in_max_bms = 0.0;
} else {
i_in_max_bms = utils_map(soc, m_conf.soc_limit_start,
m_conf.soc_limit_end, i_in_max_conf, 0.0);
}
}
if (fabsf(i_in_min_bms) < fabsf(*i_in_min)) {
*i_in_min = i_in_min_bms;
}
if (fabsf(i_in_max_bms) < fabsf(*i_in_max)) {
*i_in_max = i_in_max_bms;
}
}
void bms_process_cmd(unsigned char *data, unsigned int len,
void(*reply_func)(unsigned char *data, unsigned int len)) {
COMM_PACKET_ID packet_id;
packet_id = data[0];
data++;
len--;
switch (packet_id) {
case COMM_BMS_GET_VALUES: {
int32_t ind = 0;
uint8_t send_buffer[128];
send_buffer[ind++] = packet_id;
buffer_append_float32(send_buffer, m_values.v_tot, 1e6, &ind);
buffer_append_float32(send_buffer, m_values.v_charge, 1e6, &ind);
buffer_append_float32(send_buffer, m_values.i_in, 1e6, &ind);
buffer_append_float32(send_buffer, m_values.i_in_ic, 1e6, &ind);
buffer_append_float32(send_buffer, m_values.ah_cnt, 1e3, &ind);
buffer_append_float32(send_buffer, m_values.wh_cnt, 1e3, &ind);
// Cell voltages
send_buffer[ind++] = m_values.cell_num;
for (int i = 0;i < m_values.cell_num;i++) {
buffer_append_float16(send_buffer, m_values.v_cell[i], 1e3, &ind);
}
// Balancing state
for (int i = 0;i < m_values.cell_num;i++) {
send_buffer[ind++] = m_values.bal_state[i];
}
// Temperatures
send_buffer[ind++] = m_values.temp_adc_num;
for (int i = 0;i < m_values.temp_adc_num;i++) {
buffer_append_float16(send_buffer, m_values.temps_adc[i], 1e2, &ind);
}
buffer_append_float16(send_buffer, m_values.temp_ic, 1e2, &ind);
// Humidity
buffer_append_float16(send_buffer, m_values.temp_hum, 1e2, &ind);
buffer_append_float16(send_buffer, m_values.hum, 1e2, &ind);
// Highest cell temperature
buffer_append_float16(send_buffer, m_values.temp_max_cell, 1e2, &ind);
// State of charge and state of health
buffer_append_float16(send_buffer, m_values.soc, 1e3, &ind);
buffer_append_float16(send_buffer, m_values.soh, 1e3, &ind);
reply_func(send_buffer, ind);
} break;
default:
break;
}
if (m_conf.type == BMS_TYPE_VESC && UTILS_AGE_S(m_values.update_time) < MAX_CAN_AGE_SEC) {
switch (packet_id) {
case COMM_BMS_SET_CHARGE_ALLOWED:
case COMM_BMS_SET_BALANCE_OVERRIDE:
case COMM_BMS_RESET_COUNTERS:
case COMM_BMS_FORCE_BALANCE:
case COMM_BMS_ZERO_CURRENT_OFFSET: {
comm_can_send_buffer(m_values.can_id, data - 1, len + 1, 0);
} break;
default:
break;
}
}
}
bms_values *bms_get_values(void) {
return (bms_values*)&m_values;
}