-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonomus v1.ino
266 lines (238 loc) · 4.73 KB
/
autonomus v1.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
#include "Ultrasonic.h"
#include <AFMotor.h>
Ultrasonic sensorBack(A0, A1);
Ultrasonic sensorRight(A0, A2);
Ultrasonic sensorLeft(A0, A3);
Ultrasonic sensorFront(A0, A4);
AF_DCMotor motorFront(1, MOTOR12_64KHZ);
AF_DCMotor motorBack(2, MOTOR12_64KHZ);
AF_DCMotor wheel(3, MOTOR12_64KHZ);
const int NOT_ESCAPING = 0;
const int ESCAPING_LEFT = 1;
const int ESCAPING_RIGHT = 2;
const int ESCAPING_FRONT = 3;
const int ESCAPING_BACK = 4;
const int ESCAPING_REVERSE_LEFT = 5;
const int ESCAPING_REVERSE_RIGHT = 6;
int escaping = NOT_ESCAPING;
const int FAR_AWAY_THRESHOLD = 50;
const int FAR_THRESHOLD = 20;
const bool FAKE_MOTOR = false;
const int MAX_SPEED = 200;
int delaySensor = 0;
int distanceFront = 0, distanceBack = 0, distanceRight = 0, distanceLeft = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(50);
probeDistances();
//⇦⇧⇨⇩
//↰↱
//⇅
steerStraight();
stop();
if(escaping == NOT_ESCAPING)
{
if(isFarAway(distanceFront))
{
Serial.println("⇧");
goFront();
if(!isFar(distanceLeft) && isFar(distanceRight)) {
Serial.println("↱");
steerRight();
}
if(isFar(distanceLeft) && !isFar(distanceRight)) {
Serial.println("↰");
steerLeft();
}
}
else if(isFarAway(distanceLeft) || isFarAway(distanceRight))
{
if(compareDistance(distanceLeft, distanceRight) > 0)
{
escaping = ESCAPING_LEFT;
}
else
{
escaping = ESCAPING_RIGHT;
}
}
else if(isFarAway(distanceBack))
{
escaping = ESCAPING_BACK;
}
else
{
Serial.println("please don't move");
stop();
}
}
if(escaping != NOT_ESCAPING)
{
Serial.print("escape to : ");
if(escaping == ESCAPING_LEFT) Serial.println("⇦");
if(escaping == ESCAPING_RIGHT) Serial.println("⇨");
if(escaping == ESCAPING_REVERSE_LEFT) Serial.println("⇅⇦");
if(escaping == ESCAPING_REVERSE_RIGHT) Serial.println("⇅⇨");
if(escaping == ESCAPING_FRONT) Serial.println("⇧");
if(escaping == ESCAPING_BACK) Serial.println("⇩");
}
if(escaping == ESCAPING_LEFT)
{
if(isFarAway(distanceFront))
{
Serial.println("stop escaping to left");
escaping = NOT_ESCAPING;
}
else if(isFar(distanceFront))
{
Serial.println("⇦");
goFront();
steerLeft();
}
else
{
//can't go any further, reverse escape
escaping = ESCAPING_REVERSE_RIGHT;
}
}
else if(escaping == ESCAPING_RIGHT)
{
if(isFarAway(distanceFront))
{
Serial.println("stop escaping to right");
escaping = NOT_ESCAPING;
}
else if(isFar(distanceFront))
{
Serial.println("⇨");
goFront();
steerRight();
}
else
{
//can't go any further, reverse escape
escaping = ESCAPING_REVERSE_LEFT;
}
}
else if(escaping == ESCAPING_BACK)
{
if(isFarAway(distanceLeft) || isFarAway(distanceRight))
{
Serial.println("stop escaping to back");
escaping = NOT_ESCAPING;
}
else if(isFar(distanceBack))
{
Serial.println("⇩");
goBack();
}
else
{
//can't go any further, not escaping anymore
escaping = NOT_ESCAPING;
}
}
else if(escaping == ESCAPING_REVERSE_LEFT)
{
if(isFar(distanceFront) || !isFar(distanceBack))
{
Serial.println("stop reverse escaping to left");
escaping = ESCAPING_RIGHT;
}
else
{
Serial.println("⇩⇦");
goBack();
steerLeft();
}
}
else if(escaping == ESCAPING_REVERSE_RIGHT)
{
if(isFar(distanceFront) || !isFar(distanceBack))
{
Serial.println("stop reverse escaping to right");
escaping = ESCAPING_LEFT;
}
else
{
Serial.println("⇩⇨");
goBack();
steerRight();
}
}
}
void probeDistances()
{
distanceFront = sensorFront.Ranging(CM);
delay(delaySensor);
distanceRight = sensorRight.Ranging(CM);
delay(delaySensor);
distanceLeft = sensorLeft.Ranging(CM);
delay(delaySensor);
distanceBack = sensorBack.Ranging(CM);
delay(delaySensor);
/*
Serial.print("front : ");
Serial.print(distanceFront);
Serial.print(", left : ");
Serial.print(distanceLeft);
Serial.print(", right : ");
Serial.print(distanceRight);
Serial.print(", back : ");
Serial.println(distanceBack);
*/
}
void steerLeft()
{
steer(BACKWARD);
}
void steerRight()
{
steer(FORWARD);
}
void steerStraight()
{
steer(RELEASE);
}
void steer(int direction)
{
if(FAKE_MOTOR) return;
wheel.run(direction); //backward == left
wheel.setSpeed(255);
}
void goFront()
{
go(FORWARD);
}
void goBack()
{
go(BACKWARD);
}
void stop()
{
go(RELEASE);
}
void go(int direction)
{
if(FAKE_MOTOR) return;
motorFront.run(direction);
motorBack.run(direction);
motorFront.setSpeed(MAX_SPEED);
motorBack.setSpeed(MAX_SPEED);
}
bool isFarAway(int distance)
{
return distance > FAR_AWAY_THRESHOLD || distance == 0;
}
bool isFar(int distance)
{
return distance > FAR_THRESHOLD || distance == 0;
}
int compareDistance(int d1, int d2)
{
return d2 - d1;
}