This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallFollow.cpp
119 lines (106 loc) · 2.49 KB
/
WallFollow.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
#include "Odometry.h"
#include "PID.h"
#include "MotorControl.h"
#include "Distance.h"
short state = 5;
short counter = 0;
uint16_t dtickscurr = 0;
uint16_t dticksgoal = 0;
bool rotating = false;
uint16_t aticksgoal = 0;
float shortestDistance = 0;
uint16_t shortestDistanceTicks = 0;
uint16_t ticksBeforeRotation = 0;
extern uint16_t sumOfTicks;
void rotation(int16_t a) {
ticksBeforeRotation = sumOfTicks;
aticksgoal = sumOfTicks + a;
rotating = true;
}
bool isDoneRotating() {
if (aticksgoal <= sumOfTicks) {
rotating = false;
return true;
} else {
VelocityData vD = getVelocityValues(0.2, -0.2);
setVelocityMotors(vD.left, vD.right);
return false;
}
}
void findWall() {
if (!rotating) {
rotation(floor(360 * 3.4));
}
if (shortestDistance > getMeanDistance()) {
shortestDistance = getMeanDistance();
shortestDistanceTicks = sumOfTicks - ticksBeforeRotation;
}
if (isDoneRotating()) {
Serial.println("IS DONE ROTATING");
state = 4;
}
}
void turnWall() {
if (!rotating) rotation(shortestDistanceTicks);
if (isDoneRotating()) {
state = 3;
}
}
void driveDist(float d) {
dticksgoal = sumOfTicks + d * 2195;
dtickscurr = sumOfTicks;
}
void wait() {
if (counter < 10) {
counter++;
VelocityData vD = getVelocityValues(0, 0);
setVelocityMotors(vD.left, vD.right);
} else {
counter = 0;
state = 5;
driveDist(0.2);
}
}
bool distDriven() {
if (dticksgoal <= sumOfTicks) {
return true;
} else {
VelocityData vD = getVelocityValues(0.15, 0.15);
setVelocityMotors(vD.left, vD.right);
return false;
}
}
void drive() {
VelocityData vD = getVelocityValues(0.3, 0.3);
setVelocityMotors(vD.left, vD.right);
activateMotors();
if (distDriven() || getMeanDistance() <= 100) {
state = 1;
}
}
void parallelize() {
if (!rotating)
rotation(90 * 2.944);
if (isDoneRotating()) {
state = 4;
}
}
void motorMain() {
switch (state) {
default:
case 1:
findWall();
break;
case 2:
turnWall();
break;
case 3:
parallelize();
break;
case 4:
wait();
break;
case 5:
drive();
}
}