Skip to content

Commit 96af7a7

Browse files
committed
Reduced RAM usage by using more flash strings
Now prints "finished sending request" only in debug mode removed warning about random byte solved balance state issues!
1 parent 3e535ff commit 96af7a7

File tree

3 files changed

+63
-34
lines changed

3 files changed

+63
-34
lines changed

BMS.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ bool BMS::requestResponse(uint16_t maxWait)
1919
{
2020
// Make sure any remaining data is flushed out.
2121
clear();
22-
byte p;
2322

2423
// Send the request
2524
BMSSerial->write(header, 2);
@@ -31,7 +30,9 @@ bool BMS::requestResponse(uint16_t maxWait)
3130
BMSSerial->write(outdata.checksumB);
3231
BMSSerial->write(end);
3332
BMSSerial->flush();
33+
#if BMS_DEBUG
3434
Serial.println("\nFinished sending request...");
35+
#endif
3536
delay(10);
3637

3738
auto start = millis();
@@ -42,11 +43,13 @@ bool BMS::requestResponse(uint16_t maxWait)
4243
delay(10);
4344
}
4445

45-
p = next();
46+
4647
#if BMS_DEBUG
4748
Serial.print("Start: ");
48-
printHex(p);
49+
printHex(next());
4950
Serial.println();
51+
#else
52+
next();
5053
#endif
5154

5255
// Second byte echos command. Lets make sure it matches the original byte.
@@ -156,9 +159,9 @@ boolean BMS::update(uint16_t maxWait)
156159
outdata.checksumB = 0xFD;
157160
bool mainSuccess = requestResponse(maxWait);
158161

159-
if (cellSuccess)
162+
if (mainSuccess)
160163
{
161-
balanceState = ((uint32_t)indata.data[13]) << 24 | ((uint32_t)indata.data[14]) << 16 | ((uint32_t)indata.data[15]) << 8 | indata.data[16];
164+
balanceState = ((uint32_t)indata.data[14]) << 24 | ((uint32_t)indata.data[15]) << 16 | ((uint16_t)indata.data[12]) << 8 | indata.data[13];
162165

163166
uint16_t temp;
164167

@@ -170,7 +173,7 @@ boolean BMS::update(uint16_t maxWait)
170173

171174
if (indata.data[22] != NUM_TEMP_PROBES)
172175
{
173-
Serial.print("Error: wrong number of temp probes found. Found: ");
176+
Serial.print(F("Error: num of temp probes. Found: "));
174177
Serial.print(indata.data[22]);
175178
}
176179

@@ -180,9 +183,8 @@ boolean BMS::update(uint16_t maxWait)
180183
probes[i] = (temp - 2731) / 10.00f;
181184
}
182185

183-
Serial.println(indata.data[20]);
184-
MOSFETStatus.charge = !(indata.data[20] & (1 << 7));
185-
MOSFETStatus.discharge = !(indata.data[20] & (1 << 6));
186+
MOSFETStatus.charge = (indata.data[20] % 2) == 1;
187+
MOSFETStatus.discharge = indata.data[20] >= 2;
186188
}
187189
else
188190
{
@@ -197,12 +199,12 @@ boolean BMS::checkStatus(byte status)
197199
switch (status)
198200
{
199201
case 0x80:
200-
Serial.println("BMS returned fail code...");
202+
Serial.println(F("BMS returned fail code..."));
201203
return false;
202204
case 0x00:
203205
return true;
204206
default:
205-
Serial.println("BMS returned unknown code:");
207+
Serial.println(F("BMS returned unknown code:"));
206208
Serial.println(status);
207209
return false;
208210
}

BMS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#ifndef NUM_CELLS
88
#define NUM_CELLS 21
99
#endif
10+
1011
#ifndef NUM_TEMP_PROBES
1112
#define NUM_TEMP_PROBES 4
1213
#endif
14+
1315
#ifndef BMS_DEBUG
1416
#define BMS_DEBUG false
1517
#endif

examples/BasicRead/BasicRead.ino

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,85 @@
33
- Read the BMS cell voltages, pack voltage, and error codes.
44
*/
55

6-
7-
#define BMS_RX_PIN 2 //pin the bms is reciving signals on, where we are sending.
8-
#define BMS_TX_PIN 3 //pin the BMS is sending signals to, where we are recieving.
6+
#define BMS_RX_PIN 2 //pin the bms is receiving signals on, where we are sending.
7+
#define BMS_TX_PIN 3 //pin the BMS is sending signals to, where we are receiving.
98
#define NUM_CELLS 21
109
#define NUM_TEMP_PROBES 4
10+
#define BMS_DEBUG false
11+
// Debug mode prints the hex of every byte coming in and leaving.
12+
// You can compare the bytes you receive to what you should be receiving on the google sheet linked at the bottom of the readme.
1113

1214
#include <SoftwareSerial.h>
1315
#include <BMS.h>
1416
SoftwareSerial BMSSerial(BMS_TX_PIN, BMS_RX_PIN);
1517
BMS bms;
1618

17-
void setup() {
19+
void setup()
20+
{
1821
Serial.begin(115200);
19-
22+
2023
BMSSerial.begin(9600);
2124
bms.begin(&BMSSerial);
22-
25+
2326
Serial.println("Finished setup");
2427
}
2528

26-
void loop() {
27-
28-
bms.update();
29+
void loop()
30+
{
2931

32+
bms.update();
3033
Serial.println("Voltages: (V)");
31-
for (byte i = 0; i < NUM_CELLS; i++) {
34+
for (byte i = 0; i < NUM_CELLS; i++)
35+
{
3236
Serial.print(i + 1);
3337
Serial.print(F(" "));
34-
if (i < 9) Serial.print(F(" "));
38+
if (i < 9)
39+
Serial.print(F(" "));
3540
}
3641
Serial.println();
37-
float* cells = bms.getCells();
38-
for (byte i = 0; i < NUM_CELLS; i++) {
42+
float *cells = bms.getCells();
43+
for (byte i = 0; i < NUM_CELLS; i++)
44+
{
3945
Serial.print(cells[i], 2);
4046
Serial.print(F(" "));
4147
}
42-
Serial.print("\nPack voltage: "); Serial.println(bms.getPackVoltage());
43-
Serial.print("Min cell voltage: "); Serial.println(bms.getCellMinVoltage());
44-
Serial.print("Max cell voltage: "); Serial.println(bms.getCellMaxVoltage());
45-
Serial.print("Cell Diff: "); Serial.println(bms.getCellDiff());
48+
Serial.println();
49+
uint32_t balstate = bms.getBalanceState();
50+
for (byte i = 0; i < NUM_CELLS; i++)
51+
{
52+
Serial.print(bitRead(balstate, i));
53+
Serial.print(F(" "));
54+
}
4655

47-
Serial.print("Balance State: "); Serial.println(bms.getBalanceState(), BIN);
48-
Serial.println("Charge: "); Serial.println(bms.getMOSFETStatus().charge ? "Permitted" : "Prohibited");
49-
Serial.println("Discharge: "); Serial.println(bms.getMOSFETStatus().discharge ? "Permitted" : "Prohibited");
56+
Serial.print("\nPack voltage: ");
57+
Serial.println(bms.getPackVoltage());
58+
Serial.println("\nMax Min Average");
59+
Serial.print(bms.getCellMaxVoltage());
60+
Serial.print(" ");
61+
Serial.print(bms.getCellMinVoltage());
62+
Serial.print(" ");
63+
Serial.println(bms.getPackVoltage() / NUM_CELLS);
64+
Serial.print("Cell Diff: ");
65+
Serial.println(bms.getCellDiff());
66+
67+
Serial.print("Charge: ");
68+
Serial.println(bms.getMOSFETStatus().charge ? "Permitted" : "Prohibited");
69+
Serial.print("Discharge: ");
70+
Serial.println(bms.getMOSFETStatus().discharge ? "Permitted" : "Prohibited");
5071

5172
Serial.println("Temps: (C)");
52-
for (byte i = 0; i < NUM_TEMP_PROBES; i++) {
73+
for (byte i = 0; i < NUM_TEMP_PROBES; i++)
74+
{
5375
Serial.print(i + 1);
5476
Serial.print(F(" "));
5577
}
5678
Serial.println();
57-
float* probes = bms.getProbeData();
58-
for (byte i = 0; i < NUM_TEMP_PROBES; i++) {
79+
float *probes = bms.getProbeData();
80+
for (byte i = 0; i < NUM_TEMP_PROBES; i++)
81+
{
5982
Serial.print(probes[i], 2);
6083
Serial.print(F(" "));
6184
}
85+
86+
delay(2000);
6287
}

0 commit comments

Comments
 (0)