This repository has been archived by the owner on Aug 22, 2021. It is now read-only.
forked from AvishkaSandeepa/webots-Autonomous-Mobile-Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_controller.cpp
1127 lines (941 loc) · 46.1 KB
/
final_controller.cpp
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
/*
Authors : Team BRAND
Last Modified date : 30/07/2021
Team :
[Thalagala B.P. 180631J](/~https://github.com/bimalka98)
[Sandeepa H.K.C.A 180564F](/~https://github.com/AvishkaSandeepa)
[Hewavitharana D.R. 180241M](/~https://github.com/Hevidra)
[Nagasinghe K.R.Y. 180411K](/~https://github.com/Ravindu-Yasas-Nagasinghe)
[Kumarasinghe H.A.N.H. 180337M](/~https://github.com/nikeshi99)
Final executable code: /~https://github.com/AvishkaSandeepa/webots-Autonomous-Mobile-Robot
*/
//==============================================================================
//* Preamble *
//==============================================================================
//Adding required libraries
#include <webots/Robot.hpp>
#include <webots/Motor.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/PositionSensor.hpp>
#include <webots/Camera.hpp>
using namespace webots;
using namespace std;
// defining variables
#define TIME_STEP 16
#define MAX_SPEED 10
// start and end of the overall task
bool beginTask = false;
bool endTask = false;
double freelyForward = 4;
double baseSpeed = 4;
double lastErrorLF = 0;
double setPositionLF = 3500;
double IRsensorValues[10];
double leftWheelPSValue;
double rightWheelPSValue;
double turnValue = 8.5;
double rampAdditionalTurn = 0;
double additionalturnValue = 2;
double turnValueInitial = 8.5;
int count1 = 0;
//variables for wall following
float kpOfWF = 0.5;
float kdOfWF = 0.1;
double rightPrevWall = 0;
double leftPrevWall = 0;
double midPrevWall = 0;
double distanceToWall = 16;
// Variables related to take turns and wheels
float advancedBy = 0.7; // distanceToWall of free move when a junction is detected.
float forwardSpeed = 4; // free moving speed
float sharpturnSpeed = 4; // speed of taking turns
double leftWheelPrevSpeed;
double rightWheelPrevSpeed;
// Variables related to state of the task
int stage = 1;
int count2 = 0;
int detectingTurn = 3;
//Variables for pillar detecting
int state = 0; // after the circular area
float advancedonRamp = 1.5; // distance to move forward at the top of the ramp
int noOfPoles = 0;
bool flagPillar = false; // if a pillar is detected, make this flag true
int colorDiff = 1; // absolute difference between detected front and top colors
int wrongPillar = 0; // if the count of pillars is wrong, make this flag 1
int finishedcircle = 1;
bool rampOver = false;
double distanceToLeftPillars;
double distanceToRightPillars;
// variables for circular maze
int circular = 0; // Initially circular algorithm is not activated
bool first_exit = false;
bool second_exit = false;
bool third_exit = false;
float reverse = 3;
bool box_detected = false;
double distanceToBox = 50; // distance to the box in the unit of sharp ir measure
bool flag1_const_dist_to_box = false;
bool flag2_const_dist_to_box = false;
// variables for gate area
int gateCount = 0;
int gate = 0; // sub stages inside the gate area
// variables for color detection
string colors[4] = { "gray","red","green","blue" };
Camera* camera;
int getColorAt (int x, int y);
//==============================================================================
//* Defining custom functions *
//==============================================================================
//---------Reading the values of the sensors and convert to binary 1/0----------
void read () {
for ( int i = 0; i < 10; i++ ) {
if ( IRsensorValues[i] < 700 ) {
IRsensorValues[i] = 1;
} else {
IRsensorValues[i] = 0;
}
}
}
//--------------------------end of the read() function--------------------------
//-------------------------Function for PD calculation--------------------------
double PID_calc () {
double average = 0;
double sum = 0;
for ( int i = 0; i < 8; i++ ) {
average += IRsensorValues[i] * i * 1000;
sum += IRsensorValues[i];
}
double position = average / sum; // current position
double kp = 0.008;
double kd = 0.0002;
double e = position - setPositionLF; // deviation from the setPositionLF position
double p = kp * e;
double d = kd * ( e - lastErrorLF );
double offset = p + d;
lastErrorLF = e;
return offset;
}
//------------------------end of the PID_calc() function------------------------
//--------------------------Function for motor driving--------------------------
double Mdriver (double speed) {
if ( speed > 0 ) {
if ( speed > MAX_SPEED ) {
speed = MAX_SPEED;
}
} else {
if ( speed < -MAX_SPEED ) {
speed = -MAX_SPEED;
}
}
return speed;
}
//---------------------end of the driver() function----------------------------
// function for getting color
int getColorAt (int x, int y) {
// x,y specify the point of color extraction
const unsigned char* image = camera->getImage ();
int image_width = camera->getWidth ();
int r = camera->imageGetRed (image, image_width, x, y);
int g = camera->imageGetGreen (image, image_width, x, y);
int b = camera->imageGetBlue (image, image_width, x, y);
int color;
if ( r > g && r > b ) color = 1;
if ( g > r && g > b ) color = 2;
if ( b > g && b > r ) color = 3;
cout << "Detected color = " << colors[color] << '\n';
return color;
}
//==============================================================================
//* *
//* Main function for the robot *
//* *
//==============================================================================
int main (int argc, char** argv) {
Robot* robot = new Robot (); // initializing the robot object
// get a handler to the motors and setPositionLF target position to infinity
Motor* leftMotor = robot->getMotor ("left motor");
Motor* rightMotor = robot->getMotor ("right motor");
leftMotor->setPosition (INFINITY);
rightMotor->setPosition (INFINITY);
// defining the initial velocities of the wheels
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
//initializing the position sensors
PositionSensor* leftPs = robot->getPositionSensor ("left_ps");
leftPs->enable (TIME_STEP);
PositionSensor* rightPs = robot->getPositionSensor ("right_ps");
rightPs->enable (TIME_STEP);
// initializing ultrasound sensors
DistanceSensor* right_ultrasound = robot->getDistanceSensor ("right_ultrasound");
right_ultrasound->enable (TIME_STEP);
DistanceSensor* left_ultrasound = robot->getDistanceSensor ("left_ultrasound");
left_ultrasound->enable (TIME_STEP);
// initialize infrared sensors
char sensorNames[10][10] = {
"ir0", "ir1", "ir2", "ir3", "ir4","ir5", "ir6", "ir7", // for PID_calc()
"leftmost", "rightmost" // for junction detection
};
// enable the sensors to get measurements
DistanceSensor* ir[10];
for ( int i = 0; i < 10; i++ ) {
ir[i] = robot->getDistanceSensor (sensorNames[i]);
ir[i]->enable (TIME_STEP);
}
// initialize front SharpIR sensor
DistanceSensor* sharp_IR = robot->getDistanceSensor ("middle");
sharp_IR->enable (TIME_STEP);
// color detection camera initialization
camera = robot->getCamera ("color_sensor");
camera->enable (TIME_STEP);
//============================================================================
//* main loop *
//============================================================================
while ( robot->step (TIME_STEP) != -1 ) {
// read sensors outputs
for ( int i = 0; i < 10; i++ ) {
IRsensorValues[i] = ir[i]->getValue ();
}
read (); // call a function to get out put as binary values from the IR array
//read junction detection sensor values
double leftMostValue = IRsensorValues[8];
double rightMostValue = IRsensorValues[9];
//read position sensor values-
double leftPsVal = leftPs->getValue ();
double rightPsVal = rightPs->getValue ();
double distanceToRightWall = right_ultrasound->getValue ();
double distanceToLeftWall = left_ultrasound->getValue ();
// getting front SharpIR sensor reading values
double sharp_ir_value = sharp_IR->getValue ();
double sharp_ir = sharp_ir_value;
if ( sharp_ir_value < 600 ) { // Calibrate Front IR Sensor
sharp_ir_value = 1;
} else {
sharp_ir_value = 0;
}
//============================================================================
//* main stage machine *
//============================================================================
if ( stage == 1 ) {
if ( !beginTask ) {
if ( ( leftPsVal < freelyForward ) || ( rightPsVal < freelyForward ) ) { // Needs to calibrate
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
stage = 1;
beginTask = false;
cout << "Moving forward to identify the line..." << '\n';
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
beginTask = true;
stage = 1;
cout << "Identifed the line." << '\n';
}
} else if ( leftMostValue == 1 && rightMostValue == 0 ) {
if ( count2 > detectingTurn ) {
count2 = 0;
stage = 2;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
cout << "Left turn detected." << count1 << '\n';
count1 = 0;
} else {
leftMotor->setVelocity (10);
rightMotor->setVelocity (10);
count2 = count2 + 1;
//cout << "=========count2========= " << count2 << '\n';
}
} else if ( rightMostValue == 1 && leftMostValue == 0 ) {
if ( count2 > detectingTurn ) {
count2 = 0;
stage = 3;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
cout << "Right turn detected." << count1 << '\n';
count1 = 0;
} else {
leftMotor->setVelocity (10);
rightMotor->setVelocity (10);
count2 = count2 + 1;
//cout << "=========count2========= " << count2 << '\n';
}
} else if ( leftMostValue == 1 && rightMostValue == 1 ) {
if ( count2 > detectingTurn ) {
count2 = 0;
stage = 4;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
cout << " T junction detected." << count1 << '\n';
count1 = 0;
} else {
leftMotor->setVelocity (10);
rightMotor->setVelocity (10);
count2 = count2 + 1;
//cout << "=========count2========= " << count2 << '\n';
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
}
} else if ( IRsensorValues[0] == 0 && IRsensorValues[1] == 0 && IRsensorValues[2] == 0 && IRsensorValues[3] == 0 && IRsensorValues[4] == 0 && IRsensorValues[5] == 0 && IRsensorValues[6] == 0 && IRsensorValues[7] == 0 ) {
stage = 5;
} else if ( ( distanceToRightWall <= 15 || distanceToLeftWall <= 15 ) && wrongPillar == 0 ) {
stage = 20;
cout << "Initializing Wall following algorithm..." << '\n';
} else if ( IRsensorValues[2] == 1 && IRsensorValues[3] == 1 && IRsensorValues[4] == 1 && IRsensorValues[5] == 1 && rampOver == true && gateCount < 3 ) {
stage = 15;
cout << "Initializing Gate area algorithm..." << '\n';
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
} else {
count1 = 0;
double offset = PID_calc (); //get the offset by calling pre defined function
//setPositionLF motor speed values to minimize the error
double left = baseSpeed + offset;
double right = baseSpeed - offset;
//Call a function to map the above speds within its maximum & minimum speed
double leftSpeed = Mdriver (left);
double rightSpeed = Mdriver (right);
leftWheelPrevSpeed = leftSpeed;
rightWheelPrevSpeed = rightSpeed;
//pass the speeds to the motor for run
leftMotor->setVelocity (leftSpeed);
rightMotor->setVelocity (rightSpeed);
////print the sensor outputs from the IR array & current offset
//cout << "ir0 = " << IRsensorValues[0] << " ";
//cout << "ir1 = " << IRsensorValues[1] << " ";
//cout << "ir2 = " << IRsensorValues[2] << " ";
//cout << "ir3 = " << IRsensorValues[3] << " ";
//cout << "ir4 = " << IRsensorValues[4] << " ";
//cout << "ir5 = " << IRsensorValues[5] << " ";
//cout << "ir6 = " << IRsensorValues[6] << " ";
//cout << "ir7 = " << IRsensorValues[7] << '\n';
//cout << " offset : " << offset << '\n';
}
} else if ( stage == 2 ) {
if ( ( leftPsVal < leftWheelPSValue + advancedBy ) || ( rightPsVal < rightWheelPSValue + advancedBy ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
// advancing is over.
if ( wrongPillar == 1 or box_detected == true ) { // code to run to ignore left turn at top of ramp when pillar count is wrong and at circle.
if ( ( leftPsVal < leftWheelPSValue + 0.5 * freelyForward ) || ( rightPsVal < rightWheelPSValue + 0.5 * freelyForward ) ) {
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
wrongPillar = 0;
stage = 6;
if ( box_detected ) {
stage = 4;
state = 0;
circular = 5;
box_detected = false;
}
}
}
// taking the left turn
else {
// taking the left turn
if ( rightPsVal < rightWheelPSValue + advancedBy + turnValue ) {
cout << "Stopped then turn left..." << '\n';
leftMotor->setVelocity (0);
rightMotor->setVelocity (sharpturnSpeed);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = sharpturnSpeed;
} else {
if ( circular != 0 ) {
stage = 4;
state = 0; // for back to circular algo
} else {
stage = 1;
}
}
}
}
} else if ( stage == 3 ) {
if ( ( leftPsVal < leftWheelPSValue + advancedBy ) || ( rightPsVal < rightWheelPSValue + advancedBy ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
// advancing is over.
if ( wrongPillar == 1 ) { // Code to run at top of ramp to ignore right turn when pillar count is wrong.
if ( ( leftPsVal < leftWheelPSValue + freelyForward ) || ( rightPsVal < rightWheelPSValue + freelyForward ) ) {
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
wrongPillar = 0;
stage = 7;
}
}
// taking the left turn
else {
// takjing the left turn
if ( leftPsVal < leftWheelPSValue + advancedBy + turnValue ) {
cout << "Stopped then turn right..." << '\n';
leftMotor->setVelocity (sharpturnSpeed);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = sharpturnSpeed; rightWheelPrevSpeed = 0;
} else {
if ( circular != 0 ) {
stage = 4;
state = 0; // for back to circular algo
} else {
stage = 1;
}
}
}
}
} else if ( stage == 4 && state == 1 ) {
if ( ( leftPsVal < leftWheelPSValue + advancedBy ) || ( rightPsVal < rightWheelPSValue + advancedBy ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
if ( rightPsVal < rightWheelPSValue + advancedBy + turnValue ) {
cout << "Stopped then turn left..." << '\n';
leftMotor->setVelocity (0);
rightMotor->setVelocity (sharpturnSpeed);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = sharpturnSpeed;
} else {
if ( circular != 0 ) {
stage = 4;
state = 0; // for back to circular algo
} else {
stage = 1;
}
}
}
} else if ( stage == 4 && state == 2 ) {
if ( ( leftPsVal < leftWheelPSValue + advancedBy ) || ( rightPsVal < rightWheelPSValue + advancedBy ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
if ( leftPsVal < leftWheelPSValue + advancedBy + turnValue ) {
cout << "Stopped then turn right..." << '\n';
leftMotor->setVelocity (sharpturnSpeed);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = sharpturnSpeed; rightWheelPrevSpeed = 0;
} else {
if ( circular != 0 ) {
stage = 4;
state = 0; // for back to circular algo
} else {
stage = 1;
}
}
}
} else if ( gateCount == 2 && stage == 4 ) {
cout << "Task Done and Dusted... :-) " << '\n';
if ( ( leftPsVal < leftWheelPSValue + freelyForward ) || ( rightPsVal < rightWheelPSValue + freelyForward ) ) { // Needs to calibrate
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
endTask = false;
break;
}
}
//-----------------------------dotted line area-----------------------------
else if ( stage == 5 ) {
count1++;
if ( count1 < 8 ) { // Going forward when line is not detected.
leftMotor->setVelocity (leftWheelPrevSpeed);
rightMotor->setVelocity (rightWheelPrevSpeed);
stage = 1;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
stage = 1;
count1 = 0;
break;
}
//==============================================================================
//* Maze area algorithm *
//==============================================================================
} else if ( stage == 4 && state == 0 ) {
cout << "circular = " << circular << " stage = " << stage << " state = " << state << "\n";
if ( circular == 0 ) { // turnValue left
circular = 1;
state = 1;
} else if ( circular == 1 ) { // line follow & turn right
turnValue = turnValue + additionalturnValue;
stage = 1;
circular = 2;
} else if ( circular == 2 ) { // check the box availability
turnValue = turnValueInitial;
if ( sharp_ir_value == 1 ) { // If detected, go 22
cout << "Box detected." << "\n";
cout << " " << "\n";
circular = 22;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
} else { // If not detected, line follow
stage = 1;
state = 0;
circular = 12;
first_exit = true;
}
} else if ( circular == 22 ) { // go 10 cm backward
if ( ( leftPsVal < leftWheelPSValue + 9 ) || ( rightPsVal > rightWheelPSValue - 9 ) ) {
leftMotor->setVelocity (8);
rightMotor->setVelocity (-8);
cout << "Taking 180 turn..." << '\n';
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
stage = 4;
state = 0;
circular = 23;
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
}
} else if ( circular == 23 ) {
if ( ( leftPsVal > leftWheelPSValue - 1.2 * reverse ) || ( rightPsVal > rightWheelPSValue - 1.2 * reverse ) ) { // Needs to calibrate
leftMotor->setVelocity (-8);
rightMotor->setVelocity (-8);
} else {
turnValue = turnValue + additionalturnValue;
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
circular = 3;
state = 2;
stage = 1;
box_detected = true; // turn left
}
} else if ( circular == 3 ) { // line follow and turn left
turnValue = turnValueInitial;
stage = 1;
circular = 4;
} else if ( circular == 4 ) { // turnValue right
stage = 3;
circular = 5;
} else if ( circular == 5 ) { // line follow & turn right
stage = 1;
circular = 6;
} else if ( circular == 6 ) { // line follow & turn right
turnValue = turnValue + 0.5 * additionalturnValue;
stage = 1;
circular = 7;
state = 2;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
} else if ( circular == 7 ) { // Go 10 cm straight
turnValue = turnValueInitial;
if ( sharp_ir > distanceToBox && !flag2_const_dist_to_box ) { // Needs to calibrate
cout << "Moving forward..." << '\n';
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
flag1_const_dist_to_box = true;
} else if ( sharp_ir < distanceToBox && !flag1_const_dist_to_box ) {
cout << "Moving backward..." << '\n';
leftMotor->setVelocity (-8);
rightMotor->setVelocity (-8);
flag2_const_dist_to_box = true;
} else {
flag1_const_dist_to_box = false;
flag2_const_dist_to_box = false;
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
//------------------------------------------------
// Color detection Algorithm
colorDiff = abs (getColorAt (32, 10) - getColorAt (32, 50));
if ( colorDiff == 0 ) colorDiff = 2;
cout << "Color Difference = " << colorDiff << '\n';
//------------------------------------------------
circular = 8;
stage = 4;
state = 0;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
}
} else if ( circular == 8 ) { // turn 180 degree
if ( ( leftPsVal < leftWheelPSValue + 8.5 ) || ( rightPsVal > rightWheelPSValue - 8.5 ) ) {
leftMotor->setVelocity (8);
rightMotor->setVelocity (-8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
if ( first_exit == false ) {
circular = 9; // previously in case 1 this was 9
stage = 4;
state = 0;
first_exit = true;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
} else {
if ( second_exit == false ) {
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
circular = 13;
stage = 4;
state = 0;
second_exit = true;
} else {
if ( third_exit == false ) {
circular = 17;
stage = 4;
state = 0;
third_exit = true;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
} else {
circular = 19;
stage = 4;
state = 0;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
}
}
}
}
} else if ( circular == 9 ) { // line follow & turn left
if ( ( leftPsVal > leftWheelPSValue - 1.7 * reverse ) || ( rightPsVal > rightWheelPSValue - 1.7 * reverse ) ) { // Needs to calibrate
leftMotor->setVelocity (-8);
rightMotor->setVelocity (-8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
circular = 10;
state = 1;
stage = 1;
}
} else if ( circular == 10 ) { // line follow & turn left
turnValue = turnValue + additionalturnValue;
stage = 1;
circular = 11;
state = 1;
} else if ( circular == 21 ) { // line follow & turn left
stage = 1;
circular = 0;
state = 3; //End of circular(3)
} else if ( circular == 11 ) { // line follow & turn right
turnValue = turnValueInitial;
stage = 1;
circular = 0;
state = 3; //End of circular(1)
cout << "End of the maze." << '\n';
} else if ( circular == 12 ) { // check the box availability
if ( sharp_ir_value == 1 ) { // If detected, go 10 cm straight
stage = 4;
turnValue = turnValue + 0.5 * additionalturnValue;
circular = 7;
state = 0;
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
} else {
turnValue = turnValue + 0.5 * additionalturnValue; // If not detected, turn left
stage = 4;
state = 1;
circular = 16;
second_exit = true;
}
} else if ( circular == 13 ) { // go 20 cm straight
if ( ( leftPsVal < leftWheelPSValue + 3.2 ) || ( rightPsVal < rightWheelPSValue + 3.2 ) ) {
leftMotor->setVelocity (8);
rightMotor->setVelocity (8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
circular = 14;
stage = 4;
state = 0;
}
} else if ( circular == 14 ) { // line follow & turn right
stage = 1;
state = 2;
circular = 15;
} else if ( circular == 15 ) { // line follow & turnleft
stage = 1;
circular = 0;
state = 3; //End of circular(2)
cout << "End of the maze." << '\n';
} else if ( circular == 16 ) { // check the box availability
turnValue = turnValueInitial;
if ( sharp_ir_value == 1 ) { // If detected, go 10 cm straight
stage = 4;
turnValue = turnValue + 0.5 * additionalturnValue;
circular = 7;
state = 0;
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
} else { // If not detected, go circular 18
stage = 4;
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
circular = 18;
state = 0;
third_exit = true;
}
} else if ( circular == 17 ) { // line follow & turn right
if ( ( leftPsVal > leftWheelPSValue - 1.5 * reverse ) || ( rightPsVal > rightWheelPSValue - 1.5 * reverse ) ) { // Needs to calibrate
leftMotor->setVelocity (-8);
rightMotor->setVelocity (-8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
stage = 1;
circular = 14;
state = 2;
}
} else if ( circular == 18 ) { // turnValue 180 degree
if ( ( leftPsVal < leftWheelPSValue + 9 ) || ( rightPsVal > rightWheelPSValue - 9 ) ) { // Needs to calibrate
leftMotor->setVelocity (8);
rightMotor->setVelocity (-8);
cout << "Taking 180 turn..." << '\n';
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
stage = 4;
state = 0;
turnValue = turnValue + 0.5 * additionalturnValue;
circular = 7;
leftWheelPSValue = leftPsVal; rightWheelPSValue = rightPsVal;
}
} else if ( circular == 19 ) { // line follow & turn left
if ( ( leftPsVal > leftWheelPSValue - 1.2 * reverse ) || ( rightPsVal > rightWheelPSValue - 1.2 * reverse ) ) { // Needs to calibrate
leftMotor->setVelocity (-8);
rightMotor->setVelocity (-8);
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
stage = 1;
circular = 14;
state = 1;
}
}
}
//--------------------------------End of Circular algo--------------------------------------------
//==============================================================================
//* Wall following algorithm *
//==============================================================================
else if ( stage == 20 ) {
cout << "right = " << distanceToRightWall << " left = " << distanceToLeftWall << '\n';
double left_wall_error = distanceToLeftWall - 10;
double right_wall_error = distanceToRightWall - 10;
double left_motor_speed;
double right_motor_speed;
if ( distanceToRightWall < distanceToWall ) {
double wall_offset = kpOfWF * right_wall_error + kdOfWF * ( right_wall_error - rightPrevWall );
rightPrevWall = right_wall_error;
left_motor_speed = baseSpeed + wall_offset;
right_motor_speed = baseSpeed - wall_offset;
leftMotor->setVelocity (Mdriver (left_motor_speed));
rightMotor->setVelocity (Mdriver (right_motor_speed));
} else if ( distanceToLeftWall < distanceToWall ) {
double wall_offset = kpOfWF * left_wall_error + kdOfWF * ( left_wall_error - leftPrevWall );
leftPrevWall = left_wall_error;
left_motor_speed = baseSpeed - wall_offset;
right_motor_speed = baseSpeed + wall_offset;
leftMotor->setVelocity (Mdriver (left_motor_speed));
rightMotor->setVelocity (Mdriver (right_motor_speed));
} else if ( distanceToLeftWall < distanceToWall && distanceToRightWall < distanceToWall ) {
double wall_offset = kpOfWF * ( distanceToRightWall - distanceToLeftWall ) + kdOfWF * ( distanceToRightWall - distanceToLeftWall - midPrevWall );
midPrevWall = distanceToRightWall - distanceToLeftWall;
left_motor_speed = baseSpeed + wall_offset;
right_motor_speed = baseSpeed - wall_offset;
leftMotor->setVelocity (Mdriver (left_motor_speed));
rightMotor->setVelocity (Mdriver (right_motor_speed));
}
else {
stage = 1;
}
}
//==============================================================================
//* Ramp and Pole Detection algorithm *
//==============================================================================
else if ( stage == 4 && colorDiff == 2 && finishedcircle == 1 ) {
cout << "stage 4 color difference = 2" << '\n';
if ( ( leftPsVal < leftWheelPSValue + advancedonRamp ) || ( rightPsVal < rightWheelPSValue + advancedonRamp ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
if ( rightPsVal < rightWheelPSValue + advancedonRamp + turnValue + rampAdditionalTurn ) {
cout << "Stopped then turn left on top of ramp..." << '\n';
leftMotor->setVelocity (0);
rightMotor->setVelocity (sharpturnSpeed);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = sharpturnSpeed;
} else {
stage = 6;
}
}
} else if ( stage == 4 && colorDiff == 1 && finishedcircle == 1 ) {
cout << "stage 4 color difference = 1" << '\n';
if ( ( leftPsVal < leftWheelPSValue + advancedonRamp ) || ( rightPsVal < rightWheelPSValue + advancedonRamp ) ) {
leftMotor->setVelocity (forwardSpeed);
rightMotor->setVelocity (forwardSpeed);
//creating a memory to save wheels current speeds
leftWheelPrevSpeed = forwardSpeed; rightWheelPrevSpeed = forwardSpeed;
} else {
leftMotor->setVelocity (0);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = 0; rightWheelPrevSpeed = 0;
if ( leftPsVal < leftWheelPSValue + advancedonRamp + turnValue + rampAdditionalTurn ) {
cout << "Stopped then turn right on top of ramp..." << '\n';
leftMotor->setVelocity (sharpturnSpeed);
rightMotor->setVelocity (0);
leftWheelPrevSpeed = sharpturnSpeed; rightWheelPrevSpeed = 0;
} else {
stage = 7;
}
}
} else if ( stage == 6 ) {
double rampSpeed = 3; //Speed to travel on ramp area
cout << "stage 6 " << "No: of Poles = " << noOfPoles << "colorDiff = " << colorDiff << '\n';
if ( leftMostValue == 1 && rightMostValue == 0 && noOfPoles == colorDiff ) { // code for robot when poles are correctly found
stage = 2;
rampOver = true;
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
cout << "Left detected." << count1 << '\n';
count1 = 0;
} else if ( leftMostValue == 1 && rightMostValue == 0 && noOfPoles != colorDiff ) { // code for robot when poles are not correctly found
leftWheelPSValue = leftPsVal;
rightWheelPSValue = rightPsVal;
noOfPoles = 0;
cout << "Wrong turn." << count1 << '\n';
stage = 9;
} else {
//.........function for pillar detecting..........
distanceToRightPillars = right_ultrasound->getValue ();
distanceToLeftPillars = left_ultrasound->getValue ();
cout << "LeftSensorDistance- " << distanceToLeftPillars << " RightSensorDistance- " << distanceToRightPillars << endl;
if ( distanceToLeftPillars <= 15.0 && flagPillar == false ) { //This is done to avoid count of number of poles increasing when same pole is detected more than once.
noOfPoles += 1;
flagPillar = true;
} else if ( distanceToLeftPillars > 15.0 && flagPillar == true ) {
flagPillar = false;
} else {
count1 = 0;
double offset = PID_calc (); //get the offset by calling pre defined function
//---------------------setPositionLF motor speed values to minimize the error------------------------
double left = rampSpeed + offset;
double right = rampSpeed - offset;
//---call a function to map the above speds within its maximum & minimum speed---
double leftSpeed = Mdriver (left);
double rightSpeed = Mdriver (right);
leftWheelPrevSpeed = leftSpeed;
rightWheelPrevSpeed = rightSpeed;