Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / libscout / src / behaviors / line_follow.cpp @ 6ee555a3

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