-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMSModule.cpp
296 lines (249 loc) · 7.62 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
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
#include "config.h"
#include "BMSModule.h"
BMSModule::BMSModule()
{
for (int i = 0; i < 6; i++)
{
cellVolt[i] = 0.0f;
lowestCellVolt[i] = 5.0f;
highestCellVolt[i] = 0.0f;
}
moduleVolt = 0.0f;
temperatures[0] = 0.0f;
temperatures[1] = 0.0f;
lowestTemperature = 200.0f;
highestTemperature = -100.0f;
lowestModuleVolt = 200.0f;
highestModuleVolt = 0.0f;
exists = false;
moduleAddress = 0;
}
/*
Reading the status of the board to identify any flags, will be more useful when implementing a sleep cycle
*/
void BMSModule::readStatus()
{
uint8_t payload[3];
uint8_t buff[8];
payload[0] = moduleAddress << 1; //adresss
payload[1] = REG_ALERT_STATUS;//Alert Status start
payload[2] = 0x04;
BMSModule::sendDataWithReply(payload, 3, false, buff, 7);
alerts = buff[3];
faults = buff[4];
COVFaults = buff[5];
CUVFaults = buff[6];
}
uint8_t BMSModule::getFaults()
{
return faults;
}
uint8_t BMSModule::getAlerts()
{
return alerts;
}
uint8_t BMSModule::getCOVCells()
{
return COVFaults;
}
uint8_t BMSModule::getCUVCells()
{
return CUVFaults;
}
/*
Reading the setpoints, after a reset the default tesla setpoints are loaded
Default response : 0x10, 0x80, 0x31, 0x81, 0x08, 0x81, 0x66, 0xff
*/
/*
void BMSModule::readSetpoint()
{
uint8_t payload[3];
uint8_t buff[12];
payload[0] = moduleAddress << 1; //adresss
payload[1] = 0x40;//Alert Status start
payload[2] = 0x08;//two registers
sendData(payload, 3, false);
delay(2);
getReply(buff);
OVolt = 2.0+ (0.05* buff[5]);
UVolt = 0.7 + (0.1* buff[7]);
Tset = 35 + (5 * (buff[9] >> 4));
} */
bool BMSModule::readModuleValues()
{
uint8_t payload[4];
uint8_t buff[50];
uint8_t calcCRC;
bool retVal = false;
int retLen;
float tempCalc;
float tempTemp;
payload[0] = moduleAddress << 1;
readStatus();
payload[1] = REG_ADC_CTRL;
payload[2] = 0b00111101; //ADC Auto mode, read every ADC input we can (Both Temps, Pack, 6 cells)
BMSModule::sendDataWithReply(payload, 3, true, buff, 3);
payload[1] = REG_IO_CTRL;
payload[2] = 0b00000011; //enable temperature measurement VSS pins
BMSModule::sendDataWithReply(payload, 3, true, buff, 3);
payload[1] = REG_ADC_CONV; //start all ADC conversions
payload[2] = 1;
BMSModule::sendDataWithReply(payload, 3, true, buff, 3);
payload[1] = REG_GPAI; //start reading registers at the module voltage registers
payload[2] = 0x12; //read 18 bytes (Each value takes 2 - ModuleV, CellV1-6, Temp1, Temp2)
retLen = BMSModule::sendDataWithReply(payload, 3, false, buff, 22);
calcCRC = BMSModule::genCRC(buff, retLen-1);
//18 data bytes, address, command, length, and CRC = 22 bytes returned
//Also validate CRC to ensure we didn't get garbage data.
if ( (retLen == 22) && (buff[21] == calcCRC) )
{
if (buff[0] == (moduleAddress << 1) && buff[1] == REG_GPAI && buff[2] == 0x12) //Also ensure this is actually the reply to our intended query
{
//payload is 2 bytes gpai, 2 bytes for each of 6 cell voltages, 2 bytes for each of two temperatures (18 bytes of data)
moduleVolt = (buff[3] * 256 + buff[4]) * 0.002034609f;
if (moduleVolt > highestModuleVolt) highestModuleVolt = moduleVolt;
if (moduleVolt < lowestModuleVolt) lowestModuleVolt = moduleVolt;
for (int i = 0; i < 6; i++)
{
cellVolt[i] = (buff[5 + (i * 2)] * 256 + buff[6 + (i * 2)]) * 0.000381493f;
if (lowestCellVolt[i] > cellVolt[i] && cellVolt[i] >= IgnoreCell) lowestCellVolt[i] = cellVolt[i];
if (highestCellVolt[i] < cellVolt[i]) highestCellVolt[i] = cellVolt[i];
}
//Now using steinhart/hart equation for temperatures. We'll see if it is better than old code.
tempTemp = (1.78f / ((buff[17] * 256 + buff[18] + 2) / 33046.0f) - 3.57f);
tempTemp *= 1000.0f;
tempCalc = 1.0f / (0.0007610373573f + (0.0002728524832 * logf(tempTemp)) + (powf(logf(tempTemp), 3) * 0.0000001022822735f));
temperatures[0] = tempCalc - 273.15f;
tempTemp = 1.78f / ((buff[19] * 256 + buff[20] + 9) / 33068.0f) - 3.57f;
tempTemp *= 1000.0f;
tempCalc = 1.0f / (0.0007610373573f + (0.0002728524832 * logf(tempTemp)) + (powf(logf(tempTemp), 3) * 0.0000001022822735f));
temperatures[1] = tempCalc - 273.15f;
if (getLowTemp() < lowestTemperature) lowestTemperature = getLowTemp();
if (getHighTemp() > highestTemperature) highestTemperature = getHighTemp();
retVal = true;
}
}
else
{
Serial.printf("Invalid module response received for module %i len: %i crc: %i calc: %i",
moduleAddress, retLen, buff[21], calcCRC);
}
//turning the temperature wires off here seems to cause weird temperature glitches
// payload[1] = REG_IO_CTRL;
// payload[2] = 0b00000000; //turn off temperature measurement pins
// BMSModule::sendData(payload, 3, true);
// delay(3);
// BMSModule::getReply(buff, 50); //TODO: we're not validating the reply here. Perhaps check to see if a valid reply came back
return retVal;
}
float BMSModule::getCellVoltage(int cell)
{
if (cell < 0 || cell > 5) return 0.0f;
return cellVolt[cell];
}
float BMSModule::getLowCellV()
{
float lowVal = 10.0f;
for (int i = 0; i < 6; i++) if (cellVolt[i] < lowVal && cellVolt[i] > IgnoreCell) lowVal = cellVolt[i];
return lowVal;
}
float BMSModule::getHighCellV()
{
float hiVal = 0.0f;
for (int i = 0; i < 6; i++) if (cellVolt[i] > hiVal) hiVal = cellVolt[i];
return hiVal;
}
float BMSModule::getAverageV()
{
int x =0;
float avgVal = 0.0f;
for (int i = 0; i < 6; i++)
{
if (cellVolt[i] > IgnoreCell)
{
x++;
avgVal += cellVolt[i];
}
}
avgVal /= x;
return avgVal;
}
float BMSModule::getHighestModuleVolt()
{
return highestModuleVolt;
}
float BMSModule::getLowestModuleVolt()
{
return lowestModuleVolt;
}
float BMSModule::getHighestCellVolt(int cell)
{
if (cell < 0 || cell > 5) return 0.0f;
return highestCellVolt[cell];
}
float BMSModule::getLowestCellVolt(int cell)
{
if (cell < 0 || cell > 5) return 0.0f;
return lowestCellVolt[cell];
}
float BMSModule::getHighestTemp()
{
return highestTemperature;
}
float BMSModule::getLowestTemp()
{
return lowestTemperature;
}
float BMSModule::getLowTemp()
{
return (temperatures[0] < temperatures[1]) ? temperatures[0] : temperatures[1];
}
float BMSModule::getHighTemp()
{
return (temperatures[0] < temperatures[1]) ? temperatures[1] : temperatures[0];
}
float BMSModule::getAvgTemp()
{
if (sensor == 0)
{
return (temperatures[0] + temperatures[1]) / 2.0f;
}
else
{
return temperatures[sensor-1];
}
}
float BMSModule::getModuleVoltage()
{
return moduleVolt;
}
float BMSModule::getTemperature(int temp)
{
if (temp < 0 || temp > 1) return 0.0f;
return temperatures[temp];
}
void BMSModule::setAddress(int newAddr)
{
if (newAddr < 0 || newAddr > MAX_MODULE_ADDR) return;
moduleAddress = newAddr;
}
int BMSModule::getAddress()
{
return moduleAddress;
}
bool BMSModule::isExisting()
{
return exists;
}
void BMSModule::settempsensor(int tempsensor)
{
sensor = tempsensor;
}
void BMSModule::setExists(bool ex)
{
exists = ex;
}
void BMSModule::setIgnoreCell(float Ignore)
{
IgnoreCell = Ignore;
}