-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMS.cpp
251 lines (220 loc) · 5.72 KB
/
BMS.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
#ifndef BMS_CPP
#define BMS_CPP
//This is to define how you wish the code to send errors. Usually you would want it to send to the Serial Monitor.
//For me I wanted it to send it to the Serial Monitor but in JSON format.
#ifndef debug_msg
#define debug_msg(error) Serial.println(F("{\"s\":\"bms_error\", \"error\": \"" error "\"}"));
// \/\/\/\/ Enable to get errors on Serial Monitor. Make sure to comment out the line above, too.
//#define debug_msg(error) Serial.println(F(error));
#endif
#include "Arduino.h"
#include "BMS.h"
BMS::BMS()
{
}
void BMS::begin(SoftwareSerial *serialPort)
{
BMSSerial = serialPort;
clear();
#if BMS_DEBUG
Serial.println("BMS DEBUG ENABLED");
#endif
}
bool BMS::requestResponse(uint16_t maxWait)
{
// Make sure any remaining data is flushed out.
clear();
BMSSerial->listen();
#if BMS_DEBUG
Serial.println("Starting request...");
#endif
// Send the request
BMSSerial->write(header, 2);
BMSSerial->write(outdata.command);
BMSSerial->write(outdata.len);
if (outdata.len)
BMSSerial->write(buffer, outdata.len);
BMSSerial->write(outdata.checksumA);
BMSSerial->write(outdata.checksumB);
BMSSerial->write(end);
BMSSerial->flush();
#if BMS_DEBUG
Serial.println("Finished sending request...");
#endif
delay(10);
auto start = millis();
while (BMSSerial->peek() != 0xDD)
{
if (millis() > maxWait + start)
{
#if BMS_DEBUG
Serial.println("Got no data from the BMS. Returning nothing.");
#endif
return false;
}
delay(10);
}
#if BMS_DEBUG
Serial.print("Start: ");
printHex(next());
Serial.println();
#else
next();
#endif
// Second byte echos command. Lets make sure it matches the original byte.
indata.command = next();
#if BMS_DEBUG
Serial.print("Command: ");
printHex(indata.command);
Serial.println();
#endif
if (indata.command != outdata.command)
{
debug_msg("Ignorable error: Command byte does not match.") return false;
}
//Next byte is status.
byte status = next();
#if BMS_DEBUG
Serial.print("Status: ");
printHex(status);
Serial.println();
#endif
if (!checkStatus(status))
return false;
//Next byte is length
indata.len = next();
#if BMS_DEBUG
Serial.print("Length: ");
printHex(indata.len);
Serial.print("\nData:");
#endif
// Now lets load the payload into the... well payload.
for (int i = 0; i < indata.len; i++)
{
buffer[i] = next();
#if BMS_DEBUG
printHex(buffer[i]);
#endif
}
indata.checksumA = next();
indata.checksumB = next();
#if BMS_DEBUG
printHex(indata.checksumA);
printHex(indata.checksumB);
#endif
// make sure last byte is end byte
byte end = next();
#if BMS_DEBUG
printHex(end);
#endif
if (end != 0x77)
{
debug_msg("Fatal error: End byte does not match.");
return false;
}
return true;
}
boolean BMS::update(uint16_t maxWait)
{
//Request cell data.
outdata.command = 0x04;
outdata.len = 0x00;
outdata.checksumA = 0xFF;
outdata.checksumB = 0xFC;
bool cellSuccess = requestResponse(maxWait);
if (indata.len != NUM_CELLS * 2)
debug_msg("Fatal error: BMS returned no or incorrect data!")
else if (cellSuccess)
{
cellMax = 0, cellMin = 100;
for (int i = 0; i < (indata.len) / 2; i++)
{
byte highbyte = buffer[2 * i];
byte lowbyte = buffer[2 * i + 1];
uint16_t cellnow = (highbyte << 8) | lowbyte;
cells[i] = cellnow / 1000.0f;
if (cells[i] > cellMax)
cellMax = cells[i];
if (cells[i] < cellMin)
cellMin = cells[i];
}
cellDiff = cellMax - cellMin;
}
else
{
debug_msg("Some error getting cell data.");
}
clear();
//Request pack data.
outdata.command = 0x03;
outdata.len = 0x00;
outdata.checksumA = 0xFF;
outdata.checksumB = 0xFD;
bool mainSuccess = requestResponse(maxWait);
if (mainSuccess)
{
balanceState = ((uint32_t)buffer[14]) << 24 | ((uint32_t)buffer[15]) << 16 | ((uint16_t)buffer[12]) << 8 | buffer[13];
uint16_t temp;
temp = (buffer[0] << 8) | buffer[1];
packVoltage = temp / 100.0f; // convert to float and leave at 2 dec places
temp = (buffer[2] << 8) | buffer[3];
packCurrent = temp / 100.0f; // convert to float and leave at 2 dec places
if (buffer[22] != NUM_TEMP_PROBES)
{
Serial.print(F("Error: num of temp probes. Found: "));
Serial.print(buffer[22]);
}
for (byte i = 0; i < buffer[22]; i++)
{
temp = buffer[23 + 2 * i] << 8 | buffer[24 + 2 * i];
probes[i] = (temp - 2731) / 10.00f;
}
MOSFETStatus.charge = (buffer[20] % 2) == 1;
MOSFETStatus.discharge = buffer[20] >= 2;
}
else
{
debug_msg("Some error getting pack data.");
}
return cellSuccess && mainSuccess;
}
boolean BMS::checkStatus(byte status)
{
switch (status)
{
case 0x80:
debug_msg("BMS returned fail code...");
return false;
case 0x00:
return true;
default:
debug_msg("BMS returned unknown code: ");
Serial.println(status);
return false;
}
return false;
}
void BMS::printHex(byte x)
{
if (x < 16)
{
Serial.print("0");
}
Serial.print(x, HEX);
Serial.print(" ");
}
//Robust way to return the next byte from the port.
byte BMS::next()
{
while (!BMSSerial->available())
;
return BMSSerial->read();
}
void BMS::clear()
{
while (BMSSerial->available())
{
BMSSerial->read();
}
}
#endif