Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / libscout / src / behaviors / line_follow.cpp @ 6ee555a3

History | View | Annotate | Download (3.28 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
    do
38
    {
39
        double line_loc = linesensor->readline();
40

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

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

    
50
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
51
    }
52
    while(!linesensor->fullline());
53
    halt();
54
}
55

    
56
void line_follow::turn_straight()
57
{
58
  do
59
  {
60
    motor_l = -MOTOR_BASE;
61
    motor_r = -MOTOR_BASE;
62

    
63
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
64
  }
65
  while(linesensor->fullline());
66
}
67

    
68
void line_follow::turn_left()
69
{
70
  bool first = true;
71
  float line_loc;
72
  do
73
  {
74
    motor_l = -MOTOR_BASE;
75
    motor_r = 2*MOTOR_BASE/5;
76

    
77
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
78

    
79
    if(first)
80
    {
81
        init_turn_time.sleep();
82
        first = false;
83
    }
84

    
85
    line_loc = linesensor->readline();
86
  }
87
  while(!(-1 < line_loc && line_loc < 1));
88
}
89

    
90
void line_follow::halt()
91
{
92
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
93
}
94

    
95
void line_follow::spot_turn()
96
{
97
  bool first = true;
98
  float line_loc;
99
  do
100
  {
101
    motor_l = MOTOR_BASE;
102
    motor_r = -MOTOR_BASE;
103

    
104
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
105

    
106
    if(first)
107
    {
108
        do
109
        {
110
          line_loc = linesensor->readline();
111
        }
112
        while(line_loc < 2 && line_loc > -2);
113

    
114
        first = false;
115
    }
116
    line_loc = linesensor->readline();
117
  }
118
  while(!(-1 < line_loc && line_loc < 1));
119
}
120

    
121
void line_follow::turn_right()
122
{
123
  bool first = true;
124
  float line_loc;
125
  do
126
  {
127
    motor_l = 2*MOTOR_BASE/5;
128
    motor_r = -MOTOR_BASE;
129

    
130
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
131

    
132
    if(first)
133
    {
134
        init_turn_time.sleep();
135
        first = false;
136
    }
137

    
138
    line_loc = linesensor->readline();
139
  }
140
  while(!(-1 < line_loc && line_loc < 1));
141
}
142

    
143
void line_follow::u_turn()
144
{
145
  turn_right();
146
  follow_line();
147
  turn_right();
148
}
149

    
150
void line_follow::run()
151
{
152
  while(ok())
153
  {
154
    follow_line();
155
  }
156
}