-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMSModule.cpp
267 lines (228 loc) · 6.49 KB
/
BMSModule.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
#include "BMSModule.h"
#include "driver/gpio.h"
#include "driver/twai.h"
#define MAX_MODULE_ADDR 0x3E
#define IGNORE_CELL_LOW 0.5
#define IGNORE_CELL_HIGH 5.0
BMSModule::BMSModule() {
for (int i = 0; i < 14; i++) {
cellVolt[i] = 0.0f;
}
balance = 0;
moduleVolt = 0.0f;
temperatures[0] = 0.0f;
temperatures[1] = 0.0f;
temperatures[2] = 0.0f;
balstat = 0;
exists = false;
reset = false;
type = 1;
}
void BMSModule::clearmodule() {
for (int i = 0; i < 14; i++) {
cellVolt[i] = 0.0f;
}
moduleVolt = 0.0f;
temperatures[0] = 0.0f;
temperatures[1] = 0.0f;
temperatures[2] = 0.0f;
balstat = 0;
exists = false;
reset = false;
}
void BMSModule::decodetemp(twai_message_t &msg, int y) {
if (y == 1) // 0x00 in byte 2 means its an MEB message
{
type = 1;
if (msg.data[7] == 0xFD && msg.data[2] != 0xFD)
{
temperatures[0] = (msg.data[2] * 0.5) - 40;
}
else if (msg.data[0] < 0xDF)
{
temperatures[0] = (msg.data[0] * 0.5) - 43;
balstat = msg.data[2] + (msg.data[3] << 8);
temperatures[1] = (msg.data[4] < 0xF0) ? ((msg.data[4] * 0.5) - 43) : 0;
temperatures[2] = (msg.data[5] < 0xF0) ? ((msg.data[5] * 0.5) - 43) : 0;
}
else
{
temperatures[0] = (msg.data[3] * 0.5) - 43;
temperatures[1] = (msg.data[4] < 0xF0) ? ((msg.data[4] * 0.5) - 43) : 0;
temperatures[2] = (msg.data[5] < 0xF0) ? ((msg.data[5] * 0.5) - 43) : 0;
}
}
else
{
uint16_t temp_val = ((msg.data[5] & 0x0F) << 4) | ((msg.data[4] & 0xF0) >> 4);
switch (y)
{
case 2:
type = 2;
temperatures[0] = (temp_val * 0.5) - 40; // MEB Bits 36-44
break;
case 3:
type = 3;
temperatures[1] = (temp_val * 0.5) - 40; // MEB Bits 36-44
break;
}
}
}
void BMSModule::decodecan(int Id, twai_message_t &msg) {
static const int idToIndex[] = {0, 4, 8, 12};
int index = idToIndex[Id];
cellVolt[index++] = (uint16_t(msg.data[1] >> 4) + uint16_t(msg.data[2] << 4) + 1000) * 0.001;
cellVolt[index++] = (msg.data[3] + uint16_t((msg.data[4] & 0x0F) << 8) + 1000) * 0.001;
if (Id != 3) {
cellVolt[index++] = (uint16_t(msg.data[5] << 4) + uint16_t(msg.data[4] >> 4) + 1000) * 0.001;
cellVolt[index] = (msg.data[6] + uint16_t((msg.data[7] & 0x0F) << 8) + 1000) * 0.001;
}
}
uint16_t BMSModule::getBalance() { return balance; }
float BMSModule::getCellVoltage(int cell) {
if (cell < 0 || cell > 13)
return 0.0f;
return cellVolt[cell];
}
float BMSModule::getLowCellV() {
float lowVal = 10.0f;
for (int i = 0; i < 14; i++)
if (cellVolt[i] < lowVal && cellVolt[i] > IGNORE_CELL_LOW)
lowVal = cellVolt[i];
return lowVal;
}
float BMSModule::getHighCellV() {
float hiVal = 0.0f;
for (int i = 0; i < 14; i++)
if (cellVolt[i] > IGNORE_CELL_LOW && cellVolt[i] < IGNORE_CELL_HIGH) {
if (cellVolt[i] > hiVal)
hiVal = cellVolt[i];
}
return hiVal;
}
float BMSModule::getAverageV() {
int x = 0;
float avgVal = 0.0f;
for (int i = 0; i < 14; i++) {
if (cellVolt[i] > IGNORE_CELL_LOW && cellVolt[i] < IGNORE_CELL_HIGH) {
x++;
avgVal += cellVolt[i];
}
}
scells = x;
avgVal /= x;
if (scells == 0) {
avgVal = 0;
}
return avgVal;
}
int BMSModule::getscells() { return scells; }
float BMSModule::getLowTemp() {
if (type == 1) {
if (sensor == 0) {
if (getAvgTemp() > 0.5) {
if (temperatures[0] > 0.5) {
if (temperatures[0] < temperatures[1] &&
temperatures[0] < temperatures[2]) {
return (temperatures[0]);
}
}
if (temperatures[1] > 0.5) {
if (temperatures[1] < temperatures[0] &&
temperatures[1] < temperatures[2]) {
return (temperatures[1]);
}
}
if (temperatures[2] > 0.5) {
if (temperatures[2] < temperatures[1] &&
temperatures[2] < temperatures[0]) {
return (temperatures[2]);
}
}
}
} else {
return temperatures[sensor - 1];
}
} else {
return temperatures[0];
}
return -88;
}
float BMSModule::getHighTemp() {
if (type == 1) {
if (sensor == 0) {
return (temperatures[0] < temperatures[1]) ? temperatures[1]
: temperatures[0];
} else {
return temperatures[sensor - 1];
}
} else {
return temperatures[0];
}
return -88;
}
float BMSModule::getAvgTemp() {
if (type == 1) {
if (sensor == 0) {
if ((temperatures[0] + temperatures[1] + temperatures[2]) / 3.0f > 0.5) {
if (temperatures[0] > 0.5 && temperatures[1] > 0.5 &&
temperatures[2] > 0.5) {
return (temperatures[0] + temperatures[1] + temperatures[2]) / 3.0f;
}
if (temperatures[0] < 0.5 && temperatures[1] > 0.5 &&
temperatures[2] > 0.5) {
return (temperatures[1] + temperatures[2]) / 2.0f;
}
if (temperatures[0] > 0.5 && temperatures[1] < 0.5 &&
temperatures[2] > 0.5) {
return (temperatures[0] + temperatures[2]) / 2.0f;
}
if (temperatures[0] > 0.5 && temperatures[1] > 0.5 &&
temperatures[2] < 0.5) {
return (temperatures[0] + temperatures[1]) / 2.0f;
}
if (temperatures[0] > 0.5 && temperatures[1] < 0.5 &&
temperatures[2] < 0.5) {
return (temperatures[0]);
}
if (temperatures[0] < 0.5 && temperatures[1] > 0.5 &&
temperatures[2] < 0.5) {
return (temperatures[1]);
}
if (temperatures[0] < 0.5 && temperatures[1] < 0.5 &&
temperatures[2] > 0.5) {
return (temperatures[2]);
}
if (temperatures[0] < 0.5 && temperatures[1] < 0.5 &&
temperatures[2] < 0.5) {
return (-80);
}
}
} else {
return temperatures[sensor - 1];
}
} else {
return temperatures[0];
}
return -88;
}
float BMSModule::getModuleVoltage() {
moduleVolt = 0;
for (int i = 0; i < 14; i++) {
if (cellVolt[i] > IGNORE_CELL_LOW && cellVolt[i] < IGNORE_CELL_HIGH) {
moduleVolt = moduleVolt + cellVolt[i];
}
}
return moduleVolt;
}
float BMSModule::getTemperature(int temp) {
if (temp < 0 || temp > 2)
return 0.0f;
return temperatures[temp];
}
int BMSModule::getType() { return type; }
bool BMSModule::isExisting() { return exists; }
bool BMSModule::isReset() { return reset; }
void BMSModule::setExists(bool ex) { exists = ex; }
void BMSModule::setReset(bool ex) { reset = ex; }
void BMSModule::setBalance(uint16_t value) { balance = value; }