Project

General

Profile

Revision 1922

Added Dan's line following code

View differences:

lineFollow.c
1 1
/**
2 2
 * @file lineFollow.c
3
 * @defgroup lineFollwing Line Following
4 3
 *
5 4
 * Takes care of following a line. Running this program is done by calling the
6 5
 * init() function and then the lineFollow(speed) command.  However, direct use
7 6
 * of this class is discouraged as its behavior is used by lineDrive.c, which
8 7
 * extends this class to provide behavior functionality.
9 8
 *
10
 * @author Dan Jacobs and the Colony Project
9
 * @author Dan Jacobs
11 10
 * @date 11-1-2010
12 11
 */
13 12

  
14 13
#include "lineFollow.h"
15 14

  
16
//! The number of bits expected in a barcode
17 15
#define CODESIZE 5 
18 16

  
19 17
int countHi = 0;
......
23 21
int barCodePosition=0;
24 22

  
25 23
int turnDistance=0;
26
//! Counts the number of full line readings before we determine an intersection
27 24
int intersectionFilter=0;
28 25
int disableBarCode=0;
29 26

  
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
33 27

  
34

  
28
/** 
29
 * Initializes line following. Must be called before other line-following
30
 * behavior will work.
31
 */
35 32
void lineFollow_init()
36 33
{
37 34
	analog_init(0);
......
57 54
	//not on line
58 55
	if(position == NOLINE)
59 56
	{
60
		if(lost++ > 20)
57
		if(lost++>20)
61 58
		{
62 59
			orb2_set_color(GREEN);
63 60
			motors_off();
......
66 63
	}
67 64
	else if(position == FULL_LINE)
68 65
	{
69
		if(intersectionFilter++ > 4)
66
		if(intersectionFilter++>4)
70 67
		{
71 68
			orb2_set_color(RED);
72 69
			barCodePosition=0;
......
156 153
/**
157 154
 * Turns left at a cross of two lines.  Assumes that we are at lines in a cross
158 155
 * pattern, and turns until it sets straight on the new line.
159
 * @return 0 if turn finishes otherwise return 1
156
 * @return 1 if the turn was executed successfully, 0 otherwise.
160 157
 */
161 158
int turnLeft()
162 159
{
163
        /*motor_l_set(BACKWARD, 200);
160
        motor_l_set(BACKWARD, 200);
164 161
        motor_r_set(FORWARD, 200);
165 162
        int colors[5];
166 163
        updateLine(colors);
......
174 171
                         return 0;
175 172
                }
176 173
        }
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
        }
203 174
        return 1;
204 175
}
205 176

  
......
208 179
/**
209 180
 * Turns right at a cross of two lines.  Assumes that we are at lines in a cross
210 181
 * pattern, and turns until it sets straight on the new line.
211
 * @return 0 if the turn finishes otherwise return 1
182
 * @return 1 if the turn was executed successfully, 0 otherwise.
212 183
 */
213 184
int turnRight()
214 185
{
......
231 202

  
232 203

  
233 204

  
205

  
234 206
int getBarCode()
235 207
{
236 208
	if(barCodePosition!=CODESIZE) return NOBARCODE ;
237 209
	int temp = 0;
238
	for(int i=0; i<CODESIZE; i++)
210
	int i;
211
	for(i=0; i<CODESIZE; i++)
239 212
		temp += (barCode[i] << i);
240 213
	barCodePosition = 0;
241 214
	return temp;
......
267 240
		return NOLINE;	
268 241
	if(count==5)
269 242
		return FULL_LINE;
270
	return (wsum/count)-4; // Subtract 4 to center the index around the center.
243
	return (wsum/count)-4;
271 244
}
272 245

  
273 246

  
274 247
void updateBarCode()
275 248
{
276
	//! Note: currently only uses one of the barcode sensors.
277 249

  
250
	//NOTE: currently only uses one of the barcode sensors.
251

  
278 252
	//maps the sensors to the analog input ports
279 253
	int ports[2] = {8,1};
280 254
	int current[2];
......
310 284
}
311 285

  
312 286

  
287
//! A simple function to return the minimum of two integers.
313 288
int min(int x, int y){return x>y ? y : x;}
289
//! A simple function to return the maximum of two integers.
314 290
int max(int x, int y){return x<y ? y : x;}
315 291

  
316 292
void motorLeft(int speed){

Also available in: Unified diff