Revision 4c9fb6ba
Update smart_runaround and add old files.
scout/libscout/src/BehaviorList.cpp | ||
---|---|---|
27 | 27 |
behavior_list.push_back(behavior<line_follow>); |
28 | 28 |
behavior_list.push_back(behavior<navigationMap>); |
29 | 29 |
behavior_list.push_back(behavior<wl_test>); |
30 |
behavior_list.push_back(behavior<Odometry_new>); |
|
30 | 31 |
behavior_list.push_back(behavior<Scheduler>); |
31 | 32 |
behavior_list.push_back(behavior<WH_Robot>); |
32 | 33 |
behavior_list.push_back(behavior<maze_solve>); |
34 |
behavior_list.push_back(behavior<maze_solve_zane>); |
|
33 | 35 |
behavior_list.push_back(behavior<smart_runaround>); |
34 | 36 |
return; |
35 | 37 |
} |
scout/libscout/src/BehaviorList.h | ||
---|---|---|
28 | 28 |
#include "behaviors/line_follow.h" |
29 | 29 |
#include "behaviors/navigationMap.h" |
30 | 30 |
#include "behaviors/wl_test.h" |
31 |
#include "test_behaviors/Odometry_new.h" |
|
31 | 32 |
#include "test_behaviors/Scheduler.h" |
32 | 33 |
#include "test_behaviors/WH_Robot.h" |
33 | 34 |
#include "test_behaviors/maze_solve.h" |
35 |
#include "test_behaviors/maze_solve_zane.h" |
|
34 | 36 |
#include "test_behaviors/smart_runaround.h" |
35 | 37 |
|
36 | 38 |
template<typename T> Behavior* behavior(std::string scoutname, Sensors* sensors){ return (Behavior*)new T(scoutname, sensors); } |
scout/libscout/src/test_behaviors/maze_solve.cpp | ||
---|---|---|
25 | 25 |
|
26 | 26 |
#include "maze_solve.h" |
27 | 27 |
|
28 |
using namespace std;
|
|
29 |
|
|
28 |
#define D_THRESH 600
|
|
29 |
#define max(x,y) ((x > y) ? x : y) |
|
30 | 30 |
|
31 |
// want to have a minimal working thing, use a big enough |
|
32 |
// static array and start in the middle |
|
33 |
// we assume we are facing right, that affects where we store |
|
34 |
// wall information |
|
35 |
// -1 for wall, 0 for unseen, 1 for traveled, 2 for critical |
|
36 |
#define WALL -1 |
|
37 |
#define UNSEEN 0 |
|
38 |
#define SEEN 1 |
|
39 |
#define CRITICAL 2 |
|
40 |
// facings |
|
41 |
#define UP 0 |
|
42 |
#define RIGHT 1 |
|
43 |
#define DOWN 2 |
|
44 |
#define LEFT 3 |
|
31 |
using namespace std; |
|
45 | 32 |
|
46 |
// TODO This is bad! It's defined globally across all files. Please put it inside a good scope. -Alex |
|
47 |
Duration sonar_update_time(1.5); |
|
33 |
void maze_solve::spin_for(double duration) |
|
34 |
{ |
|
35 |
ros::Rate r(100); // 100 hz |
|
36 |
int ticks = int(duration * 100); |
|
37 |
for (int i = 0; i < ticks; i++) |
|
38 |
{ |
|
39 |
spinOnce(); |
|
40 |
r.sleep(); |
|
41 |
} |
|
42 |
} |
|
48 | 43 |
|
49 | 44 |
void maze_solve::run() |
50 | 45 |
{ |
51 |
// TODO:first initialize map to all 0's |
|
52 | 46 |
ROS_INFO("Starting to solve the maze"); |
47 |
|
|
53 | 48 |
// Go up to the first line. |
54 | 49 |
follow_line(); |
50 |
|
|
55 | 51 |
// Turn the sonar on. |
56 | 52 |
sonar->set_on(); |
57 | 53 |
sonar->set_range(0, 23); |
58 | 54 |
|
59 |
// Wait for the sonar to initialize. |
|
60 |
while(!look_around(25, 25, RIGHT) && ok()) |
|
61 |
{ |
|
62 |
spinOnce(); |
|
63 |
} |
|
64 |
|
|
65 |
// Solve the maze |
|
66 |
bool finished = solve(25,25, RIGHT); |
|
67 |
|
|
68 |
// Check and report final condition. |
|
69 |
if (finished) |
|
70 |
ROS_INFO("YAY! I have solved the maze"); |
|
71 |
else |
|
72 |
ROS_INFO("NO! The maze is unsolvable"); |
|
73 |
} |
|
74 |
|
|
75 |
bool maze_solve::solve(int row, int col, int dir) |
|
76 |
{ |
|
77 |
int initial_dir = dir; |
|
78 |
|
|
79 |
ROS_INFO("I am at direction %d", dir); |
|
80 |
|
|
81 |
// use backtracking to solve the maze |
|
82 |
if (at_destination()) |
|
83 |
return true; |
|
55 |
ROS_INFO("Off we go!"); |
|
84 | 56 |
|
85 |
// Wait for sonar to update. |
|
86 |
sonar_update_time.sleep(); |
|
87 |
|
|
88 |
// this function should fill the adjacent cells around me with |
|
89 |
// wall's or paths |
|
90 |
while(!look_around(row, col, dir) && ok()) |
|
57 |
while (!at_destination()) |
|
91 | 58 |
{ |
92 |
spinOnce(); |
|
93 |
} |
|
94 |
|
|
95 |
/* try go up */ |
|
96 |
if (map[row-1][col] != WALL && initial_dir != UP) |
|
97 |
{ |
|
98 |
ROS_INFO("GOING UP!"); |
|
99 |
// Turn up. |
|
100 |
turn_from_to(dir, UP); |
|
101 |
follow_line(); |
|
102 |
// Solve recursively. |
|
103 |
bool solved = solve(row-1, col, DOWN); |
|
104 |
if (solved) |
|
59 |
// Wait for sonar to update. |
|
60 |
spin_for(1.5); |
|
61 |
int* readings = sonar->get_sonar_readings(); |
|
62 |
|
|
63 |
// Wait until the sonar gives us real values. |
|
64 |
bool readings_ok = true; |
|
65 |
cout << "["; |
|
66 |
for (int i = 0; i < 48; i++) |
|
105 | 67 |
{ |
106 |
return solved; |
|
68 |
if (i == 24 || i == 36 || i == 0) |
|
69 |
{ |
|
70 |
cout << "(" << readings[i] << ") "; |
|
71 |
} |
|
72 |
else |
|
73 |
{ |
|
74 |
cout << readings[i] << " "; |
|
75 |
} |
|
76 |
if (readings[i] == 0) |
|
77 |
{ |
|
78 |
ROS_INFO("Waiting. readings[%d] == 0.", i); |
|
79 |
readings_ok = false; |
|
80 |
break; |
|
81 |
} |
|
107 | 82 |
} |
108 |
else |
|
109 |
{ |
|
110 |
//Update where we are. |
|
111 |
dir = UP; |
|
112 |
} |
|
113 |
} |
|
114 |
/* try right */ |
|
115 |
if (map[row][col+1] != WALL && initial_dir != RIGHT) |
|
116 |
{ |
|
117 |
ROS_INFO("GOING RIGHT!"); |
|
118 |
// Turn right. |
|
119 |
turn_from_to(dir, RIGHT); |
|
120 |
follow_line(); |
|
121 |
// Solve recursively. |
|
122 |
bool solved = solve(row, col+1, LEFT); |
|
123 |
if (solved) |
|
124 |
{ |
|
125 |
return solved; |
|
126 |
} |
|
127 |
else |
|
83 |
cout << endl; |
|
84 |
|
|
85 |
if (!readings_ok) |
|
128 | 86 |
{ |
129 |
//Update where we are. |
|
130 |
dir = RIGHT; |
|
87 |
continue; |
|
131 | 88 |
} |
132 |
} |
|
133 |
/* try down */ |
|
134 |
if (map[row+1][col] != WALL && initial_dir != DOWN) |
|
135 |
{ |
|
136 |
ROS_INFO("GOING DOWN!"); |
|
137 |
// Turn down. |
|
138 |
turn_from_to(dir, DOWN); |
|
139 |
follow_line(); |
|
140 |
// Solve recursively. |
|
141 |
bool solved = solve(row+1, col, UP); |
|
142 |
if (solved) |
|
89 |
|
|
90 |
int r_read = max(readings[23], max(readings[24], readings[25])); |
|
91 |
int s_read = max(readings[35], max(readings[36], readings[37])); |
|
92 |
int l_read = max(readings[47], max(readings[0], readings[1])); |
|
93 |
|
|
94 |
ROS_INFO("Left: %d. Straight: %d. Right: %d.", l_read, s_read, r_read); |
|
95 |
|
|
96 |
if (r_read > D_THRESH) // Right |
|
143 | 97 |
{ |
144 |
return solved; |
|
98 |
ROS_INFO("Right."); |
|
99 |
turn_right(); |
|
145 | 100 |
} |
146 |
else |
|
101 |
else if (s_read > D_THRESH) // Straight
|
|
147 | 102 |
{ |
148 |
//Update where we are.
|
|
149 |
dir = DOWN;
|
|
103 |
ROS_INFO("Straight.");
|
|
104 |
turn_straight();
|
|
150 | 105 |
} |
151 |
} |
|
152 |
/* try left */ |
|
153 |
if (map[row][col-1] != WALL && initial_dir != LEFT) |
|
154 |
{ |
|
155 |
ROS_INFO("GOING LEFT!"); |
|
156 |
// Turn down. |
|
157 |
turn_from_to(dir, LEFT); |
|
158 |
follow_line(); |
|
159 |
// Solve recursively. |
|
160 |
bool solved = solve(row, col-1, RIGHT); |
|
161 |
if (solved) |
|
106 |
else if (l_read > D_THRESH) // Left |
|
162 | 107 |
{ |
163 |
return solved; |
|
108 |
ROS_INFO("Left."); |
|
109 |
turn_left(); |
|
164 | 110 |
} |
165 |
else |
|
111 |
else // Deadend
|
|
166 | 112 |
{ |
167 |
//Update where we are. |
|
168 |
dir = LEFT; |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
ROS_INFO("DEAD END FOUND, TURNING BACK."); |
|
173 |
// we have exhausted all the options. This path is clearly a |
|
174 |
// dead end. go back to where we come from and return false. |
|
175 |
turn_from_to(dir, initial_dir); |
|
176 |
follow_line(); |
|
177 |
return false; |
|
178 |
} |
|
179 |
|
|
180 |
// this function takes in the current direction and turns the scout |
|
181 |
// into it intended direction |
|
182 |
void maze_solve::turn_from_to(int current_dir, int intended_dir) { |
|
183 |
switch ((4 + intended_dir - current_dir) % 4) |
|
184 |
{ |
|
185 |
case 0: |
|
113 |
ROS_INFO("Dead end."); |
|
186 | 114 |
spot_turn(); |
187 |
break; |
|
188 |
case 1: |
|
189 |
turn_left(); |
|
190 |
break; |
|
191 |
case 2: |
|
192 |
turn_straight(); |
|
193 |
break; |
|
194 |
case 3: |
|
195 |
turn_right(); |
|
196 |
break; |
|
197 |
} |
|
198 |
} |
|
199 |
|
|
200 |
bool maze_solve::look_around(int row, int col, int dir) |
|
201 |
{ |
|
202 |
// look around current place using sonar |
|
203 |
// store whether or not |
|
204 |
// there is a wall into the map |
|
205 |
// stores at row col 2 if point is critical, 1 otherwise |
|
206 |
|
|
207 |
int* readings = sonar->get_sonar_readings(); |
|
208 |
spinOnce(); |
|
209 |
|
|
210 |
/* |
|
211 |
// Look to the left. |
|
212 |
int left_distance = (readings[1] + readings[0] + readings[47])/3; |
|
213 |
// Look to the front. |
|
214 |
int front_distance = (readings[35] + readings[36] + readings[37])/3; |
|
215 |
// Look to the right. |
|
216 |
int right_distance = (readings[23] + readings[24] + readings[25])/3; |
|
217 |
*/ |
|
218 |
// Look to the left. |
|
219 |
int left_distance = readings[0]; |
|
220 |
// Look to the front. |
|
221 |
int front_distance = readings[36]; |
|
222 |
// Look to the right. |
|
223 |
int right_distance = readings[24]; |
|
224 |
|
|
225 |
ROS_INFO("front: %d left: %d right: %d", front_distance, left_distance, right_distance); |
|
226 |
|
|
227 |
if (right_distance == 0 || front_distance == 0 || left_distance == 0) |
|
228 |
return false; |
|
115 |
} |
|
229 | 116 |
|
230 |
switch (dir) |
|
231 |
{ |
|
232 |
case UP: |
|
233 |
// If the distance is less than 500, mark the area as a wall otherwise |
|
234 |
// mark it as seen. |
|
235 |
map[row][col+1] = (left_distance < 500)?WALL:SEEN; |
|
236 |
map[row+1][col] = (front_distance < 500)?WALL:SEEN; |
|
237 |
map[row][col-1] = (right_distance < 500)?WALL:SEEN; |
|
238 |
break; |
|
239 |
case RIGHT: |
|
240 |
// If the distance is less than 500, mark the area as a wall otherwise |
|
241 |
// mark it as seen. |
|
242 |
map[row+1][col] = (left_distance < 500)?WALL:SEEN; |
|
243 |
map[row][col-1] = (front_distance < 500)?WALL:SEEN; |
|
244 |
map[row-1][col] = (right_distance < 500)?WALL:SEEN; |
|
245 |
break; |
|
246 |
case DOWN: |
|
247 |
// If the distance is less than 500, mark the area as a wall otherwise |
|
248 |
// mark it as seen. |
|
249 |
map[row][col-1] = (left_distance < 500)?WALL:SEEN; |
|
250 |
map[row-1][col] = (front_distance < 500)?WALL:SEEN; |
|
251 |
map[row][col+1] = (right_distance < 500)?WALL:SEEN; |
|
252 |
break; |
|
253 |
case LEFT: |
|
254 |
// If the distance is less than 500, mark the area as a wall otherwise |
|
255 |
// mark it as seen. |
|
256 |
map[row-1][col] = (left_distance < 500)?WALL:SEEN; |
|
257 |
map[row][col+1] = (front_distance < 500)?WALL:SEEN; |
|
258 |
map[row+1][col] = (right_distance < 500)?WALL:SEEN; |
|
259 |
break; |
|
117 |
follow_line(); |
|
260 | 118 |
} |
261 |
|
|
262 |
return true; |
|
263 | 119 |
} |
264 | 120 |
|
265 | 121 |
bool maze_solve::at_destination() |
266 | 122 |
{ |
267 | 123 |
vector<uint32_t> readings = linesensor->query(); |
124 |
//ROS_INFO("Readings: %d %d %d %d %d %d %d %d.", readings[0], readings[1], readings[2], readings[3], readings[4], readings[5], readings[6], readings[7]); |
|
268 | 125 |
if ( readings[0] > 200 && |
269 | 126 |
readings[1] < 55 && |
270 | 127 |
readings[2] < 55 && |
... | ... | |
274 | 131 |
readings[6] < 55 && |
275 | 132 |
readings[7] > 200 ) |
276 | 133 |
{ |
134 |
ROS_INFO("\n\nDESTINATION\n\n"); |
|
277 | 135 |
return true; |
278 | 136 |
} |
279 | 137 |
return false; |
scout/libscout/src/test_behaviors/maze_solve.h | ||
---|---|---|
26 | 26 |
#ifndef _MAZE_SOLVE_H_ |
27 | 27 |
#define _MAZE_SOLVE_H_ |
28 | 28 |
|
29 |
#include <stdio.h> |
|
29 | 30 |
#include "../behaviors/line_follow.h" |
30 | 31 |
|
31 | 32 |
class maze_solve: public line_follow |
... | ... | |
35 | 36 |
line_follow(scoutname, "maze_solve", sensors) {}; |
36 | 37 |
void run(); |
37 | 38 |
private: |
38 |
bool solve(int row, int col, int dir); |
|
39 |
void turn_from_to(int current_dir, int intended_dir); |
|
40 |
bool look_around(int row, int col, int dir); |
|
39 |
void spin_for(double duration); |
|
41 | 40 |
bool at_destination(); |
42 | 41 |
|
42 |
Duration sonar_update_time; |
|
43 |
|
|
43 | 44 |
int map[60][60]; |
44 | 45 |
}; |
45 | 46 |
#endif |
scout/libscout/src/test_behaviors/smart_runaround.cpp | ||
---|---|---|
59 | 59 |
return pixels/200.0; |
60 | 60 |
} |
61 | 61 |
|
62 |
// meters to pixels |
|
63 |
int m_to_idx(float meters) |
|
62 |
// millimeters to pixels
|
|
63 |
int mm_to_idx(float meters)
|
|
64 | 64 |
{ |
65 |
float pixels = meters*200.0; |
|
65 |
// 200 pixels per meter, and 1000 millimeters per meter |
|
66 |
float pixels = meters*0.2; |
|
66 | 67 |
float idx = pixels/BLOCK_LENGTH; |
67 | 68 |
return floor(idx+0.5); |
68 | 69 |
} |
69 | 70 |
|
70 |
/* return a direction (if any) where adjacent block |
|
71 |
* is labeled "info" on map. Searches clockwise |
|
72 |
* starting at up. Returns -1 if no direction valid. |
|
73 |
*/ |
|
74 |
int smart_runaround::choose_direc(int row, int col, int info) |
|
71 |
float *matrix_mult(float inputs[2], float matrix[2][2]) |
|
75 | 72 |
{ |
76 |
if (map[row-1][col] == info) |
|
77 |
return UP; |
|
78 |
else if (map[row][col+1] == info) |
|
79 |
return RIGHT; |
|
80 |
else if (map[row+1][col] == info) |
|
81 |
return DOWN; |
|
82 |
else if (map[row][col-1] == info) |
|
83 |
return LEFT; |
|
84 |
return -1; |
|
73 |
float newX = matrix[0][0]*inputs[0]+matrix[0][1]*inputs[1]; |
|
74 |
float newY = matrix[1][0]*inputs[0]+matrix[1][1]*inputs[1]; |
|
75 |
float output[2] = {newX, newY}; |
|
76 |
return output; |
|
85 | 77 |
} |
86 | 78 |
|
87 | 79 |
// TODO This is bad! It's defined globally across all behaviors. Please fix this. -Alex |
... | ... | |
117 | 109 |
|
118 | 110 |
|
119 | 111 |
int dir = RIGHT; // current direction |
120 |
int new_dir = RIGHT; // direction in which to turn after a scan |
|
112 |
//int new_dir = RIGHT; // direction in which to turn after a scan
|
|
121 | 113 |
bool success = false; // true when maze solved |
122 | 114 |
while(ok()) |
123 | 115 |
{ |
124 |
// Look left, right, and forward |
|
125 | 116 |
look_around(row, col, dir); |
126 | 117 |
// Try moving in each direction |
127 |
new_dir = choose_direc(row, col, UNSEEN); |
|
118 |
/*new_dir = choose_direc(row, col, UNSEEN);
|
|
128 | 119 |
if(new_dir < 0) |
129 | 120 |
new_dir = choose_direc(row, col, SEEN); |
130 | 121 |
if(new_dir >= 0) { |
131 | 122 |
turn_from_to(dir, new_dir); |
132 | 123 |
dir = new_dir; |
133 |
} |
|
124 |
}*/
|
|
134 | 125 |
} |
135 | 126 |
|
136 | 127 |
// Check and report final condition. |
... | ... | |
140 | 131 |
ROS_INFO("NO! The maze is unsolvable"); |
141 | 132 |
} |
142 | 133 |
|
143 |
// NOT CURRENTLY USED!!! |
|
144 |
bool smart_runaround::solve(int row, int col, int dir) |
|
134 |
/* return a direction (if any) where adjacent block |
|
135 |
* is labeled "info" on map. Searches clockwise |
|
136 |
* starting at up. Returns -1 if no direction valid. |
|
137 |
*/ |
|
138 |
int smart_runaround::choose_direc(int row, int col, int info) |
|
145 | 139 |
{ |
146 |
int initial_dir = dir; |
|
147 |
|
|
148 |
ROS_INFO("I am at direction %d", dir); |
|
149 |
|
|
150 |
// use backtracking to solve the maze |
|
151 |
if (at_destination()) |
|
152 |
return true; |
|
153 |
|
|
154 |
// Wait for sonar to update. |
|
155 |
sonar_update_time2.sleep(); |
|
156 |
|
|
157 |
// this function should fill the adjacent cells around me with |
|
158 |
// wall's or paths |
|
159 |
while(!look_around(row, col, dir) && ok()) |
|
160 |
{ |
|
161 |
spinOnce(); |
|
162 |
} |
|
163 |
|
|
164 |
/* try go up */ |
|
165 |
if (map[row-1][col] != WALL && initial_dir != UP) |
|
166 |
{ |
|
167 |
ROS_INFO("GOING UP!"); |
|
168 |
// Turn up. |
|
169 |
turn_from_to(dir, UP); |
|
170 |
follow_line(); |
|
171 |
// Solve recursively. |
|
172 |
bool solved = solve(row-1, col, DOWN); |
|
173 |
if (solved) |
|
174 |
{ |
|
175 |
return solved; |
|
176 |
} |
|
177 |
else |
|
178 |
{ |
|
179 |
//Update where we are. |
|
180 |
dir = UP; |
|
181 |
} |
|
182 |
} |
|
183 |
/* try right */ |
|
184 |
if (map[row][col+1] != WALL && initial_dir != RIGHT) |
|
185 |
{ |
|
186 |
ROS_INFO("GOING RIGHT!"); |
|
187 |
// Turn right. |
|
188 |
turn_from_to(dir, RIGHT); |
|
189 |
follow_line(); |
|
190 |
// Solve recursively. |
|
191 |
bool solved = solve(row, col+1, LEFT); |
|
192 |
if (solved) |
|
193 |
{ |
|
194 |
return solved; |
|
195 |
} |
|
196 |
else |
|
197 |
{ |
|
198 |
//Update where we are. |
|
199 |
dir = RIGHT; |
|
200 |
} |
|
201 |
} |
|
202 |
/* try down */ |
|
203 |
if (map[row+1][col] != WALL && initial_dir != DOWN) |
|
204 |
{ |
|
205 |
ROS_INFO("GOING DOWN!"); |
|
206 |
// Turn down. |
|
207 |
turn_from_to(dir, DOWN); |
|
208 |
follow_line(); |
|
209 |
// Solve recursively. |
|
210 |
bool solved = solve(row+1, col, UP); |
|
211 |
if (solved) |
|
212 |
{ |
|
213 |
return solved; |
|
214 |
} |
|
215 |
else |
|
216 |
{ |
|
217 |
//Update where we are. |
|
218 |
dir = DOWN; |
|
219 |
} |
|
220 |
} |
|
221 |
/* try left */ |
|
222 |
if (map[row][col-1] != WALL && initial_dir != LEFT) |
|
223 |
{ |
|
224 |
ROS_INFO("GOING LEFT!"); |
|
225 |
// Turn down. |
|
226 |
turn_from_to(dir, LEFT); |
|
227 |
follow_line(); |
|
228 |
// Solve recursively. |
|
229 |
bool solved = solve(row, col-1, RIGHT); |
|
230 |
if (solved) |
|
231 |
{ |
|
232 |
return solved; |
|
233 |
} |
|
234 |
else |
|
235 |
{ |
|
236 |
//Update where we are. |
|
237 |
dir = LEFT; |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
ROS_INFO("DEAD END FOUND, TURNING BACK."); |
|
242 |
// we have exhausted all the options. This path is clearly a |
|
243 |
// dead end. go back to where we come from and return false. |
|
244 |
turn_from_to(dir, initial_dir); |
|
245 |
follow_line(); |
|
246 |
return false; |
|
140 |
if (map[row-1][col] == info) |
|
141 |
return UP; |
|
142 |
else if (map[row][col+1] == info) |
|
143 |
return RIGHT; |
|
144 |
else if (map[row+1][col] == info) |
|
145 |
return DOWN; |
|
146 |
else if (map[row][col-1] == info) |
|
147 |
return LEFT; |
|
148 |
return -1; |
|
247 | 149 |
} |
248 | 150 |
|
249 | 151 |
/* this function takes in the current direction, |
... | ... | |
267 | 169 |
} |
268 | 170 |
} |
269 | 171 |
|
172 |
/* Purpose: look front, left, and right using sonar, and update |
|
173 |
* map accordingly. Returns true if and only if sonar is initialized. |
|
174 |
*/ |
|
270 | 175 |
bool smart_runaround::look_around(int row, int col, int dir) |
271 | 176 |
{ |
272 |
// look around current place using sonar |
|
273 |
// store whether or not |
|
274 |
// there is a wall into the map |
|
275 |
// stores at row col 2 if point is critical, 1 otherwise |
|
276 |
|
|
277 | 177 |
int* readings = sonar->get_sonar_readings(); |
278 | 178 |
spinOnce(); |
279 | 179 |
|
280 | 180 |
// Assumption: readings are given in millimeters - Zane |
281 | 181 |
|
282 |
// distances with respect to robot, NOT map |
|
283 |
// Look to the left. |
|
284 |
float left_distance = readings[0]/1000.0; |
|
285 |
int left_idx = m_to_idx(left_distance); |
|
286 |
// Look to the front. |
|
287 |
float front_distance = readings[36]/1000.0; |
|
288 |
int front_idx = m_to_idx(front_distance); |
|
289 |
// Look to the right. |
|
290 |
float right_distance = readings[24]/1000.0; |
|
291 |
int right_idx = m_to_idx(right_distance); |
|
292 |
|
|
293 |
ROS_INFO("front: %d left: %d right: %d", front_distance, left_distance, right_distance); |
|
294 |
if (right_distance == 0 || front_distance == 0 || left_distance == 0) |
|
295 |
return false; |
|
296 |
|
|
297 |
// determine relative distances on map, based on robot position |
|
298 |
int up_d, right_d, down_d, left_d; |
|
299 |
// determine upward distance |
|
300 |
switch (dir) |
|
301 |
{ |
|
302 |
case UP: |
|
303 |
up_d = front_idx; |
|
304 |
right_d = right_idx; |
|
305 |
down_d = 0; // unknown |
|
306 |
left_d = left_idx; |
|
307 |
break; |
|
308 |
case RIGHT: |
|
309 |
up_d = left_idx; |
|
310 |
right_d = front_idx; |
|
311 |
down_d = right_idx; |
|
312 |
left_d = 0; // unknown |
|
313 |
break; |
|
314 |
case DOWN: |
|
315 |
up_d = 0; // unknown |
|
316 |
right_d = left_idx; |
|
317 |
down_d = front_idx; |
|
318 |
left_d = right_idx; |
|
319 |
break; |
|
320 |
case LEFT: |
|
321 |
up_d = right_idx; |
|
322 |
right_d = 0; // unknown |
|
323 |
down_d = left_idx; |
|
324 |
left_d = front_idx; |
|
325 |
break; |
|
326 |
} |
|
327 |
|
|
328 |
// change map until wall index, or until reading < 500 |
|
329 |
// reading < 500 <=> left_idx < 8 (approx.) |
|
330 |
|
|
331 |
// map blocks above robot (on map) |
|
332 |
for(int u = 0; u < 8; u++) |
|
333 |
{ |
|
334 |
if(u = up_d) { |
|
335 |
map[row-u][col] = (up_d)?WALL:SEEN; |
|
336 |
break; |
|
337 |
} |
|
338 |
map[row-u][col] = SEEN; |
|
339 |
} |
|
340 |
|
|
341 |
// map blocks to right of robot |
|
342 |
for(int r = 0; r < 8; r++) |
|
343 |
{ |
|
344 |
if(r = right_d) { |
|
345 |
map[row][col+r] = (right_d)?WALL:SEEN; |
|
346 |
break; |
|
347 |
} |
|
348 |
map[row][col+r] = SEEN; |
|
349 |
} |
|
350 |
|
|
351 |
// map blocks under robot (on map) |
|
352 |
for(int d = 0; d < 8; d++) |
|
353 |
{ |
|
354 |
if(d = down_d) { |
|
355 |
map[row+d][col] = (down_d)?WALL:SEEN; |
|
356 |
break; |
|
357 |
} |
|
358 |
map[row+d][col] = SEEN; |
|
359 |
} |
|
360 |
|
|
361 |
// map blocks to left of robot |
|
362 |
for(int l = 0; l < 8; l++) |
|
363 |
{ |
|
364 |
if(l = left_d) { |
|
365 |
map[row][col-l] = (left_d)?WALL:SEEN; |
|
366 |
break; |
|
367 |
} |
|
368 |
map[row][col-l] = SEEN; |
|
182 |
// matrices for going from robot's frame to base frame |
|
183 |
float rightMat[2][2] = {{0, 1}, {-1, 0}}; |
|
184 |
float downMat[2][2] = {{-1, 0}, {0, -1}}; |
|
185 |
float leftMat[2][2] = {{0, 1}, {1, 0}}; |
|
186 |
|
|
187 |
// Look to the left (and update map). |
|
188 |
float left_distance = readings[0]; // w.r.t. robot |
|
189 |
if(left_distance == 0) |
|
190 |
return false; |
|
191 |
int left_idx = -mm_to_idx(left_distance); // w.r.t. map |
|
192 |
// plot to map if indices are valid |
|
193 |
if (0 <= left_idx && left_idx < MAP_LENGTH) |
|
194 |
map[row][col+left_idx] = SEEN; |
|
195 |
|
|
196 |
// Look in the other directions (and update map). |
|
197 |
for (int i = 24; i < 48; i++) { |
|
198 |
float distance = readings[i]; // w.r.t. robot |
|
199 |
if(distance == 0) |
|
200 |
return false; |
|
201 |
if(distance >= 500) |
|
202 |
break; // too far to be accurate |
|
203 |
float theta = (M_PI/24)*i - M_PI; |
|
204 |
float xDist = distance*cos(theta); // w.r.t. robot |
|
205 |
float yDist = distance*sin(theta); // w.r.t. robot |
|
206 |
float inputs[2] = {xDist, yDist}; |
|
207 |
float *ans; |
|
208 |
|
|
209 |
// re-orient x and y distances based on direction |
|
210 |
switch(dir) { |
|
211 |
case UP: |
|
212 |
ans[0] = xDist; |
|
213 |
ans[1] = yDist; |
|
214 |
break; |
|
215 |
case RIGHT: |
|
216 |
ans = matrix_mult(inputs, rightMat); |
|
217 |
break; |
|
218 |
case DOWN: |
|
219 |
ans = matrix_mult(inputs, downMat); |
|
220 |
break; |
|
221 |
case LEFT: |
|
222 |
ans = matrix_mult(inputs, leftMat); |
|
223 |
break; |
|
224 |
} |
|
225 |
// indices into the map |
|
226 |
int pixDistX = row + mm_to_idx(ans[0]); |
|
227 |
int pixDistY = col + mm_to_idx(ans[1]); |
|
228 |
// plot to map if indices are valid |
|
229 |
if (0 <= pixDistX && pixDistX < MAP_LENGTH |
|
230 |
&& 0 <= pixDistY && pixDistY < MAP_LENGTH) |
|
231 |
map[pixDistX][pixDistY] = SEEN; |
|
369 | 232 |
} |
370 |
|
|
371 | 233 |
return true; |
372 | 234 |
} |
373 | 235 |
|
scout/libscout/src/test_behaviors/smart_runaround.h | ||
---|---|---|
26 | 26 |
#ifndef _SMART_RUNAROUND_H_ |
27 | 27 |
#define _SMART_RUNAROUND_H_ |
28 | 28 |
|
29 |
#include <math.h> |
|
29 | 30 |
#include "../behaviors/line_follow.h" |
30 | 31 |
/* Details about map: |
31 | 32 |
* 1 meter = 200 pixels |
... | ... | |
45 | 46 |
void run(); |
46 | 47 |
private: |
47 | 48 |
int choose_direc(int row, int col, int info); |
48 |
bool solve(int row, int col, int dir); |
|
49 | 49 |
void turn_from_to(int current_dir, int intended_dir); |
50 | 50 |
bool look_around(int row, int col, int dir); |
51 | 51 |
bool at_destination(); |
... | ... | |
60 | 60 |
* map even if it starts at a boundary and goes to |
61 | 61 |
* the other side. |
62 | 62 |
* Rows top to bottom, and columns left to right |
63 |
*/ |
|
63 |
*/
|
|
64 | 64 |
int map[MAP_LENGTH][MAP_LENGTH]; |
65 | 65 |
}; |
66 | 66 |
#endif |
scout/scoutsim/src/sim_frame.cpp | ||
---|---|---|
87 | 87 |
|
88 | 88 |
wxBitmap lines_bitmap; |
89 | 89 |
wxBitmap walls_bitmap; |
90 |
ROS_INFO("Loading map: %s", display_map_name.c_str()); |
|
90 | 91 |
path_bitmap.LoadFile(wxString::FromAscii(display_map_name.c_str())); |
91 | 92 |
|
92 | 93 |
// Try to load the file; if it fails, make a new blank file |
Also available in: Unified diff