Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / test_behaviors / maze_solve.cpp @ 7f2ccb46

History | View | Annotate | Download (7.79 KB)

1 64ee446f Yuyang
/**
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
#include "maze_solve.h"
27
28
using namespace std;
29
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
45 754da79f Priya
46 7f2ccb46 Alex
// TODO This is bad! It's defined globally across all files. Please put it inside a good scope. -Alex
47 6ee555a3 Priya
Duration sonar_update_time(1.5);
48 c840fbe6 Priya
49 a69f6363 viki
void maze_solve::run()
50
{    
51 64ee446f Yuyang
    // TODO:first initialize map to all 0's
52 2b0c2534 Priya
    ROS_INFO("Starting to solve the maze");
53
    // Go up to the first line.
54
    follow_line();
55
    // Turn the sonar on.
56
    sonar->set_on();
57
    sonar->set_range(0, 23);
58
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 64ee446f Yuyang
}
74 f878b5f9 Priya
75
bool maze_solve::solve(int row, int col, int dir)
76
{
77 754da79f Priya
    int initial_dir = dir;
78 f878b5f9 Priya
79 c840fbe6 Priya
    ROS_INFO("I am at direction %d", dir);
80
81 64ee446f Yuyang
    // use backtracking to solve the maze
82 f878b5f9 Priya
    if (at_destination())
83 64ee446f Yuyang
        return true;
84
85 c840fbe6 Priya
    // Wait for sonar to update.
86
    sonar_update_time.sleep();
87
88 f878b5f9 Priya
    // this function should fill the adjacent cells around me with
89
    // wall's or paths
90 2b0c2534 Priya
    while(!look_around(row, col, dir) && ok())
91
    {
92
        spinOnce();
93
    }
94 f878b5f9 Priya
95
    /* try go up */
96
    if (map[row-1][col] != WALL && initial_dir != UP)
97 64ee446f Yuyang
    {
98 c840fbe6 Priya
    ROS_INFO("GOING UP!");
99 f878b5f9 Priya
        // Turn up.
100
        turn_from_to(dir, UP);
101 754da79f Priya
        follow_line();
102 f878b5f9 Priya
        // Solve recursively.
103
        bool solved = solve(row-1, col, DOWN);
104
        if (solved)
105 64ee446f Yuyang
        {
106 f878b5f9 Priya
            return solved;
107 64ee446f Yuyang
        }
108 f878b5f9 Priya
        else
109 64ee446f Yuyang
        {
110 f878b5f9 Priya
            //Update where we are.
111
            dir = UP;
112 64ee446f Yuyang
        }
113 f878b5f9 Priya
    }
114
    /* try right */
115
    if (map[row][col+1] != WALL && initial_dir != RIGHT)
116
    {
117 c840fbe6 Priya
    ROS_INFO("GOING RIGHT!");
118 f878b5f9 Priya
        // Turn right.
119
        turn_from_to(dir, RIGHT);
120 754da79f Priya
        follow_line();
121 f878b5f9 Priya
        // Solve recursively.
122
        bool solved = solve(row, col+1, LEFT);
123
        if (solved)
124 64ee446f Yuyang
        {
125 f878b5f9 Priya
            return solved;
126
        }
127
        else
128
        {
129
            //Update where we are.
130
            dir = RIGHT;
131 64ee446f Yuyang
        }
132
    }
133 f878b5f9 Priya
    /* try down */
134
    if (map[row+1][col] != WALL && initial_dir != DOWN)
135
    {
136 c840fbe6 Priya
    ROS_INFO("GOING DOWN!");
137 f878b5f9 Priya
        // Turn down.
138
        turn_from_to(dir, DOWN);
139 754da79f Priya
        follow_line();
140 f878b5f9 Priya
        // Solve recursively.
141
        bool solved = solve(row+1, col, UP);
142
        if (solved)
143
        {
144
            return solved;
145
        }
146
        else
147
        {
148
            //Update where we are.
149
            dir = DOWN;
150
        }
151
    }
152
    /* try left */
153
    if (map[row][col-1] != WALL && initial_dir != LEFT)
154
    {
155 c840fbe6 Priya
    ROS_INFO("GOING LEFT!");
156 f878b5f9 Priya
        // Turn down.
157
        turn_from_to(dir, LEFT);
158 754da79f Priya
        follow_line();
159 f878b5f9 Priya
        // Solve recursively.
160
        bool solved = solve(row, col-1, RIGHT);
161
        if (solved)
162
        {
163
            return solved;
164
        }
165
        else
166
        {
167
            //Update where we are.
168
            dir = LEFT;
169
        }
170
    }
171 6ee555a3 Priya
172
    ROS_INFO("DEAD END FOUND, TURNING BACK.");
173 f878b5f9 Priya
    // 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 754da79f Priya
    follow_line();
177 f878b5f9 Priya
    return false;
178 64ee446f Yuyang
}
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 754da79f Priya
    {
185 64ee446f Yuyang
        case 0:
186 f878b5f9 Priya
            spot_turn();
187 64ee446f Yuyang
            break;
188
        case 1:
189 c840fbe6 Priya
            turn_left();
190 64ee446f Yuyang
            break;
191
        case 2:
192 f878b5f9 Priya
            turn_straight();
193 64ee446f Yuyang
            break;
194
        case 3:
195 c840fbe6 Priya
            turn_right();
196 64ee446f Yuyang
            break;
197 754da79f Priya
    }
198 64ee446f Yuyang
}
199
200 2b0c2534 Priya
bool maze_solve::look_around(int row, int col, int dir)
201 64ee446f Yuyang
{
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 754da79f Priya
    int* readings = sonar->get_sonar_readings();
208 2b0c2534 Priya
    spinOnce();
209 f878b5f9 Priya
210 2b0c2534 Priya
/*
211
    // Look to the left.
212
    int left_distance = (readings[1] + readings[0] + readings[47])/3;
213 f878b5f9 Priya
    // Look to the front.
214 2b0c2534 Priya
    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 f878b5f9 Priya
    // Look to the left.
219 2b0c2534 Priya
    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;
229
230 f878b5f9 Priya
    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;
260
    }
261 2b0c2534 Priya
262
    return true;
263 64ee446f Yuyang
}
264
265 f878b5f9 Priya
bool maze_solve::at_destination() 
266 64ee446f Yuyang
{
267 f878b5f9 Priya
    vector<uint32_t> readings = linesensor->query();
268
    if ( readings[0] > 200 &&
269
         readings[1] < 55 &&
270
         readings[2] < 55 &&
271
         readings[3] > 200 &&
272
         readings[4] > 200 &&
273
         readings[5] < 55 &&
274
         readings[6] < 55 &&
275
         readings[7] > 200 )
276
    {
277
        return true;
278
    }
279
    return false;
280 64ee446f Yuyang
}