root / scout / libscout / src / behaviors / line_follow.cpp @ 80224838
History | View | Annotate | Download (3.2 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(1.5); |
34 |
|
35 |
void line_follow::follow_line()
|
36 |
{ |
37 |
do
|
38 |
{ |
39 |
double line_loc = linesensor->readline();
|
40 |
|
41 |
motor_l = min(max((int) (-MOTOR_BASE + SCALE * line_loc), -128), 127); |
42 |
motor_r = min(max((int) (-MOTOR_BASE - SCALE * line_loc), -128), 127); |
43 |
|
44 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
45 |
} |
46 |
while(!linesensor->fullline());
|
47 |
halt(); |
48 |
ROS_INFO("Intersection reached!");
|
49 |
} |
50 |
|
51 |
void line_follow::turn_straight()
|
52 |
{ |
53 |
do
|
54 |
{ |
55 |
motor_l = -MOTOR_BASE; |
56 |
motor_r = -MOTOR_BASE; |
57 |
|
58 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
59 |
} |
60 |
while(linesensor->fullline());
|
61 |
} |
62 |
|
63 |
void line_follow::turn_left()
|
64 |
{ |
65 |
bool first = true; |
66 |
float line_loc;
|
67 |
do
|
68 |
{ |
69 |
motor_l = -MOTOR_BASE; |
70 |
motor_r = 3*MOTOR_BASE/5; |
71 |
|
72 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
73 |
|
74 |
if(first)
|
75 |
{ |
76 |
init_turn_time.sleep(); |
77 |
first = false;
|
78 |
} |
79 |
|
80 |
line_loc = linesensor->readline(); |
81 |
} |
82 |
while(!(-1 < line_loc && line_loc < 1)); |
83 |
} |
84 |
|
85 |
void line_follow::halt()
|
86 |
{ |
87 |
motors->set_sides(0, 0, MOTOR_ABSOLUTE); |
88 |
} |
89 |
|
90 |
void line_follow::spot_turn()
|
91 |
{ |
92 |
bool first = true; |
93 |
float line_loc;
|
94 |
do
|
95 |
{ |
96 |
motor_l = MOTOR_BASE; |
97 |
motor_r = -MOTOR_BASE; |
98 |
|
99 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
100 |
|
101 |
if(first)
|
102 |
{ |
103 |
do
|
104 |
{ |
105 |
line_loc = linesensor->readline(); |
106 |
} |
107 |
while(line_loc < 2 && line_loc > -2); |
108 |
|
109 |
first = false;
|
110 |
} |
111 |
line_loc = linesensor->readline(); |
112 |
} |
113 |
while(!(-1 < line_loc && line_loc < 1)); |
114 |
} |
115 |
|
116 |
void line_follow::turn_right()
|
117 |
{ |
118 |
bool first = true; |
119 |
float line_loc;
|
120 |
do
|
121 |
{ |
122 |
motor_l = 3*MOTOR_BASE/5; |
123 |
motor_r = -MOTOR_BASE; |
124 |
|
125 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
126 |
|
127 |
if(first)
|
128 |
{ |
129 |
init_turn_time.sleep(); |
130 |
first = false;
|
131 |
} |
132 |
|
133 |
line_loc = linesensor->readline(); |
134 |
} |
135 |
while(!(-1 < line_loc && line_loc < 1)); |
136 |
} |
137 |
|
138 |
void line_follow::u_turn()
|
139 |
{ |
140 |
turn_right(); |
141 |
follow_line(); |
142 |
turn_right(); |
143 |
} |
144 |
|
145 |
void line_follow::run()
|
146 |
{ |
147 |
while(ok())
|
148 |
{ |
149 |
follow_line(); |
150 |
} |
151 |
} |