-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstopWhenMazeDone.ino
More file actions
1811 lines (1628 loc) · 57.4 KB
/
stopWhenMazeDone.ino
File metadata and controls
1811 lines (1628 loc) · 57.4 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Servo.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
/*Treasure and FFT Stuff*/
#define LIN_OUT 1 // use the log output function
#define FFT_N 128 // set to 256 point fft
#include <FFT.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdio.h>
#include <inttypes.h>
int signal = 0; //the maximum index of the fft
int consistency = 0; //consistency factor of the signal.
int start_execution = 0;
#include <StackList.h>
//#include <StackArray.h>
StackList<int> myStack;
int adjacentUnexploredCoordinate[2];
boolean noAdjacentUnexplored; //true iff there are no adjacent unexplored coordinates to current coordinate
int frontCoordinate[2];
int rightCoordinate[2];
int leftCoordinate[2];
int nodesToVisit;
int oldNodesToVisit;
boolean wasVisited;
RF24 radio(9,10); //initializing setup data
const uint64_t pipes[2] = { 0x000000002LL, 0x0000000003LL };
typedef enum { role_ping_out = 1, role_pong_back } role_e;
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
role_e role = role_ping_out;
unsigned char temp1[32];
unsigned char temp2[32];
Servo myServoLeft;
Servo myServoRight;
int rightServo = 4;
int leftServo = 5;
//convention: right and left are from 1st person perspective
//(so if you look at the robot from behind, your right is the robots right in this code)
int finished = 0; //0 means not finished, 1 means finished
//configure the mux
int selectA = 2;
int selectB = 3;
int selectC = 8;
int muxRead = A2; //in order (from 0-6): left wall, center wall, right wall, OL, FL, FR, OR
//configure the line sensors
boolean FR; //true iff front right sensor is ON black line
boolean FL; //true iff front left sensor is ON black line
boolean OR; //true iff outside right sensor is ON black line
boolean OL; //true iff outside left sensor is ON black line
const int lineThres = 4;
boolean frontWall;
boolean leftWall;
boolean rightWall;
double frontWallThreshold = 15.5;//16.57475;//15.7475; //was 11.7475
double rightWallThreshold = 10.5;//9.5; //was 7.3025;
double leftWallThreshold = 10.5;//9.5; //7.3025;
//for debugging sensors
int frontLight =0;
int rightLight = A5;
int leftLight = A4;
int flagLight = 1;
/*******************STATE VARIBLES***************************************/
int orientationState = 0;
int lineState = 0;
int intersectionState = 0;
/*
lineState definitions:
0: initial point. both front sensors on line
1: robot is slightly right of line
2: robot is slightly left of line
3: robot is far right of line
4: robot is far left of line
*/
/*
intersectionState definitions: (yes, the ordering sucks and is stupid... my bad)
0: initial point. all 3 sensors do not detect wall
1: only both side sensors detect wall
2: only front and right side sensor detect wall
3: only front and left side sensor detect wall
4: only front side sensor detects wall
5: all 3 sensors do detect wall
6: only right sensor detects wall
7: only left sensor detects wall
*/
/*
orientationState definitions:
0: robot is facing south
1: robot is facing west
2: robot is facing north
3: robos is facing east
*/
/*
xCoordinate at top left is 0, increments upwards as you walk down the matrix column
yCoordinate at top left is 0, increments upwards as you walk across the matrix row
*/
/************************************************************/
/***********************THE MATRIX********************************************************/
int xCoordinate = 1; //this is the xCoordinate that we are GOING to. depending on HOW the robot is orientaed at the start,
//does the robot start just before or jst after the intersection (we can't have it start at the intersection)
//if robot starts just before the first intersection, then the xCoordinate should be 0.
//if the robot starts just after the first intersection, then the xCoordinate should be 1
//UPDATE: robot starts just after the intersection
int yCoordinate = 0;
boolean intersectionMatrix[5][4][6] =
{
{ {true, true, false, false, true,false}, {false, true, false, false, false,false}, {false, true, false, false, false,false}, {false, true, false, true, false,false} },
{ {false, false, false, false, true,false}, {false, false, false, false, false,false}, {false, false, false, false, false,false}, {false, false, false, true, false,false} },
{ {false, false, false, false, true,false}, {false, false, false, false, false,false}, {false, false, false, false, false,false}, {false, false, false, true, false,false} },
{ {false, false, false, false, true,false}, {false, false, false, false, false,false}, {false, false, false, false, false,false}, {false, false, false, true, false,false} },
{ {false, false, true, false, true,false}, {false, false, true, false, false,false}, {false, false, true, false, false,false}, {false, false, true, true, false,false} }
};
//notation: {explored, top, bottom, right, left} => {explored, north, south, east, west, unexplorable}
//false indicates not explored,.. no wall at that position
//matrix is preset to account for outer walls
//false indicates not explored,.. no wall at that position, not yet deemed unexplorable
//true indicates explored,.. wall at that position, deemed unexplorable
/*****************************************************************************************/
volatile int T1capture = 0;
volatile int period = 0;
volatile int lastT1capture = 0;
volatile int treasure_consistancy = 0;
volatile int found_treasure = 0;
volatile int count =0;
volatile int ISRin = 0;
int TrY = -1;
int TrX = -1;
ISR (ANALOG_COMP_vect)
{
period = TCNT2;
TCNT2 = 0;
if(period > 75 && period < 87)
{
treasure_consistancy++;
if(treasure_consistancy == 7)
{
found_treasure = 1;
TrX = xCoordinate;
TrY = yCoordinate;
ACSR -= (1 << ACIE);
}
}
else
{
treasure_consistancy = 0;
}
}
void setup() {
// put your setup code here, to run once:
ACSR = (1<<ACIE) | (1<<ACIS1) | (1<<ACIS0);
TCCR2B = 2; // running with clock divided by 8
TCCR2A=0;
while(start_execution == 0){
check_for_start();
}
printf_begin();
myStack.push(0);
myStack.push(0); //push coordinates (0,0) onto stack
pinMode(rightServo, OUTPUT);
pinMode(leftServo,OUTPUT);
pinMode(selectA,OUTPUT);
pinMode(selectB,OUTPUT);
pinMode(selectC,OUTPUT);
myServoRight.attach(rightServo);
myServoLeft.attach(leftServo);
pinMode(13,OUTPUT);
pinMode(frontLight,OUTPUT);
pinMode(flagLight,OUTPUT);
digitalWrite(frontLight,LOW);
digitalWrite(flagLight,LOW);
analogWrite(rightLight,0);
analogWrite(leftLight,0);
//channel 0. look for wall on left at very start
digitalWrite(selectA,LOW);
digitalWrite(selectB,LOW);
digitalWrite(selectC,LOW);
double leftSensorValue = analogRead(muxRead);
double leftSensorDistance = convertToDistance(leftSensorValue); //leftSensorDistance is distance between left sensor and wall in centimeters
leftWall = leftSensorDistance <= leftWallThreshold;
if (leftWall){
intersectionMatrix[0][0][3]=true;
intersectionMatrix[0][1][4]=true;
myStack.push(1); //push (1,0) onto stack
myStack.push(0);
nodesToVisit = 1;
}
else{// if (!leftWall){
digitalWrite(selectA, HIGH); //channel 1, read front sensor
double frontSensorValue = analogRead(muxRead);
double frontSensorDistance = convertToDistance(frontSensorValue); //frontSensorDistance is distance between front sensor and wall in centimeters
frontWall = frontSensorDistance <= frontWallThreshold;
if (frontWall){
turn_left();
orientationState =3; //going east
xCoordinate = 0;
yCoordinate =1;
intersectionMatrix[0][0][2]=true;
intersectionMatrix[1][0][1]=true;
myStack.push(0); //push (0,1) onto stack
myStack.push(1);
nodesToVisit = 1;
}
else{ //no left wall and no front wall
myStack.push(1); //push (1,0)
myStack.push(0);
nodesToVisit = 2;
}
}
radio.begin(); //sets up radio
radio.setRetries(15,15);
radio.setAutoAck(true);
radio.setChannel(0x50);
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate(RF24_250KBPS);
if ( role == role_ping_out ) //sets reading and writing pipes
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
radio.startListening(); //open to receiving
radio.printDetails();
}
void check_for_start()
{
check_override();
if(start_execution == 0)
{
check_660();
}
}
void check_override()
{
int temp;
temp = analogRead(A1);
if( 1000 < temp )
{
start_execution = 1;
}
}
void check_660()
{
for( int i = 0; i < 256; i +=2) //get input data
{
fft_input[i] = analogRead(A0);
fft_input[i+1] = 0;
}
fft_window(); //does the reordering algorithm and the fft
fft_reorder();
fft_run();
fft_mag_lin();
signal = maximum(); //calculates the maximum in the signal
if(((signal == 9) || (signal == 10) || (signal == 11)|| (signal==12)) && (fft_lin_out[signal] > 10)) //checks for consistency in the maximum and if its around 660 Hz
{
consistency = consistency + 1;
if(consistency >= 20)
start_execution = 1; //says that the input frequency is 660 Hz.
}
else
{
consistency = 0;
start_execution = 0;
}
}
int maximum() //computes the maximum of the fft_out[]
{
int i;
int index = 5;
for(i = 5; i < 50; i++)
{
if(fft_lin_out[index] < fft_lin_out[i])
index = i;
}
return index;
}
boolean at_intersection(){
if (FR && FL && OR && OL){ //all sensors are on the line{
return true;
}
else{
return false;
}
}
void stay_straight() //go straight
{
myServoLeft.write(180);
myServoRight.write(0);
}
void veer_right()
{
//right wheel slows down
myServoLeft.write(170);
myServoRight.write(85);
}
void veer_left()
{
//left wheel slows down
myServoLeft.write(95);
myServoRight.write(10);
}
void stopRobot(){
while (finished){
myServoLeft.write(90);
myServoRight.write(90);
//radio_transmit();
}
}
void turn_left()
{
//stop left wheel
//keep going with right wheel
myServoLeft.write(90);
myServoRight.write(0);
//delay(1200); //initial turning delay was 1200
delay(300);
//set to channel 5 - read front right sensor
digitalWrite(selectA, HIGH);
digitalWrite(selectB,LOW);
digitalWrite(selectC,HIGH);
while (analogRead(muxRead)*.0049<4){ //while both front sensors are off line
myServoLeft.write(90);
myServoRight.write(0);
}
stay_straight();
}
void turn_right()
{
//stop right wheel
//keep going with left wheel
myServoLeft.write(180);
myServoRight.write(90);
//delay(1200); //initial turning delay used to be 1200
delay(300);
//set to channel 4 - read front left sensor
digitalWrite(selectA, LOW);
digitalWrite(selectB,LOW);
digitalWrite(selectC,HIGH);
while (analogRead(muxRead)*.0049<4){ //while both front sensors are off line
myServoLeft.write(180);
myServoRight.write(90);
}
stay_straight();
}
void turn_180degrees(){
myServoLeft.write(180);
myServoRight.write(180);
delay(250); //delay so that robot gets past intersection
//channel 6 - outside right sensor
digitalWrite(selectA,LOW);
digitalWrite(selectB,HIGH);
digitalWrite(selectC,HIGH);
while (analogRead(muxRead)*.0049>4){ //while outside right sensor is ON black line
//this is just so that we don't count the line that we are currently on
}
//start turning around clockwise
// myServoLeft.write(180);
//myServoRight.write(180);
//channel 6 - outside right sensor
digitalWrite(selectA,LOW);
digitalWrite(selectB,HIGH);
digitalWrite(selectC,HIGH);
while (analogRead(muxRead)*.0049 <4){ //while outside right sensor is NOT on black line
}
//channel 3 - outside left sensor
digitalWrite(selectA,HIGH);
digitalWrite(selectB,HIGH);
digitalWrite(selectC,LOW);
delay(75);
while (analogRead(muxRead)*.0049<4){ //while outside left sensor is not on black line
}
//channel 6 - outside right sensor
digitalWrite(selectA,LOW);
digitalWrite(selectB,HIGH);
digitalWrite(selectC,HIGH);
//slow down
myServoLeft.write(150);
myServoRight.write(135);
while (analogRead(muxRead)*.0049<4){ //while outside right sensor is not on black line
}
//slow down more
myServoLeft.write(140);
myServoRight.write(120);
while (analogRead(muxRead)*.0049>4){ //while outside right sensor IS ON black line
}
//set to channel 5 - read front right sensor
digitalWrite(selectA, HIGH);
digitalWrite(selectB,LOW);
digitalWrite(selectC,HIGH);
while (analogRead(muxRead)*.0049<4){ //while front right sensor is not on black line
}
//set to channel 4 - read front left sensor
digitalWrite(selectA, LOW);
digitalWrite(selectB,LOW);
digitalWrite(selectC,HIGH);
if (analogRead(muxRead)*.0049>4){ //if front left sensor is on black line
myServoLeft.write(90);
myServoRight.write(90);
}
myServoLeft.write(90);
myServoRight.write(90);
delay(100);
stay_straight();
}
void turn_180degrees2(){
//turn clockwise
//keep moving forward with left wheel
//reverse right wheel
myServoLeft.write(90);
myServoRight.write(90);
myServoLeft.write(180); //
myServoRight.write(180);
delay(150);
//set to channel 4 - read front left sensor
digitalWrite(selectA, LOW);
digitalWrite(selectB,LOW);
digitalWrite(selectC,HIGH);
int howManyLines=0;
while (analogRead(muxRead)*.0049<4){//while left front sensor is off line
}
howManyLines = 1;
delay(200);
while (howManyLines == 1){
myServoLeft.write(180);
myServoRight.write(180);
if (analogRead(muxRead)*.0049>4){//left front sensor is on line
howManyLines=2; //break out of while loop, and method ends
}
}
//digitalWrite(flagLight,HIGH);
stay_straight();
}
double convertToDistance(double sensorValue){
/*
input: analog value on sensor pin [0-1024)
output: analog value converted to centimeters
this is meant exclusively for the wall sensors
*/
double newValue =sensorValue * 0.0049; //converts value of 0-1024 to 0-5Volts
double distanceFromWall = (1/newValue);
distanceFromWall = distanceFromWall*12.75;
distanceFromWall = distanceFromWall-0.4; //uses line of best fit to calculate distance in centimeters
return distanceFromWall;
}
int intersectionStateNumberGenerator(){
//turn based on wall information - basically, this won't hit walls
if (intersectionState == 0){ //all 3 sensors do not detect wall
return 0;// random(0,3);
}
else if (intersectionState ==1){ //1: only both side sensors detect wall
return 0; //go straight
}
else if (intersectionState ==2){ //2: only front and right side sensor detect wall
return 1; //turn left
}
else if (intersectionState == 3){ //3: only front and left side sensor detect wall
return 2; //turn right
}
else if (intersectionState==4){ //4: only front sensor detects wall
//digitalWrite(13,HIGH);
return 2; //turn right
}
else if (intersectionState ==5){ //5: all 3 sensors do detect wall
//must turn around
return 3;
//turnaround
}
else if (intersectionState ==6){ //6: only right sensor detects wall
return 0; //go straight
}
else if (intersectionState==7){ //7: only left sensor detects wall
return 0; //go straight
}
}
void updateIntersection(){ //sensor distances are all GLOBAL variables
if (!frontWall && !rightWall && !leftWall){ //no sensor detects a wall
intersectionState = 0;
}
else if (!frontWall && rightWall && leftWall){ //only right and left sensor detects wall
intersectionState = 1;
}
else if (frontWall && rightWall && !leftWall){ //only right and front sensor detects wall
intersectionState = 2;
}
else if (frontWall && !rightWall && leftWall){ //only front and left sensor detect wall
intersectionState = 3;
}
else if (frontWall && !rightWall && !leftWall){ //only front sensor detecs wall
intersectionState = 4;
}
else if (frontWall && rightWall && leftWall){ //all 3 sensors detect wall
intersectionState = 5;
}
else if(!frontWall && rightWall && !leftWall){ //only right sensor detects wall
intersectionState = 6;
}
else if(!frontWall && !rightWall && leftWall){ //only left sensor detects wall
intersectionState = 7;
}
}
void updateOuterWalls(){
if (xCoordinate ==4 && orientationState == 0){ //going south into southern wall
frontWall=true;
}
else if(xCoordinate==0 && orientationState == 2){ //going north into northern wall
frontWall=true;
}
else if (yCoordinate == 0 && orientationState == 1){ //going west into western wall
frontWall = true;
}
else if (yCoordinate == 3 && orientationState == 3){ //going east into eastern wall
frontWall=true;
}
if (yCoordinate ==3 && orientationState == 0){ //going south on east edge of maze
leftWall = true;
}
else if (yCoordinate == 3 && orientationState == 2){ //going north on east edge of maze
rightWall = true;
}
else if (yCoordinate == 0 && orientationState == 0){ //going south on west edge of maze
rightWall = true;
}
else if (yCoordinate == 0 && orientationState == 2){ //going north on west edge of maze
leftWall = true;
}
else if (xCoordinate == 0 && orientationState == 1){ //going west on top wall
rightWall = true;
}
else if (xCoordinate ==0 && orientationState == 3){ //going east on top wall
leftWall = true;
}
else if(xCoordinate == 4 && orientationState == 1){ //going west on bottom wall
leftWall=true;
}
else if (xCoordinate == 4 && orientationState == 3){ //going east on bottom wall
rightWall = true;
}
}
String myFunct(){
return "hi";
}
void turnOrGoStraight(int whichDirection){
//Serial.println(whichDirection);
if (whichDirection == 0)
{
stay_straight();
delay(200); //give sensors time to pass the intersection
}
else if (whichDirection == 1)
{
delay(50); //allow robot to go a bit past intersection
turn_left();
}
//whichDirection must have value of 2
else if (whichDirection == 2){
delay(50); //allow robot to go a bit past intersection
turn_right();
}
else if (whichDirection == 4){
stopRobot();
}
else{ //whichDirection must have a value of 3
//need to turn 180 degrees
turn_180degrees();
}
}
/***********************FUNCTION TO UPDATE THE MATRIX************************/
void populateIntersectionMatrix(){
/*
all of these variables are GLOBAL - int intersectionState,int orientationState, int xCoordinate, int yCoordinate)
this method serves to update the intersectionMatrix's wall variables and the explored field of its current position (it doesn't update the xCoordinate, yCoordinate)
robot just hit intersection now. it has not yet determined which direction it will turn in
orientationState and xCoordinate and yCoordinate represent the robot's CURRENT status
intersectionState is updated (as in, it reflects the CURRENT intersection)
populate the intersectoinMatrix accoridngly using these global variables
*/
intersectionMatrix[xCoordinate][yCoordinate][0] = true; //set this current position to EXPLORED regardless of orientation
if (intersectionState == 0){ //all 3 sensor do NOT detect wall
//nothing to update
}
else if (intersectionState == 1){ //only both side sensors detect wall
if (orientationState == 0 || orientationState == 2){ //robot is facing south OR north
intersectionMatrix[xCoordinate][yCoordinate][3] = true; //set right wall true
intersectionMatrix[xCoordinate][yCoordinate][4]= true; //set left wall true
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3] = true; //set right wall of left coordinate true
}
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //set left wall of right coordinate true
}
}
else{ //orientationState must be 1 or 3 //robot is facing east OR west
intersectionMatrix[xCoordinate][yCoordinate][1]= true;
intersectionMatrix[xCoordinate][yCoordinate][2]= true;
if (xCoordinate !=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1]=true;
}
if (xCoordinate != 0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true;
}
}
}
else if (intersectionState == 2){ //only front and right side sensor detect wall
if (orientationState == 0){ //facing south
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
if (xCoordinate != 4){
intersectionMatrix[xCoordinate+1][yCoordinate][1]=true; // set top wall of lower coordinate
}
if (yCoordinate != 0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]=true; //set right wall of left coordinate
}
}
else if (orientationState == 1){ //facing west
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
intersectionMatrix[xCoordinate][yCoordinate][4]= true; //left
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]=true; //set right wall of left coordinate
}
if (xCoordinate != 0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true; //set bottom wall of upper coordinate
}
}
else if (orientationState ==2){ //robot is facing north
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
if (yCoordinate !=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //set left wall of right coordinate
}
if (xCoordinate !=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2] = true; //set bottom wall of upper coordinate
}
}
else if (orientationState == 3){ //robot is facing east
intersectionMatrix[xCoordinate][yCoordinate][3] = true; //right
intersectionMatrix[xCoordinate][yCoordinate][2] = true; //bottom
if (yCoordinate !=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //set left wall of right coordinate
}
if (xCoordinate != 4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //set top wall of lower coordinate
}
}
}
else if (intersectionState == 3){ //only front and left side sensor detect wall
if (orientationState == 0){ //robot is facing south
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
if (xCoordinate != 4){
intersectionMatrix[xCoordinate+1][yCoordinate][1]=true; //set top wall of lower coordinate
}
if (yCoordinate != 3){
intersectionMatrix[xCoordinate][yCoordinate+1][4]; //set left wall of right coordinate
}
}
else if (orientationState == 1){ //facing west
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
if (xCoordinate !=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1]=true; //set top wall of lower coordinate
}
if (yCoordinate !=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]=true; //set right wall of left coordinate
}
}
else if (orientationState ==2){ //robot facing north
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
if (xCoordinate != 0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true; //set bottom wall of upper coordinate
}
if (yCoordinate !=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3] = true; //set right wall of left coordinate
}
}
else if (orientationState == 3){//robot is facing east
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
intersectionMatrix[xCoordinate][yCoordinate][1]=true;
if (xCoordinate != 0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true; //bottom wall of upper coordinate
}
if (yCoordinate !=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4]= true; //left wall of right coordinate
}
}
}
else if (intersectionState == 4){ //only front sensor detects wall
if (orientationState == 0){ //robot is facing south
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
if (xCoordinate != 4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //top wall of lower coordinate
}
}
else if (orientationState == 1){ //robot is facing west
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]=true; //set right wall of left coordinate
}
}
else if (orientationState ==2){//robot is facing north
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2] = true; //bottom wall of upper coordinate
}
}
else if (orientationState == 3){ //robot is facing east
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4]=true; //set left wall of right coordinate
}
}
}
else if (intersectionState == 5){//all 3 sensors detect wall
if (orientationState == 0){ //robot facing south
intersectionMatrix[xCoordinate][yCoordinate][2] = true; //bottom
intersectionMatrix[xCoordinate][yCoordinate][3] = true; //right
intersectionMatrix[xCoordinate][yCoordinate][4] = true;//left
if (xCoordinate!=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //top wall of lower coordinate
}
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3] = true; //right wall of left coordinate
}
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //left wall of right coordinate
}
}
else if (orientationState == 1){ //facing west
intersectionMatrix[xCoordinate][yCoordinate][2] = true; //bottom
intersectionMatrix[xCoordinate][yCoordinate][1] = true; //top
intersectionMatrix[xCoordinate][yCoordinate][4] = true;//left
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2] = true; //bottom wall of upper coordinate
}
if (xCoordinate!=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //top wall of lower coordinate
}
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3] = true; //right wall of left coordinate
}
}
else if (orientationState ==2){ //facing north
intersectionMatrix[xCoordinate][yCoordinate][1] = true; //top
intersectionMatrix[xCoordinate][yCoordinate][3] = true; //right
intersectionMatrix[xCoordinate][yCoordinate][4] = true;//left
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2] = true; //bottom wall of upper coordinate
}
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3] = true; //right wall of left coordinate
}
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //left wall of right coordinate
}
}
else if (orientationState == 3){ //facing east
intersectionMatrix[xCoordinate][yCoordinate][2] = true; //bottom
intersectionMatrix[xCoordinate][yCoordinate][3] = true; //right
intersectionMatrix[xCoordinate][yCoordinate][1] = true;//top
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2] = true; //bottom wall of upper coordinate
}
if (xCoordinate!=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //top wall of lower coordinate
}
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //left wall of right coordinate
}
}
}
else if (intersectionState == 6){ //only right sensor detects wall
if (orientationState ==0){ //facing south
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]==true; //right wall of left coordinate
}
}
else if (orientationState ==1){ //facing west
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true; //bottom wall of top coordinate
}
}
else if(orientationState ==2){ //facing north
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4]=true; //left wall of right coordinate
}
}
else if (orientationState == 3){ //facing east
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
if (xCoordinate!=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1]=true; //top wall of bottom coordinate
}
}
}
else if (intersectionState ==7){ //only left sensor detects wall
if (orientationState ==0){ //facing south
intersectionMatrix[xCoordinate][yCoordinate][3]=true; //right
if (yCoordinate!=3){
intersectionMatrix[xCoordinate][yCoordinate+1][4] = true; //left wall of right coordinate
}
}
else if(orientationState==1){ //facing west
intersectionMatrix[xCoordinate][yCoordinate][2]=true; //bottom
if (xCoordinate!=4){
intersectionMatrix[xCoordinate+1][yCoordinate][1] = true; //top wall of bottom coordinate
}
}
else if(orientationState == 2){ //facing north
intersectionMatrix[xCoordinate][yCoordinate][4]=true; //left
if (yCoordinate!=0){
intersectionMatrix[xCoordinate][yCoordinate-1][3]=true; //right wall of left coordinate
}
}
else if(orientationState == 3){ //facing east
intersectionMatrix[xCoordinate][yCoordinate][1]=true; //top
if (xCoordinate!=0){
intersectionMatrix[xCoordinate-1][yCoordinate][2]=true; //bottom wall of top coordinate
}
}
}
}
/******************************/
/**************UPDATE ORIENTATION AND COORDINATES************/
void updateOrientation(int whichDirection){
/**
updates orientationState of robot according to whichDirection, which determines which direction the robot
is about to go (straight, right, left, or turnaround)
for example, if robot WAS moving north and IS NOW turning left, then the robot will
be going west. this method updates orienatationState to reflect that properly
*/
if (whichDirection == 0){ //going straight
orientationState = orientationState;
}
else if (whichDirection == 1){ //turning left
if (orientationState !=0){
orientationState = orientationState - 1;
}
else{
orientationState = 3;
}
}
else if (whichDirection == 2){ //turning right
if (orientationState != 3){
orientationState = orientationState +1;
}
else{
orientationState=0;
}
}
else if (whichDirection == 3){ //making a 180 degree turn
if (orientationState == 1){
orientationState = 3;
}
else if(orientationState == 0){
orientationState = 2;
}
else{
orientationState = orientationState -2;
}
}
}
void updateCoordinates(){
/**
updates x and y coordinates of robot according to ONLY the newly updated orientationState
sets x and y coordinates of robot to the x and y coordinates that IT IS NOW SET TO MOVE TOWARDS
example: if robot is at (3,2), and it's orientationState is 1, then the robot is moving west so this
updates the coordinates to (3,1)
*/
if (orientationState == 0){ //going south
xCoordinate += 1;
yCoordinate = yCoordinate;
}
else if (orientationState == 1){ //going west
xCoordinate = xCoordinate;
yCoordinate -=1;
}
else if (orientationState == 2){ //going north
xCoordinate -= 1;
yCoordinate = yCoordinate;
}
else{ //orientationState == 3 -- going east
xCoordinate = xCoordinate;
yCoordinate += 1;
}
}