-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRounds_1_and_2_with_matrix_formatted.ino
1437 lines (1306 loc) · 42.4 KB
/
Rounds_1_and_2_with_matrix_formatted.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
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
/**********************************************************************************************************************************************************************************************
*********************************************************SLAM-BOT'S BLOODHOUND PROJECT MOTION FUNCTION PROGRAM*********************************************************************************
********************************************************KEMP HARTZOG, CHRIS HARRIS, THOMAS MILLER, CORY LANDETA********************************************************************************
**********************************************************************************************************************************************************************************************/
/***************************************************************************LIBRARIES & SETUP*********************************************************************************************************/
#include <Wire.h> //i2c communication
#include <elapsedMillis.h>
//neo pixel library
#include <Adafruit_GFX.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>
#include <gamma.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//neomatrix setup
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, 6 /*pin*/,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE);
//color definitions
#define UNKNOWN_SQUARE 0
#define DEAD_END_COLOR 0x001F //blue
#define OBJECTIVE_COLOR 0xF800 //red
#define YELLOW_COLOR 0xFFE0 //yellow
// Kemp <-> Cory
#define UP 53 // blue <-> blue
#define DOWN 51 // green <-> green
#define LEFT 49 // yellow <-> yellow
#define RIGHT 47 // orange <-> orange
#define TRANSMIT 52 // brown <-> brown
#define RECEIVE 50 // red <-> red
//Ultrasonics library
#include <NewPing.h>
//Ultrasonic initialization
#define MAX_DISTANCE 280 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//Beta side trigger and echo
#define BravoR_TRIGGER 24
#define BravoR_ECHO 25
NewPing BravoR(BravoR_TRIGGER, BravoR_ECHO, MAX_DISTANCE);
#define BravoL_TRIGGER 26
#define BravoL_ECHO 27
NewPing BravoL(BravoL_TRIGGER, BravoL_ECHO, MAX_DISTANCE);
//Alpha side trigger and echo
#define AlphaR_TRIGGER 28
#define AlphaR_ECHO 29
NewPing AlphaR(AlphaR_TRIGGER, AlphaR_ECHO, MAX_DISTANCE);
#define AlphaL_TRIGGER 30
#define AlphaL_ECHO 31
NewPing AlphaL(AlphaL_TRIGGER, AlphaL_ECHO, MAX_DISTANCE);
//Delta side trigger and echo
#define DeltaR_TRIGGER 32
#define DeltaR_ECHO 33
NewPing DeltaR(DeltaR_TRIGGER, DeltaR_ECHO, MAX_DISTANCE);
#define DeltaL_TRIGGER 34
#define DeltaL_ECHO 35
NewPing DeltaL(DeltaL_TRIGGER, DeltaL_ECHO, MAX_DISTANCE);
//Charlie side triggera and echo
#define CharlieR_TRIGGER 36
#define CharlieR_ECHO 37
NewPing CharlieR(CharlieR_TRIGGER, CharlieR_ECHO, MAX_DISTANCE);
#define CharlieL_TRIGGER 22
#define CharlieL_ECHO 23
NewPing CharlieL(CharlieL_TRIGGER, CharlieL_ECHO, MAX_DISTANCE);
//pins for Teensy
#define ThomCall 5
#define ThomReady 7
#define teensy_hollow 4
#define teensy_wire A3
//definitions of map matrix designators
#define UNKNOWN_SQUARE 0
#define SOLID 1
#define OBJECTIVE 2
#define DEAD_END 3
#define START 4
#define CACHE 5
//motor shield library
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4.
Adafruit_DCMotor* M1Motor = AFMS.getMotor(1);
Adafruit_DCMotor* M2Motor = AFMS.getMotor(2);
Adafruit_DCMotor* M3Motor = AFMS.getMotor(3);
Adafruit_DCMotor* M4Motor = AFMS.getMotor(4);
//Setup variables for moving one block and initial speed
int oneBlock = 306;
int Ninety = 135;
int M1Speed = 50;
int M2Speed = 50;
int M3Speed = 50;
int M4Speed = 50;
//interrupt pins and motor ticks count
int M1tick = 0;
int M2tick = 0;
int M3tick = 0;
int M4tick = 0;
#define M1interrupt 19
#define M2interrupt 18
#define M3interrupt 3
#define M4interrupt 2
//servo library
#include <Servo.h>
//declare the grabber as servo
Servo grabber;
//elapsedMillis timers
elapsedMillis thomTeensyTimer;
//initialize grabber position to be used in code
#define grabber_pin 10
int grabber_start = 180;
//pin for green start button
#define Starter 39
//tic tracer threshold
#define tic_thresh 450
/*****************************************************************************INITIALIZATION**************************************************************************************************/
//initialize x,y cordinates
int x_pos = 0;
int y_pos = 0;
//arrays to use for determining under obstacle
int grid_map[7][7]; //map of detected grid locations
/**************************************************************************START OF PROGRAM***************************************************************************************************/
//SET THIS = 1 TO ENABLE SERIAL MONITOR TROUBLESHOOTING. 0 TO DISABLE.
bool verboseSerial = 1;
//Line dancing moves
void setup()
{
AFMS.begin(); // create with the default frequency 1.6KHz
#if defined(__AVR_ATtiny85__)
if (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
//end of trinket special code
//initalize grabber pin and set to start position
grabber.attach(grabber_pin);
grabber.write(40);
//Set speed that will be used
M1Motor->setSpeed(M1Speed);
M2Motor->setSpeed(M2Speed);
M3Motor->setSpeed(M3Speed);
M4Motor->setSpeed(M4Speed);
//setup for motor interrupt pins
pinMode(M1interrupt, INPUT);
pinMode(M2interrupt, INPUT);
pinMode(M3interrupt, INPUT);
pinMode(M4interrupt, INPUT);
//interrupt function attachment and call modes
attachInterrupt(digitalPinToInterrupt(M1interrupt), M1count, RISING);
attachInterrupt(digitalPinToInterrupt(M2interrupt), M2count, RISING);
attachInterrupt(digitalPinToInterrupt(M3interrupt), M3count, RISING);
attachInterrupt(digitalPinToInterrupt(M4interrupt), M4count, RISING);
//port setup for printing
Serial.begin(115200);
//set function and initial value of arduino <-> pi communication pins
pinMode(RECEIVE, INPUT);
pinMode(TRANSMIT, OUTPUT);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);
//set green button for input
pinMode(Starter, INPUT);
//set pins to Teensy
pinMode(ThomCall, OUTPUT);
pinMode(ThomReady, INPUT);
pinMode(teensy_hollow, INPUT);
pinMode(teensy_wire, INPUT);
matrix.begin();
matrix.clear();
matrix.setBrightness(10);
grid_map[0][0] = START;
matrix.drawPixel(0, 0, YELLOW_COLOR);
matrix.show();
//loop that goes until green button is pressed
int starterread = digitalRead(Starter);
while (starterread != 1) {
if (verboseSerial == 1)
Serial.println("Waiting For Green Button");
delay(1);
starterread = digitalRead(Starter);
delay(10);
if (digitalRead(Starter) != starterread)
starterread = 0;
// Serial.println(starterread);
}
delay(300);
//warm up strecth
Start(); //Locate();
blockStatus();
if (verboseSerial == 1)
Serial.println("Start pixel drawn");
/**************************BEGIN GRID SEARCH************************************/
while (x_pos < 5 || y_pos < 5) { //ends upon arrival at F2 (row 5, col 5)
//odd column electric slide forward
if (x_pos % 2 == 1 && y_pos < 5) {
Go_to(x_pos, y_pos + 1);
Calibrate(); //Locate();
blockStatus();
}
//top of odd column, slide to the right
else if (x_pos % 2 == 1 && y_pos == 5) {
Go_to(x_pos + 1, y_pos);
Calibrate(); //Locate();
blockStatus();
}
//even column, electric slide backward
else if (x_pos % 2 == 0 && y_pos > 1) {
Go_to(x_pos, y_pos - 1);
Calibrate(); //Locate();
blockStatus();
}
//bottom of even column, slide to the right
else if (x_pos % 2 == 0 && y_pos == 1) {
Go_to(x_pos + 1, y_pos);
Calibrate(); //Locate();
blockStatus();
}
// if (verboseSerial == 1) {
// Serial.print("(X,Y) and Value:");
// Serial.print(x_pos);
// Serial.print(" ");
// Serial.print(y_pos);
// Serial.print(" ");
// Serial.println(grid_map[x_pos][y_pos]);
// }
}
/*******************************END GRID SEARCH***************************/
/*************************FIND STATUS OF EDGE SQUARES*********************/
for (int x = 1; x <= 5; x++) {
if (grid_map[x][1] == SOLID) {
grid_map[x][0] = SOLID;
Serial.print(x);
Serial.println(" 0: SOLID");
}
if (grid_map[x][1] == OBJECTIVE) {
int adjacentObj1 = 0;
if (grid_map[x - 1][1] == OBJECTIVE) {
adjacentObj1++;
}
if (grid_map[x + 1][1] == OBJECTIVE) {
adjacentObj1++;
}
if (grid_map[x][2] == OBJECTIVE) {
adjacentObj1++;
}
if (adjacentObj1 == 1) {
grid_map[x][0] = CACHE;
Serial.print(x);
Serial.println(" 0: CACHE");
matrix.drawPixel(x, 0, OBJECTIVE_COLOR);
}
else if (adjacentObj1 == 2) {
grid_map[x][0] = SOLID;
Serial.print(x);
Serial.println(" 0: SOLID BUT NEXT TO OT");
}
if (grid_map[x][5] == SOLID) {
grid_map[x][6] = SOLID;
Serial.print(x);
Serial.println(" 0: SOLID");
}
}
if (grid_map[x][5] == OBJECTIVE) {
int adjacentObj5 = 0;
if (grid_map[x - 1][5] == OBJECTIVE) {
adjacentObj5++;
}
if (grid_map[x + 1][5] == OBJECTIVE) {
adjacentObj5++;
}
if (grid_map[x][4] == OBJECTIVE) {
adjacentObj5++;
}
if (adjacentObj5 == 1) {
grid_map[x][6] = CACHE;
Serial.print(x);
Serial.println(" 6: CACHE");
matrix.drawPixel(x, 6, OBJECTIVE_COLOR);
}
else if (adjacentObj5 == 2) {
grid_map[x][6] = SOLID;
Serial.print(x);
Serial.println(" 6: SOLID BUT NEXT TO OT");
}
}
}
for (int y = 1; y <= 5; y++) {
if (grid_map[1][y] == SOLID) {
grid_map[0][y] = SOLID;
Serial.print("0 ");
Serial.print(y);
Serial.println(": SOLID");
}
if (grid_map[1][y] == OBJECTIVE) {
int adjacentObj1 = 0;
if (grid_map[1][y - 1] == OBJECTIVE) {
adjacentObj1++;
}
if (grid_map[1][y + 1] == OBJECTIVE) {
adjacentObj1++;
}
if (grid_map[2][y] == OBJECTIVE) {
adjacentObj1++;
}
if (adjacentObj1 == 1) {
grid_map[0][y] = CACHE;
Serial.print("0 ");
Serial.print(y);
Serial.println(": CACHE");
matrix.drawPixel(0, y, OBJECTIVE_COLOR);
}
else if (adjacentObj1 == 2) {
grid_map[0][y] = SOLID;
Serial.print("0 ");
Serial.print(y);
Serial.println(": SOLID BUT NEXT TO OT");
}
}
if (grid_map[5][y] == OBJECTIVE) {
int adjacentObj5 = 0;
if (grid_map[5][y - 1] == OBJECTIVE) {
adjacentObj5++;
}
if (grid_map[5][y + 1] == OBJECTIVE) {
adjacentObj5++;
}
if (grid_map[4][y] == OBJECTIVE) {
adjacentObj5++;
}
if (adjacentObj5 == 1) {
grid_map[6][y] = CACHE;
Serial.print("6 ");
Serial.print(y);
Serial.println(": CACHE");
matrix.drawPixel(6, y, OBJECTIVE_COLOR);
}
else if (adjacentObj5 == 2) {
grid_map[6][y] = SOLID;
Serial.print("6 ");
Serial.print(y);
Serial.println(": SOLID BUT NEXT TO OT");
}
}
}
matrix.show();
/******************************GO TO A TUNNEL END**************************/
for (int y = 0; y < 7; y++) {
for (int x = 0; x < 7; x++) {
if (grid_map[x][y] == CACHE) {
if (x == 0) {
Go_to(x + 1, y);
Camera(x, y);
Cory();
}
if (x == 6) {
Go_to(x - 1, y);
Camera(x, y);
}
if (y == 0) {
Go_to(x, y + 1);
Camera(x, y);
}
if (y == 6) {
Go_to(x, y - 1);
Camera(x, y);
}
}
}
}
/******************************LED MATRIX OUTPUT**************************/
// for (int y_pos = 0 ; y_pos<7 ; y_pos++) {
// for (int x_pos = 0 ; x_pos<7 ; x_pos++) {
// matrix.drawPixel(x_pos,y_pos,grid_map[x_pos][y_pos]);
// }
// }
//
//matrix.show();
//Congratulations ET Everythings unlimited with the new TMobile ONE plan
// Home();
}
/**********************************************************************START OF FUNCTIONS(DANCE MOVES)****************************************************************************************/
void blockStatus()
{
delay(200);
digitalWrite(ThomCall, HIGH);
while (!digitalRead(ThomReady)) {
delay(1);
}
while (digitalRead(ThomReady)) {
delay(1);
}
while (!digitalRead(ThomReady)) {
delay(1);
}
// while(!digitalRead(ThomReady)){
// delay(200);
// }
digitalWrite(ThomCall, LOW);
bool hollow = digitalRead(teensy_hollow);
int wire = digitalRead(teensy_wire);
Serial.println(wire);
if (wire == 1) {
grid_map[x_pos][y_pos] = OBJECTIVE;
matrix.drawPixel(x_pos, y_pos, OBJECTIVE_COLOR);
Serial.print("(");
Serial.print(x_pos);
Serial.print(",");
Serial.print(y_pos);
Serial.println("): OBJECTIVE");
}
else if (hollow == true) {
grid_map[x_pos][y_pos] = DEAD_END;
matrix.drawPixel(x_pos, y_pos, DEAD_END_COLOR);
Serial.print("(");
Serial.print(x_pos);
Serial.print(",");
Serial.print(y_pos);
Serial.println("): DEAD END");
}
else {
grid_map[x_pos][y_pos] = SOLID;
Serial.print("(");
Serial.print(x_pos);
Serial.print(",");
Serial.print(y_pos);
Serial.println("): SOLID");
}
matrix.show();
}
//start program, Move to (1,1)
void Start()
{
Right(oneBlock / 2);
align_Bravo();
Forward(oneBlock);
align_Delta();
Right(oneBlock / 2);
x_pos = 1;
y_pos = 1;
Calibrate();
}
//return to (0,0) from already known location
void Home()
{
Go_to(1, 5);
Calibrate();
Go_to(1, 1);
Calibrate();
Left(oneBlock / 2);
align_Delta();
Backward(oneBlock);
Left(oneBlock / 2);
x_pos = 0;
y_pos = 0;
Calibrate();
}
//function that will go to an x,y position based on your current x,y position
void Go_to(int x_finish, int y_finish)
{
int x_difference = x_finish - x_pos;
int y_difference = y_finish - y_pos;
while (x_difference != 0) {
if (x_difference > 0) {
Right(oneBlock);
}
else {
Left(oneBlock);
}
x_difference = x_finish - x_pos;
}
while (y_difference != 0) {
if (y_difference > 0) {
Forward(oneBlock);
}
else {
Backward(oneBlock);
}
y_difference = y_finish - y_pos;
}
}
//Move Right 12" so many Blocks times
void Right(int Blocks)
{
int i = 0;
M2tick = 0;
M4tick = 0; //encoder counts
M2Motor->run(FORWARD);
M4Motor->run(BACKWARD); //start motors moving
//loop for distance based on amount of encoder ticks
while (M2tick < Blocks && M4tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 200; i++) {
M2Motor->setSpeed(i);
M4Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M2tick); Serial.print(M4tick);
delayMicroseconds(350);
}
M2Motor->run(RELEASE);
M4Motor->run(RELEASE); // turn off motors
x_pos++;
delay(50);
}
//Move Left 12" so many Blocks times
void Left(int Blocks)
{
int i = 0;
M2tick = 0;
M4tick = 0; //encoder counts
M2Motor->run(BACKWARD);
M4Motor->run(FORWARD); //start motors moving
//loop for distance based on amount of encoder ticks
while (M2tick < Blocks && M4tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 200; i++) {
M2Motor->setSpeed(i);
M4Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M2tick); Serial.print(M4tick);
delayMicroseconds(350);
}
M2Motor->run(RELEASE);
M4Motor->run(RELEASE); //turn off motors
x_pos--;
delay(50);
}
//Move Backward 12" so many Blocks times
void Backward(int Blocks)
{
int i = 0;
M1tick = 0;
M3tick = 0; //encoder counts
M1Motor->run(BACKWARD);
M3Motor->run(FORWARD); //start motors moving
//loop for distance based on amount of encoder ticks
while (M1tick < Blocks && M3tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 200; i++) {
M1Motor->setSpeed(i);
M3Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M1tick); Serial.print(M3tick);
delayMicroseconds(350);
}
M1Motor->run(RELEASE);
M3Motor->run(RELEASE); //turn off motors
y_pos--;
delay(50);
}
//Move Forward 12" so many Blocks times
void Forward(int Blocks)
{
int i = 0;
M1tick = 0;
M3tick = 0; //encoder counts
M1Motor->run(FORWARD);
M3Motor->run(BACKWARD); //start motors moving
//loop for distance based on amount of encoder ticks
while (M1tick < Blocks && M3tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 200; i++) {
M1Motor->setSpeed(i);
M3Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M1tick); Serial.print(M3tick);
delayMicroseconds(350);
}
M1Motor->run(RELEASE);
M3Motor->run(RELEASE); //turn off motors
y_pos++;
delay(50);
}
//Rotate 90 degrees clockwise so many times
void turnCW(int Blocks)
{
int i = 0;
M1tick = 0;
M2tick = 0;
M3tick = 0;
M4tick = 0; //encoder counts
//start motors moving
M1Motor->run(FORWARD);
M2Motor->run(FORWARD);
M3Motor->run(FORWARD);
M4Motor->run(FORWARD);
while (M1tick < Blocks && M2tick < Blocks && M3tick < Blocks && M4tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 150; i++) {
M1Motor->setSpeed(i);
M3Motor->setSpeed(i);
M2Motor->setSpeed(i);
M4Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M1tick);
delayMicroseconds(175);
}
M1Motor->run(RELEASE);
M2Motor->run(RELEASE);
M3Motor->run(RELEASE);
M4Motor->run(RELEASE);
delay(50);
}
//Rotate 90 degrees counter-clockwise so many times
void turnCCW(int Blocks)
{
int i = 0;
M1tick = 0;
M2tick = 0;
M3tick = 0;
M4tick = 0; //encoder counts
//start motors moving
M1Motor->run(BACKWARD);
M2Motor->run(BACKWARD);
M3Motor->run(BACKWARD);
M4Motor->run(BACKWARD);
while (M1tick < Blocks && M2tick < Blocks && M3tick < Blocks && M4tick < Blocks) {
if (i == 0) {
//adjust speed for acceleration
for (i = 50; i < 150; i++) {
M1Motor->setSpeed(i);
M3Motor->setSpeed(i);
M2Motor->setSpeed(i);
M4Motor->setSpeed(i);
delayMicroseconds(100);
}
}
// Serial.print(M1tick);
delayMicroseconds(175);
}
M1Motor->run(RELEASE);
M2Motor->run(RELEASE);
M3Motor->run(RELEASE);
M4Motor->run(RELEASE);
delay(50);
}
//adjust offset from wall based on ultrasonic readings
void offset_Alpha(int multiple)
{
M1Motor->setSpeed(M1Speed);
M3Motor->setSpeed(M3Speed);
int goal = multiple * 1780 + 215; //goal distance based on multiple parameter from blocks between wall
unsigned int AlphaR_time = AlphaR.ping_median(5); // Send ping, get ping time in microseconds (uS).
unsigned int AlphaL_time = AlphaL.ping_median(5); // Send ping, get ping time in microseconds (uS).
//difference from goal and absolute value for error correction
int differenceL = AlphaL_time - goal;
int differenceR = AlphaR_time - goal;
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//loop for error correction
int gone = 0;
int old_movement;
int positive = 0;
int negative = 0;
while (differenceR > 50 && differenceL > 50) {
AlphaL_time = AlphaL.ping();
AlphaR_time = AlphaR.ping();
differenceL = AlphaL_time - goal;
differenceR = AlphaR_time - goal;
int movement = (differenceR + differenceL) / 2; //movement to give motion towards or away from wall
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//motion adjustment based on positive or negative movement to or from wall
if(movement > old_movement && positive == 1){
offset_Alpha(multiple);
break;
}
if(old_movement>movement && negative == 1){
offset_Alpha(multiple);
break;
}
if (movement > 0 && gone == 0) {
old_movement = movement;
positive=1;
gone = 1;
M1Motor->run(FORWARD);
M3Motor->run(BACKWARD);
}
if (movement < 0 && gone == 0) {
negative = 1;
gone = 1;
M1Motor->run(BACKWARD);
M3Motor->run(FORWARD);
}
}
M1Motor->run(RELEASE);
M3Motor->run(RELEASE); //turn off motors
delay(50);
}
//adjust offset from wall based on ultrasonic readings
void offset_Bravo(int multiple)
{
M2Motor->setSpeed(M2Speed);
M4Motor->setSpeed(M4Speed);
int goal = multiple * 1780 + 215; //goal distance based on multiple parameter from blocks between wall
unsigned int BravoR_time = BravoR.ping_median(5); // Send ping, get ping time in microseconds (uS).
unsigned int BravoL_time = BravoL.ping_median(5); // Send ping, get ping time in microseconds (uS).
//difference from goal and absolute value for error correction
int differenceL = BravoL_time - goal;
int differenceR = BravoR_time - goal;
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//loop for error correction
int gone = 0;
int old_movement;
int positive = 0;
int negative = 0;
while (differenceR > 50 && differenceL > 50) {
BravoL_time = BravoL.ping();
BravoR_time = BravoR.ping();
differenceL = BravoL_time - goal;
differenceR = BravoR_time - goal;
int movement = (differenceR + differenceL) / 2; //movement to give motion towards or away from wall
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//motion adjustment based on positive or negative movement to or from wall
if (movement > old_movement && positive == 1) {
offset_Bravo(multiple);
break;
}
if (old_movement>movement && negative == 1){
offset_Bravo(multiple);
break;
}
if (movement > 0 && gone == 0) {
old_movement = movement;
positive = 1;
gone = 1;
M2Motor->run(BACKWARD);
M4Motor->run(FORWARD);
}
if (movement < 0 && gone == 0) {
old_movement = movement;
negative = 1;
gone = 1;
M2Motor->run(FORWARD);
M4Motor->run(BACKWARD);
}
}
M2Motor->run(RELEASE);
M4Motor->run(RELEASE); //turn off motors
delay(50);
}
//adjust offset from wall based on ultrasonic readings
void offset_Charlie(int multiple)
{
M1Motor->setSpeed(M1Speed);
M3Motor->setSpeed(M3Speed);
int goal = multiple * 1780 + 215; //goal distance based on multiple parameter from blocks between wall
unsigned int CharlieR_time = CharlieR.ping_median(5); // Send ping, get ping time in microseconds (uS).
unsigned int CharlieL_time = CharlieL.ping_median(5); // Send ping, get ping time in microseconds (uS).
//difference from goal and absolute value for error correction
int differenceL = CharlieL_time - goal;
int differenceR = CharlieR_time - goal;
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//loop for error correction
int gone = 0;
int old_movement;
int positive = 0;
int negative = 0;
while (differenceR > 50 && differenceL > 50) {
CharlieL_time = CharlieL.ping();
CharlieR_time = CharlieR.ping();
differenceL = CharlieL_time - goal;
differenceR = CharlieR_time - goal;
int movement = (differenceR + differenceL) / 2; //movement to give motion towards or away from wall
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//motion adjustment based on positive or negative movement to or from wall
if (movement > old_movement && positive == 1) {
offset_Charlie(multiple);
break;
}
if (old_movement > movement && negative == 1){
offset_Charlie(multiple);
break;
}
if (movement > 0 && gone == 0) {
old_movement = movement;
positive = 1;
gone = 1;
M1Motor->run(BACKWARD);
M3Motor->run(FORWARD);
}
if (movement < 0 && gone == 0) {
old_movement = movement;
negative = 1;
gone = 1;
M1Motor->run(FORWARD);
M3Motor->run(BACKWARD);
}
}
M1Motor->run(RELEASE);
M3Motor->run(RELEASE); //turn off motors
delay(50);
}
//adjust offset from wall based on ultrasonic readings
void offset_Delta(int multiple)
{
M2Motor->setSpeed(M2Speed);
M4Motor->setSpeed(M4Speed);
int goal = multiple * 1780 + 215; //goal distance based on multiple parameter from blocks between wall
unsigned int DeltaR_time = DeltaR.ping_median(5); // Send ping, get ping time in microseconds (uS).
unsigned int DeltaL_time = DeltaL.ping_median(5); // Send ping, get ping time in microseconds (uS).
//difference from goal and absolute value for error correction
int differenceL = DeltaL_time - goal;
int differenceR = DeltaR_time - goal;
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//loop for error correction
int gone = 0;
int old_movement;
int positive = 0;
int negative = 0;
while (differenceR > 50 && differenceL > 50) {
DeltaL_time = DeltaL.ping();
DeltaR_time = DeltaR.ping();
differenceL = DeltaL_time - goal;
differenceR = DeltaR_time - goal;
int movement = (differenceR + differenceL) / 2; //movement to give motion towards or away from wall
differenceL = abs(differenceL);
differenceR = abs(differenceR);
//motion adjustment based on positive or negative movement to or from wall
if (movement > old_movement && positive == 1){
offset_Delta(multiple);
break;
}
if (old_movement > movement && negative == 1){
offset_Delta(multiple);
break;
}
if (movement > 0 && gone == 0) {
old_movement = movement;
positive = 1;
gone = 1;
M2Motor->run(FORWARD);
M4Motor->run(BACKWARD);
}
if (movement < 0 && gone == 0) {
old_movement = movement;
negative = 1;
gone = 1;
M2Motor->run(BACKWARD);
M4Motor->run(FORWARD);
}
}
M2Motor->run(RELEASE);
M4Motor->run(RELEASE); //turn off motors
delay(50);
}
//align when not parrallel with wall
void align_Alpha()
{
//set motor speed
M1Motor->setSpeed(M1Speed - 10);
M3Motor->setSpeed(M3Speed - 10);
M2Motor->setSpeed(M2Speed - 10);
M4Motor->setSpeed(M4Speed - 10);
//get time and difference between two sensors
unsigned int AlphaR_time = AlphaR.ping_median(5);
unsigned int AlphaL_time = AlphaL.ping_median(5);
int difference = AlphaR_time - AlphaL_time;
difference = abs(difference);
//loop that turns until parrallel with wall
int goal = 10;
int ccw = 0;
while (difference > goal) {
AlphaL_time = AlphaL.ping();
AlphaR_time = AlphaR.ping();
difference = AlphaR_time - AlphaL_time;
//when difference is positive turn cw, when negative turn ccw
if (difference > 0) {
if (ccw == 1) {
break;
}
M1Motor->run(FORWARD);
M2Motor->run(FORWARD);
M3Motor->run(FORWARD);
M4Motor->run(FORWARD);
}
if (difference < 0) {
ccw = 1;
M1Motor->run(BACKWARD);
M2Motor->run(BACKWARD);
M3Motor->run(BACKWARD);
M4Motor->run(BACKWARD);
}
difference = abs(difference);
}
//turn off motors
M1Motor->run(RELEASE);
M2Motor->run(RELEASE);
M3Motor->run(RELEASE);
M4Motor->run(RELEASE);
delay(50);
}