Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ 6fab3966

History | View | Annotate | Download (3.22 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 6fab3966 Alex
        ROS_INFO("Line location: %lf.\n", line_loc);
42
43
        motor_l = min(max((int) (-MOTOR_BASE + SCALE * line_loc), -128), 127);
44
        motor_r = min(max((int) (-MOTOR_BASE - SCALE * line_loc), -128), 127);
45 9143e077 Lalitha Ganesan
46 58371433 Priya
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
47
    }
48 afa9104d Priya
    while(!linesensor->fullline());
49 58371433 Priya
    halt();
50 1905324e Priya
    ROS_INFO("Intersection reached!");
51 58371433 Priya
}
52 9143e077 Lalitha Ganesan
53 afa9104d Priya
void line_follow::turn_straight()
54
{
55
  do
56
  {
57
    motor_l = -MOTOR_BASE;
58
    motor_r = -MOTOR_BASE;
59
60
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
61
  }
62
  while(linesensor->fullline());
63
}
64
65 58371433 Priya
void line_follow::turn_left()
66 60b98383 Priya
{
67 58371433 Priya
  bool first = true;
68 afa9104d Priya
  float line_loc;
69 58371433 Priya
  do
70
  {
71
    motor_l = -MOTOR_BASE;
72
    motor_r = MOTOR_BASE/8;
73 60b98383 Priya
74 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
75 60b98383 Priya
76 58371433 Priya
    if(first)
77 9143e077 Lalitha Ganesan
    {
78 afa9104d Priya
        init_turn_time.sleep();
79 58371433 Priya
        first = false;
80 9143e077 Lalitha Ganesan
    }
81 afa9104d Priya
82
    line_loc = linesensor->readline();
83 60b98383 Priya
  }
84 58c19c15 Priya
  while(!(-1 < line_loc && line_loc < 1));
85 9143e077 Lalitha Ganesan
}
86
87 58371433 Priya
void line_follow::halt()
88 9143e077 Lalitha Ganesan
{
89 58371433 Priya
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
90 9143e077 Lalitha Ganesan
}
91
92 afa9104d Priya
void line_follow::spot_turn()
93
{
94
  bool first = true;
95
  float line_loc;
96
  do
97
  {
98
    motor_l = MOTOR_BASE;
99
    motor_r = -MOTOR_BASE;
100
101
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
102
103
    if(first)
104
    {
105
        while(line_loc < 2 && line_loc > -2)
106
          line_loc = linesensor->readline();
107
108
        first = false;
109
    }
110
    line_loc = linesensor->readline();
111
  }
112
  while(!(-1 < line_loc && line_loc < 1));
113
}
114
115 58371433 Priya
void line_follow::turn_right()
116 9143e077 Lalitha Ganesan
{
117 58371433 Priya
  bool first = true;
118 afa9104d Priya
  float line_loc;
119 58371433 Priya
  do
120
  {
121
    motor_l = MOTOR_BASE/8;
122
    motor_r = -MOTOR_BASE;
123 60b98383 Priya
124 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
125 60b98383 Priya
126 58371433 Priya
    if(first)
127 60b98383 Priya
    {
128 afa9104d Priya
        init_turn_time.sleep();
129 58371433 Priya
        first = false;
130 60b98383 Priya
    }
131 afa9104d Priya
132
    line_loc = linesensor->readline();
133 60b98383 Priya
  }
134 afa9104d Priya
  while(!(-1 < line_loc && line_loc < 1));
135 9143e077 Lalitha Ganesan
}
136
137 1905324e Priya
void line_follow::u_turn()
138 9143e077 Lalitha Ganesan
{
139 1905324e Priya
  turn_right();
140 58371433 Priya
  follow_line();
141
  turn_right();
142 1905324e Priya
}
143
144
void line_follow::run()
145
{
146 25694a03 Priya
  while(ok())
147
  {
148
    follow_line();
149
  }
150 9143e077 Lalitha Ganesan
}