Project

General

Profile

Revision 4026134b

ID4026134b3992a1c11aa320bef116820fab7547b0
Parent 4bdd00ba
Child c33e5aa4

Added by Alex Zirbel about 11 years ago

Added Behavior::wait() to spinOnce for a duration.

Also added maze_solve_simple which always turns right.

View differences:

scout/libscout/src/Behavior.cpp
106 106
    ros::spinOnce();
107 107
    return;
108 108
}
109

  
110
/**
111
 * Calls ros::spinOnce() in a loop until the duration is up.
112
 */
113
void Behavior::wait(float duration)
114
{
115
    ros::Rate r(WAIT_HZ);
116
    int ticks = int(duration * 100);
117
    for (int i = 0; i < ticks; i++)
118
    {
119
        spinOnce();
120
        r.sleep();
121
    }
122
}
scout/libscout/src/Behavior.h
56 56
typedef ros::Time Time;
57 57
typedef ros::Duration Duration;
58 58

  
59
#define WAIT_HZ 100     // Frequency of spinOnce() calls in wait()
60

  
59 61
class Behavior
60 62
{
61 63
    public:
......
98 100
        static bool ok();
99 101
        static void spin();
100 102
        static void spinOnce();
103

  
104
        static void wait(float duration);
101 105
};
102 106

  
103 107
#endif
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_simple>);
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_simple.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_simple.cpp
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
}
scout/libscout/src/test_behaviors/maze_solve_simple.h
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
#ifndef _MAZE_SOLVE_SIMPLE_H_
27
#define _MAZE_SOLVE_SIMPLE_H_
28

  
29
#include <stdio.h>
30
#include "../behaviors/line_follow.h"
31

  
32
/**
33
 * A very simple maze solver that always turns right.
34
 */
35
class maze_solve_simple: public line_follow
36
{
37
    public:
38
        maze_solve_simple(std::string scoutname, Sensors* sensors):
39
            line_follow(scoutname, "maze_solve_simple", sensors) {};
40
        void run();
41
    private:
42
        bool at_destination();
43
};
44
#endif
45

  

Also available in: Unified diff