-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpike_TrafficController.c
More file actions
520 lines (463 loc) · 17.7 KB
/
Spike_TrafficController.c
File metadata and controls
520 lines (463 loc) · 17.7 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <stdio.h>
#include <stdlib.h>
/*
* In this code to simulate time, we assume that one iteration of any while loop is 1ms
* Therefore, 1s = 1000 ms = 1000 iterations.
* If nobody is present at the signals, this code would give priority to side 1
*/
//Function prototypes
void turnOn(const int);
void turnOff(const int);
int getValue(const int);
void turnOnYellow(int *, const int, const int, int *);
void turnOffLight(int *, const int, const int, int *);
void turnOffPriorityLight(const int, const int, const int);
void ONLimit(const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int);
void perform(const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, int *, int *);
void printRegister(void);
void print(int);
/*
* Turns on the LED at port "port"
* @param port : The port of the LED light to turn on (from x30)
*/
void turnOn(const int port)
{
int pin_mask = 1 << port;
asm volatile(
"or x30, x30, %0\n\t"
:
: "r"(pin_mask)
: "x30"
);
}
/*
* Turns off the LED at port "port"
* @param port : The port of the LED light to turn off (from x30)
*/
void turnOff(const int port)
{
int pin_mask = ~(1 << port);
asm volatile(
"and x30, x30, %0\n\t"
:
: "r"(pin_mask)
: "x30"
);
}
/*
* Returns the value of the sensor at port "port"
* @param port : The port of the sensor (from x30)
* @return : The value reported by the sensor
*/
int getValue(const int port)
{
int result = 0;
asm volatile(
"addi %0, x30, 0x0\n\t"
:"=r"(result)
:
:"x30"
);
return (result >> port) & 1;
}
/*
* Turns off the green light and turns on the yellow light
* @param green : pointer to the number that says that green is on/off
* @param greenLED : port of the green LED
* @param yellowLED : port of the yellow LED
* @param yellow : pointer to the number that says that yellow is on/off
*/
void turnOnYellow(int * green, const int greenLED, const int yellowLED, int * yellow)
{
*green = 0;
turnOff(greenLED);
turnOn(yellowLED);
*yellow = 1;
}
/*
* Turns off the given light and turns on the next light
* @param lightOff : pointer to the number that says that light is on/off
* @param lightOffLED : port of the LED that is to be turned off
* @param lightOnLED : port of the LED that is to be turned on
* @param lightOn : pointer to the number that says that light is on/off
*/
void turnOffLight(int * lightOff, const int lightOffLED, const int lightOnLED, int * lightOn)
{
*lightOff = 0;
turnOff(lightOffLED);
turnOn(lightOnLED);
*lightOn = 1;
}
/*
* Turns off the priority light that is used when there is no traffic.
* @param red: the port of the red light
* @param yellow: the port of the yellow light
* @param green: the port of the green light
*/
void turnOffPriorityLight(const int red, const int yellow, const int green)
{
int counter = 0;
turnOff(green);
turnOn(yellow);
while(counter != 1000)
counter++;
turnOff(yellow);
turnOn(red);
}
/*
* First turns on right light and the straight light.
* @param sensor1Straight : Port of Straight Sensor 1
* @param sensor1Right : Port of Straight Sensor 1
* @param sensor2Straight : Port of Straight Sensor 2
* @param sensor2Right : Port of Straight Sensor 2
* @param red1Straight : Port of Straight red light 1
* @param red2Straight : Port of Straight red light 2
* @param red1Right : Port of Right red light 1
* @param red2Right : Port of Right red light 2
* @param yellow1Straight : Port of Straight yellow light 1
* @param yellow2Straight : Port of Straight yellow light 2
* @param yellow1Right : Port of Right yellow light 1
* @param yellow2Right : Port of Right yellow light 2
* @param green1Straight : Port of Straight green light 1
* @param green2Straight : Port of Straight green light 2
* @param green1Right : Port of Right green light 1
* @param green2Right : Port of Right green light 2
*/
void OnLimit(const int sensor1Straight, const int sensor1Right, const int sensor2Straight, const int sensor2Right, const int red1Straight, const int red2Straight, const int red1Right, const int red2Right, const int yellow1Straight, const int yellow2Straight, const int yellow1Right, const int yellow2Right, const int green1Straight, const int green2Straight, const int green1Right, const int green2Right)
{
const int RIGHT_TIME_LIMIT = 15;
const int STRAIGHT_TIME_LIMIT = 45;
int greenOn1 = 0;
int greenOn2 = 0;
perform(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right, RIGHT_TIME_LIMIT, 0, &greenOn1, &greenOn2);
perform(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right, STRAIGHT_TIME_LIMIT, 1, &greenOn1, &greenOn2);
}
/*
* Turns on the lights specified.
* @param sensor1Straight : Port of Straight Sensor 1
* @param sensor1Right : Port of Straight Sensor 1
* @param sensor2Straight : Port of Straight Sensor 2
* @param sensor2Right : Port of Straight Sensor 2
* @param red1Straight : Port of Straight red light 1
* @param red2Straight : Port of Straight red light 2
* @param red1Right : Port of Right red light 1
* @param red2Right : Port of Right red light 2
* @param yellow1Straight : Port of Straight yellow light 1
* @param yellow2Straight : Port of Straight yellow light 2
* @param yellow1Right : Port of Right yellow light 1
* @param yellow2Right : Port of Right yellow light 2
* @param green1Straight : Port of Straight green light 1
* @param green2Straight : Port of Straight green light 2
* @param green1Right : Port of Right green light 1
* @param green2Right : Port of Right green light 2
* @param Limit : The time limit
* @param goStraight : If you want to go straight = 1; otherwise = 0
* @param greenOn1 : If you want to go straight then greenOn would store if the green signal 1 is already on
* @param greenOn2 : If you want to go straight then greenOn would store if the green signal 2 is already on
*/
void perform(const int sensor1Straight, const int sensor1Right, const int sensor2Straight, const int sensor2Right, const int red1Straight, const int red2Straight, const int red1Right, const int red2Right, const int yellow1Straight, const int yellow2Straight, const int yellow1Right, const int yellow2Right, const int green1Straight, const int green2Straight, const int green1Right, const int green2Right, const int LIMIT, const int goStraight, int * greenOn1, int * greenOn2)
{
int counter = 0;
//For turning on green signals for going straight if this function is for going right
int done1 = 0, done2 = 0;
//If red light is on then value is 1; otherwise it is 0
int red1 = 1, red2 = 1;
//If green light is on then value is 1; otherwise it is 0
int green1 = 0, green2 = 0;
//If yellow light is on then value is 1; otherwise it is 0
int yellow1 = 0, yellow2 = 0;
//We need to store the time when yellow light was turned on
int yellowCounter1 = 0, yellowCounter2 = 0;
if(goStraight && greenOn1)
{
red1 = 0;
green1 = 1;
}
if(goStraight && greenOn2)
{
red2 = 0;
green2 = 1;
}
if(goStraight == 1 && getValue(sensor1Straight))
turnOffLight(&red1, red1Straight, green1Straight, &green1);
else if(goStraight == 0 && getValue(sensor1Right))
turnOffLight(&red1, red1Right, green1Right, &green1);
if(goStraight == 1 && getValue(sensor2Straight))
turnOffLight(&red2, red2Straight, green2Straight, &green2);
else if(goStraight == 0 && getValue(sensor2Right))
turnOffLight(&red2, red2Right, green2Right, &green2);
//After this loop, only red light would be turned on if you want to go straight.
//If you want to go right, it is possible that the going straight light would be on.
//However, going right light would be off.
while(!red1 || !red2)
{
printRegister();
if(green1 && goStraight == 1 && (getValue(sensor1Straight) == 1 || counter >= LIMIT * 1000))
turnOnYellow(&green1, green1Straight, yellow1Straight, &yellow1);
else if(green1 && goStraight == 0 && (getValue(sensor1Right) == 1 || counter >= LIMIT * 1000))
turnOnYellow(&green1, green1Right, yellow1Right, &yellow1);
if(green2 && goStraight == 1 && (getValue(sensor2Straight) == 1 || counter >= LIMIT * 1000))
turnOnYellow(&green2, green2Straight, yellow2Straight, &yellow2);
else if(green2 && goStraight == 0 && (getValue(sensor2Right) == 1 || counter >= LIMIT * 1000))
turnOnYellow(&green2, green2Right, yellow2Right, &yellow2);
if(yellow1 && goStraight == 1 && yellowCounter1 >= 1 * 1000)
turnOffLight(&yellow1, yellow1Straight, red1Straight, &red1);
else if(yellow1 && goStraight == 0 && yellowCounter1 >= 1 * 1000)
turnOffLight(&yellow1, yellow1Right, red1Right, &red1);
if(yellow2 && goStraight == 1 && yellowCounter2 >= 1 * 1000)
turnOffLight(&yellow2, yellow2Straight, red2Straight, &red2);
else if(yellow2 && goStraight == 0 && yellowCounter2 >= 1 * 1000)
turnOffLight(&yellow2, yellow2Right, red2Right, &red2);
//If this function performs for going right and the opposite lane already stopped going right then open up going straight for this side
if(!goStraight && red2 && !done1 && getValue(sensor1Straight))
{
done1 = 1;
turnOff(red1Straight);
turnOn(green1Straight);
}
//If this function performs for going right and opposite lane already stopped going right then open up going straight for this side
if(!goStraight && red1 && !done2 && getValue(sensor2Straight))
{
done2 = 1;
turnOff(red2Straight);
turnOn(green2Straight);
}
counter++;
if(yellow1)
yellowCounter1++;
if(yellow2)
yellowCounter2++;
}
* greenOn1 = done1;
* greenOn2 = done2;
}
void printRegister(void)
{
int result = 0;
asm volatile(
"addi %0, x30, 0x0\n\t"
:"=r"(result)
:
:"x30"
);
int index = 0;
while(result != 0)
{
if(result & 1 == 1)
print(index);
result >>= 1;
index++;
}
}
void print(int index)
{
switch(index)
{
case 0:
printf("Car detected for going straight at side 1.\n");
break;
case 1:
printf("Car detected for going straight at side 2.\n");
break;
case 2:
printf("Car detected for going straight at side 3.\n");
break;
case 3:
printf("Car detected for going straight at side 4.\n");
break;
case 4:
printf("Car detected for going right at side 1.\n");
break;
case 5:
printf("Car detected for going right at side 2.\n");
break;
case 6:
printf("Car detected for going right at side 3.\n");
break;
case 7:
printf("Car detected for going right at side 4.\n");
break;
case 8:
printf("Straight Red Light Side 1 is on.\n");
break;
case 9:
printf("Straight Red Light Side 2 is on.\n");
break;
case 10:
printf("Straight Red Light Side 3 is on.\n");
break;
case 11:
printf("Straight Red Light Side 4 is on.\n");
break;
case 12:
printf("Right Red Light Side 1 is on.\n");
break;
case 13:
printf("Right Red Light Side 2 is on.\n");
break;
case 14:
printf("Right Red Light Side 3 is on.\n");
break;
case 15:
printf("Right Red Light Side 4 is on.\n");
break;
case 16:
printf("Straight Yellow Light Side 1 is on.\n");
break;
case 17:
printf("Straight Yellow Light Side 2 is on.\n");
break;
case 18:
printf("Straight Yellow Light Side 3 is on.\n");
break;
case 19:
printf("Straight Yellow Light Side 4 is on.\n");
break;
case 20:
printf("Right Yellow Light Side 1 is on.\n");
break;
case 21:
printf("Right Yellow Light Side 2 is on.\n");
break;
case 22:
printf("Right Yellow Light Side 3 is on.\n");
break;
case 23:
printf("Right Yellow Light Side 4 is on.\n");
break;
case 24:
printf("Straight Green Light Side 1 is on.\n");
break;
case 25:
printf("Straight Green Light Side 2 is on.\n");
break;
case 26:
printf("Straight Green Light Side 3 is on.\n");
break;
case 27:
printf("Straight Green Light Side 4 is on.\n");
break;
case 28:
printf("Right Green Light Side 1 is on.\n");
break;
case 29:
printf("Right Green Light Side 2 is on.\n");
break;
case 30:
printf("Right Green Light Side 3 is on.\n");
break;
case 31:
printf("Right Yellow Light Side 4 is on.\n");
break;
}
}
int main(void)
{
//Format: Side1, Side2, Side3, Side4
//These are on x30
const int sensor1Straight = 0, sensor2Straight = 1, sensor3Straight = 2, sensor4Straight = 3;
const int sensor1Right = 4, sensor2Right = 5, sensor3Right = 6, sensor4Right = 7;
//These are on x30
const int red1Straight = 8, red2Straight = 9, red3Straight = 10, red4Straight = 11;
const int red1Right = 12, red2Right = 13, red3Right = 14, red4Right = 15;
const int yellow1Straight = 16, yellow2Straight = 17, yellow3Straight = 18, yellow4Straight = 19;
const int yellow1Right = 20, yellow2Right = 21, yellow3Right = 22, yellow4Right = 23;
const int green1Straight = 24, green2Straight = 25, green3Straight = 26, green4Straight = 27;
const int green1Right = 28, green2Right = 29, green3Right = 30, green4Right = 31;
//open == 0 means open sides 1 and 2 (opposite paths)
//open == 2 means open sides 3 and 4 (opposite paths)
int open = 0;
//Reset all bits on x30
asm volatile(
"andi x30, x30, 0x0\n\t" //x30 & 0 and store in x30
:
:
: "x30" //This specifies that x30 is being changed after this instruction
);
//Masked value to turn on all red lights
int pin_mask = 0;
pin_mask |= 1 << red1Straight | 1 << red1Right | 1 << red2Straight | 1 << red2Right | 1 << red3Straight | 1 << red3Right | 1 << red4Straight | 1 << red4Right;
//Turn on all red lights
asm volatile(
"or x30, x30, %0\n\t" //x30 or pin_mask and store in x30 (%0 means go to first input that is given)
:
: "r"(pin_mask)
: "x30"
);
int on = 0;
int testCase = 1;
//Random test cases
switch(testCase)
{
case 0:
//No light on
break;
case 1:
//lane1 right on and lane4 straight and right on
pin_mask = 0;
pin_mask |= 1 << sensor1Right | 1 << sensor4Straight | 1 << sensor4Right | 1 << sensor1Straight;
asm volatile(
"or x30, x30, %0\n\t"
:
: "r"(pin_mask)
: "x30"
);
break;
}
int iterations = 0;
while(iterations != 200)
{
int count = getValue(sensor1Straight) + getValue(sensor2Straight) + getValue(sensor3Straight) + getValue(sensor4Straight) + getValue(sensor1Right) + getValue(sensor2Right) + getValue(sensor3Right) + getValue(sensor4Right);
if(count != 0)
{
if(on)
{
turnOffPriorityLight(red1Straight, yellow1Straight, green1Straight);
on = 0;
}
if(count == 4 || count == 3)
{
if(open == 0)
OnLimit(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right);
else
OnLimit(sensor3Straight, sensor3Right, sensor4Straight, sensor4Right, red3Straight, red4Straight, red3Right, red4Right, yellow3Straight, yellow4Straight, yellow3Right, yellow4Right, green3Straight, green4Straight, green3Right, green4Right);
open = (open + 2) % 4;
if(open == 0)
OnLimit(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right);
else
OnLimit(sensor3Straight, sensor3Right, sensor4Straight, sensor4Right, red3Straight, red4Straight, red3Right, red4Right, yellow3Straight, yellow4Straight, yellow3Right, yellow4Right, green3Straight, green4Straight, green3Right, green4Right);
open = (open + 2) % 4;
}
else if(count == 2 || count == 1)
{
if(open == 0 && getValue(sensor1Straight) || getValue(sensor1Right) || getValue(sensor2Straight) || getValue(sensor2Right))
OnLimit(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right);
else if(getValue(sensor3Straight) || getValue(sensor3Right) || getValue(sensor4Straight) || getValue(sensor4Right))
OnLimit(sensor3Straight, sensor3Right, sensor4Straight, sensor4Right, red3Straight, red4Straight, red3Right, red4Right, yellow3Straight, yellow4Straight, yellow3Right, yellow4Right, green3Straight, green4Straight, green3Right, green4Right);
open = (open + 2) % 4;
if(open == 0 && getValue(sensor1Straight) || getValue(sensor1Right) || getValue(sensor2Straight) || getValue(sensor2Right))
{
OnLimit(sensor1Straight, sensor1Right, sensor2Straight, sensor2Right, red1Straight, red2Straight, red1Right, red2Right, yellow1Straight, yellow2Straight, yellow1Right, yellow2Right, green1Straight, green2Straight, green1Right, green2Right);
open = (open + 2) % 4;
}
else if(getValue(sensor3Straight) || getValue(sensor3Right) || getValue(sensor4Straight) || getValue(sensor4Right))
{
OnLimit(sensor3Straight, sensor3Right, sensor4Straight, sensor4Right, red3Straight, red4Straight, red3Right, red4Right, yellow3Straight, yellow4Straight, yellow3Right, yellow4Right, green3Straight, green4Straight, green3Right, green4Right);
open = (open + 2) % 4;
}
}
}
else
{
if(!on)
{
turnOff(red1Straight);
turnOn(green1Straight);
on = 1;
}
}
printRegister();
iterations++;
}
return 0;
}