-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcan.h
302 lines (261 loc) · 7.05 KB
/
can.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
//https://github.com/coryjfowler/MCP_CAN_lib
#define CAN_CS 10
// name to display - 8 chars
uint8_t can_name[] = "DALY.BMS";
// mfg to display - 8 chars
uint8_t can_mfg[] = "DALY.CAN";
#include <mcp_can.h>
//#include <SPI.h>
MCP_CAN CAN(CAN_CS); //set CS pin for can controlelr
void can_setup() {
if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("MCP2515 Initialized Successfully!");
} else {
Serial.println("Error Initializing MCP2515...");
while(1); // fixme - error handler
}
CAN.setMode(MCP_NORMAL);
}
void can_set_word(int i, uint16_t w) {
mbuf[i] = lowByte(w);
mbuf[i+1] = highByte(w);
}
void can_clear(void) {
memset(mbuf, 0, 8);
}
void can_send(uint16_t cmd, int len, uint8_t * b) {
int i;
/*Serial.print(cmd, HEX);
Serial.print(" [");
Serial.print(len);
Serial.print("]");
for(i = 0; i < len; i++) {
Serial.print(" ");
Serial.print(b[i], HEX);
}
Serial.println("");*/
CAN.sendMsgBuf(cmd, 0, len, b);
}
int can_update(void) {
// pylontech
#if 0
// build 0x359 - alarms
can_clear();
mbuf[5] = 'P';
mbuf[6] = 'N';
CAN.sendMsgBuf(0x359, 0, 8, mbuf);
delay(5);
// build 0x351 - charge specs
can_clear();
can_set_word(0, bat_chg_v/100U);
can_set_word(2, bat_chg_i/10U);
can_set_word(4, bat_dis_i/10U);
CAN.sendMsgBuf(0x351, 0, 8, mbuf);
delay(5);
// build 0x355 - bat state
can_clear();
can_set_word(0, bat_soc);
can_set_word(2, bat_soh);
can_set_word(3, bat_soc * 10); // from vecan?
CAN.sendMsgBuf(0x355, 0, 8, mbuf);
delay(5);
// build 0x356 - bat measurements
can_clear();
can_set_word(0, bat_v);
can_set_word(2, bat_i);
can_set_word(4, bat_t);
CAN.sendMsgBuf(0x356, 0, 8, mbuf);
delay(5);
// build 0x35a - no idea - from vecan
can_clear();
CAN.sendMsgBuf(0x35A, 0, 8, mbuf);
delay(5);
// send 0x370 - name
CAN.sendMsgBuf(0x370, 0, 8, can_name);
delay(5);
// send 0x35e - mfg
CAN.sendMsgBuf(0x35E, 0, 8, can_mfg);
delay(5);
// build 0x35c - charge/discharge status
can_clear();
// fixme - set from override flags
mbuf[0] = 0xe0;
CAN.sendMsgBuf(0x35AC, 0, 8, mbuf);
#endif
// elithium
#if 0
// build 0x351 - charge specs
can_clear();
can_set_word(0, bat_chg_v/100U);
can_set_word(2, bat_chg_i/10U);
can_set_word(4, bat_dis_i/10U);
can_set_word(6, 400);
CAN.sendMsgBuf(0x351, 0, 8, mbuf);
delay(5);
// build 0x355 - bat state
can_clear();
can_set_word(0, bat_soc);
can_set_word(2, bat_soh);
CAN.sendMsgBuf(0x355, 0, 8, mbuf);
delay(5);
// build 0x356 - bat measurements
can_clear();
can_set_word(0, bat_v);
can_set_word(2, bat_i);
can_set_word(4, bat_t);
CAN.sendMsgBuf(0x356, 0, 8, mbuf);
delay(5);
// build 0x35a - fault/warning flags
can_clear();
CAN.sendMsgBuf(0x35A, 0, 8, mbuf);
#endif
// BYD DUMP
//https://powerforum.co.za/topic/4406-revovbydvictron/
// can0 305 [8] 00 00 00 00 00 00 00 00 -- inverter response
// can0 351 [8] 3A 02 64 00 64 00 A4 01
// can0 355 [6] 32 00 64 00 00 00
// can0 356 [6] B4 14 64 00 F0 00
// can0 35A [8] A8 82 82 02 A8 82 82 02
// can0 35E [8] 42 59 44 00 00 00 00 00
// can0 35F [8] 4C 69 01 02 00 00 00 00
#if 0
// build 0x351 - charge specs
// can0 351 [8] 3A 02 64 00 64 00 A4 01
// 570 100 100 420
can_clear();
can_set_word(0, bat_chg_v/100U);
can_set_word(2, bat_chg_i/10U);
can_set_word(4, bat_dis_i/10U);
can_set_word(6, bat_dis_v/100U);
CAN.sendMsgBuf(0x351, 0, 8, mbuf);
delay(5);
// build 0x355 - bat state
// can0 355 [6] 32 00 64 00 00 00
// 50 100 0
can_clear();
can_set_word(0, bat_soc);
can_set_word(2, bat_soh);
CAN.sendMsgBuf(0x355, 0, 6, mbuf);
delay(5);
// build 0x356 - bat measurements
// can0 356 [6] B4 14 64 00 F0 00
// 5300 100 240
can_clear();
can_set_word(0, bat_v);
can_set_word(2, bat_i);
can_set_word(4, bat_t);
CAN.sendMsgBuf(0x356, 0, 6, mbuf);
delay(5);
// build 0x35a - fault/warning flags
// can0 35A [8] A8 82 82 02 A8 82 82 02
// should be safe to send as zeros
can_clear();
CAN.sendMsgBuf(0x35A, 0, 8, mbuf);
delay(5);
// build 0x35e - mfg name
// can0 35E [8] 42 59 44 00 00 00 00 00
can_clear();
mbuf[0] = 0x42;
mbuf[1] = 0x59;
mbuf[2] = 0x44;
CAN.sendMsgBuf(0x35E, 0, 8, mbuf);
delay(5);
// build 0x35a - chemistry
// can0 35F [8] 4C 69 01 02 00 00 00 00
// c1 c2 1.2 0 0.0
// should be safe to send as zeros
can_clear();
mbuf[0] = 0x4c;
mbuf[1] = 0x69;
mbuf[2] = 0x01;
mbuf[3] = 0x02;
can_set_word(4, 360); // capacity?
CAN.sendMsgBuf(0x35F, 0, 8, mbuf);
#endif
// simpbms
#if 0
// build 0x351 - charge specs
can_clear();
can_set_word(0, bat_dis_i/10U);
can_set_word(2, 400);
can_set_word(4, bat_chg_i/10U);
can_set_word(6, bat_chg_v/100U);
CAN.sendMsgBuf(0x351, 0, 8, mbuf);
delay(5);
// build 0x355 - bat state
can_clear();
can_set_word(0, bat_soc);
can_set_word(2, bat_soh);
can_set_word(3, bat_soc * 100); // from vecan?
CAN.sendMsgBuf(0x355, 0, 8, mbuf);
delay(5);
// build 0x356 - bat measurements
can_clear();
can_set_word(0, bat_v);
can_set_word(2, bat_i);
can_set_word(4, bat_t);
CAN.sendMsgBuf(0x356, 0, 8, mbuf);
delay(5);
// build 0x35a - no idea - from vecan
can_clear();
CAN.sendMsgBuf(0x35A, 0, 8, mbuf);
delay(5);
// send 0x370 - name
can_set_word(0, 3300);
can_set_word(2, 3400);
can_set_word(4, bat_t + 273);
can_set_word(6, bat_t + 273);
CAN.sendMsgBuf(0x373, 0, 8, can_name);
#endif
#if 1
// can dump from plonkster@powerforum
// can1 351 [6] 14 02 50 02 C8 05
// 532 592 1480
can_clear();
can_set_word(0, bat_chg_v/100U);
can_set_word(2, bat_chg_i/10U);
can_set_word(4, bat_dis_i/10U);
can_send(0x351, 6, mbuf);
// can1 355 [4] 5F 00 63 00
// 95 99
delay(5);
can_clear();
can_set_word(0, bat_soc);
can_set_word(2, bat_soh);
can_send(0x355, 4, mbuf);
// can1 356 [6] 77 13 A9 FE 0B 01
// 4983 -343 267
delay(5);
can_clear();
can_set_word(0, bat_v);
can_set_word(2, bat_i);
can_set_word(4, bat_t);
can_send(0x356, 6, mbuf);
// can1 359 [7] 00 00 00 00 04 50 4E
delay(5);
can_clear();
mbuf[4] = 1;
mbuf[5] = 'P';
mbuf[6] = 'N';
can_send(0x359, 7, mbuf);
// can1 35C [2] C0 00
delay(5);
can_clear();
if(bat_chg_i) mbuf[0] |= 0x80;
if(bat_dis_i) mbuf[0] |= 0x40;
//mbuf[0] = 0xC0;
can_send(0x35C, 2, mbuf);
// can1 35E [8] 50 59 4C 4F 4E 20 20 20
delay(5);
can_clear();
mbuf[0] = 0x50;
mbuf[1] = 0x59;
mbuf[2] = 0x4C;
mbuf[3] = 0x4F;
mbuf[4] = 0x4E;
mbuf[5] = 0x20;
mbuf[6] = 0x20;
mbuf[7] = 0x20;
can_send(0x35E, 8, mbuf);
#endif
}