root / scout / libscout / src / behaviors / line_follow.cpp @ 1905324e
History | View | Annotate | Download (2.56 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 |
void line_follow::follow_line()
|
34 |
{ |
35 |
while(!linesensor->fullline())
|
36 |
{ |
37 |
double line_loc = linesensor->readline();
|
38 |
|
39 |
motor_l = -MOTOR_BASE + SCALE * line_loc; |
40 |
motor_r = -MOTOR_BASE - SCALE * line_loc; |
41 |
|
42 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
43 |
} |
44 |
halt(); |
45 |
ROS_INFO("Intersection reached!");
|
46 |
} |
47 |
|
48 |
void line_follow::turn_left()
|
49 |
{ |
50 |
bool first = true; |
51 |
do
|
52 |
{ |
53 |
motor_l = -MOTOR_BASE; |
54 |
motor_r = MOTOR_BASE/8;
|
55 |
|
56 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
57 |
|
58 |
if(first)
|
59 |
{ |
60 |
loop_rate->sleep(); |
61 |
loop_rate->sleep(); |
62 |
loop_rate->sleep(); |
63 |
first = false;
|
64 |
} |
65 |
} |
66 |
while(linesensor->readline());
|
67 |
} |
68 |
|
69 |
void line_follow::halt()
|
70 |
{ |
71 |
motors->set_sides(0, 0, MOTOR_ABSOLUTE); |
72 |
} |
73 |
|
74 |
void line_follow::turn_right()
|
75 |
{ |
76 |
bool first = true; |
77 |
do
|
78 |
{ |
79 |
motor_l = MOTOR_BASE/8;
|
80 |
motor_r = -MOTOR_BASE; |
81 |
|
82 |
motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE); |
83 |
|
84 |
if(first)
|
85 |
{ |
86 |
loop_rate->sleep(); |
87 |
loop_rate->sleep(); |
88 |
loop_rate->sleep(); |
89 |
first = false;
|
90 |
} |
91 |
} |
92 |
while(linesensor->readline());
|
93 |
} |
94 |
|
95 |
void line_follow::u_turn()
|
96 |
{ |
97 |
turn_right(); |
98 |
follow_line(); |
99 |
turn_right(); |
100 |
} |
101 |
|
102 |
void line_follow::run()
|
103 |
{ |
104 |
follow_line(); |
105 |
follow_line(); |
106 |
/*
|
107 |
follow_line();
|
108 |
u_turn();
|
109 |
follow_line();
|
110 |
u_turn();
|
111 |
follow_line();
|
112 |
follow_line();
|
113 |
u_turn();
|
114 |
follow_line();
|
115 |
*/
|
116 |
} |