forked from maland16/daly-bms-uart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaly-bms-uart.cpp
183 lines (147 loc) · 5.33 KB
/
daly-bms-uart.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
#include "Arduino.h"
#include "daly-bms-uart.h"
//----------------------------------------------------------------------
// Public Functions
//----------------------------------------------------------------------
Daly_BMS_UART::Daly_BMS_UART(HardwareSerial &serial_peripheral)
{
this->my_serialIntf = &serial_peripheral;
}
bool Daly_BMS_UART::Init()
{
// Null check the serial interface
if (this->my_serialIntf == NULL)
{
#ifdef DALY_BMS_DEBUG
Serial.println("<DALY-BMS DEBUG> ERROR: No serial peripheral specificed!");
#endif
return false;
}
// Intialize the serial link to 9600 baud with 8 data bits and no parity bits, per the Daly BMS spec
this->my_serialIntf->begin(9600, SERIAL_8N1);
// Set up the output buffer with some values that won't be changing
this->my_txBuffer[0] = 0xA5; // Start byte
this->my_txBuffer[1] = 0x80; // Host address
// this->my_txBuffer[2] is where our command ID goes
this->my_txBuffer[3] = 0x08; // Length?
// Fill bytes 5-11 with 0s
for (uint8_t i = 4; i < 12; i++)
{
this->my_txBuffer[i] = 0x00;
}
return true;
}
bool Daly_BMS_UART::getPackMeasurements(float &voltage, float ¤t, float &SOC)
{
this->sendCommand(COMMAND::VOUT_IOUT_SOC);
if (!this->receiveBytes())
{
#ifdef DALY_BMS_DEBUG
Serial.printf("<DALY-BMS DEBUG> Receive failed, V, I, & SOC values won't be modified!\n");
#endif
return false;
}
// Pull the relevent values out of the buffer
voltage = (float)((this->my_rxBuffer[4] << 8) | this->my_rxBuffer[5]) / 10;
// The current measurement is given with a 30000 unit offset
current = (float)(((this->my_rxBuffer[8] << 8) | this->my_rxBuffer[9]) - 30000) / 10;
SOC = (float)((this->my_rxBuffer[10] << 8) | this->my_rxBuffer[11]) / 10;
return true;
}
bool Daly_BMS_UART::getPackTemp(int8_t &temp)
{
this->sendCommand(COMMAND::MIN_MAX_TEMPERATURE);
if (!this->receiveBytes())
{
#ifdef DALY_BMS_DEBUG
Serial.printf("<DALY-BMS DEBUG> Receive failed, Temp value won't be modified!\n");
#endif
return false;
}
int8_t max_temp = (this->my_rxBuffer[4] - 40);
int8_t min_temp = (this->my_rxBuffer[6] - 40);
temp = (max_temp + min_temp) / 2;
return true;
}
bool Daly_BMS_UART::getMinMaxCellVoltage(float &minCellV, uint8_t &minCellVNum, float &maxCellV, uint8_t &maxCellVNum)
{
this->sendCommand(COMMAND::MIN_MAX_CELL_VOLTAGE);
if (!receiveBytes())
{
#ifdef DALY_BMS_DEBUG
Serial.printf("<DALY-BMS DEBUG> Receive failed, min/max cell values won't be modified!\n");
#endif
return false;
}
maxCellV = (float)((this->my_rxBuffer[4] << 8) | this->my_rxBuffer[5]) / 1000; // Given in mV, convert to V
maxCellVNum = this->my_rxBuffer[6];
minCellV = (float)((this->my_rxBuffer[7] << 8) | this->my_rxBuffer[8]) / 1000; // Given in mV, convert to V
minCellVNum = this->my_rxBuffer[9];
return true;
}
//----------------------------------------------------------------------
// Private Functions
//----------------------------------------------------------------------
void Daly_BMS_UART::sendCommand(COMMAND cmdID)
{
this->my_txBuffer[2] = cmdID;
// We can cheat a little when calculating the CRC of the outgoing UART transmission beacause
// the only thing that changes in the outgoing buffer is the command, the rest stays the same.
// Checksum = sum of all bytes, truncated to an 8 bit integer. See the readme for more info.
// Checksum = (0xA5 + 0x80 + Command Num + 0x08) = (0x2D + Command Num)
uint8_t checksum = 0x2D + cmdID;
this->my_txBuffer[12] = checksum;
#ifdef DALY_BMS_DEBUG
Serial.print("<DALY-BMS DEBUG> Checksum = 0x");
Serial.println(checksum, HEX);
#endif
this->my_serialIntf->write(this->my_txBuffer, XFER_BUFFER_LENGTH);
}
bool Daly_BMS_UART::receiveBytes(void)
{
// Clear out the input buffer
memset(this->my_rxBuffer, 0, XFER_BUFFER_LENGTH);
// Read bytes from the specified serial interface
uint8_t rxByteNum = this->my_serialIntf->readBytes(this->my_rxBuffer, XFER_BUFFER_LENGTH);
// Make sure we got the correct number of bytes
if (rxByteNum != XFER_BUFFER_LENGTH)
{
#ifdef DALY_BMS_DEBUG
Serial.print("<DALY-BMS DEBUG> Error: Received the wrong number of bytes! Expected 13, got ");
Serial.println(rxByteNum, DEC);
this->barfRXBuffer();
#endif
return false;
}
if (!validateChecksum())
{
#ifdef DALY_BMS_DEBUG
Serial.println("<DALY-BMS DEBUG> Error: Checksum failed!");
this->barfRXBuffer();
#endif
return false;
}
return true;
}
bool Daly_BMS_UART::validateChecksum()
{
uint8_t checksum = 0x00;
for (int i = 0; i < XFER_BUFFER_LENGTH - 1; i++)
{
checksum += this->my_rxBuffer[i];
}
#ifdef DALY_BMS_DEBUG
Serial.printf("<DALY-BMS DEBUG> Calculated checksum: 0x%x, Received checksum: 0x%x\n", checksum, this->my_rxBuffer[XFER_BUFFER_LENGTH - 1]);
#endif
// Compare the calculated checksum to the real checksum (the last received byte)
return (checksum == this->my_rxBuffer[XFER_BUFFER_LENGTH - 1]);
}
void Daly_BMS_UART::barfRXBuffer(void)
{
Serial.printf("<DALY-BMS DEBUG> RX Buffer: [");
for (int i = 0; i < XFER_BUFFER_LENGTH; i++)
{
Serial.printf("0x%x,", this->my_rxBuffer[i]);
}
Serial.printf("]\n");
}