-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobstacleavoidance.ino
186 lines (162 loc) · 4.61 KB
/
obstacleavoidance.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
boolean debug = false;
// setup ultrasonic distance sensor
const int pingPin = 2;
unsigned int duration, distance;
int sensorWait = 10;
// setup servo under sensor
#include <Servo.h>
Servo sensingServo;
int pos = 1300;
int sensingServoMin = 800;
int sensingServoMax = 1700;
int sensingServoResolution = 25;
boolean sweepLeft = true;
// setup array for storing directions and distances
// array length = ((sensingServoMax - sensingServoMin) / sensingServoResolution) + 1;
int directionsAndDistances[37][2];
int directionsAndDistancesLength = 37;
int i;
// setup arrray for choosing direction
int obstacleFreeDirections[37][3];
int minimumDistance = 48;
int waypointDirection = 1400;
void readSensor()
{
// move servo into position
sensingServo.writeMicroseconds(pos);
// read ultrasonic sensor
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
distance = duration / 74 / 2;
// write position and distance to array
directionsAndDistances[i][0] = pos;
directionsAndDistances[i][1] = distance;
}
int chooseDirection()
{
if(debug)
{
Serial.println("Directions and distances");
for (int i = 0; i < directionsAndDistancesLength; i++)
{
Serial.print(directionsAndDistances[i][0]);
Serial.print(",");
Serial.print(directionsAndDistances[i][1]);
Serial.print("\n");
}
}
// filter out directions with obstacles
for(int i,j = 0; i < directionsAndDistancesLength; i++)
{
if (directionsAndDistances[i][1] >= minimumDistance)
{
obstacleFreeDirections[j][0] = directionsAndDistances[i][0];
obstacleFreeDirections[j][1] = directionsAndDistances[i][1];
}
else
{
obstacleFreeDirections[j][0] = 0;
obstacleFreeDirections[j][1] = 0;
}
obstacleFreeDirections[j][2] = 0;
j++;
}
if(debug)
{
Serial.println("Obstacle free directions");
for (int i = 0; i < directionsAndDistancesLength; i++)
{
Serial.print(obstacleFreeDirections[i][0]);
Serial.print(",");
Serial.print(obstacleFreeDirections[i][1]);
Serial.print(",");
Serial.print(obstacleFreeDirections[i][2]);
Serial.print("\n");
}
}
// calculate the discrepancy between the waypoint direction and each direction
for(int i = 0; i < directionsAndDistancesLength; i++)
{
if(obstacleFreeDirections[i][0] != 0)
{
obstacleFreeDirections[i][2] = abs(waypointDirection - obstacleFreeDirections[i][0]);
}
}
if(debug)
{
Serial.println("Discrepancies from way point");
for (int i = 0; i < directionsAndDistancesLength; i++)
{
Serial.print(obstacleFreeDirections[i][0]);
Serial.print(",");
Serial.print(obstacleFreeDirections[i][1]);
Serial.print(",");
Serial.print(obstacleFreeDirections[i][2]);
Serial.print("\n");
}
}
// choose the direction closest to the waypoint direction with no obstacles
// find the first obstacle free direction
int solutionDirectionReference = 0;
while(obstacleFreeDirections[solutionDirectionReference][0] == 0)
{
solutionDirectionReference++;
}
// find the obstacle free direction closest to the waypoint direction
for(int i = 0; i < directionsAndDistancesLength; i++)
{
if(obstacleFreeDirections[i][0] != 0 && obstacleFreeDirections[i][2] < obstacleFreeDirections[solutionDirectionReference][2])
{
solutionDirectionReference = i;
}
}
if(obstacleFreeDirections[solutionDirectionReference][0] > 15000)
{
return 0;
}
else
{
return obstacleFreeDirections[solutionDirectionReference][0];
}
}
void setup()
{
Serial.begin(115200);
sensingServo.attach(5);
}
void loop() {
if (sweepLeft)
{
// sweep left
i = 0;
for(pos = sensingServoMin; pos <= sensingServoMax; pos += sensingServoResolution)
{
readSensor();
i++;
delay(sensorWait);
}
sweepLeft = false;
}
else
{
// sweep right
i = directionsAndDistancesLength - 1;
for(pos = sensingServoMax; pos >= sensingServoMin; pos -= sensingServoResolution)
{
readSensor();
i--;
delay(sensorWait);
}
sweepLeft = true;
}
int chosenDirection = chooseDirection();
// Serial.print("Direction chosen: ");
Serial.print(chosenDirection);
Serial.print("\n");
}