Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ 3db79f25

History | View | Annotate | Download (3.26 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 afa9104d Priya
Duration init_turn_time(0.5);
34
35 58371433 Priya
void line_follow::follow_line()
36
{
37 afa9104d Priya
    do
38 58371433 Priya
    {
39
        double line_loc = linesensor->readline();
40 9143e077 Lalitha Ganesan
41 58371433 Priya
        motor_l = -MOTOR_BASE + SCALE * line_loc;
42
        motor_r = -MOTOR_BASE - SCALE * line_loc;
43 9143e077 Lalitha Ganesan
44 58371433 Priya
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
45
    }
46 afa9104d Priya
    while(!linesensor->fullline());
47 58371433 Priya
    halt();
48 1905324e Priya
    ROS_INFO("Intersection reached!");
49 58371433 Priya
}
50 9143e077 Lalitha Ganesan
51 afa9104d Priya
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 58371433 Priya
void line_follow::turn_left()
64 60b98383 Priya
{
65 58371433 Priya
  bool first = true;
66 afa9104d Priya
  float line_loc;
67 58371433 Priya
  do
68
  {
69
    motor_l = -MOTOR_BASE;
70
    motor_r = MOTOR_BASE/8;
71 60b98383 Priya
72 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
73 60b98383 Priya
74 58371433 Priya
    if(first)
75 9143e077 Lalitha Ganesan
    {
76 afa9104d Priya
        init_turn_time.sleep();
77 58371433 Priya
        first = false;
78 9143e077 Lalitha Ganesan
    }
79 afa9104d Priya
80
    line_loc = linesensor->readline();
81 60b98383 Priya
  }
82 58c19c15 Priya
  while(!(-1 < line_loc && line_loc < 1));
83 9143e077 Lalitha Ganesan
}
84
85 58371433 Priya
void line_follow::halt()
86 9143e077 Lalitha Ganesan
{
87 58371433 Priya
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
88 9143e077 Lalitha Ganesan
}
89
90 afa9104d Priya
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 58371433 Priya
void line_follow::turn_right()
114 9143e077 Lalitha Ganesan
{
115 58371433 Priya
  bool first = true;
116 afa9104d Priya
  float line_loc;
117 58371433 Priya
  do
118
  {
119
    motor_l = MOTOR_BASE/8;
120
    motor_r = -MOTOR_BASE;
121 60b98383 Priya
122 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
123 60b98383 Priya
124 58371433 Priya
    if(first)
125 60b98383 Priya
    {
126 afa9104d Priya
        init_turn_time.sleep();
127 58371433 Priya
        first = false;
128 60b98383 Priya
    }
129 afa9104d Priya
130
    line_loc = linesensor->readline();
131 60b98383 Priya
  }
132 afa9104d Priya
  while(!(-1 < line_loc && line_loc < 1));
133 9143e077 Lalitha Ganesan
}
134
135 1905324e Priya
void line_follow::u_turn()
136 9143e077 Lalitha Ganesan
{
137 1905324e Priya
  turn_right();
138 58371433 Priya
  follow_line();
139
  turn_right();
140 1905324e Priya
}
141
142
void line_follow::run()
143
{
144 58371433 Priya
  follow_line();
145 afa9104d Priya
  turn_right();
146 58371433 Priya
  follow_line();
147 afa9104d Priya
  turn_straight();
148
  motors->set_sides(-MOTOR_BASE, -MOTOR_BASE, MOTOR_ABSOLUTE);
149
  Duration meow(2);
150
  meow.sleep();
151
  spot_turn();
152 58371433 Priya
  follow_line();
153 9143e077 Lalitha Ganesan
}