Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / libscout / src / behaviors / line_follow.cpp @ af7e0f94

History | View | Annotate | Download (3.63 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 6ee555a3 Priya
Duration init_turn_time(0.4);
34 afa9104d Priya
35 58371433 Priya
void line_follow::follow_line()
36
{
37 af7e0f94 Alex
    vector<uint32_t> readings;
38 afa9104d Priya
    do
39 58371433 Priya
    {
40
        double line_loc = linesensor->readline();
41 af7e0f94 Alex
        ROS_INFO("Line loc: %lf", line_loc);
42 9143e077 Lalitha Ganesan
43 6ee555a3 Priya
        if (line_loc == 0.0)
44
        {
45
            motors->set_sides(-60, -60, MOTOR_ABSOLUTE);
46
            continue;
47
        }
48
49
        motor_l = min(max((int) (-MOTOR_BASE + KP * line_loc), -128), 127);
50
        motor_r = min(max((int) (-MOTOR_BASE - KP * line_loc), -128), 127);
51 9143e077 Lalitha Ganesan
52 58371433 Priya
        motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
53 af7e0f94 Alex
        readings = linesensor->query();
54 58371433 Priya
    }
55 af7e0f94 Alex
    while(!linesensor->fullline(readings) &&
56
          !linesensor->destination(readings) &&
57
          ok());
58 58371433 Priya
    halt();
59
}
60 9143e077 Lalitha Ganesan
61 afa9104d Priya
void line_follow::turn_straight()
62
{
63 af7e0f94 Alex
  vector<uint32_t> readings;
64 afa9104d Priya
  do
65
  {
66
    motor_l = -MOTOR_BASE;
67
    motor_r = -MOTOR_BASE;
68
69
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
70 af7e0f94 Alex
    readings = linesensor->query();
71 afa9104d Priya
  }
72 af7e0f94 Alex
  while(!linesensor->fullline(readings) &&
73
        !linesensor->destination(readings) &&
74
        ok());
75 afa9104d Priya
}
76
77 58371433 Priya
void line_follow::turn_left()
78 60b98383 Priya
{
79 58371433 Priya
  bool first = true;
80 afa9104d Priya
  float line_loc;
81 58371433 Priya
  do
82
  {
83
    motor_l = -MOTOR_BASE;
84 6ee555a3 Priya
    motor_r = 2*MOTOR_BASE/5;
85 60b98383 Priya
86 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
87 60b98383 Priya
88 58371433 Priya
    if(first)
89 9143e077 Lalitha Ganesan
    {
90 afa9104d Priya
        init_turn_time.sleep();
91 58371433 Priya
        first = false;
92 9143e077 Lalitha Ganesan
    }
93 afa9104d Priya
94
    line_loc = linesensor->readline();
95 60b98383 Priya
  }
96 af7e0f94 Alex
  while(!(-1 < line_loc && line_loc < 1) && ok());
97 9143e077 Lalitha Ganesan
}
98
99 58371433 Priya
void line_follow::halt()
100 9143e077 Lalitha Ganesan
{
101 58371433 Priya
    motors->set_sides(0, 0, MOTOR_ABSOLUTE);
102 9143e077 Lalitha Ganesan
}
103
104 afa9104d Priya
void line_follow::spot_turn()
105
{
106
  bool first = true;
107
  float line_loc;
108
  do
109
  {
110
    motor_l = MOTOR_BASE;
111
    motor_r = -MOTOR_BASE;
112
113
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
114
115
    if(first)
116
    {
117 c840fbe6 Priya
        do
118
        {
119 afa9104d Priya
          line_loc = linesensor->readline();
120 c840fbe6 Priya
        }
121 af7e0f94 Alex
        while(line_loc < 2 && line_loc > -2 && ok());
122 afa9104d Priya
123
        first = false;
124
    }
125
    line_loc = linesensor->readline();
126
  }
127 af7e0f94 Alex
  while(!(-1 < line_loc && line_loc < 1) && ok());
128 afa9104d Priya
}
129
130 58371433 Priya
void line_follow::turn_right()
131 9143e077 Lalitha Ganesan
{
132 58371433 Priya
  bool first = true;
133 afa9104d Priya
  float line_loc;
134 58371433 Priya
  do
135
  {
136 6ee555a3 Priya
    motor_l = 2*MOTOR_BASE/5;
137 58371433 Priya
    motor_r = -MOTOR_BASE;
138 60b98383 Priya
139 58371433 Priya
    motors->set_sides(motor_l, motor_r, MOTOR_ABSOLUTE);
140 60b98383 Priya
141 58371433 Priya
    if(first)
142 60b98383 Priya
    {
143 afa9104d Priya
        init_turn_time.sleep();
144 58371433 Priya
        first = false;
145 60b98383 Priya
    }
146 afa9104d Priya
147
    line_loc = linesensor->readline();
148 60b98383 Priya
  }
149 af7e0f94 Alex
  while(!(-1 < line_loc && line_loc < 1) && ok());
150 9143e077 Lalitha Ganesan
}
151
152 1905324e Priya
void line_follow::u_turn()
153 9143e077 Lalitha Ganesan
{
154 1905324e Priya
  turn_right();
155 58371433 Priya
  follow_line();
156
  turn_right();
157 1905324e Priya
}
158
159
void line_follow::run()
160
{
161 25694a03 Priya
  while(ok())
162
  {
163
    follow_line();
164
  }
165 9143e077 Lalitha Ganesan
}