-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfilter prob.nxc
1630 lines (1516 loc) · 49 KB
/
pfilter prob.nxc
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
/* Written by Kevin Alexander Lee
This is an implementation of NXT robot localization using probability and
particle filters.
Using the grid based map accompanying the code, the robot which initially
has no information about its coordinate position will be able to locate itself
and move to a specific target and destination.
The NXT robot uses aspects of particle filters and probability sensing to
determine which position it is likely at.
*/
/* the following section defines macros */
/* macros for directions, chosen to not conflict with other definitions
and to allow arithmetic between directions*/
#define NORTH 100
#define EAST 101
#define SOUTH 102
#define WEST 103
/* macros for diagonal directions */
#define NORTHEAST 1000
#define SOUTHEAST 1001
#define SOUTHWEST 1002
#define NORTHWEST 1003
/* File related macros */
#define FILENAME "mapdata.txt"
#define READSIZE 10
/* Function like macro to convert characters from files to the corresponding
int value.*/
#define ASCII_TO_INT(a) a-48
/* macros for the map dimensions. Based on yellow grid intersections*/
#define GWIDTH 3
#define GHEIGHT 5
/* macros for probability sensing
THRESHOLD - a decimal value in (0,1). Determines when a particle is likely enough to be the robot's
position
MATCH - a decimal value in (0,1). How much to affect the probability when a particle's data matches
the robot's surroundings, should be greater than MISMATCH
MISMATCH - a decimal value in (0,1). How much to affect the probability when a particle's data does not
match the robot's surroundings, should be less than MATCH
decide these values for yourself
*/
#define THRESHOLD 0.7
#define MATCH
#define MISMATCH
#define SPEED 30
#define ROTATIONA 40
#define BLACKAVGR 40
#define BLACKAVGG 45
#define BLACKAVGB 40
#define REDAVGR 235
#define REDAVGG 92
#define REDAVGB 64
#define YELLOWAVGR 228
#define YELLOWAVGG 212
#define YELLOWAVGB 95
#define BLUEAVGR 85
#define BLUEAVGG 88
#define BLUEAVGB 120
#define GREENAVGR 74
#define GREENAVGG 124
#define GREENAVGB 65
#define WHITEAVGR 230
#define WHITEAVGG 210
#define WHITEAVGB 220
/* You may want to define macros to keep track of certain turning angles and
speeds throughout your code. This provides an easy way to change them */
/* Represents a single particle.
Each particle has a coordinate given by x and y and also a direction.
The probability attached to a particle is the likelihood of it being the
actual position and heading of the robot. This is updated based on the
robot's surroundings.
*/
struct particle {
int x;
int y;
int theta;
float prob; /* weight of a particle representing probability */
};
/* Represents an intersection (yellow square) on the grid.
Holds data on the surroundings of the intersection, namely coloured(or white)
squares to the NE, SE, SW, NW of the intersection.
Note that the directions are by default in terms of compass directions.
When given a heading, the directions will change relative to the heading.
*/
struct intersection {
int NE;
int SE;
int SW;
int NW;
};
int get_color(ColorSensorReadType &csr) {
bool red = true,
black = true,
blue = true,
green = true,
yellow = true,
white = true;
// check if input color is black
black&=csr.NormalizedArray[0] <= BLACKAVGR * 1.8;
black&=csr.NormalizedArray[0] >= BLACKAVGR / 1.8;
black&=csr.NormalizedArray[1] <= BLACKAVGG * 1.8;
black&=csr.NormalizedArray[1] >= BLACKAVGG / 1.8;
black&=csr.NormalizedArray[2] <= BLACKAVGB * 1.8;
black&=csr.NormalizedArray[2] >= BLACKAVGB / 1.8;
// check if input color is yellow
yellow&=csr.NormalizedArray[0] <= YELLOWAVGR * 1.3;
yellow&=csr.NormalizedArray[0] >= YELLOWAVGR / 1.3;
yellow&=csr.NormalizedArray[1] <= YELLOWAVGG * 1.3;
yellow&=csr.NormalizedArray[1] >= YELLOWAVGG / 1.3;
yellow&=csr.NormalizedArray[2] <= YELLOWAVGB * 1.3;
yellow&=csr.NormalizedArray[2] >= YELLOWAVGB / 1.3;
// check if input color is red
red&=csr.NormalizedArray[0] <= REDAVGR * 1.3;
red&=csr.NormalizedArray[0] >= REDAVGR / 1.3;
red&=csr.NormalizedArray[1] <= REDAVGG * 1.3;
red&=csr.NormalizedArray[1] >= REDAVGG / 1.3;
red&=csr.NormalizedArray[2] <= REDAVGB * 1.3;
red&=csr.NormalizedArray[2] >= REDAVGB / 1.3;
// check if input color is blue
blue&=csr.NormalizedArray[0] <= BLUEAVGR * 1.3;
blue&=csr.NormalizedArray[0] >= BLUEAVGR / 1.3;
blue&=csr.NormalizedArray[1] <= BLUEAVGG * 1.3;
blue&=csr.NormalizedArray[1] >= BLUEAVGG / 1.3;
blue&=csr.NormalizedArray[2] <= BLUEAVGB * 1.3;
blue&=csr.NormalizedArray[2] >= BLUEAVGB / 1.3;
// check if input color is green
green&=csr.NormalizedArray[0] <= GREENAVGR * 1.3;
green&=csr.NormalizedArray[0] >= GREENAVGR / 1.3;
green&=csr.NormalizedArray[1] <= GREENAVGG * 1.3;
green&=csr.NormalizedArray[1] >= GREENAVGG / 1.3;
green&=csr.NormalizedArray[2] <= GREENAVGB * 1.3;
green&=csr.NormalizedArray[2] >= GREENAVGB / 1.3;
// check if input color is white
white&=csr.NormalizedArray[0] <= WHITEAVGR * 1.1;
white&=csr.NormalizedArray[0] >= WHITEAVGR / 1.1;
white&=csr.NormalizedArray[1] <= WHITEAVGG * 1.1;
white&=csr.NormalizedArray[1] >= WHITEAVGG / 1.1;
white&=csr.NormalizedArray[2] <= WHITEAVGB * 1.1;
white&=csr.NormalizedArray[2] >= WHITEAVGB / 1.1;
if (black) {return 1;}
else if (blue) {return 2;}
else if (green) {return 3;}
else if (yellow) {return 4;}
else if (red) {return 5;}
else if (white) {return 6;}
else {return -1;}
}
/* function prototypes, to be added if needed
*/
/* Subroutine which moves the robot forward from one intersection to the next
*/
sub move_robot(ColorSensorReadType &csr) {
////////////////////////////////////////////////////////////////////////////
// TO DO:
//
// Complete this subroutine.
// Your robot will need to nagivates its way from one intersection to the
// next. If it reaches a red boundary, it will do a 180 degree turn and
// return to the previous intersection facing in the opposite direction.
//
// You may need to account for motor "noise" as your robot will not always
// make turns perfectly and go straight forward each time.
//
////////////////////////////////////////////////////////////////////////////
bool red = false;
int prev_color = 0;
int current_color = -1;
while(true) {
SysColorSensorRead(csr);
current_color = get_color(csr);
NumOut(0,LCD_LINE1,csr.NormalizedArray[0]);
NumOut(0,LCD_LINE2,csr.NormalizedArray[1]);
NumOut(0,LCD_LINE3,csr.NormalizedArray[2]);
if (prev_color == 0 && current_color != -1) {
prev_color = current_color;
}
// set curr to prev color to prevent curr is unknown
else if (prev_color != 0 && prev_color != -1 && current_color == -1 ) {
current_color = prev_color;
}
/* if (current_color == -1){
Off(OUT_AC);
Wait(1000);}*/
if(current_color == 1) {
if (red) { // move from red to yellow
OnRev(OUT_AC, SPEED); // move backwards with an output power of SPEED
} else {
OnFwd(OUT_AC, SPEED); // move forwards with an output power of SPEED
}
} else if(current_color == 5) { // reads red
red = true;
OnRev(OUT_AC, SPEED); // move backwards with an output power of SPEED
} else if (current_color == 4) { // read yellow
until (current_color == 1) {
OnFwd(OUT_AC, SPEED);
SysColorSensorRead(csr);
current_color = get_color(csr);
}
red = false;
break;
} else if (current_color == 2 || current_color == 3 || current_color == 4 || current_color == 6){ //went off black, rotate back to black
until (current_color == 1) {
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
Wait(1000);
SysColorSensorRead(csr);
current_color = get_color(csr);
if (current_color == 1){
break;
}
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
Wait(2000);
SysColorSensorRead(csr);
current_color = get_color(csr);
if (current_color == 1){
break;
}
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
Wait(800);
OnRev(OUT_AC, SPEED);
Wait(200);
SysColorSensorRead(csr);
current_color = get_color(csr);
}
}
if (current_color != -1) {
prev_color = current_color;
}
}
}
/* This function scans the surroundings of the robot.
It takes readings of the colour of the 4 squares surrounding the
intersection and returns an intersection variable which holds the data.
*/
intersection scan_surroundings(ColorSensorReadType &csr) {
intersection rsense;
////////////////////////////////////////////////////////////////////////////
// TO DO:
//
// Implement a function in which the robot will scan the colour of the
// surrounding squares at an intersection.
// It will use the colour sensor to take readings from all four squares
// in each of the NE, SE, SW, NW directions relative to the forward facing
// direction of the robot.
//
////////////////////////////////////////////////////////////////////////////
int current_color;
int prev_color = 0;
bool wait = false;
while(rsense.NE == 0 || rsense.NW == 0 || rsense.SE == 0 || rsense.SW == 0) {
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
SysColorSensorRead(csr);
current_color = get_color(csr);
if (current_color != -1 && prev_color == 0){
prev_color = current_color;
}
if (current_color == -1 && prev_color != 0){
current_color = prev_color;
}
if (current_color == 1) {
wait = false;
} else if (wait == false){
if (rsense.NE == 0 && rsense.NW == 0 && rsense.SE == 0 &&
rsense.SW == 0) {
rsense.NE = current_color;
wait = true;
} else if (rsense.NW == 0 && rsense.SE == 0 && rsense.SW == 0) {
rsense.SE = current_color;
wait = true;
} else if (rsense.NW == 0 && rsense.SW == 0) {
rsense.SW = current_color;
wait = true;
} else {
rsense.NW = current_color;
wait = true;
}
}
if (current_color != -1){
prev_color = current_color;
}
NumOut(0, LCD_LINE5, rsense.NE);
NumOut(0, LCD_LINE6, rsense.SE);
NumOut(0, LCD_LINE7, rsense.SW);
NumOut(0, LCD_LINE8, rsense.NW);
Wait(10);
}
ClearScreen();
/*until (get_color(csr) != 1){ // 90 degrees left
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
}*/
SysColorSensorRead(csr);
current_color = get_color(csr);
while (current_color != 1) { // back to original position
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
SysColorSensorRead(csr);
current_color = get_color(csr);
}
Off(OUT_AC);
return rsense;
}
/* Given the original heading and a new heading, turns the robot to reflect
the new heading
This subroutine is used after the robot knows its position
*/
sub change_direction(int c, ColorSensorReadType &csr){
////////////////////////////////////////////////////////////////////////////
// TO DO:
//
// Implement each of the turns specified below.
//
////////////////////////////////////////////////////////////////////////////
SysColorSensorRead(csr);
int dest_color = get_color(csr);
int prev_color = dest_color;
int curr_color = dest_color;
// int change = new - orig;
int second_color;
// selects the appropriate turn based on the headings given
switch (c){
case 3: //turn right 45 degrees
while (curr_color == dest_color){ // color changes
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
break;
case -1: //turn right 90 degrees
// color changes twice
while (curr_color == dest_color){ // color changes
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
SysColorSensorRead(csr);
second_color = get_color(csr);
curr_color = second_color;
NumOut(0, LCD_LINE1, second_color);
NumOut(0, LCD_LINE2, curr_color);
while (curr_color != 1){ // color changes
TextOut(0, LCD_LINE5, "IN LOOP");
OnFwd(OUT_A, ROTATIONA);
OnRev(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
break;
case -3: //turn left 45 degrees
while (curr_color == dest_color){ // color changes
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
break;
case 1: //turn left 90 degrees
// color changes twice
while (curr_color == dest_color){ // color changes
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
SysColorSensorRead(csr);
second_color = get_color(csr);
curr_color = second_color;
NumOut(0, LCD_LINE1, second_color);
NumOut(0, LCD_LINE2, curr_color);
while (curr_color != 1){ // color changes
TextOut(0, LCD_LINE5, "IN LOOP");
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
SysColorSensorRead(csr);
curr_color = get_color(csr);
if (curr_color == -1 && prev_color != -1){
curr_color = prev_color;
}
if (curr_color != -1){
prev_color = curr_color;
}
}
break;
case 2:
case -2:
// 180 degree turn
/*
until (get_color(csr) != dest_color){ // color changes
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
}
int second_color = get_color(csr);
until (get_color(csr) != second_color){ // color changes twice
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
}
int third_color = get_color(csr);
until (get_color(csr) != second_color){ // color changes three times
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
}int forth_color = get_color(csr);
until (get_color(csr) != second_color){ // color changes four times
OnRev(OUT_A, ROTATIONA);
OnFwd(OUT_C, SPEED);
}*/
break;
}
}
/* Given a start coordinate, finish coordinate, and a heading, this function
moves the robot from the start to finish.
The coordinates given are in terms of index in the map array. The x and y
coordinates need to be extracted from that index.
After the robot has finished moving, the current heading of the robot is
returned.
This function is used after the robot knows it's own position.
*/
int move_to(int s, int f, int h, ColorSensorReadType &csr){
int sx, sy, fx, fy, tx, ty;
int heading;
// extract coordinates from given information
sx = s/GHEIGHT;
sy = s%GHEIGHT;
fx = f/GHEIGHT;
fy = f%GHEIGHT;
/* calculate how far to go in manhatten distances */
tx = fx - sx;
ty = fy - sy;
heading = h;
///////////////////////////////////////////////////////////////////////////
// TO DO:
//
// Move the robot from the starting point described by (sx, sy) to (fx, fy)
// Be sure to keep track of the direction the robot is facing when it
// makes turns.
//
////////////////////////////////////////////////////////////////////////////
/*
move to changes direction once, calls move robot, call move to again
*/
while(tx > 0 || ty > 0){
if (heading == NORTH){
if (tx > 0){
change_direction(-1, csr); //right 90 degrees
move_robot(csr);
heading = EAST;
}else if (tx < 0){
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = WEST;
}else if (ty > 0){
move_robot(csr);
}else if (ty < 0){
change_direction(1, csr); //left 90 degrees
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = SOUTH;
}
}else if (heading == EAST){
if (tx > 0){
move_robot(csr);
}else if (tx < 0){
change_direction(1, csr); //left 90 degrees
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = WEST;
}else if (ty > 0){
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = NORTH;
}else if (ty < 0){
change_direction(-1, csr); //right 90 degrees
move_robot(csr);
heading = SOUTH;
}
}else if (heading == SOUTH){
if (tx > 0){
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = EAST;
}else if (tx < 0){
change_direction(-1, csr); //right 90 degrees
move_robot(csr);
heading = WEST;
}else if (ty > 0){
change_direction(1, csr); //left 90 degrees
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = NORTH;
}else if (ty < 0){
move_robot(csr);
}
}else if (heading == WEST){
if (tx > 0){
change_direction(-1, csr); //right 90 degrees
change_direction(-1, csr); //right 90 degrees
move_robot(csr);
heading = EAST;
}else if (tx < 0){
move_robot(csr);
}else if (ty > 0){
change_direction(-1, csr); //right 90 degrees
move_robot(csr);
heading = NORTH;
}else if (ty < 0){
change_direction(1, csr); //left 90 degrees
move_robot(csr);
heading = SOUTH;
}
}
}
return heading;
}
/* This function takes a particle and returns an updated particle after moving
To "move" a particle is to move it forward in the direction of the particle.
We move these particles in the same way we would move the robot.
Upon reaching a boundary, the particle is turned 180 degrees around.
*/
particle move(particle p) {
///////////////////////////////////////////////////////////////////////
// TO DO:
//
// Move particle p forward one intersection in the direction it is
// facing.
// Similar to the robot's behaviour, if a particle reaches the end of
// the map, it turns in the opposite direction and returns to the
// previous intersection it was at.
//
///////////////////////////////////////////////////////////////////////
if(p.theta == NORTH) {
if(p.y != 4) {
p.y = p.y + 1;
} else {
// prob doubles
}
} else if(p.theta == EAST) {
if(p.x != 2) {
p.x = p.x + 1;
} else {
// prob doubles
}
} else if(p.theta == SOUTH) {
if(p.y != 0) {
p.y = p.y - 1;
} else {
// prob doubles
}
} else if(p.theta == WEST) {
if(p.x != 0) {
p.x = p.x + 1;
} else {
// prob doubles
}
}
return p;
}
/* This function returns data on intersection surroundings for particles.
Based on the map data array, we want the colour of the square at the given x
and y coordinates in direction dir which is one of NE, SE, SW, NW.
Take note that the heading given will change the perceived directions.
For example, facing north, NE in terms of compass directions is northeast.
Facing south however, NE in terms of compass directions is southwest.
Because the robot does not know it's own heading, the particles must reflect
this by using sensing relative to heading.
*/
int senseParticle(intersection map[], int x, int y, int dir, int heading){
////////////////////////////////////////////////////////////////////////////
// TO DO:
//
// Implement the function which checks the surroundings of the particle at
// (x, y) facing the direction given by heading.
//
// The data you need to determine the particle's surroundings is given by
// the map data array.
//
// The integer returned is the
// colour of the square in the direction of dir which is one of NE, SE, SW,
// or NW.
// Keep in mind that the direction is relative to the heading as mentioned
// in the function description.
//
////////////////////////////////////////////////////////////////////////////
int color = 0;
if(x == 0 && y == 0){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[0].NE;
}else if(dir == NORTHWEST){
color = map[0].NW;
}else if(dir == SOUTHEAST){
color = map[0].SE;
}else if(dir == SOUTHWEST){
color = map[0].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[0].SE;
}else if(dir == NORTHWEST){
color = map[0].NE;
}else if(dir == SOUTHEAST){
color = map[0].SW;
}else if(dir == SOUTHWEST){
color = map[0].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[0].SW;
}else if(dir == NORTHWEST){
color = map[0].SE;
}else if(dir == SOUTHEAST){
color = map[0].NW;
}else if(dir == SOUTHWEST){
color = map[0].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[0].NW;
}else if(dir == NORTHWEST){
color = map[0].SW;
}else if(dir == SOUTHEAST){
color = map[0].NE;
}else if(dir == SOUTHWEST){
color = map[0].SE;
}
}
}else if(x == 1 && y == 0){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[5].NE;
}else if(dir == NORTHWEST){
color = map[5].NW;
}else if(dir == SOUTHEAST){
color = map[5].SE;
}else if(dir == SOUTHWEST){
color = map[5].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[5].SE;
}else if(dir == NORTHWEST){
color = map[5].NE;
}else if(dir == SOUTHEAST){
color = map[5].SW;
}else if(dir == SOUTHWEST){
color = map[5].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[5].SW;
}else if(dir == NORTHWEST){
color = map[5].SE;
}else if(dir == SOUTHEAST){
color = map[5].NW;
}else if(dir == SOUTHWEST){
color = map[5].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[5].NW;
}else if(dir == NORTHWEST){
color = map[5].SW;
}else if(dir == SOUTHEAST){
color = map[5].NE;
}else if(dir == SOUTHWEST){
color = map[5].SE;
}
}
}else if(x == 2 && y == 0){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[10].NE;
}else if(dir == NORTHWEST){
color = map[10].NW;
}else if(dir == SOUTHEAST){
color = map[10].SE;
}else if(dir == SOUTHWEST){
color = map[10].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[10].SE;
}else if(dir == NORTHWEST){
color = map[10].NE;
}else if(dir == SOUTHEAST){
color = map[10].SW;
}else if(dir == SOUTHWEST){
color = map[10].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[10].SW;
}else if(dir == NORTHWEST){
color = map[10].SE;
}else if(dir == SOUTHEAST){
color = map[10].NW;
}else if(dir == SOUTHWEST){
color = map[10].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[10].NW;
}else if(dir == NORTHWEST){
color = map[10].SW;
}else if(dir == SOUTHEAST){
color = map[10].NE;
}else if(dir == SOUTHWEST){
color = map[10].SE;
}
}
}else if(x == 0 && y == 1){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[1].NE;
}else if(dir == NORTHWEST){
color = map[1].NW;
}else if(dir == SOUTHEAST){
color = map[1].SE;
}else if(dir == SOUTHWEST){
color = map[1].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[1].SE;
}else if(dir == NORTHWEST){
color = map[1].NE;
}else if(dir == SOUTHEAST){
color = map[1].SW;
}else if(dir == SOUTHWEST){
color = map[1].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[1].SW;
}else if(dir == NORTHWEST){
color = map[1].SE;
}else if(dir == SOUTHEAST){
color = map[1].NW;
}else if(dir == SOUTHWEST){
color = map[1].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[1].NW;
}else if(dir == NORTHWEST){
color = map[1].SW;
}else if(dir == SOUTHEAST){
color = map[1].NE;
}else if(dir == SOUTHWEST){
color = map[1].SE;
}
}
}else if(x == 1 && y == 1){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[6].NE;
}else if(dir == NORTHWEST){
color = map[6].NW;
}else if(dir == SOUTHEAST){
color = map[6].SE;
}else if(dir == SOUTHWEST){
color = map[6].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[6].SE;
}else if(dir == NORTHWEST){
color = map[6].NE;
}else if(dir == SOUTHEAST){
color = map[6].SW;
}else if(dir == SOUTHWEST){
color = map[6].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[6].SW;
}else if(dir == NORTHWEST){
color = map[6].SE;
}else if(dir == SOUTHEAST){
color = map[6].NW;
}else if(dir == SOUTHWEST){
color = map[6].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[6].NW;
}else if(dir == NORTHWEST){
color = map[6].SW;
}else if(dir == SOUTHEAST){
color = map[6].NE;
}else if(dir == SOUTHWEST){
color = map[6].SE;
}
}
}else if(x == 2 && y == 1){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[11].NE;
}else if(dir == NORTHWEST){
color = map[11].NW;
}else if(dir == SOUTHEAST){
color = map[11].SE;
}else if(dir == SOUTHWEST){
color = map[11].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[11].SE;
}else if(dir == NORTHWEST){
color = map[11].NE;
}else if(dir == SOUTHEAST){
color = map[11].SW;
}else if(dir == SOUTHWEST){
color = map[11].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[11].SW;
}else if(dir == NORTHWEST){
color = map[11].SE;
}else if(dir == SOUTHEAST){
color = map[11].NW;
}else if(dir == SOUTHWEST){
color = map[11].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[11].NW;
}else if(dir == NORTHWEST){
color = map[11].SW;
}else if(dir == SOUTHEAST){
color = map[11].NE;
}else if(dir == SOUTHWEST){
color = map[11].SE;
}
}
}else if(x == 0 && y == 2){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[2].NE;
}else if(dir == NORTHWEST){
color = map[2].NW;
}else if(dir == SOUTHEAST){
color = map[2].SE;
}else if(dir == SOUTHWEST){
color = map[2].SW;
}
}else if (heading == EAST){
if(dir == NORTHEAST){
color = map[2].SE;
}else if(dir == NORTHWEST){
color = map[2].NE;
}else if(dir == SOUTHEAST){
color = map[2].SW;
}else if(dir == SOUTHWEST){
color = map[2].NW;
}
}else if (heading == SOUTH){
if(dir == NORTHEAST){
color = map[2].SW;
}else if(dir == NORTHWEST){
color = map[2].SE;
}else if(dir == SOUTHEAST){
color = map[2].NW;
}else if(dir == SOUTHWEST){
color = map[2].NE;
}
}else if (heading == WEST){
if(dir == NORTHEAST){
color = map[2].NW;
}else if(dir == NORTHWEST){
color = map[2].SW;
}else if(dir == SOUTHEAST){
color = map[2].NE;
}else if(dir == SOUTHWEST){
color = map[2].SE;
}
}
}else if(x == 1 && y == 2){
if (heading == NORTH){
if(dir == NORTHEAST){
color = map[7].NE;
}else if(dir == NORTHWEST){
color = map[7].NW;
}else if(dir == SOUTHEAST){
color = map[7].SE;
}else if(dir == SOUTHWEST){