-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathTesla_charger_Tom_v1.ino
319 lines (245 loc) · 7.76 KB
/
Tesla_charger_Tom_v1.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
/*
Tesla Gen 2 Charger Phase 1 driver experimental code v1
2017
D.Maguire
Tweaks by T de Bree
*/
#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 = 1; // 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 =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))
{
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))
{
Serial.println("Using CAN0 - initialization completed.\n");
}
else Serial.println("CAN0 initialization (sync) ERROR\n");
/////////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:
if (Can1.available() > 0)
{
Can1.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 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("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;
break;
case 1://Charger on
test52=0x14;
test53=0x2E;
test21=0xc4;
test24=0xfe;
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];
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()
{
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;
Can1.sendFrame(outframe);
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;
Can1.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;
Can1.sendFrame(outframe);
////////////////////////////////////////////////////////////////////////////////////////////////////////
}