Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ 2fd5122a

History | View | Annotate | Download (3.54 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 line_loc;
105
  do
106
  {
107
    motor_l = MOTOR_BASE;
108
    motor_r = -MOTOR_BASE;
109

    
110
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
111

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

    
120
        first = false;
121
    }
122
    line_loc = linesensor->readline();
123
  }
124
  while(!(-1 < line_loc && line_loc < 1) && ok());
125
}
126

    
127
void line_follow::turn_right()
128
{
129
  bool first = true;
130
  float line_loc;
131
  do
132
  {
133
    motor_l = 0;
134
    motor_r = -MOTOR_BASE;
135

    
136
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
137

    
138
    if(first)
139
    {
140
        init_turn_time.sleep();
141
        first = false;
142
    }
143

    
144
    line_loc = linesensor->readline();
145
  }
146
  while(!(-1 < line_loc && line_loc < 1) && ok());
147
}
148

    
149
void line_follow::u_turn()
150
{
151
  turn_right();
152
  follow_line();
153
  turn_right();
154
}
155

    
156
void line_follow::run()
157
{
158
  while(ok())
159
  {
160
    follow_line();
161
  }
162
}