forked from tudelft/SGBA_CF2_App_layer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallfollowing_multiranger_onboard.c
331 lines (263 loc) · 8.64 KB
/
wallfollowing_multiranger_onboard.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
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
/*
* wall_follower_multi_ranger_onboard.c
*
* Created on: Aug 7, 2018
* Author: knmcguire
*/
#include "wallfollowing_multiranger_onboard.h"
#include <math.h>
#include "usec_time.h"
// variables
static float ref_distance_from_wall = 0;
static float max_speed = 0.5;
static float max_rate = 0.5;
static float direction = 1;
static float first_run = false;
static int state = 1;
float state_start_time;
void wall_follower_init(float new_ref_distance_from_wall, float max_speed_ref, int init_state)
{
ref_distance_from_wall = new_ref_distance_from_wall;
max_speed = max_speed_ref;
first_run = true;
state = init_state;
}
// Static helper functions
static bool logicIsCloseTo(float real_value, float checked_value, float margin)
{
if (real_value > checked_value - margin && real_value < checked_value + margin) {
return true;
} else {
return false;
}
}
static float wraptopi(float number)
{
if (number > (float)M_PI) {
return (number - (float)(2 * M_PI));
} else if (number < (float)(-1 * M_PI)) {
return (number + (float)(2 * M_PI));
} else {
return (number);
}
}
// Static command functions
static void commandTurn(float *vel_x, float *vel_w, float ref_rate)
{
*vel_x = 0.0;
*vel_w = direction * ref_rate;
}
static void commandAlignCorner(float *vel_y, float *vel_w, float ref_rate, float range,
float wanted_distance_from_corner)
{
if (range > wanted_distance_from_corner + 0.3f) {
*vel_w = direction * ref_rate;
*vel_y = 0;
} else {
if (range > wanted_distance_from_corner) {
*vel_y = direction * (-1 * max_speed / 3);
} else {
*vel_y = direction * (max_speed / 3);
}
*vel_w = 0;
}
}
static void commandHover(float *vel_x, float *vel_y, float *vel_w)
{
*vel_x = 0.0;
*vel_y = 0.0;
*vel_w = 0.0;
}
static void commandForwardAlongWall(float *vel_x, float *vel_y, float range)
{
*vel_x = max_speed;
bool check_distance_wall = logicIsCloseTo(ref_distance_from_wall, range, 0.1);
*vel_y = 0;
if (!check_distance_wall) {
if (range > ref_distance_from_wall) {
*vel_y = direction * (-1 * max_speed / 2);
} else {
*vel_y = direction * (max_speed / 2);
}
}
}
static void commandTurnAroundCornerAndAdjust(float *vel_x, float *vel_y, float *vel_w, float radius, float range)
{
*vel_x = max_speed;
*vel_w = direction * (-1 * (*vel_x) / radius);
bool check_distance_to_wall = logicIsCloseTo(ref_distance_from_wall, range, 0.1);
if (!check_distance_to_wall) {
if (range > ref_distance_from_wall) {
*vel_y = direction * (-1 * max_speed / 3);
}
else {
*vel_y = direction * (max_speed / 3);
}
}
}
static void commandTurnAndAdjust(float *vel_y, float *vel_w, float rate, float range)
{
*vel_w = direction * rate;
*vel_y = 0;
}
static int transition(int new_state)
{
float t = usecTimestamp() / 1e6;
state_start_time = t;
return new_state;
}
void adjustDistanceWall(float distance_wall_new)
{
ref_distance_from_wall = distance_wall_new;
}
int wall_follower(float *vel_x, float *vel_y, float *vel_w, float front_range, float side_range, float current_heading,
int direction_turn)
{
direction = direction_turn;
static float previous_heading = 0;
static float angle = 0;
static bool around_corner_go_back = false;
float now = usecTimestamp() / 1e6;
if (first_run) {
previous_heading = current_heading;
// around_corner_first_turn = false;
around_corner_go_back = false;
first_run = false;
}
/***********************************************************
* State definitions
***********************************************************/
// 1 = forward
// 2 = hover
// 3 = turn_to_find_wall
// 4 = turn_to_allign_to_wall
// 5 = forward along wall
// 6 = rotate_around_wall
// 7 = rotate_in_corner
// 8 = find corner
/***********************************************************
* Handle state transitions
***********************************************************/
if (state == 1) { //FORWARD
if (front_range < ref_distance_from_wall + 0.2f) {
state = transition(3);
}
} else if (state == 2) { // HOVER
} else if (state == 3) { // TURN_TO_FIND_WALL
// check if wall is found
bool side_range_check = side_range < ref_distance_from_wall / (float)cos(0.78f) + 0.2f;
bool front_range_check = front_range < ref_distance_from_wall / (float)cos(0.78f) + 0.2f;
if (side_range_check && front_range_check) {
previous_heading = current_heading;
angle = direction * (1.57f - (float)atan(front_range / side_range) + 0.1f);
state = transition(4); // go to turn_to_allign_to_wall
}
if (side_range < 1.0f && front_range > 2.0f) {
// around_corner_first_turn = true;
around_corner_go_back = false;
previous_heading = current_heading;
state = transition(8); // go to rotate_around_wall
}
} else if (state == 4) { //TURN_TO_ALLIGN_TO_WALL
bool allign_wall_check = logicIsCloseTo(wraptopi(current_heading - previous_heading), angle, 0.1f);
if (allign_wall_check) {
// prev_side_range = side_range;
state = transition(5);
}
} else if (state == 5) { //FORWARD_ALONG_WALL
// If side range is out of reach,
// end of the wall is reached
if (side_range > ref_distance_from_wall + 0.3f) {
// around_corner_first_turn = true;
state = transition(8);
}
// If front range is small
// then corner is reached
if (front_range < ref_distance_from_wall + 0.2f) {
previous_heading = current_heading;
state = transition(7);
}
} else if (state == 6) { //ROTATE_AROUND_WALL
if (front_range < ref_distance_from_wall + 0.2f) {
state = transition(3);
}
} else if (state == 7) { //ROTATE_IN_CORNER
// Check if heading goes over 0.8 rad
bool check_heading_corner = logicIsCloseTo(fabs(wraptopi(current_heading - previous_heading)), 0.8f, 0.1f);
if (check_heading_corner) {
state = transition(3);
}
} else if (state == 8) { //FIND_CORNER
if (side_range <= ref_distance_from_wall) {
state = transition(6);
}
}
else {
}
/***********************************************************
* Handle state actions
***********************************************************/
float temp_vel_x = 0;
float temp_vel_y = 0;
float temp_vel_w = 0;
if (state == 1) { //FORWARD
temp_vel_x = max_speed;
temp_vel_y = 0.0;
temp_vel_w = 0.0;
} else if (state == 2) { // HOVER
commandHover(&temp_vel_x, &temp_vel_y, &temp_vel_w);
} else if (state == 3) { // TURN_TO_FIND_WALL
commandTurn(&temp_vel_x, &temp_vel_w, max_rate);
temp_vel_y = 0.0;
} else if (state == 4) { //TURN_TO_ALLIGN_TO_WALL
if (now - state_start_time < 1.0f)
{
commandHover(&temp_vel_x, &temp_vel_y, &temp_vel_w);
} else { // then turn again
commandTurn(&temp_vel_x, &temp_vel_w, max_rate);
temp_vel_y = 0;
}
} else if (state == 5) { //FORWARD_ALONG_WALL
commandForwardAlongWall(&temp_vel_x, &temp_vel_y, side_range);
temp_vel_w = 0.0f;
//commandForwardAlongWallHeadingSine(&temp_vel_x, &temp_vel_y,&temp_vel_w, side_range);
} else if (state == 6) { //ROTATE_AROUND_WALL
// If first time around corner
//first try to find the corner again
// if side range is larger than prefered distance from wall
if (side_range > ref_distance_from_wall + 0.5f) {
// check if scanning has already occured
if (wraptopi(fabs(current_heading - previous_heading)) > 0.8f) {
around_corner_go_back = true;
}
// turn and adjust distnace to corner from that point
if (around_corner_go_back) {
// go back if it already went into one direction
commandTurnAndAdjust(&temp_vel_y, &temp_vel_w, -1 * max_rate, side_range);
temp_vel_x = 0.0f;
} else {
commandTurnAndAdjust(&temp_vel_y, &temp_vel_w, max_rate, side_range);
temp_vel_x = 0.0f;
}
} else {
// continue to turn around corner
previous_heading = current_heading;
around_corner_go_back = false;
commandTurnAroundCornerAndAdjust(&temp_vel_x, &temp_vel_y, &temp_vel_w, ref_distance_from_wall, side_range);
}
} else if (state == 7) { //ROTATE_IN_CORNER
commandTurn(&temp_vel_x, &temp_vel_w, max_rate);
temp_vel_y = 0;
} else if (state == 8) { //FIND_CORNER
commandAlignCorner(&temp_vel_y, &temp_vel_w, -1 * max_rate, side_range, ref_distance_from_wall);
temp_vel_x = 0;
}
else {
//State does not exist so hover!!
commandHover(&temp_vel_x, &temp_vel_y, &temp_vel_w);
}
*vel_x = temp_vel_x;
*vel_y = temp_vel_y;
*vel_w = temp_vel_w;
return state;
}