Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ 7b5ea072

History | View | Annotate | Download (3.75 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.8);
34

    
35
void line_follow::follow_line()
36
{
37
    vector<uint32_t> readings;
38
    do
39
    {
40
        double line_loc = linesensor->readline();
41

    
42
        if (line_loc == 0.0)
43
        {
44
            motors->set_sides(-60, -60, MOTOR_ABSOLUTE);
45
        }
46
        else
47
        {
48
            motor_l = min(max((int) (-MOTOR_BASE + KP * line_loc), -128), 127);
49
            motor_r = min(max((int) (-MOTOR_BASE - KP * line_loc), -128), 127);
50

    
51
            motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
52
        }
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
    motors->set_sides(-MOTOR_BASE, -MOTOR_BASE, MOTOR_ABSOLUTE);
67
    readings = linesensor->query();
68
  }
69
  while(!linesensor->fullline(readings) &&
70
        !linesensor->destination(readings) &&
71
        ok());
72
}
73

    
74
void line_follow::turn_left()
75
{
76
  bool first = true;
77
  float line_loc;
78
  do
79
  {
80
    motor_l = -MOTOR_BASE;
81
    motor_r = 0;
82

    
83
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
84

    
85
    if(first)
86
    {
87
        init_turn_time.sleep();
88
        first = false;
89
    }
90

    
91
    line_loc = linesensor->readline();
92
  }
93
  while(!(-1 < line_loc && line_loc < 1) && ok());
94
}
95

    
96
void line_follow::halt()
97
{
98
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
99
}
100

    
101
void line_follow::spot_turn()
102
{
103
  bool first = true;
104
  float initial_loc = linesensor->readline();  
105
  float line_loc = initial_loc;
106

    
107
  do
108
  {
109
    motor_l = MOTOR_BASE;
110
    motor_r = -MOTOR_BASE;
111

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

    
114
    if(first)
115
    {
116
        init_turn_time.sleep();
117
        do 
118
        {
119
            line_loc = linesensor->readline();
120
        }
121
        while ((abs(line_loc - initial_loc) < 0.0001 ||
122
                (line_loc < -2 || line_loc > 2)) && ok());
123
        first = false;
124
    }
125
    line_loc = linesensor->readline();
126
  } 
127
  while(!(-1 < line_loc && line_loc < 1) && ok());
128
    //keep repeating until line_loc gets a fresh reading
129
}
130

    
131
void line_follow::turn_right()
132
{
133
  bool first = true;
134
  float line_loc;
135
  do
136
  {
137
    motor_l = 0;
138
    motor_r = -MOTOR_BASE;
139

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

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

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

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

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