-
Notifications
You must be signed in to change notification settings - Fork 49
/
Tesla_charger_Tom_v2_test.ino
470 lines (367 loc) · 13.7 KB
/
Tesla_charger_Tom_v2_test.ino
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Tesla Gen 2 Charger Phase 1 driver experimental code v1
2017
D.Maguire
Tweaks by T de Bree
Runs on OpenSource Logic board V1 in Gen2 charger. Commands all modules.
"s" starts or stops charging
"v" sets voltage setpoint
"c" sets charge current. WARNING! this current will be pumped out by all modules equally. So if you set 5Amps you will get 5 amps from all modules (if they have mains) for a total of 15A into the battery.
"r" sets ramp time in milliseconds. r500 sets 500ms ramp time.
*/
#include <due_can.h>
#include <due_wire.h>
#include <DueTimer.h>
#include <Wire_EEPROM.h>
#define Serial SerialUSB
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
// Useful macros for setting and resetting bits
//#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
//#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
//*********GENERAL VARIABLE DATA ******************
int debug = 0; // 1 = show canbus feedback
uint16_t voltset = 0;
uint16_t curset = 0;
uint16_t curreq = 0;
uint16_t currampt = 500; //500ms ramptime as default
signed long curramp = 0;
int setting = 1;
int incomingByte = 0;
int state,enab =0;
unsigned long tlast =0;
//*********Feedback from charge VARIABLE DATA ******************
uint16_t dcvolt = 0;
uint16_t dccur = 0;
uint16_t acvolt = 0;
uint16_t accur = 0;
int newframe=0;
CAN_FRAME outframe; //A structured variable according to due_can library for transmitting CAN data.
CAN_FRAME incoming; //structure to keep inbound frames
//setup bytes to contain CAN data
//bytes for 0x045c/////////////////////
byte test52; //0x45c byte 2 test 2
byte test53; //0x45c byte 3 test 3
/////////////////////////////////////////
//bytes for 0x042c/////////////////////
byte test20; //0x42c byte 0 test 4
byte test21; //0x42c byte 1 test 5
byte test24; //0x42c byte 4 test 8
/////////////////////////////////////////
void setup()
{
Serial.begin(9600); //Initialize our USB port which will always be redefined as SerialUSB to use the Native USB port tied directly to the SAM3X processor.
Timer3.attachInterrupt(Charger_msgs).start(100000); // charger messages every 100ms
// Initialize CAN ports
pinMode(48,OUTPUT);
if (Can1.begin(500000,48)) //can1 external bus
{
Serial.println("Using CAN1 - initialization completed.\n");
}
else Serial.println("CAN1 initialization (sync) ERROR\n");
// Initialize CAN0
pinMode(50,OUTPUT);
if (Can0.begin(500000,50)) //can0 charger modules
{
Serial.println("Using CAN0 - initialization completed.\n");
}
else Serial.println("CAN0 initialization (sync) ERROR\n");
int filter;
//extended
for (filter = 0; filter < 3; filter++) {
Can0.setRXFilter(filter, 0, 0, true);
Can1.setRXFilter(filter, 0, 0, true);
}
//standard
for (int filter = 3; filter < 7; filter++) {
Can0.setRXFilter(filter, 0, 0, false);
Can1.setRXFilter(filter, 0, 0, false);
}
///////////////////CHARGER ENABLE AND ACTIVATE LINES///////////////////////////////////
pinMode(LED_BUILTIN, OUTPUT);
pinMode(3, OUTPUT); //CHG1 ENABLE
pinMode(4, OUTPUT); //CHG2 ENABLE
pinMode(5, OUTPUT); //CHG3 ENABLE
pinMode(62, OUTPUT); //CHG1 ACTIVATE
pinMode(63, OUTPUT); //CHG2 ACTIVATE
pinMode(64, OUTPUT); //CHG3 ACTIVATE
//////////////////////////////////////////////////////////////////////////////////////
//////////////DIGITAL OUTPUTS MAPPED TO X046. 10 PIN CONNECTOR ON LEFT//////////////////////////////////////////
pinMode(48, OUTPUT); //OP1 - X046 PIN 6
pinMode(49, OUTPUT); //OP2
pinMode(50, OUTPUT); //OP2
pinMode(51, OUTPUT); //OP3
///////////////////////////////////////////////////////////////////////////////////////
delay(1000); // wait for a second
digitalWrite(3, HIGH);//enable phase 1 power module
delay(1000); // wait for a second
digitalWrite(4, HIGH);//enable phase 2 power module
delay(1000); // wait for a second
digitalWrite(5, HIGH);//enable phase 3 power module
/////////Setup initial state of 2 variable CAN messages///////////////////////////
test52=0x15;
test53=0x0e;
test20=0x42;
test21=0x60;
test24=0x64;
///////////////////////////////////////////////////////////////////////////////////
}
void loop() {
// put your main code here, to run repeatedly:
//You need to decode in order to read the can bus.
if (Can0.available())
{
Can0.read(incoming);
candecode(incoming);
}
if (Serial.available() > 0)
{
incomingByte = Serial.read(); // read the incoming byte:
switch (incomingByte)
{
case 118://v for voltage setting in whole numbers
if (Serial.available() > 0)
{
voltset = (Serial.parseInt()*100);
setting = 1;
}
break;
case 116://t for current ramp time
if (Serial.available() > 0)
{
currampt = Serial.parseInt();
setting = 1;
}
break;
case 115://s for start AND stop
if (Serial.available() > 0)
{
state = !state;
setting = 1;
}
break;
case 101://e for enabling chargers followed by numbers to indicate which ones to run
if (Serial.available() > 0)
{
enab = Serial.parseInt();
setting = 1;
}
break;
case 99: //c for current setting in whole numbers
if (Serial.available() > 0)
{
curset = (Serial.parseInt()*1500);
setting = 1;
}
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
}
if (setting == 1) //display if any setting changed
{
Serial.println();
if (state == 1)
{
Serial.print("Charger On ");
}
else
{
Serial.print("Charger Off ");
}
Serial.print("Enabled Phases : ");
Serial.print(enab);
Serial.print("Set voltage : ");
Serial.print(voltset*0.01,0);
Serial.print("V | Set current : ");
Serial.print(curset*0.00066666,0);
Serial.print(" A ");
Serial.print(" ms | Set ramptime : ");
Serial.print(currampt);
Serial.print(" Ramp current : ");
curramp = (curset-curreq)/500;
Serial.print(curramp);
setting = 0;
}
switch (state)
{
case 0: //Charger off
test52=0x15;
test53=0x0e;
test21=0x60;
test24=0x64;
digitalWrite(48, LOW);//MAINS OFF
delay(10);
digitalWrite(62, LOW); //chargeph1 deactivate
digitalWrite(63, LOW); //chargeph2 deactivate
digitalWrite(64, LOW); //chargeph3 deactivate
break;
case 1://Charger on
switch(enab)
{
case 1:
digitalWrite(62, HIGH);
break;
case 2:
digitalWrite(63, HIGH);
break;
case 3:
digitalWrite(64, HIGH);
break;
case 12:
digitalWrite(62, HIGH);
digitalWrite(63, HIGH);
break;
case 13:
digitalWrite(62, HIGH);
digitalWrite(64, HIGH);
break;
case 123:
digitalWrite(62, HIGH);
digitalWrite(63, HIGH);
digitalWrite(64, HIGH);
break;
case 23:
digitalWrite(63, HIGH);
digitalWrite(64, HIGH);
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
delay(10);
test52=0x14;
test53=0x2E;
test21=0xc4;
test24=0xfe;
digitalWrite(48, HIGH);//MAINS ON
break;
default:
// if nothing else matches, do the default
break;
}
if (curreq != curset)
{
if ((millis()- tlast) > 1)
{
tlast = millis();
curreq = curreq + curramp;
}
}
if (debug != 0)
{
if (newframe & 3 != 0)
{
Serial.println();
Serial.print(millis());
Serial.print(" Charger Feebback // AC voltage : ");
Serial.print(acvolt);
Serial.print(" AC current : ");
Serial.print(accur/28);
Serial.print(" DC voltage : ");
Serial.print(dcvolt/100,2);
Serial.print(" DC current : ");
Serial.print(dccur/1000,2);
}
}
}
void candecode(CAN_FRAME &frame)
{
switch(frame.id)
{
case 0x207:
acvolt = frame.data.bytes[1];
accur = ((frame.data.bytes[8]&=3)*256+frame.data.bytes[7]);
newframe = newframe | 1;
break;
case 0x227: //dc feedback
//dccur = frame.data.bytes[7]*256+frame.data.bytes[6];
dccur = (frame.data.bytes[5]+frame.data.bytes[4])*0.000839233;
dcvolt = frame.data.bytes[3]*256+frame.data.bytes[2];
newframe = newframe | 2;
break;
default:
// if nothing else matches, do the default
break;
}
}
void Charger_msgs()
{
/////////////////////This msg addresses all modules/////////////////////////////////////////////////
outframe.id = 0x045c; // Set our transmission address ID
outframe.length = 8; // Data payload 8 bytes
outframe.extended = 0; // Extended addresses - 0=11-bit 1=29bit
outframe.rtr=1; //No request
outframe.data.bytes[0]=lowByte(voltset); //Voltage setpoint
outframe.data.bytes[1]=highByte(voltset);//Voltage setpoint
outframe.data.bytes[2]=test52;
outframe.data.bytes[3]=test53;
outframe.data.bytes[4]=0x00;
outframe.data.bytes[5]=0x00;
outframe.data.bytes[6]=0x90;
outframe.data.bytes[7]=0x8c;
Can0.sendFrame(outframe);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////Phase 1 command message////////////////////////////////////////
outframe.id = 0x042c; // Set our transmission address ID
outframe.length = 8; // Data payload 8 bytes
outframe.extended = 0; // Extended addresses - 0=11-bit 1=29bit
outframe.rtr=1; //No request
outframe.data.bytes[0]=test20;
outframe.data.bytes[1]=test21;
outframe.data.bytes[2]=lowByte(curreq); //Current setpoint
outframe.data.bytes[3]=highByte(curreq); //Current setpoint
outframe.data.bytes[4]=test24;
outframe.data.bytes[5]=0x00;
outframe.data.bytes[6]=0x00;
outframe.data.bytes[7]=0x00;
Can0.sendFrame(outframe);
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////Phase 2 command message//////////////////////////////////////////////
outframe.id = 0x043c; // Set our transmission address ID
outframe.length = 8; // Data payload 8 bytes
outframe.extended = 0; // Extended addresses - 0=11-bit 1=29bit
outframe.rtr=1; //No request
outframe.data.bytes[0]=test20;
outframe.data.bytes[1]=test21;
outframe.data.bytes[2]=lowByte(curreq); //Current setpoint
outframe.data.bytes[3]=highByte(curreq); //Current setpoint
outframe.data.bytes[4]=test24;
outframe.data.bytes[5]=0x00;
outframe.data.bytes[6]=0x00;
outframe.data.bytes[7]=0x00;
Can0.sendFrame(outframe);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////Phase 3 command message/////////////////////////////////////////////
outframe.id = 0x044c; // Set our transmission address ID
outframe.length = 8; // Data payload 8 bytes
outframe.extended = 0; // Extended addresses - 0=11-bit 1=29bit
outframe.rtr=1; //No request
outframe.data.bytes[0]=test20;
outframe.data.bytes[1]=test21;
outframe.data.bytes[2]=lowByte(curreq); //Current setpoint
outframe.data.bytes[3]=highByte(curreq); //Current setpoint
outframe.data.bytes[4]=test24;
outframe.data.bytes[5]=0x00;
outframe.data.bytes[6]=0x00;
outframe.data.bytes[7]=0x00;
Can0.sendFrame(outframe);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////Static Frame every 100ms///////////////////////////////////////////////////////////////////
outframe.id = 0x368; // Set our transmission address ID
outframe.length = 8; // Data payload 8 bytes
outframe.extended = 0; // Extended addresses - 0=11-bit 1=29bit
outframe.rtr=1; //No request
outframe.data.bytes[0]=0x03;
outframe.data.bytes[1]=0x49;
outframe.data.bytes[2]=0x29;
outframe.data.bytes[3]=0x11;
outframe.data.bytes[4]=0x00;
outframe.data.bytes[5]=0x0c;
outframe.data.bytes[6]=0x40;
outframe.data.bytes[7]=0xff;
Can0.sendFrame(outframe);
////////////////////////////////////////////////////////////////////////////////////////////////////////
}