Statistics
| Branch: | Revision:

root / scout / libscout / src / test_behaviors / maze_solve_simple.cpp @ 4026134b

History | View | Annotate | Download (3.08 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_simple.h"
27

    
28
#define D_THRESH 600
29
#define max(x,y) ((x > y) ? x : y)
30

    
31
using namespace std;
32

    
33
void maze_solve_simple::run()
34
{    
35
    ROS_INFO("Starting to solve the maze");
36

    
37
    // Go up to the first line.
38
    follow_line();
39

    
40
    // Turn the sonar on.
41
    sonar->set_on();
42
    sonar->set_range(0, 23);
43

    
44
    while (!at_destination())
45
    {
46
        // Wait for sonar to update.
47
        wait(1.5);
48
        int* readings = sonar->get_sonar_readings();
49
        
50
        // Wait until the sonar gives us real values.
51
        bool readings_ok = true;
52
        for (int i = 0; i < 48; i++)
53
        {
54
            if (readings[i] == 0)
55
            {
56
                readings_ok = false;
57
                break;
58
            }
59
        }
60
        if (!readings_ok)
61
        {
62
            continue;
63
        }
64

    
65
        int r_read = max(readings[23], max(readings[24], readings[25]));
66
        int s_read = max(readings[35], max(readings[36], readings[37]));
67
        int l_read = max(readings[47], max(readings[0],  readings[1]));
68

    
69
        if (r_read > D_THRESH)      // Right
70
        {
71
            ROS_INFO("Right.");
72
            turn_right();
73
        }
74
        else if (s_read > D_THRESH) // Straight
75
        {
76
            ROS_INFO("Straight.");
77
            turn_straight();
78
        }
79
        else if (l_read > D_THRESH)   // Left
80
        {
81
            ROS_INFO("Left.");
82
            turn_left();
83
        }
84
        else                                                        // Deadend
85
        {
86
            ROS_INFO("Dead end.");
87
            spot_turn();
88
        }
89

    
90
        follow_line();
91
    }
92

    
93
    ROS_INFO("I have solved the maze!");
94
}
95

    
96
bool maze_solve_simple::at_destination() 
97
{
98
    vector<uint32_t> readings = linesensor->query();
99
    if ( readings[0] > 200 &&
100
         readings[1] < 55 &&
101
         readings[2] < 55 &&
102
         readings[3] > 200 &&
103
         readings[4] > 200 &&
104
         readings[5] < 55 &&
105
         readings[6] < 55 &&
106
         readings[7] > 200 )
107
    {
108
        return true;
109
    }
110
    return false;
111
}