forked from funk222/Cruise_ECU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_smarter_cruise_ECU.ino
320 lines (284 loc) · 6.64 KB
/
a_smarter_cruise_ECU.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
#include <CAN.h>
//______________BUTTONS AND SWITCHES
int button4 = 8;
int button3 = 7;
int button2 = 6;
int button1 = 9;
int pedal = A4;
boolean pedalstate = false;
int buttonstate4;
int lastbuttonstate4;
int buttonstate3;
int lastbuttonstate3;
int buttonstate2;
int lastbuttonstate2;
int buttonstate1;
int lastbuttonstate1;
//______________VALUES SEND ON CAN
boolean OP_ON = false;
uint8_t set_speed = 0x0;
int gas_pedal_state = 0; // TODO: Remove gas_pedal_state
int brake_pedal_state = 0; // TODO: Remove brake_pedal_state
double average = 0;
boolean blinker_left = true;
boolean blinker_right = true;
//______________FOR SMOOTHING SPD
const int numReadings = 160;
float readings[numReadings];
int readIndex = 0;
double total = 0;
//______________FOR READING VSS SENSOR
const byte interruptPin = 3;
int inc = 0;
int half_revolutions = 0;
int spd;
unsigned long lastmillis;
unsigned long duration;
uint8_t encoder = 0;
void setup() {
CAN.begin(500E3);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), rpm, FALLING);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
//______________initialize smoothing inputs
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
//______________READ SPD SENSOR
attachInterrupt(1,rpm, FALLING);
if (half_revolutions >= 1) {
detachInterrupt(1);
duration = (micros() - lastmillis);
spd = half_revolutions * (0.000135 / (duration * 0.000001)) * 3600;
lastmillis = micros();
half_revolutions = 0;
attachInterrupt(1, rpm, FALLING);
}
//______________SMOOTH SPD TO AVERAGE
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = spd;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
//______________READING BUTTONS AND SWITCHES
pedalstate = digitalRead(pedal);
buttonstate4 = digitalRead(button4);
buttonstate3 = digitalRead(button3);
buttonstate2 = digitalRead(button2);
buttonstate1 = digitalRead(button1);
if (buttonstate4 != lastbuttonstate4)
{
if (buttonstate4 == LOW)
{
if (OP_ON == true)
{
OP_ON = false;
}
else if(OP_ON == false)
{
OP_ON = true;
set_speed = (average += 3);
}
}
}
if (buttonstate3 == LOW)
{
blinker_right = false;
}
else
{
blinker_right = true;
}
if (buttonstate2 == LOW)
{
blinker_left = false;
}
else
{
blinker_left = true;
}
if (buttonstate1 != lastbuttonstate1)
{
if (buttonstate1 == LOW)
{
set_speed += 5;
}
}
if (pedalstate == LOW)
{
}
lastbuttonstate1 = buttonstate1;
lastbuttonstate2 = buttonstate2;
lastbuttonstate3 = buttonstate3;
lastbuttonstate4 = buttonstate4;
//______________SENDING_CAN_MESSAGES
//0x1d2 msg PCM_CRUISE
uint8_t dat[8];
dat[0] = (OP_ON << 5) & 0x20 | (!gas_pedal_state << 4) & 0x10;
dat[1] = 0x0;
dat[2] = 0x0;
dat[3] = 0x0;
dat[4] = 0x0;
dat[5] = 0x0;
dat[6] = (OP_ON << 7) & 0x80;
dat[7] = can_cksum(dat, 7, 0x1d2);
CAN.beginPacket(0x1d2);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat[ii]);
}
CAN.endPacket();
//0x1d3 msg PCM_CRUISE_2
uint8_t dat2[8];
dat2[0] = 0x0;
dat2[1] = (OP_ON << 7) & 0x80 | 0x28;
dat2[2] = set_speed;
dat2[3] = 0x0;
dat2[4] = 0x0;
dat2[5] = 0x0;
dat2[6] = 0x0;
dat2[7] = can_cksum(dat2, 7, 0x1d3);
CAN.beginPacket(0x1d3);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat2[ii]);
}
CAN.endPacket();
//0xaa msg defaults 1a 6f WHEEL_SPEEDS
uint8_t dat3[8];
uint16_t wheelspeed = 0x1a6f + (average * 100);
dat3[0] = (wheelspeed >> 8) & 0xFF;
dat3[1] = (wheelspeed >> 0) & 0xFF;
dat3[2] = (wheelspeed >> 8) & 0xFF;
dat3[3] = (wheelspeed >> 0) & 0xFF;
dat3[4] = (wheelspeed >> 8) & 0xFF;
dat3[5] = (wheelspeed >> 0) & 0xFF;
dat3[6] = (wheelspeed >> 8) & 0xFF;
dat3[7] = (wheelspeed >> 0) & 0xFF;
CAN.beginPacket(0xaa);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat3[ii]);
}
CAN.endPacket();
//0x3b7 msg ESP_CONTROL
uint8_t dat5[8];
dat5[0] = 0x0;
dat5[1] = 0x0;
dat5[2] = 0x0;
dat5[3] = 0x0;
dat5[4] = 0x0;
dat5[5] = 0x0;
dat5[6] = 0x0;
dat5[7] = 0x08;
CAN.beginPacket(0x3b7);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat5[ii]);
}
CAN.endPacket();
//0x620 msg STEATS_DOORS
uint8_t dat6[8];
dat6[0] = 0x10;
dat6[1] = 0x0;
dat6[2] = 0x0;
dat6[3] = 0x1d;
dat6[4] = 0xb0;
dat6[5] = 0x40;
dat6[6] = 0x0;
dat6[7] = 0x0;
CAN.beginPacket(0x620);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat6[ii]);
}
CAN.endPacket();
// 0x3bc msg GEAR_PACKET
uint8_t dat7[8];
dat7[0] = 0x0;
dat7[1] = 0x0;
dat7[2] = 0x0;
dat7[3] = 0x0;
dat7[4] = 0x0;
dat7[5] = 0x80;
dat7[6] = 0x0;
dat7[7] = 0x0;
CAN.beginPacket(0x3bc);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat7[ii]);
}
CAN.endPacket();
// 0x2c1 msg GAS_PEDAL
uint8_t dat10[8];
dat10[0] = (!gas_pedal_state << 3) & 0x08;
dat10[1] = 0x0;
dat10[2] = 0x0;
dat10[3] = 0x0;
dat10[4] = 0x0;
dat10[5] = 0x0;
dat10[6] = 0x0;
dat10[7] = 0x0;
CAN.beginPacket(0x2c1);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat10[ii]);
}
CAN.endPacket();
//0x224 msg fake brake module
uint8_t dat11[8];
dat11[0] = 0x0;
dat11[1] = 0x0;
dat11[2] = 0x0;
dat11[3] = 0x0;
dat11[4] = 0x0;
dat11[5] = 0x0;
dat11[6] = 0x0;
dat11[7] = 0x8;
CAN.beginPacket(0x224);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat11[ii]);
}
CAN.endPacket();
//0x614 msg steering_levers
uint8_t dat614[8];
dat614[0] = 0x29;
dat614[1] = 0x0;
dat614[2] = 0x01;
dat614[3] = (blinker_left << 5) & 0x20 |(blinker_right << 4) & 0x10;
dat614[4] = 0x0;
dat614[5] = 0x0;
dat614[6] = 0x76;
dat614[7] = can_cksum(dat614, 7, 0x614);
CAN.beginPacket(0x614);
for (int ii = 0; ii < 8; ii++) {
CAN.write(dat614[ii]);
}
CAN.endPacket();
}
void rpm() {
half_revolutions++;
if (encoder > 255)
{
encoder = 0;
}
encoder++;
}
//TOYOTA CAN CHECKSUM
int can_cksum (uint8_t *dat, uint8_t len, uint16_t addr) {
uint8_t checksum = 0;
checksum = ((addr & 0xFF00) >> 8) + (addr & 0x00FF) + len + 1;
//uint16_t temp_msg = msg;
for (int ii = 0; ii < len; ii++) {
checksum += (dat[ii]);
}
return checksum;
}