Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / maze_solve.cpp @ f878b5f9

History | View | Annotate | Download (6.45 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
#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
static int map[60][60];
37
#define WALL -1
38
#define UNSEEN 0
39
#define SEEN 1
40
#define CRITICAL 2
41
// facings
42
#define UP 0
43
#define RIGHT 1
44
#define DOWN 2
45
#define LEFT 3
46
void maze_solve::run(){
47
    
48
    // TODO:first initialize map to all 0's
49
    solve(25,25, RIGHT);
50
}
51

    
52
bool maze_solve::solve(int row, int col, int dir)
53
{
54
    int intial_dir = dir;
55

    
56
    // use backtracking to solve the maze
57
    if (at_destination())
58
        return true;
59

    
60
    // this function should fill the adjacent cells around me with
61
    // wall's or paths
62
    look_around(row, col, dir);
63

    
64
    /* try go up */
65
    if (map[row-1][col] != WALL && initial_dir != UP)
66
    {
67
        // Turn up.
68
        turn_from_to(dir, UP);
69
        line_follow();
70
        // Solve recursively.
71
        bool solved = solve(row-1, col, DOWN);
72
        if (solved)
73
        {
74
            return solved;
75
        }
76
        else
77
        {
78
            //Update where we are.
79
            dir = UP;
80
        }
81
    }
82
    /* try right */
83
    if (map[row][col+1] != WALL && initial_dir != RIGHT)
84
    {
85
        // Turn right.
86
        turn_from_to(dir, RIGHT);
87
        line_follow();
88
        // Solve recursively.
89
        bool solved = solve(row, col+1, LEFT);
90
        if (solved)
91
        {
92
            return solved;
93
        }
94
        else
95
        {
96
            //Update where we are.
97
            dir = RIGHT;
98
        }
99
    }
100
    /* try down */
101
    if (map[row+1][col] != WALL && initial_dir != DOWN)
102
    {
103
        // Turn down.
104
        turn_from_to(dir, DOWN);
105
        line_follow();
106
        // Solve recursively.
107
        bool solved = solve(row+1, col, UP);
108
        if (solved)
109
        {
110
            return solved;
111
        }
112
        else
113
        {
114
            //Update where we are.
115
            dir = DOWN;
116
        }
117
    }
118
    /* try left */
119
    if (map[row][col-1] != WALL && initial_dir != LEFT)
120
    {
121
        // Turn down.
122
        turn_from_to(dir, LEFT);
123
        line_follow();
124
        // Solve recursively.
125
        bool solved = solve(row, col-1, RIGHT);
126
        if (solved)
127
        {
128
            return solved;
129
        }
130
        else
131
        {
132
            //Update where we are.
133
            dir = LEFT;
134
        }
135
    }
136
    // we have exhausted all the options. This path is clearly a
137
    // dead end. go back to where we come from and return false.
138
    turn_from_to(dir, initial_dir);
139
    line_follow();
140
    return false;
141
}
142

    
143
// this function takes in the current direction and turns the scout
144
// into it intended direction
145
void maze_solve::turn_from_to(int current_dir, int intended_dir) {
146
    switch ((4 + intended_dir - current_dir) % 4) 
147
        case 0:
148
            spot_turn();
149
            break;
150
        case 1:
151
            turn_right();
152
            break;
153
        case 2:
154
            turn_straight();
155
            break;
156
        case 3:
157
            turn_left();
158
            break;
159
}
160

    
161
void maze_solve::look_around(int row, int col, int dir)
162
{
163
    // look around current place using sonar
164
    // store whether or not
165
    // there is a wall into the map
166
    // stores at row col 2 if point is critical, 1 otherwise
167
    
168
    int* readings = get_sonar_readings();
169

    
170
    // Look to the right.
171
    int right_distance = (readings[1] + readings[0] + readings[47])/3;
172
    // Look to the front.
173
    int front_distance = (readings[11] + readings[12] + readings[13])/3;
174
    // Look to the left.
175
    int left_distance = (readings[23] + readings[24] + readings[25])/3;
176
    switch (dir)
177
    {
178
        case UP:
179
            // If the distance is less than 500, mark the area as a wall otherwise
180
            // mark it as seen.
181
            map[row][col+1] = (left_distance < 500)?WALL:SEEN;
182
            map[row+1][col] = (front_distance < 500)?WALL:SEEN;
183
            map[row][col-1] = (right_distance < 500)?WALL:SEEN;
184
            break;
185
        case RIGHT:
186
            // If the distance is less than 500, mark the area as a wall otherwise
187
            // mark it as seen.
188
            map[row+1][col] = (left_distance < 500)?WALL:SEEN;
189
            map[row][col-1] = (front_distance < 500)?WALL:SEEN;
190
            map[row-1][col] = (right_distance < 500)?WALL:SEEN;
191
            break;
192
        case DOWN:
193
            // If the distance is less than 500, mark the area as a wall otherwise
194
            // mark it as seen.
195
            map[row][col-1] = (left_distance < 500)?WALL:SEEN;
196
            map[row-1][col] = (front_distance < 500)?WALL:SEEN;
197
            map[row][col+1] = (right_distance < 500)?WALL:SEEN;
198
            break;
199
        case LEFT:
200
            // If the distance is less than 500, mark the area as a wall otherwise
201
            // mark it as seen.
202
            map[row-1][col] = (left_distance < 500)?WALL:SEEN;
203
            map[row][col+1] = (front_distance < 500)?WALL:SEEN;
204
            map[row+1][col] = (right_distance < 500)?WALL:SEEN;
205
            break;
206
    }
207
}
208

    
209
bool maze_solve::at_destination() 
210
{
211
    vector<uint32_t> readings = linesensor->query();
212
    if ( readings[0] > 200 &&
213
         readings[1] < 55 &&
214
         readings[2] < 55 &&
215
         readings[3] > 200 &&
216
         readings[4] > 200 &&
217
         readings[5] < 55 &&
218
         readings[6] < 55 &&
219
         readings[7] > 200 )
220
    {
221
        return true;
222
    }
223
    return false;
224
}