Project

General

Profile

Statistics
| Branch: | Revision:

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

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

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

    
41
        ROS_INFO("Line location: %lf.\n", line_loc);
42

    
43
        motor_l = min(max((int) (-MOTOR_BASE + SCALE * line_loc), -128), 127);
44
        motor_r = min(max((int) (-MOTOR_BASE - SCALE * line_loc), -128), 127);
45

    
46
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
47
    }
48
    while(!linesensor->fullline());
49
    halt();
50
    ROS_INFO("Intersection reached!");
51
}
52

    
53
void line_follow::turn_straight()
54
{
55
  do
56
  {
57
    motor_l = -MOTOR_BASE;
58
    motor_r = -MOTOR_BASE;
59

    
60
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
61
  }
62
  while(linesensor->fullline());
63
}
64

    
65
void line_follow::turn_left()
66
{
67
  bool first = true;
68
  float line_loc;
69
  do
70
  {
71
    motor_l = -MOTOR_BASE;
72
    motor_r = MOTOR_BASE/8;
73

    
74
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
75

    
76
    if(first)
77
    {
78
        init_turn_time.sleep();
79
        first = false;
80
    }
81

    
82
    line_loc = linesensor->readline();
83
  }
84
  while(!(-1 < line_loc && line_loc < 1));
85
}
86

    
87
void line_follow::halt()
88
{
89
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
90
}
91

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

    
101
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
102

    
103
    if(first)
104
    {
105
        while(line_loc < 2 && line_loc > -2)
106
          line_loc = linesensor->readline();
107

    
108
        first = false;
109
    }
110
    line_loc = linesensor->readline();
111
  }
112
  while(!(-1 < line_loc && line_loc < 1));
113
}
114

    
115
void line_follow::turn_right()
116
{
117
  bool first = true;
118
  float line_loc;
119
  do
120
  {
121
    motor_l = MOTOR_BASE/8;
122
    motor_r = -MOTOR_BASE;
123

    
124
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
125

    
126
    if(first)
127
    {
128
        init_turn_time.sleep();
129
        first = false;
130
    }
131

    
132
    line_loc = linesensor->readline();
133
  }
134
  while(!(-1 < line_loc && line_loc < 1));
135
}
136

    
137
void line_follow::u_turn()
138
{
139
  turn_right();
140
  follow_line();
141
  turn_right();
142
}
143

    
144
void line_follow::run()
145
{
146
  while(ok())
147
  {
148
    follow_line();
149
  }
150
}