/
follow_line.c
234 lines (198 loc) · 4.89 KB
/
follow_line.c
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
#pragma config(Sensor, S2, light, sensorLightActive)
#pragma config(Sensor, S4, sonar, sensorSONAR)
#pragma config(Motor, motorB, right, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorC, left, tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "config.h"
#include "compass.c"
#include "light_sensor.c"
#include "types.h"
#include "error.c"
#define LEFT false
#define RIGHT true
#define SONAR_THRESHOLD 20
#define FOUND_ERROR -1
#define FOUND_CAN 1
#define FOUND_NODE 2
#define FOUND_HOTEL 4
#define FOUND_LEAF 8
bool followingEdge = true;
bool direction = LEFT;
int sweeps;
void edgeFollow(bool& running){
sweeps = 0;
if(direction == LEFT) {
motor[left] = -SPEED;
motor[right] = SPEED;
} else {
motor[left] = SPEED;
motor[right] = -SPEED;
}
bool wasDark = isDark();
do{
//Find border point
if(wasDark != isDark()) {
wasDark = isDark();
wait10Msec(3);
if(direction == LEFT) {
direction = RIGHT;
motor[left] = SPEED;
motor[right] = 0;
} else if( direction == RIGHT) {
direction = LEFT;
motor[right] = SPEED;
motor[left] = 0;
}
sweeps++;
abortTimeslice();
}
}while(running);
}
bool detectCan(void) {
return SensorValue[sonar] < SONAR_THRESHOLD;
}
bool detectNode(int forwards) {
return abs(angleDifference(currentDirection(), forwards)) > ANGLE_THRESHOLD;
}
int gatherForwardsData(void) {
wait10Msec(100);
PlaySound(soundBlip);
time100[T2] = 0;
int timeToWait = minDistance*10/(2*sweepSpeed) - 10;
int count = 0;
int total = 0;
int forwards = 0;
while(time100[T2] < timeToWait) {
//forwards = forwards + (angleDifference(currentDirection(), forwards))/++count;
count+= 2;
total += count;
//This formula give more weight to recent samples
forwards = forwards + (count * (angleDifference(currentDirection(), forwards))/total);
nxtDisplayCenteredTextLine(5, "%i", forwards);
wait10Msec(5);
}
PlaySound(soundBlip);
PlaySound(soundBlip);
return forwards;
}
task FollowEdge(){
if(!isDark()){
panic("Not on an edge!");
wait10Msec(1000);
StopAllTasks();
}
followingEdge = true;
edgeFollow(followingEdge);
}
bool checkIsLeaf(void) {
int direction = currentDirection();
while(!isDark()) {
if( abs(angleDifference(currentDirection(), direction)) > 170) {
return true;
}
}
return false;
}
void spinInDirection(void) {
if(direction == LEFT) {
motor[left] = -SPIN_SPEED;
motor[right] = SPIN_SPEED;
} else {
motor[left] = SPIN_SPEED;
motor[right] = -SPIN_SPEED;
}
}
void turnToAngle(int angle, int undershoot) {
if( angleDifference(angle, currentDirection()) < 0 ) {
direction = LEFT;
} else {
direction = RIGHT;
}
spinInDirection();
while(abs(angleDifference(currentDirection(), angle)) > undershoot){
wait1Msec(1);
}
motor[left] = 0;
motor[right] = 0;
}
/**
* Turns around until black is found
*/
void turnToLine(void) {
spinInDirection();
while(!isDark()) {
abortTimeslice();
}
int startDir = currentDirection();
while(isDark()) {
abortTimeslice();
}
motor[left] = 0;
motor[right] = 0;
int centre = startDir + (angleDifference(currentDirection(), startDir)/2);
nxtDisplayCenteredTextLine(6, "cen: %i", centre);
turnToAngle(centre, 0);
}
int FollowLineTilLeaf(void) {
StartTask(FollowEdge);
int forwards = gatherForwardsData();
for(;;) {
if(detectNode(forwards)) {
if(checkIsLeaf()) {
followingEdge = false;
StopTask(FollowEdge);
return FOUND_LEAF;
} else {
forwards = gatherForwardsData();
}
}
abortTimeslice();
}
}
/**
*/
void faceForward(int forwards){
if(angleDifference(currentDirection(), forwards) > 0){//too far to the right
motor[left] = -SPEED; //turn left
} else {
motor[right] = -SPEED; //turn right
}
while(abs(angleDifference(currentDirection(), forwards)) != 0){
wait1Msec(1);
}
}
/**
Moves forwards so that the centre of rotation is approximately where the light sensor was.
*/
void moveToSensor() {
int timeToWait = (sensorToAxle * 1000)/cruiseSpeed;
time1[T3] = 0;
motor[left] = SPEED;
motor[right] = SPEED;
while(time1[T3] < timeToWait) {
wait1Msec(1);
};
motor[left] = 0;
motor[right] = 0;
}
int FollowSegmentTilEnd(Edge &edge) {
StartTask(FollowEdge);
int forwards = gatherForwardsData();
edge.angle = forwards;
int found = FOUND_ERROR;
while(found == FOUND_ERROR) {
if(detectCan()) {
found = FOUND_CAN;
}
if(detectNode(forwards)) {
found = FOUND_NODE;
}
//abortTimeslice();
}
followingEdge = false;
StopTask(FollowEdge);
edge.length = sweeps;
faceForward(forwards);
moveToSensor();
return found;
}