Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / line_follow.cpp @ 2fd5122a

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