-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshinwabms.h
319 lines (276 loc) · 7.55 KB
/
shinwabms.h
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "esphome.h"
class ShinwaBMSuartComponent : public Component, public UARTDevice {
public:
ShinwaBMSuartComponent(UARTComponent *parent) : UARTDevice(parent) {}
Sensor *batteryVoltage = new Sensor();
Sensor *batteryCurrent = new Sensor();
//Sensor *tempSensor[5] = {new Sensor(),new Sensor(),new Sensor(),new Sensor(),new Sensor()};
Sensor *tempSensorMax = new Sensor();
Sensor *tempSensorAvg = new Sensor();
Sensor *tempSensorMin = new Sensor();
/*Sensor *batteryCell[16] = {
new Sensor(),new Sensor(),new Sensor(),new Sensor(),
new Sensor(),new Sensor(),new Sensor(),new Sensor(),
new Sensor(),new Sensor(),new Sensor(),new Sensor(),
new Sensor(),new Sensor(),new Sensor(),new Sensor()
};*/
Sensor *batteryCellMax = new Sensor();
Sensor *batteryCellMin = new Sensor();
Sensor *batteryCellAvg = new Sensor();
Sensor *batteryCapacity = new Sensor();
float cellVoltage[16];
//TODO flags (balance, undervoltage, overvoltage)
float current;
int soc;
float capacity;
float temperature[5];
int alarm[5];
int cyclesCount;
float voltage;
float soh;
int reserved;
byte b_readed;
char crc(char *buf, byte len)
{
byte i, chk = 0;
int sum = 0;
for (i = 0; i < len; i++)
{
chk ^= buf[i];
sum += buf[i];
}
return (byte)((chk ^ sum) & 0xFF);
}
void send(byte addr, byte cmd, char *data)
{
static char buf[10];
byte size = 0;
buf[size++] = 0x7E;
buf[size++] = addr;
buf[size++] = cmd;
buf[size++] = 0x00; //data length
//TODO: add data
/*
if($data === null){
$pkt .= chr(0);
}else{
$pkt .= chr(strlen($data));
$pkt .= $data;
}
*/
byte checksum = crc(buf, size);
buf[size++] = checksum;
buf[size++] = 0x0D;
ESP_LOGD("custom", "Send pkt %d %d checksum: %d", addr, cmd, checksum);
write_array((const uint8_t*)buf, size);
}
void setup() override {
ESP_LOGD("custom", "setup()");
byte limit = 20;
while (!available()) {
send(0x00, 0x01, NULL);
if(--limit <= 0){
ESP_LOGD("custom", "hear nothing, break");
break;
}
//usleep(1000); TODO?
}
}
int readblock()
{
b_readed = 0;
byte subcmd = readlog();
switch (subcmd)
{
case 0x01:{ //cell voltage
byte cellcount = readlog();
if(cellcount>16){
ESP_LOGD("custom", "Cell count too high: %d", cellcount);
return 100;
}
const byte f_bal = 0x80;
const byte f_ov = 0x40;
const byte f_uv = 0x20;
float cellmin = 10.0;
float cellmax = 0.0;
float cellavg = 0.0;
for(byte i=0; i<cellcount; i++){
byte val = readlog();
if(val & f_bal){
val &= (byte)(~f_bal);
//TODO set flag
}
if(val & f_ov){
val &= (byte)(~f_ov);
//TODO set flag
}
if(val & f_uv){
val &= (byte)(~f_uv);
//TODO set flag
}
cellVoltage[i] = (val*256 + readlog())/1000.0;
ESP_LOGD("custom", "Cell %d %f V", i, cellVoltage[i]);
//batteryCell[i]->publish_state(cellVoltage[i]);
if(cellVoltage[i] > cellmax)cellmax = cellVoltage[i];
if(cellVoltage[i] < cellmin)cellmin = cellVoltage[i];
cellavg += cellVoltage[i];
}
batteryCellMin->publish_state(cellmin);
batteryCellMax->publish_state(cellmax);
batteryCellAvg->publish_state(cellavg/cellcount);
break;
}
case 0x02:{ //current
byte shunts = readlog();
for(byte i=0; i<shunts; i++){
current = (30000 - (readlog()*256 + readlog())) / 100.0;
ESP_LOGD("custom", "Current %d %f A", i, current);
batteryCurrent->publish_state(current);
}
break;
}
case 0x03:{ //SOC (must be 0-100)
byte socs = readlog();
for(byte i=0; i<socs; i++){
soc = ((readlog()*256 + readlog())) / 100;
ESP_LOGD("custom", "SoC %d %d %%", i, soc);
}
break;
}
case 0x04:{ //Capacity (1-600Ah, 0.01Ah unit)
byte caps = readlog();
for(byte i=0; i<caps; i++){
capacity = ((readlog()*256 + readlog())) / 100.0;
batteryCapacity->publish_state(capacity);
ESP_LOGD("custom", "Capacity %d %f Ah", i, capacity);
}
break;
}
case 0x05:{ //Tempearature (offset -50V)
byte temps = readlog();
float tempmax = 0.0, tempmin = 255.0, avgtemp = 0;
for(byte i=0; i<temps; i++){
temperature[i] = 50-(readlog()*256 + readlog())/10.0;
ESP_LOGD("custom", "Temp %d %f C", i, temperature[i]);
//TODO: fix this stupid spike (hw problem?)
if(temperature[i] < 0 || temperature[i] > 40)continue;
if(i < 4){
//TODO: temp4 remove flags?
//tempSensor[i]->publish_state(temperature[i]);
if(temperature[i] > tempmax)tempmax = temperature[i];
if(temperature[i] < tempmin)tempmin = temperature[i];
avgtemp += temperature[i];
}
}
tempSensorMax->publish_state(tempmax);
tempSensorAvg->publish_state(avgtemp / (temps-1));
tempSensorMin->publish_state(tempmin);
break;
}
case 0x06:{ //Alarm
byte alarms = readlog();
for(byte i=0; i<alarms; i++){
alarm[i] = (readlog()*256 + readlog());
ESP_LOGD("custom", "Alarm %d %d", i, alarm[i]);
}
break;
}
case 0x07:{ //Cycle count
byte cycles = readlog();
for(byte i=0; i<cycles; i++){
cyclesCount = (readlog()*256 + readlog());
ESP_LOGD("custom", "Cycles %d %d", i, cyclesCount);
}
break;
}
case 0x08:{ //Battery voltage (10mV unit)
byte batts = readlog();
for(byte i=0; i<batts; i++){
voltage = (readlog()*256 + readlog())/100.0;
ESP_LOGD("custom", "Battery %d %f", i, voltage);
batteryVoltage->publish_state(voltage);
}
break;
}
case 0x09:{ //SOH (must be 0-100)
byte sohs = readlog();
for(byte i=0; i<sohs; i++){
soh = (readlog()*256 + readlog())/100.0;
ESP_LOGD("custom", "SOH %d %f", i, soh);
}
break;
}
case 0x0A:{ //Reserved
byte reserv = readlog();
for(byte i=0; i<reserv; i++){
reserved = (readlog()*256 + readlog());
ESP_LOGD("custom", "Reserved %d %d", i, reserved);
}
break;
}
case 0x20:{ //NOOP
ESP_LOGD("custom", "NOOP");
break;
}
case 0x0D:{ //EOF
ESP_LOGD("custom", "End of packet");
break;
}
default:{ //some unexpected data received
ESP_LOGD("custom", "Dummy read (%d)", subcmd);
while(available()){
byte dummy = readlog();
if(dummy == 0x0D || dummy == 0xFF)break;
}
}
}
return b_readed;
}
int recv()
{
//header magic
byte magic = readlog();
if(magic != 0x7E){
ESP_LOGD("custom", "Invalid magic: %d", magic);
return -1;
}
//device address
byte addr = readlog();
if(addr == 0x00){
//command id
byte cmd = readlog();
if(cmd == 0x01){
byte datalen = readlog();
byte readed = 0;
while(readed < datalen){
ESP_LOGD("custom", "Readed %d of %d", readed, datalen);
readed += readblock();
}
return 1;
}else{
ESP_LOGD("custom", "Unknown command: %d", cmd);
}
}else{
ESP_LOGD("custom", "Unknown address: %d", addr);
}
return 0;
}
byte readlog()
{
byte tmp = read();
//ESP_LOGD("custom", " %02x", tmp);
b_readed++;
return tmp;
}
void loop() override {
static int last_update;
if((last_update + (30 * 1000)) < millis()){
last_update = millis();
ESP_LOGD("custom", "loop()");
while(available())read();
//TODO: send init packet when bms stops sending data?
send(0x00, 0x01, NULL);
if(recv()){}
}
}
};