Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ af7e0f94

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

    
28
using namespace std;
29

    
30
static int motor_l;
31
static int motor_r;
32

    
33
Duration init_turn_time(0.4);
34

    
35
void line_follow::follow_line()
36
{
37
    vector<uint32_t> readings;
38
    do
39
    {
40
        double line_loc = linesensor->readline();
41
        ROS_INFO("Line loc: %lf", line_loc);
42

    
43
        if (line_loc == 0.0)
44
        {
45
            motors->set_sides(-60, -60, MOTOR_ABSOLUTE);
46
            continue;
47
        }
48

    
49
        motor_l = min(max((int) (-MOTOR_BASE + KP * line_loc), -128), 127);
50
        motor_r = min(max((int) (-MOTOR_BASE - KP * line_loc), -128), 127);
51

    
52
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
53
        readings = linesensor->query();
54
    }
55
    while(!linesensor->fullline(readings) &&
56
          !linesensor->destination(readings) &&
57
          ok());
58
    halt();
59
}
60

    
61
void line_follow::turn_straight()
62
{
63
  vector<uint32_t> readings;
64
  do
65
  {
66
    motor_l = -MOTOR_BASE;
67
    motor_r = -MOTOR_BASE;
68

    
69
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
70
    readings = linesensor->query();
71
  }
72
  while(!linesensor->fullline(readings) &&
73
        !linesensor->destination(readings) &&
74
        ok());
75
}
76

    
77
void line_follow::turn_left()
78
{
79
  bool first = true;
80
  float line_loc;
81
  do
82
  {
83
    motor_l = -MOTOR_BASE;
84
    motor_r = 2*MOTOR_BASE/5;
85

    
86
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
87

    
88
    if(first)
89
    {
90
        init_turn_time.sleep();
91
        first = false;
92
    }
93

    
94
    line_loc = linesensor->readline();
95
  }
96
  while(!(-1 < line_loc && line_loc < 1) && ok());
97
}
98

    
99
void line_follow::halt()
100
{
101
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
102
}
103

    
104
void line_follow::spot_turn()
105
{
106
  bool first = true;
107
  float line_loc;
108
  do
109
  {
110
    motor_l = MOTOR_BASE;
111
    motor_r = -MOTOR_BASE;
112

    
113
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
114

    
115
    if(first)
116
    {
117
        do
118
        {
119
          line_loc = linesensor->readline();
120
        }
121
        while(line_loc < 2 && line_loc > -2 && ok());
122

    
123
        first = false;
124
    }
125
    line_loc = linesensor->readline();
126
  }
127
  while(!(-1 < line_loc && line_loc < 1) && ok());
128
}
129

    
130
void line_follow::turn_right()
131
{
132
  bool first = true;
133
  float line_loc;
134
  do
135
  {
136
    motor_l = 2*MOTOR_BASE/5;
137
    motor_r = -MOTOR_BASE;
138

    
139
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
140

    
141
    if(first)
142
    {
143
        init_turn_time.sleep();
144
        first = false;
145
    }
146

    
147
    line_loc = linesensor->readline();
148
  }
149
  while(!(-1 < line_loc && line_loc < 1) && ok());
150
}
151

    
152
void line_follow::u_turn()
153
{
154
  turn_right();
155
  follow_line();
156
  turn_right();
157
}
158

    
159
void line_follow::run()
160
{
161
  while(ok())
162
  {
163
    follow_line();
164
  }
165
}