root / scout / libscout / src / behaviors / line_follow.cpp @ ddfeb111
History | View | Annotate | Download (3.26 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 |
motor_l = -MOTOR_BASE + SCALE * line_loc; |
42 |
motor_r = -MOTOR_BASE - SCALE * line_loc; |
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 = MOTOR_BASE/8;
|
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 |
while(line_loc < 2 && line_loc > -2) |
104 |
line_loc = linesensor->readline(); |
105 |
|
106 |
first = false;
|
107 |
} |
108 |
line_loc = linesensor->readline(); |
109 |
} |
110 |
while(!(-1 < line_loc && line_loc < 1)); |
111 |
} |
112 |
|
113 |
void line_follow::turn_right()
|
114 |
{ |
115 |
bool first = true; |
116 |
float line_loc;
|
117 |
do
|
118 |
{ |
119 |
motor_l = MOTOR_BASE/8;
|
120 |
motor_r = -MOTOR_BASE; |
121 |
|
122 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
123 |
|
124 |
if(first)
|
125 |
{ |
126 |
init_turn_time.sleep(); |
127 |
first = false;
|
128 |
} |
129 |
|
130 |
line_loc = linesensor->readline(); |
131 |
} |
132 |
while(!(-1 < line_loc && line_loc < 1)); |
133 |
} |
134 |
|
135 |
void line_follow::u_turn()
|
136 |
{ |
137 |
turn_right(); |
138 |
follow_line(); |
139 |
turn_right(); |
140 |
} |
141 |
|
142 |
void line_follow::run()
|
143 |
{ |
144 |
follow_line(); |
145 |
turn_right(); |
146 |
follow_line(); |
147 |
turn_straight(); |
148 |
motors->set_sides(-MOTOR_BASE, -MOTOR_BASE, MOTOR_ABSOLUTE); |
149 |
Duration meow(2);
|
150 |
meow.sleep(); |
151 |
spot_turn(); |
152 |
follow_line(); |
153 |
} |