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