-
Notifications
You must be signed in to change notification settings - Fork 1
/
stateprocessing.c
393 lines (326 loc) · 9.09 KB
/
stateprocessing.c
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
#include "hal.h"
#include "inputs.h"
#include "actuators.h"
#include "ticker.h"
#include "candriver.h" //TODO check if this works
#include "canprocessing.h"
#include "lcddrv.h"
#include <stdio.h>
// The ID of each door is set following the design document
// as a numeric constant
#define DOOR_ID 1 // Front Left
//#define DOOR_ID 2 // Front Right
//#define DOOR_ID 3 // Rear Right
//#define DOOR_ID 4 // Rear Left
//#define DOOR_ID 5 // Trunk
// A numeric value is assigned to each state
#define THEFT_AVOIDANCE_ST 1
#define LOCKED_ST 2
#define UNLOCKED_ST 3
#define OPEN_ST 4
#define CLOSED_ST 5
// Define ON as 1 and OFF as 0b
#define ON 1
#define OFF 0
// Define servomotor positions
#define THEFT_SERVO 90
#define LOCKED_SERVO 0
#define UNLOCKED_SERVO -90
// Door IDs
#define FRONT_LEFT 1 // Front Left
#define FRONT_RIGHT 2 // Front Right
#define REAR_RIGHT 3 // Rear Right
#define REAR_LEFT 4 // Rear Left
#define TRUNK 5 // Trunk
// A numeric value is assigned to each CAN message for
// easier processing
#define CRASH_ON_CAN 1
#define CRASH_OFF_CAN 2
#define SPEED_ON_CAN 3
#define SPEED_OFF_CAN 4
#define LIGHTS_ON_CAN 5
#define LIGHTS_OFF_CAN 6
#define CHILD_LOCK_ON_CAN 7
#define CHILD_LOCK_OFF_CAN 8
#define CENTRAL_LOCK_CAN 9
#define CENTRAL_UNLOCK_CAN 10
#define DOOR_EXT_OPEN_CAN 11
#define DOOR_INT_OPEN_CAN 12
#define DOOR_LOCK_UNLOCK_CAN 13
#define LOCK_ALL_CAN 14
#define UNLOCK_ALL_CAN 15
#define UNLOCKED_CAN 16
#define LOCKED_CAN 17
#define KEEP_ALIVE_CAN 18
#define THEFT_AVOIDANCE_ON_CAN 19
#define THEFT_AVOIDANCE_OFF_CAN 20
// A numeric value is assigned to each input possibility
#define CRASH_ON_IN 1
#define CRASH_OFF_IN 2
#define SPEED_ON_IN 3
#define SPEED_OFF_IN 4
#define LIGHTS_ON_IN 5
#define LIGHTS_OFF_IN 6
#define CHILD_LOCK_ON_IN 7
#define CHILD_LOCK_OFF_IN 8
#define CENTRAL_LOCK_IN 9
#define CENTRAL_UNLOCK_IN 10
#define DOOR_EXT_OPEN_IN 11
#define DOOR_INT_OPEN_IN 12
//#define DOOR_LOCK_UNLOCK_IN 13
#define LOCK_ALL_IN 14
#define UNLOCK_ALL_IN 15
#define FRONT_RIGHT_UNLOCKED_IN 16
#define FRONT_RIGHT_LOCKED_IN 17
#define REAR_LEFT_UNLOCKED_IN 18
#define REAR_LEFT_LOCKED_IN 19
#define REAR_RIGHT_UNLOCKED_IN 20
#define REAR_RIGHT_LOCKED_IN 21
#define TRUNK_UNLOCKED_IN 22
#define TRUNK_LOCKED_IN 23
#define THEFT_AVOIDANCE_ON_IN 24
#define THEFT_AVOIDANCE_OFF_IN 25
#define DOOR_CLOSED_IN 26
#define KEEP_ALIVE_IN 27
#define DOOR_LOCK_UNLOCK_ON_IN 28
#define DOOR_LOCK_UNLOCK_OFF_IN 29
// Declaration of global variables
int Current_State = UNLOCKED_ST; // Initialized as unlocked
// DEclaration of flags
int Crash = 0;
int Speed = 0;
int Child_Lock = 0;
int Lights = 0;
int Doors_Opened = 0;
// Char for input debugging
unsigned char var[17];
//Here we define the Servo positions, they're not going to be used to the door states.
// 0 = Unlocked
// 1 = Locked
// 2 = Theft Avoidance
int Servo_Position = 0;
// Defining door States where 0 is unlocked and 1 is locked and 2 Theft locked
int Door_States[5] = {0, 0, 0, 0, 0}; //the 5th door(TRUNK) will only have 0 or 1
int Open_Doors[5]= {0,0,0,0,0}; //If there´s a 0, the door is open, if 1, the door is closed
int cont=0; //This counter will let us close the doors, when reached a limit.
//////////////////// State Changes/Processing //////////////////////
// PROCESS INPUT
// Purpose: Reacts to any input (which are sent to an initial variable int input)
// and depending on each case, it switches to the correct state, using the Crash,
// Speed, Child_Lock, Lights, and DOOR_ID flags/identifiers for further validation
// Initial values: int input
// Return values: none
void processInput(int input){
ledToggle(6);
switch (input) {
case CRASH_ON_IN:
Crash = 1;
Current_State = UNLOCKED_ST;
if (DOOR_ID == 1)
writeCrash(DOOR_ID, ON); //activate the CRASH in order to unlock the doors
break;
case CRASH_OFF_IN:
Crash = 0;
Current_State = UNLOCKED_ST;
if (DOOR_ID == 1)
writeCrash(DOOR_ID, OFF);
break;
case SPEED_ON_IN:
ledToggle(5);
Speed = 1;
writeSpeed(DOOR_ID, ON); //Indicator that speed has passed 10kmh
break;
case SPEED_OFF_IN:
Speed = 0;
writeSpeed(DOOR_ID, OFF);
break;
case LIGHTS_ON_IN:
Lights = 1;
break;
case LIGHTS_OFF_IN:
Lights = 0;
break;
case CHILD_LOCK_ON_IN:
Child_Lock = 1;
if (DOOR_ID == 1)
writeChildLock(DOOR_ID, ON);
break;
case CHILD_LOCK_OFF_IN:
Child_Lock = 0;
if (DOOR_ID == 1)
writeChildLock(DOOR_ID, OFF);
break;
case CENTRAL_LOCK_IN:
Current_State = THEFT_AVOIDANCE_ST;
Door_States[0]=2;
if (DOOR_ID == 1)
writeTheftAvoidance(DOOR_ID, ON);
break;
case CENTRAL_UNLOCK_IN:
Current_State = UNLOCKED_ST;
if (DOOR_ID == 1)
writeUnlockAll(ON);
break;
case DOOR_EXT_OPEN_IN:
if (Current_State != LOCKED_ST && Current_State != THEFT_AVOIDANCE_ST)
Current_State = OPEN_ST;
break;
case DOOR_INT_OPEN_IN:
if (Current_State != LOCKED_ST && Current_State != THEFT_AVOIDANCE_ST
&& ((DOOR_ID != 3 && DOOR_ID != 4) && Child_Lock == 0) ) Current_State = OPEN_ST;
break;
case DOOR_LOCK_UNLOCK_ON_IN:
if(Current_State != THEFT_AVOIDANCE_ST)
Current_State=UNLOCKED_ST;
break;
case DOOR_LOCK_UNLOCK_OFF_IN:
if(Current_State != THEFT_AVOIDANCE_ST)
Current_State=LOCKED_ST;
break;
case LOCK_ALL_IN:
if (Current_State == UNLOCKED_ST){
Current_State = LOCKED_ST;
writeLockAll(1);
}
break;
case UNLOCK_ALL_IN:
if (Current_State == LOCKED_ST && Current_State != THEFT_AVOIDANCE_ST) {
Current_State = UNLOCKED_ST;
writeUnlockAll(1);
}
else if( Current_State == THEFT_AVOIDANCE_ST)
Current_State= THEFT_AVOIDANCE_ST;
break;
case DOOR_CLOSED_IN:
if(Current_State != CLOSED_ST)
Current_State = CLOSED_ST;
Open_Doors[0]=0;
break;
case FRONT_RIGHT_UNLOCKED_IN:
// Front Left is unlocked
Door_States[1] = 0;
break;
case FRONT_RIGHT_LOCKED_IN:
// Front Left is locked
Door_States[1] = 1;
break;
case REAR_LEFT_UNLOCKED_IN:
// Rear Left is unlocked
Door_States[2] = 0;
break;
case REAR_LEFT_LOCKED_IN:
// Rear Left is locked
Door_States[2] = 1;
break;
case REAR_RIGHT_UNLOCKED_IN:
// Rear Right is unlocked
Door_States[3] = 0;
break;
case REAR_RIGHT_LOCKED_IN:
// Rear Right is locked
Door_States[3] = 1;
break;
case TRUNK_UNLOCKED_IN:
// Trunk is unlocked
Door_States[4] = 0;
break;
case TRUNK_LOCKED_IN:
// Trunk is locked
Door_States[4] = 1;
break;
default:
// No change
Current_State = Current_State;
break;
}
}
void processState(){
int door_id;
switch (Current_State){
case THEFT_AVOIDANCE_ST:
lockPosition(THEFT_SERVO);
break;
case LOCKED_ST:
lockPosition(LOCKED_SERVO);
// If door is not FL door, it must inform the main door that it is
// unlocked
break;
case UNLOCKED_ST:
lockPosition(UNLOCKED_SERVO);
// If door is not FL door, it must inform the main door that it is
// locked
if(Speed==1 ){ //if we're going faster than 10km/h and the current state is unlocked(NOT OPEN), then lock doors
if(cont > 200){ //Counter added instead of a delay
Door_States[0]=1;
Current_State= LOCKED_ST;
cont=0;
}
cont++;
}
else
Door_States[0] = 0;
break;
case OPEN_ST:
Open_Doors[0] = 1;
break;
case CLOSED_ST:
Open_Doors[0] = 0;
Doors_Opened=0;
beeperOff();
Current_State = UNLOCKED_ST;
break;
}
// Turns ON/OFF Leds depending on which doors are Open/Closed
for (door_id = 0; door_id <= 4; door_id++){
// If Door i is opened, the led must be turned ON
if (Open_Doors[door_id] == 1)
ledOn(door_id);
// If Door i is closed, the led must be turned OFF
else
ledOff(door_id);
}
//PROCESS FOR THE BEEPER
if (Lights == 1){ //podria hacerse un and the puerta izq abierta y lights 1
// If the Front Left door is opened (Driver's door) and the lights on, then beeper state must be ON
if (Open_Doors[0] == 1) {
beeperOn(90000);// TODO: checar este pedo, necesita una frecuencia adentro, creo.
}
// If the Front left door is locked, the beeper state can be activated if any door is open and speed>10
}
else if(Speed==1) {
for(door_id = 0; door_id <= 4; door_id++){
if(Open_Doors[door_id]==1)
Doors_Opened = 1;
}
if (Doors_Opened == 1) //call function returns 1 if a door is open
beeperOn(90000);
else
beeperOff();// TODO: checar este pedo del beeper
}else
//Doors_Opened=0;
beeperOff();
}
void refreshDisplay(){
char state[17];
switch(Current_State){
case THEFT_AVOIDANCE_ST:
writeLine("State: Theft ", 0);
break;
case LOCKED_ST:
writeLine("State: Locked ", 0);
break;
case UNLOCKED_ST:
writeLine("State: Unlocked", 0);
break;
case OPEN_ST:
writeLine("State: Open ", 0);
break;
case CLOSED_ST:
writeLine("State: Closed ", 0);
break;
default:
break;
}
sprintf(var, "Sp:%i CL:%i L:%i", Speed, Child_Lock, Lights);
writeLine(var, 1);
}