root / scout / libscout / src / behaviors / navigationMap.cpp @ 11aa087a
History | View | Annotate | Download (10.7 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2011 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
*/
|
25 |
|
26 |
/**
|
27 |
* @file navigationMap.cpp
|
28 |
* @brief Contains navigation map Behavior declarations and definitions
|
29 |
*
|
30 |
* Contains functions and definitions for the use of
|
31 |
* navigation map Behavior
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
* @author Priya Deo
|
35 |
* @author Lalitha
|
36 |
* @author James
|
37 |
* @author Leon
|
38 |
**/
|
39 |
|
40 |
#include "navigationMap.h" |
41 |
|
42 |
using namespace std; |
43 |
|
44 |
/**
|
45 |
* @brief Initializes the navigation map
|
46 |
*
|
47 |
* Initialize the navigation map.
|
48 |
* The map itself is represented as a dynamic array of edge arrays
|
49 |
* @param the string name of the scout
|
50 |
*/
|
51 |
navigationMap::navigationMap(string scoutname) : Behavior(scoutname, "navigationMap") |
52 |
{ |
53 |
/** Initialize Map
|
54 |
* _____
|
55 |
* 1 2 | | 3 4
|
56 |
* ----|-----------|--|----|--|---------|---------->
|
57 |
* <---|--5--------|--6-------|--7------|--8-------
|
58 |
* | | | |
|
59 |
* 9| 10| 11| 12|
|
60 |
* | | | |
|
61 |
* ---13 ---14 ---15 ---16
|
62 |
*/
|
63 |
|
64 |
Edge* a1 = new Edge[ARRAY_SIZE];
|
65 |
a1[0] = MAKE_EDGE(ISTRAIGHT, 2, 10); |
66 |
a1[1] = MAKE_EDGE(IRIGHT, 13, 40); |
67 |
a1[2] = MAKE_EDGE(IUTURN, DEADEND, 0); |
68 |
|
69 |
Edge* a2 = new Edge[ARRAY_SIZE];
|
70 |
a2[0] = MAKE_EDGE(ISTRAIGHT, 3, 10); |
71 |
a2[1] = MAKE_EDGE(IRIGHT, 14, 40); |
72 |
a2[2] = MAKE_EDGE(IUTURN, 5, 10); |
73 |
|
74 |
Edge* a3 = new Edge[ARRAY_SIZE];
|
75 |
a3[0] = MAKE_EDGE(ISTRAIGHT, 4, 10); |
76 |
a3[1] = MAKE_EDGE(IRIGHT, 15, 40); |
77 |
a3[2] = MAKE_EDGE(IUTURN, 6, 10); |
78 |
|
79 |
Edge* a4 = new Edge[ARRAY_SIZE];
|
80 |
a4[0] = MAKE_EDGE(ISTRAIGHT, DEADEND, 0); |
81 |
a4[1] = MAKE_EDGE(IRIGHT, 16, 40); |
82 |
a4[2] = MAKE_EDGE(IUTURN, 7, 10); |
83 |
|
84 |
Edge* a5 = new Edge[ARRAY_SIZE];
|
85 |
a5[0] = MAKE_EDGE(ISTRAIGHT, DEADEND, 0); |
86 |
a5[1] = MAKE_EDGE(ILEFT, 13, 40); |
87 |
a5[2] = MAKE_EDGE(IUTURN, 2, 10); |
88 |
|
89 |
Edge* a6 = new Edge[ARRAY_SIZE];
|
90 |
a6[0] = MAKE_EDGE(ISTRAIGHT, 5, 0); |
91 |
a6[1] = MAKE_EDGE(ILEFT, 14, 40); |
92 |
a6[2] = MAKE_EDGE(IUTURN, 3, 10); |
93 |
|
94 |
Edge* a7 = new Edge[ARRAY_SIZE];
|
95 |
a7[0] = MAKE_EDGE(ISTRAIGHT, 6, 0); |
96 |
a7[1] = MAKE_EDGE(ILEFT, 15, 40); |
97 |
a7[2] = MAKE_EDGE(IUTURN, 4, 10); |
98 |
|
99 |
Edge* a8 = new Edge[ARRAY_SIZE];
|
100 |
a8[0] = MAKE_EDGE(ISTRAIGHT, 7, 0); |
101 |
a8[1] = MAKE_EDGE(ILEFT, 16, 40); |
102 |
a8[2] = MAKE_EDGE(IUTURN, DEADEND, 10); |
103 |
|
104 |
Edge* a9 = new Edge[ARRAY_SIZE];
|
105 |
a9[0] = MAKE_EDGE(IRIGHT, 2, 10); |
106 |
a9[1] = MAKE_EDGE(ILEFT, DEADEND, 0); |
107 |
a9[2] = MAKE_EDGE(ISPOTTURN, 13, 40); |
108 |
|
109 |
Edge* a10 = new Edge[ARRAY_SIZE];
|
110 |
a10[0] = MAKE_EDGE(IRIGHT, 3, 10); |
111 |
a10[1] = MAKE_EDGE(ILEFT, 5, 10); |
112 |
a10[2] = MAKE_EDGE(ISPOTTURN, 14, 40); |
113 |
|
114 |
Edge* a11 = new Edge[ARRAY_SIZE];
|
115 |
a11[0] = MAKE_EDGE(IRIGHT, 4, 10); |
116 |
a11[1] = MAKE_EDGE(ILEFT, 6, 10); |
117 |
a11[2] = MAKE_EDGE(ISPOTTURN, 15, 40); |
118 |
|
119 |
Edge* a12 = new Edge[ARRAY_SIZE];
|
120 |
a12[0] = MAKE_EDGE(IRIGHT, DEADEND, 0); |
121 |
a12[1] = MAKE_EDGE(ILEFT, 7, 10); |
122 |
a12[2] = MAKE_EDGE(ISPOTTURN, 16, 40); |
123 |
|
124 |
Edge* a13 = new Edge[ARRAY_SIZE];
|
125 |
a13[0] = MAKE_EDGE(IRIGHT, DEADEND, 0); |
126 |
a13[1] = MAKE_EDGE(ILEFT, DEADEND, 0); |
127 |
a13[2] = MAKE_EDGE(ISPOTTURN, 9, 40); |
128 |
|
129 |
Edge* a14 = new Edge[ARRAY_SIZE];
|
130 |
a14[0] = MAKE_EDGE(IRIGHT, DEADEND, 0); |
131 |
a14[1] = MAKE_EDGE(ILEFT, DEADEND, 0); |
132 |
a14[2] = MAKE_EDGE(ISPOTTURN, 10, 40); |
133 |
|
134 |
Edge* a15 = new Edge[ARRAY_SIZE];
|
135 |
a15[0] = MAKE_EDGE(IRIGHT, DEADEND, 0); |
136 |
a15[1] = MAKE_EDGE(ILEFT, DEADEND, 0); |
137 |
a15[2] = MAKE_EDGE(ISPOTTURN, 11, 40); |
138 |
|
139 |
Edge* a16 = new Edge[ARRAY_SIZE];
|
140 |
a16[0] = MAKE_EDGE(IRIGHT, DEADEND, 0); |
141 |
a16[1] = MAKE_EDGE(ILEFT, DEADEND, 0); |
142 |
a16[2] = MAKE_EDGE(ISPOTTURN, 12, 40); |
143 |
|
144 |
|
145 |
map.push_back(a1); |
146 |
map.push_back(a2); |
147 |
map.push_back(a3); |
148 |
map.push_back(a4); |
149 |
map.push_back(a5); |
150 |
map.push_back(a6); |
151 |
map.push_back(a7); |
152 |
map.push_back(a8); |
153 |
map.push_back(a9); |
154 |
map.push_back(a10); |
155 |
map.push_back(a11); |
156 |
map.push_back(a12); |
157 |
map.push_back(a13); |
158 |
map.push_back(a14); |
159 |
map.push_back(a15); |
160 |
map.push_back(a16); |
161 |
|
162 |
curr_state = START_STATE; |
163 |
arrival_time = ros::TIME_MAX; |
164 |
} |
165 |
|
166 |
/** @brief Goes through and frees all allocated memory */
|
167 |
navigationMap::~navigationMap() |
168 |
{ |
169 |
while(!map.empty())
|
170 |
{ |
171 |
Edge* temp = map.back(); |
172 |
map.pop_back(); |
173 |
delete temp;
|
174 |
} |
175 |
return;
|
176 |
} |
177 |
|
178 |
/** @brief FSM implementation*/
|
179 |
void navigationMap::run()
|
180 |
{ |
181 |
Duration t; |
182 |
|
183 |
ROS_INFO("Going to state 16\n");
|
184 |
Path path = shortest_path(16);
|
185 |
if(path.path == NULL) |
186 |
{ |
187 |
ROS_WARN("There is no path to state 16");
|
188 |
return;
|
189 |
} |
190 |
|
191 |
ROS_INFO("Worst case time to 16 is %d", get_worst_case_time(curr_state, 6).sec); |
192 |
|
193 |
for(int i=0; i<path.len; i++) |
194 |
{ |
195 |
update_state(path.path[i]); |
196 |
ROS_INFO("Made turn %d, at state %d\n", path.path[i], curr_state);
|
197 |
t = get_time_remaining(); |
198 |
while(t.sec > 0) |
199 |
t = get_time_remaining(); |
200 |
ROS_INFO("Now at state %d\n", curr_state);
|
201 |
} |
202 |
|
203 |
ROS_INFO("Traveled route!\n");
|
204 |
|
205 |
while(ok())
|
206 |
continue;
|
207 |
} |
208 |
|
209 |
/**@brief sets the current state to the state associated with the turn made
|
210 |
* @param the Turn that we made from our state
|
211 |
* @return our new State after making the turn
|
212 |
*/
|
213 |
State navigationMap::update_state(Turn turn_made) |
214 |
{ |
215 |
Edge* possible_edges = get_outbound_edges(curr_state); |
216 |
for(int i=0;i<ARRAY_SIZE;i++) |
217 |
{ |
218 |
//sets the current state to the state associated with the turn made
|
219 |
if(GET_EDGE_DIR(possible_edges[i]) == turn_made)
|
220 |
{ |
221 |
//TODO: get actual speed
|
222 |
int speed = 10; |
223 |
curr_state = GET_EDGE_STATE(possible_edges[i]); |
224 |
Duration travel_time(GET_EDGE_DIST(possible_edges[i])/speed); |
225 |
arrival_time = Time::now() + travel_time; |
226 |
return curr_state;
|
227 |
} |
228 |
} |
229 |
return -1;//failure to succeed |
230 |
} |
231 |
|
232 |
/**@brief returns the predicted time of arrival for our current task
|
233 |
* @return the predicted time of arrival for our current task
|
234 |
*/
|
235 |
Time navigationMap::get_eta() |
236 |
{ |
237 |
return arrival_time;
|
238 |
} |
239 |
|
240 |
/**@brief returns the predicted amount of time it will take to finish our task
|
241 |
* @return the predicted amount of time it will take to finish our task
|
242 |
*/
|
243 |
Duration navigationMap::get_time_remaining() |
244 |
{ |
245 |
return (arrival_time - Time::now());
|
246 |
} |
247 |
|
248 |
/**@brief returns the current state of the scout in the map
|
249 |
* @return the current State (ie: int) of the scout in the map
|
250 |
*/
|
251 |
State navigationMap::get_state() |
252 |
{ |
253 |
return curr_state;
|
254 |
} |
255 |
|
256 |
/**@brief returns the Edges connecting from a given State
|
257 |
* @param the State whose edges we want to get
|
258 |
* @return the Edges connecting from the given State
|
259 |
*/
|
260 |
Edge* navigationMap::get_outbound_edges(State state) |
261 |
{ |
262 |
return map.at(state-1); |
263 |
} |
264 |
|
265 |
/**@brief uses BFS to find the shortest path to a target State node
|
266 |
* @param target_state the State that we want to get to
|
267 |
* @return a Path struct containing the Turn* to take to get to the target state
|
268 |
*/
|
269 |
Path navigationMap::shortest_path(State target_state) |
270 |
{ |
271 |
return shortest_path(curr_state, target_state);
|
272 |
} |
273 |
|
274 |
/**@brief uses BFS to find the shortest path to a target State node
|
275 |
* @param start_state the State that we start from
|
276 |
* @param target_state the State that we want to get to
|
277 |
* @return a Path struct containing the Turn* to take to get to the target state
|
278 |
*/
|
279 |
Path navigationMap::shortest_path(State start_state, State target_state) |
280 |
{ |
281 |
// BFS algorithm
|
282 |
State curr_state = start_state; |
283 |
int visited[MAX_NODES+1] = {0}; |
284 |
|
285 |
queue<State> q; |
286 |
q.push(curr_state); |
287 |
// not zero = visited, zero = unvisited, negative = start state
|
288 |
visited[curr_state] = -1;
|
289 |
|
290 |
while (!q.empty())
|
291 |
{ |
292 |
State state = q.front(); |
293 |
//actually dequeue it
|
294 |
q.pop(); |
295 |
if (state == target_state)
|
296 |
{ |
297 |
Path path; |
298 |
path.path = (Turn*)calloc(sizeof(Turn), MAX_NODES);
|
299 |
int j = 0; // counter |
300 |
for(State child = state; visited[child] >= 0; |
301 |
child = visited[child]) //while not start state
|
302 |
{ |
303 |
State parent = visited[child]; |
304 |
Edge* edges = get_outbound_edges(parent); |
305 |
for (int i = 0; i < ARRAY_SIZE; i++) |
306 |
{ |
307 |
if (GET_EDGE_STATE(edges[i]) == child)
|
308 |
{ |
309 |
path.path[j] = GET_EDGE_DIR(edges[i]); |
310 |
j++; |
311 |
break;
|
312 |
} |
313 |
} |
314 |
} |
315 |
/** Reverse moves list */
|
316 |
for (int i = 0; i < j/2; i++) |
317 |
{ |
318 |
path.path[i] ^= path.path[j-i-1];
|
319 |
path.path[j-i-1] ^= path.path[i];
|
320 |
path.path[i] ^= path.path[j-i-1];
|
321 |
} |
322 |
path.len = j; |
323 |
return path;
|
324 |
} |
325 |
Edge* edges = get_outbound_edges(state); |
326 |
|
327 |
for (int i = 0; i < ARRAY_SIZE; i++) |
328 |
{ |
329 |
State new_state = GET_EDGE_STATE(edges[i]); |
330 |
if (new_state != DEADEND && !visited[new_state])
|
331 |
{ |
332 |
// set this state in visited as the parent of the new_state
|
333 |
visited[new_state] = state; |
334 |
q.push(new_state); |
335 |
} |
336 |
} |
337 |
} |
338 |
//oops, no way to get to target from state
|
339 |
Path path; |
340 |
path.len = 0;
|
341 |
path.path = NULL;
|
342 |
return path;
|
343 |
} |
344 |
|
345 |
/** @brief returns worst case time to travel to dest
|
346 |
*
|
347 |
* Takes into account speed of robot and interactions with other robots
|
348 |
*
|
349 |
* @param start_state Node that we start at
|
350 |
* @param target_state Node that we end up at
|
351 |
* @return the worst case time to travel to target node
|
352 |
*/
|
353 |
Duration navigationMap::get_worst_case_time(State start_state, State target_state) |
354 |
{ |
355 |
Path path = shortest_path(start_state, target_state); |
356 |
Duration worst_case_time(0);
|
357 |
|
358 |
State curr_state = start_state; |
359 |
//keep iterating over path while there are turns
|
360 |
for(int i=0; i<path.len; i++) |
361 |
{ |
362 |
Edge* edges = get_outbound_edges(curr_state); |
363 |
for(int j=0; j<ARRAY_SIZE; j++) |
364 |
{ |
365 |
if(GET_EDGE_DIR(edges[j]) == path.path[i])
|
366 |
{ |
367 |
Duration turn_time(TURN_TIME + (GET_EDGE_DIST(edges[j])/SPEED)); |
368 |
worst_case_time += turn_time; |
369 |
} |
370 |
} |
371 |
} |
372 |
Duration additional_time(DROPOFF_TIME + WAIT_TIME); |
373 |
worst_case_time += additional_time; |
374 |
|
375 |
return worst_case_time;
|
376 |
} |