Revision 1931
fixed ??? trunk
lineFollow.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* @file lineFollow.c |
3 |
* @defgroup lineFollwing Line Following |
|
3 | 4 |
* |
4 | 5 |
* Takes care of following a line. Running this program is done by calling the |
5 | 6 |
* init() function and then the lineFollow(speed) command. However, direct use |
6 | 7 |
* of this class is discouraged as its behavior is used by lineDrive.c, which |
7 | 8 |
* extends this class to provide behavior functionality. |
8 | 9 |
* |
9 |
* @author Dan Jacobs |
|
10 |
* @author Dan Jacobs and the Colony Project
|
|
10 | 11 |
* @date 11-1-2010 |
11 | 12 |
*/ |
12 | 13 |
|
13 | 14 |
#include "lineFollow.h" |
14 | 15 |
|
16 |
//! The number of bits expected in a barcode |
|
15 | 17 |
#define CODESIZE 5 |
16 | 18 |
|
17 | 19 |
int countHi = 0; |
... | ... | |
21 | 23 |
int barCodePosition=0; |
22 | 24 |
|
23 | 25 |
int turnDistance=0; |
26 |
//! Counts the number of full line readings before we determine an intersection |
|
24 | 27 |
int intersectionFilter=0; |
25 | 28 |
int disableBarCode=0; |
26 | 29 |
|
30 |
//! Keeps track of where the encoder of one motor started, for use in turns. |
|
31 |
int encoderStart = -1; |
|
32 |
int encoderReset = 0; // 0 if encoderStart has no value set |
|
27 | 33 |
|
28 |
/** |
|
29 |
* Initializes line following. Must be called before other line-following |
|
30 |
* behavior will work. |
|
31 |
*/ |
|
34 |
|
|
32 | 35 |
void lineFollow_init() |
33 | 36 |
{ |
34 | 37 |
analog_init(0); |
... | ... | |
54 | 57 |
//not on line |
55 | 58 |
if(position == NOLINE) |
56 | 59 |
{ |
57 |
if(lost++>20)
|
|
60 |
if(lost++ > 20)
|
|
58 | 61 |
{ |
59 | 62 |
orb2_set_color(GREEN); |
60 | 63 |
motors_off(); |
... | ... | |
63 | 66 |
} |
64 | 67 |
else if(position == FULL_LINE) |
65 | 68 |
{ |
66 |
if(intersectionFilter++>4)
|
|
69 |
if(intersectionFilter++ > 4)
|
|
67 | 70 |
{ |
68 | 71 |
orb2_set_color(RED); |
69 | 72 |
barCodePosition=0; |
... | ... | |
153 | 156 |
/** |
154 | 157 |
* Turns left at a cross of two lines. Assumes that we are at lines in a cross |
155 | 158 |
* pattern, and turns until it sets straight on the new line. |
156 |
* @return 1 if the turn was executed successfully, 0 otherwise.
|
|
159 |
* @return 0 if turn finishes otherwise return 1
|
|
157 | 160 |
*/ |
158 | 161 |
int turnLeft() |
159 | 162 |
{ |
160 |
motor_l_set(BACKWARD, 200); |
|
163 |
/*motor_l_set(BACKWARD, 200);
|
|
161 | 164 |
motor_r_set(FORWARD, 200); |
162 | 165 |
int colors[5]; |
163 | 166 |
updateLine(colors); |
... | ... | |
171 | 174 |
return 0; |
172 | 175 |
} |
173 | 176 |
} |
177 |
return 1;*/ |
|
178 |
|
|
179 |
motor_l_set(BACKWARD,200); |
|
180 |
motor_r_set(FORWARD,200); |
|
181 |
if(!encoderReset) |
|
182 |
{ |
|
183 |
encoderStart = encoder_get_x(RIGHT); |
|
184 |
encoderReset = 1; |
|
185 |
} |
|
186 |
|
|
187 |
if(encoder_get_x(RIGHT) < encoderStart) |
|
188 |
{ |
|
189 |
encoderStart = 0; |
|
190 |
// Temporary: display an "error message" in case of overflow. |
|
191 |
// Using this for debugging, take it out soon! |
|
192 |
motor_l_set(FORWARD,0); |
|
193 |
motor_r_set(FORWARD,0); |
|
194 |
orb_set_color(WHITE); |
|
195 |
delay_ms(2000); |
|
196 |
} |
|
197 |
|
|
198 |
if(encoder_get_x(RIGHT) - encoderStart > 300) |
|
199 |
{ |
|
200 |
encoderReset = 0; |
|
201 |
return 0; |
|
202 |
} |
|
174 | 203 |
return 1; |
175 | 204 |
} |
176 | 205 |
|
... | ... | |
179 | 208 |
/** |
180 | 209 |
* Turns right at a cross of two lines. Assumes that we are at lines in a cross |
181 | 210 |
* pattern, and turns until it sets straight on the new line. |
182 |
* @return 1 if the turn was executed successfully, 0 otherwise.
|
|
211 |
* @return 0 if the turn finishes otherwise return 1
|
|
183 | 212 |
*/ |
184 | 213 |
int turnRight() |
185 | 214 |
{ |
... | ... | |
202 | 231 |
|
203 | 232 |
|
204 | 233 |
|
205 |
|
|
206 | 234 |
int getBarCode() |
207 | 235 |
{ |
208 | 236 |
if(barCodePosition!=CODESIZE) return NOBARCODE ; |
209 | 237 |
int temp = 0; |
210 |
int i; |
|
211 |
for(i=0; i<CODESIZE; i++) |
|
238 |
for(int i=0; i<CODESIZE; i++) |
|
212 | 239 |
temp += (barCode[i] << i); |
213 | 240 |
barCodePosition = 0; |
214 | 241 |
return temp; |
... | ... | |
240 | 267 |
return NOLINE; |
241 | 268 |
if(count==5) |
242 | 269 |
return FULL_LINE; |
243 |
return (wsum/count)-4; |
|
270 |
return (wsum/count)-4; // Subtract 4 to center the index around the center.
|
|
244 | 271 |
} |
245 | 272 |
|
246 | 273 |
|
247 | 274 |
void updateBarCode() |
248 | 275 |
{ |
276 |
//! Note: currently only uses one of the barcode sensors. |
|
249 | 277 |
|
250 |
//NOTE: currently only uses one of the barcode sensors. |
|
251 |
|
|
252 | 278 |
//maps the sensors to the analog input ports |
253 | 279 |
int ports[2] = {8,1}; |
254 | 280 |
int current[2]; |
... | ... | |
284 | 310 |
} |
285 | 311 |
|
286 | 312 |
|
287 |
//! A simple function to return the minimum of two integers. |
|
288 | 313 |
int min(int x, int y){return x>y ? y : x;} |
289 |
//! A simple function to return the maximum of two integers. |
|
290 | 314 |
int max(int x, int y){return x<y ? y : x;} |
291 | 315 |
|
292 | 316 |
void motorLeft(int speed){ |
Also available in: Unified diff