-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotDriveCodeZRev15.ino
More file actions
4632 lines (4524 loc) · 152 KB
/
Copy pathRobotDriveCodeZRev15.ino
File metadata and controls
4632 lines (4524 loc) · 152 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
/*
7.2 Changes
-Adapted 7.1 changes to full battery
-Uncommented Color Checking
7.1 Changes
-Added New IR sensor functionality to the code
7 Changes:
Added FindPeg() to code and several color sensing functions outside main loop
5.9 Changes:
-TurnRight() now correctly handles undershoot.
-DriveLeftSense(), DriveRightSense(), and DriveFrontSense() have brakes.
-DriveRightNotLeft() now works correctly [DriveLeftNotRight() was never broken]
-Some superfluous delays in sensing were removed (important ones remain).
-Some superfluous checks from old ideas were removed for code speed and readability (no change to functionality).
-Specific serial printing commented out for code speed.
5.5 Changes:
-Changes to DriveRightSense() and DriveLeftSense() ?????
5.0 Changes:
-TurnLeft() and TurnRight() programmed to work with new IMU.
-TurnLeft() and TurnRight() now rely on two parameters. First parameter is speed (integer between 0 and 200) and second is target direction.
-Target direction is defined as EastFace, NorthFace, WestFace, or SouthFace. Robot starts oriented at EastFace.
4.0 Changes:
-RobotStart() function added to replace initializations. This function initializes the compass's reference value, kills all motors, and sets the initial position of the gripper to up.
-Compass reference value now calculated via polling to improve accuracy.
-TurnLeft() and TurnRight() functions added for 90 degree turns via compass. Currently poorly parameterized.
-Initial readings of sensors moved into DriveLeftSense() and DriveRightSense() to simplify calls to those functions.
3.5 Changes:
-None (Input changes here)
3.0 Changes:
-Put DriveLeftSense() and DriveRightSense() as pre-defined functions.
-Made turning while driving forward slightly faster.
-Right sensor pins defined.
-Cleaned up FrontIR sensor use to respond more quickly.
-Added in Howard's compass code, did appropriate integration work and cast all compass variables as floats.
-Defined function RobotPark() that causes the robot to stay parked indefinitely upon being called.
-Swapped Pin assignment of L/R Ultrasonics back and moved them to make room for other I/O on board
1.6 Changes:
-Added FrontIR sensor to code, if the sensor reads less than 27, the robot stops.
-Cleared commented code blocks from v1f.5
-Swapped Pin assignment of L/R side Ultrasonics
1.5 Changes:
-Serial1 printing of current state
-LED on indicator
1.4 Changes
-Added Serial1 Printing
*/
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <Average.h>
Average<byte> ave(15);
Adafruit_BNO055 bno = Adafruit_BNO055();
// These constants won't change. They're used to give names to the pins used;
const byte MotorB1 = 3;
const byte MotorB2 = 4;
const byte MotorA1 = 5;
const byte MotorA2 = 6;
const byte SideBLPulse = 10;
const byte SideBLEcho = 9;
const byte SideFLPulse = 8;
const byte SideFLEcho = 7;
const byte SideBRPulse = 14;
const byte SideBREcho = 15;
const byte SideFRPulse = 16;
const byte SideFREcho = 17;
const byte FrontLPulse = 22;
const byte FrontLEcho = 23;
const byte FrontRPulse = 20;
const byte FrontREcho = 21;
const byte S0 = 24;//pinB
const byte S1 = 25;//pinA
const byte S2 = 26;//pinE
const byte S3 = 27;//pinF
const byte LED = 13;//pinD
const byte taosOutPin = 28;//pinC
byte colorcounter = 0;
byte loopcontrol = 10;
//int IRcounter = 1; //Counter for checking IR
const byte Turf = 255;
const byte City = 200;
const byte up = 37 ;
const byte down = 165;
const byte openit = 63;
const byte closeit = 175;
const byte fwd = 150;
const byte tfwd = 200;
char report[80];
// These are the variables that will vary at runtime.
int FrontSideSense = 0;
int BackSideSense = 0;
int ExtraSense = 0;
int FrontIR = 0;
int GripperIR = 0;
float EastFace;
float WestFace;
float NorthFace;
float SouthFace;
float CurrentTurn;
bool PegColor = false;
bool RiverPeg = false;
//---------------------------servo setup**************************
Servo tilt; // create servo object to control a servo
Servo grip; // create servo object to control a servo
//------------------------------------
void setup() {
// Set pins to appropriate I/O modes.
TCS3200setup();
pinMode(MotorB1, OUTPUT);
pinMode(MotorB2, OUTPUT);
pinMode(MotorA1, OUTPUT);
pinMode(MotorA2, OUTPUT);
pinMode(SideFLPulse, OUTPUT);
pinMode(SideFLEcho, INPUT);
pinMode(SideBLPulse, OUTPUT);
pinMode(SideBLEcho, INPUT);
pinMode(SideFRPulse, OUTPUT);
pinMode(SideFREcho, INPUT);
pinMode(SideBRPulse, OUTPUT);
pinMode(SideBREcho, INPUT);
pinMode(FrontLPulse, OUTPUT);
pinMode(FrontLEcho, INPUT);
pinMode(FrontRPulse, OUTPUT);
pinMode(FrontREcho, INPUT);
pinMode(LED, OUTPUT); //For "ON" inidication from LED
//**************************************************************
tilt.attach(11); // attaches tilt servo to pin 11
grip.attach(12); // attaches grip servo to pin 12
Serial1.begin(9600);
Wire.begin();
if (!bno.begin())
{
Serial1.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while (1);
}
delay(1000);
bno.setExtCrystalUse(true);
}
void loop() {
// Motor 1 side is the positive side, Motor 2 side is the negative terminal. Set A2 and B1 high to turn clockwise; A1 and B2 high will turn counterclockwise.
// Motors operate with speeds varying from about 100 to 200.
RobotStart();
// Peg 1 Code
DriveFrontBlind(255, 35);
DriveLeftSenseIMUCity(fwd, 1, EastFace);
DriveBackBlind(fwd, 25);
delay(300);
TurnLeftBurst();
TurnLeft(150, NorthFace);
TurnStraight(190, NorthFace);
delay(300);
DriveFrontBlind(255, 35);
DriveFrontSense(120, 27);
delay(300);
TurnRightBurst();
TurnRight(145, EastFace);
TurnStraight(190, EastFace);
delay(300);
DriveFrontBlind(255, 35);
DriveLeftSenseIMUCity(fwd, 1, EastFace);
delay(200);
TurnStraight(190, EastFace + 5);
delay(200);
DriveFrontBlind(255, 35);
DriveRightNotLeftSenseIMU(fwd, EastFace);
DriveCenterSenseIMU(fwd, 12, EastFace);
TurnStraight(190, EastFace);
DriveFrontBlind(fwd, 450);
delay(300);
TurnStraight(190, EastFace);
FindPeg(City);
delay(300);
TurnStraight(190, EastFace);
delay(300);
DriveBackSense(fwd, 10);
DriveBackBlind(fwd, 200);
delay(300);
TurnRightBurst();
TurnRight(145, SouthFace);
delay(500);
DriveFrontSense(120, 15);
delay(300);
TurnRightBurst();
TurnRight(145, WestFace);
TurnStraight(190, WestFace);
delay(300);
DriveFrontBlind(255, 35);
DriveLeftSenseCloseIMU(fwd, 10, WestFace);
ReturnPeg();
// Peg 2 Code
delay(300);
DriveFrontBlind(255, 35);
DriveLeftSenseCloseIMU(fwd, 1, EastFace);
delay(300);
TurnLeftBurst();
TurnLeft(145, NorthFace);
TurnStraight(190, NorthFace);
delay(300);
DriveForwardPeg2(fwd);
DriveFrontBlind(fwd, 300);
delay(300);
TurnLeftBurst();
TurnLeft(145, WestFace);
TurnStraight(190, WestFace);
delay(300);
DriveFrontBlind(255, 35);
DriveFrontBlind(fwd, 500);
TurnStraight(190, WestFace);
DriveFrontBlind(fwd, 300);
DriveCenterSenseTime(fwd, 18, 15);
delay(300);
TurnStraight(190, WestFace);
delay(200);
FindPeg(City);
delay(300);
TurnStraight(190, WestFace);
delay(300);
DriveBackBlind(255, 35);
DriveBackSenseIMU(fwd + 10, 10, WestFace);
DriveBackBlind(fwd + 10, 50);
delay(300);
TurnLeftBurst();
TurnLeft(145, SouthFace);
TurnStraight(190, SouthFace);
delay(300);
DriveFrontSense(120, 27);
delay(300);
TurnRightBurst();
TurnRight(145, WestFace+2);
TurnStraight(190, WestFace+2);
delay(300);
DriveFrontBlind(255, 35);
DriveLeftSenseCloseIMU(fwd, 1, WestFace);
delay(300);
ReturnPeg();
delay(300);
// Peg 3 Code
DriveFrontBlind(255, 35);
DriveLeftSenseIMUCity(fwd, 1, EastFace);
delay(200);
TurnStraight(190, EastFace + 5);
delay(200);
DriveFrontBlind(255, 35);
DriveRightNotLeftSenseIMU(fwd, EastFace);
delay(200);
DriveLeftSenseCloseIMU(fwd, 1, EastFace);
delay(200);
TurnLeft(145, NorthFace);
TurnStraight(190, NorthFace);
delay(200);
DriveFrontBlind(255, 35);
DriveForwardPeg2RightSide(fwd);
DriveFrontBlind(fwd, 500);
DriveForwardPeg2RightSide(fwd);
delay(200);
TurnRight(145, NorthFace + 60);
delay(200);
DriveFrontIMUMixed(tfwd, fwd+20, 60000, NorthFace + 60, 26);
DriveBackBlind(tfwd, 280);
delay(200);
TurnLeft(145, NorthFace);
delay(200);
DriveFrontBlind(tfwd, 800);
delay(200);
DriveFrontSearchPeg4(200, 200);
ReturnPeg4();
// Peg 4 Code
DriveFrontBlind(255, 35);
DriveLeftSenseCloseIMU(fwd, 1, EastFace);
DriveBackBlind(fwd, 50);
delay(300);
TurnLeftBurst();
TurnLeft(145, NorthFace);
delay(300);
DriveForwardPeg2(fwd);
DriveFrontBlind(fwd, 350);
DriveForwardPeg2(fwd);
DriveFrontBlind(fwd, 445);
delay(300);
TurnLeft(145, WestFace+25);
TurnStraight(255, WestFace);
delay(300);
DriveFrontIMUMixed(tfwd, tfwd, 550, WestFace, 1);
delay(300);
DriveFrontSearch(fwd+20, tfwd);
delay(300);
GetPeg3();
DriveFrontIMUMixed(tfwd, tfwd, 1000, WestFace, 1);
TurnStraight(255, WestFace + 42);
DriveFrontSearchFinal(tfwd, 150);
delay(200);
TurnLeft(145, WestFace - 35);
delay(200);
FindPegTurf(Turf);
delay(200);
TurnRight(145, NorthFace);
delay(200);
DriveFrontSense(tfwd, 27);
delay(200);
TurnRight(145, EastFace);
delay(200);
DriveLeftSenseIMU(tfwd, 27, EastFace);
delay(200);
TurnRight(145, SouthFace);
delay(200);
LeftWallSpace(200);
DriveLeftSenseIMUSpecial(tfwd + 20, 26, SouthFace);
delay(200);
DriveBackBlind(255, 180);
TurnRight(145, SouthFace + 68);
delay(200);
DriveFrontBlind(tfwd, 450);
DriveFrontSpecial(tfwd);
delay(200);
TurnLeft(145, SouthFace);
TurnStraight(190, SouthFace);
delay(200);
DriveFrontSense(fwd, 27);
delay(200);
TurnRight(145, WestFace+2);
TurnStraight(190, WestFace+2);
delay(200);
DriveFrontBlind(255, 35);
DriveLeftSenseCloseIMU(fwd, 1, WestFace);
ReturnPegFinal();
RobotPark();
}
int ReadSonicSensor(byte Pulse, byte Echo) {
long duration;
digitalWrite(Pulse, LOW);
delayMicroseconds(2);
digitalWrite(Pulse, HIGH);
delayMicroseconds(10);
digitalWrite(Pulse, LOW);
duration = pulseIn(Echo, HIGH);
//Calculate the distance (in m m) based on the speed of sound.
return duration / 5.82;
}
float ReadCompass() {
imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
//Serial1.println(euler.x());
return euler.x();
}
void DriveRightSense(int Speed, int Distance) {
byte i = 0;
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor (SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
while ((FrontSideSense < 300 || BackSideSense < 300) && FrontIR > Distance) {
while (abs(FrontSideSense - BackSideSense) < 3 && FrontSideSense > 100 && FrontSideSense < 150 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Straight", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorB1, Speed + 0);
analogWrite(MotorA1, Speed);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
while (((FrontSideSense > BackSideSense) || FrontSideSense > 150) && FrontSideSense < 300 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Runaway", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorA1, 0);
analogWrite(MotorB1, Speed + 0);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
while (((FrontSideSense < BackSideSense) || FrontSideSense < 90) && FrontSideSense < 300 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Crash", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorA1, Speed);
analogWrite(MotorB1, 0);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
analogWrite (MotorB1, Speed + 0);
analogWrite (MotorA1, Speed);
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Broken", FrontSideSense, BackSideSense);
Serial1.println(report);
}
analogWrite(MotorA1, 0);
analogWrite(MotorB1, 0);
analogWrite(MotorA2, Speed/4);
analogWrite(MotorB2, Speed/4);
delay(25);
analogWrite(MotorA2, Speed/2);
analogWrite(MotorB2, Speed/2);
delay(50);
analogWrite(MotorA2, Speed*3/4);
analogWrite(MotorB2, Speed*3/4);
delay(100);
analogWrite(MotorA2, Speed);
analogWrite(MotorB2, Speed);
delay(100);
analogWrite(MotorA2, 0);
analogWrite(MotorB2, 0);
return;
}
void DriveLeftSense(int Speed, int Distance) {
byte i = 0;
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor (SideBLPulse, SideBLEcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Start", FrontSideSense, BackSideSense);
Serial1.println(report);
while ((FrontSideSense < 300 || BackSideSense < 300) && FrontIR > Distance) {
while (abs(FrontSideSense - BackSideSense) < 3 && FrontSideSense > 100 && FrontSideSense < 150 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Straight", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorB1, Speed + 0);
analogWrite(MotorA1, Speed);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
delay(2);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
while (((FrontSideSense > BackSideSense) || FrontSideSense > 150) && FrontSideSense < 300 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Runaway", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorB1, 0);
analogWrite(MotorA1, Speed);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
while (((FrontSideSense < BackSideSense) || FrontSideSense < 90) && FrontSideSense < 300 && FrontIR > Distance) {
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Crash", FrontSideSense, BackSideSense);
Serial1.println(report);
analogWrite(MotorB1, Speed + 0);
analogWrite(MotorA1, 0);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
analogWrite (MotorB1, Speed);
analogWrite (MotorA1, Speed);
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Broken", FrontSideSense, BackSideSense);
Serial1.println(report);
}
analogWrite(MotorA1, 0);
analogWrite(MotorB1, 0);
analogWrite(MotorA2, Speed/4);
analogWrite(MotorB2, Speed/4);
delay(25);
analogWrite(MotorA2, Speed/2);
analogWrite(MotorB2, Speed/2);
delay(50);
analogWrite(MotorA2, Speed*3/4);
analogWrite(MotorB2, Speed*3/4);
delay(100);
analogWrite(MotorA2, Speed);
analogWrite(MotorB2, Speed);
delay(100);
analogWrite(MotorA2, 0);
analogWrite(MotorB2, 0);
return;
}
void TurnLeft(int Speed, float TargetDirection) {
int TempSpeed = Speed;
boolean Overshoot = true;
float CurrentTurn = ReadCompass();
byte i = 10;
int j = 0;
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
float InitialTurn = CurrentTurn;
float TempTurn;
Serial1.println("Before Turning");
Serial1.println(CurrentTurn);
while (abs(CurrentTurn) > 30) {
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
analogWrite(MotorA1, Speed);
analogWrite(MotorB2, Speed);
i--;
j++;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 10;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 200) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(200);
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
InitialTurn = CurrentTurn;
Serial1.println("Undershoot?");
Serial1.println(CurrentTurn);
i = 10;
while (CurrentTurn > 5) {
Overshoot = false;
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
analogWrite(MotorA1, Speed);
analogWrite(MotorB2, Speed);
i--;
j++;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 10;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 200) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(1000);
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
InitialTurn = CurrentTurn;
i = 20;
Serial1.println("Overshoot?");
Serial1.println(CurrentTurn);
Speed = TempSpeed;
if (abs(CurrentTurn) > 25 || Overshoot == true) {
Speed = Speed * .6;
}
while (abs(CurrentTurn) > 5) {
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
// Serial1.println("Iterator");
// Serial1.println(i);
// Serial1.println("Speed");
// Serial1.println(Speed);
analogWrite(MotorA2, Speed);
analogWrite(MotorB1, Speed);
i--;
j++;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 20;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 180) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
analogWrite(MotorA2, 0);
analogWrite(MotorB1, 0);
delay(100);
}
void TurnRight(int Speed, float TargetDirection) {
int TempSpeed = Speed;
boolean Overshoot = true;
float CurrentTurn = ReadCompass();
byte i = 10;
int j = 0;
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
float InitialTurn = CurrentTurn;
float TempTurn;
Serial1.println("Before Turning");
Serial1.println(CurrentTurn);
while (abs(CurrentTurn) > 30) {
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
analogWrite(MotorA2, Speed);
analogWrite(MotorB1, Speed);
i--;
j++;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 10;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 200) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
j = 0;
analogWrite(MotorA2, 0);
analogWrite(MotorB1, 0);
delay(200);
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
InitialTurn = CurrentTurn;
Serial1.println("Undershoot?");
Serial1.println(CurrentTurn);
i = 10;
while (CurrentTurn < -5) {
Overshoot = false;
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
analogWrite(MotorA2, Speed);
analogWrite(MotorB1, Speed);
i--;
j++;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 10;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 200) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
j = 0;
analogWrite(MotorA2, 0);
analogWrite(MotorB1, 0);
delay(1000);
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
InitialTurn = CurrentTurn;
i = 20;
Serial1.println("Overshoot?");
Serial1.println(CurrentTurn);
Speed = TempSpeed;
if (abs(CurrentTurn) > 25 || Overshoot == true) {
Speed = Speed * .6;
}
while (abs(CurrentTurn) > 5) {
CurrentTurn = ReadCompass();
CurrentTurn = CurrentTurn + 360 - TargetDirection;
while (CurrentTurn > 180) {
CurrentTurn = CurrentTurn - 360;
}
analogWrite(MotorA1, Speed);
analogWrite(MotorB2, Speed);
i--;
TempTurn = CurrentTurn - InitialTurn;
if (i == 0 && abs(TempTurn) < 2) {
i = 20;
InitialTurn = CurrentTurn;
Speed = Speed * 1.2;
}
if (i == 180) {
InitialTurn = CurrentTurn;
i = 10;
}
if (j == 1750) {
j = 0;
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(500);
DriveFrontBlind(255, 150);
}
}
analogWrite(MotorA1, 0);
analogWrite(MotorB2, 0);
delay(100);
}
void RobotPark() {
while (1) {
analogWrite(MotorA1, 0);
analogWrite(MotorA2, 0);
analogWrite(MotorB1, 0);
analogWrite(MotorB2, 0);
digitalWrite(LED, LOW);
}
}
void DriveFrontSense(int Speed, int Distance) {
FrontIR = (480 - analogRead(A14)) / 25 + 27;
while (FrontIR > Distance) {
analogWrite (MotorB1, Speed);
analogWrite (MotorA1, Speed);
FrontIR = (480 - analogRead(A14)) / 25 + 27;
Serial1.println(FrontIR);
}
analogWrite(MotorA1, 0);
analogWrite(MotorB1, 0);
analogWrite(MotorA2, Speed/4);
analogWrite(MotorB2, Speed/4);
delay(25);
analogWrite(MotorA2, Speed/2);
analogWrite(MotorB2, Speed/2);
delay(50);
analogWrite(MotorA2, Speed*3/4);
analogWrite(MotorB2, Speed*3/4);
delay(100);
analogWrite(MotorA2, Speed);
analogWrite(MotorB2, Speed);
delay(100);
analogWrite(MotorA2, 0);
analogWrite(MotorB2, 0);
return;
}
void DriveFrontBlind(int Speed, int Time) {
int i = 0;
while (i < Time) {
analogWrite (MotorB1, Speed);
analogWrite (MotorA1, Speed);
delay(1);
i++;
}
analogWrite(MotorA1, 0);
analogWrite(MotorB1, 0);
return;
}
void DriveBackBlind(int Speed, int Time) {
int i = 0;
while (i < Time) {
analogWrite (MotorB2, Speed);
analogWrite (MotorA2, Speed);
delay(1);
i++;
}
analogWrite(MotorA2, 0);
analogWrite(MotorB2, 0);
return;
}
void RobotStart() {
delay(400);
EastFace = ReadCompass();
SouthFace = EastFace + 90;
if (SouthFace >= 360) {
SouthFace = SouthFace - 360;
}
WestFace = SouthFace + 90;
if (WestFace >= 360) {
WestFace = WestFace - 360;
}
NorthFace = WestFace + 90;
if (NorthFace >= 360) {
NorthFace = NorthFace - 360;
}
analogWrite(MotorA1, 0);
analogWrite(MotorA2, 0);
analogWrite(MotorB1, 0);
analogWrite(MotorB2, 0);
digitalWrite(LED, HIGH); //turn on LED
//************************************************************
delay(60); //Delay for Servo position consistency
tilt.write(37); //lift to ~90 degrees
delay(60);
while (1) {
FrontSideSense = ReadSonicSensor(SideFLPulse, SideFLEcho);
Serial1.println("Stuck in FL loop");
if (FrontSideSense > 10) {
break;
}
}
while (1) {
FrontSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
Serial1.println("Stuck in BL loop");
if (FrontSideSense > 10) {
break;
}
}
while (1) {
FrontSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
Serial1.println("Stuck in BR loop");
if (FrontSideSense > 10) {
break;
}
}
while (1) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
Serial1.println("Stuck in FR loop");
if (FrontSideSense > 10) {
break;
}
}
while (1) {
FrontSideSense = ReadSonicSensor(FrontLPulse, FrontLEcho);
Serial1.println("Stuck in L loop");
if (FrontSideSense > 10) {
break;
}
}
while (1) {
FrontSideSense = ReadSonicSensor(FrontRPulse, FrontREcho);
Serial1.println("Stuck in R loop");
if (FrontSideSense > 10) {
break;
}
}
}
void DriveRightNotLeftSense(int Speed) {
byte i = 0;
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor (SideBRPulse, SideBREcho);
delay(2);
int BackOppositeSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
while ((FrontSideSense < 300 || BackSideSense < 300) && BackOppositeSideSense > 250) {
while (abs(FrontSideSense - BackSideSense) < 3 && FrontSideSense > 100 && FrontSideSense < 150 && BackOppositeSideSense > 250) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
delay(2);
BackOppositeSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Opp: {%6d} Straight", FrontSideSense, BackSideSense, BackOppositeSideSense);
Serial1.println(report);
analogWrite(MotorB1, Speed + 0);
analogWrite(MotorA1, Speed);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
delay(2);
BackOppositeSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
while (((FrontSideSense > BackSideSense) || FrontSideSense > 150) && FrontSideSense < 300 && BackOppositeSideSense > 250) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
delay(2);
BackOppositeSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Opp: {%6d} Runaway", FrontSideSense, BackSideSense, BackOppositeSideSense);
Serial1.println(report);
analogWrite(MotorA1, 0);
analogWrite(MotorB1, Speed + 0);
i++;
if (i == 150) {
i = 0;
DriveFrontBlind(255, 60);
Serial1.println("Whooooooooooooooooooaaaaaaaaaaaaaaah!");
Speed = Speed * 1.1;
}
}
while (((FrontSideSense < BackSideSense) || FrontSideSense < 90) && FrontSideSense < 300 && BackOppositeSideSense > 250) {
FrontSideSense = ReadSonicSensor(SideFRPulse, SideFREcho);
delay(2);
BackSideSense = ReadSonicSensor(SideBRPulse, SideBREcho);
delay(2);
BackOppositeSideSense = ReadSonicSensor(SideBLPulse, SideBLEcho);
snprintf(report, sizeof(report), "Front: {%6d} Back: {%6d} Opp: {%6d} Crash", FrontSideSense, BackSideSense, BackOppositeSideSense);
Serial1.println(report);
analogWrite(MotorA1, Speed);
analogWrite(MotorB1, 0);
i++;