Project

General

Profile

Revision 1942

Updated barcode code to use the new line sensor, and to work better.

View differences:

trunk/code/projects/linefollowing/lineFollow.h
15 15
#define NOLINE			-50
16 16
#define FULL_LINE 		-51
17 17

  
18
// Number of consecutive barcode color readings to establish a significant
19
// reading.
20
#define MAX_DURATION    20
21

  
18 22
/**
19 23
 * @brief Initializes line following.
20 24
 * 
trunk/code/projects/linefollowing/main.c
2 2
#include <lineFollow.h>
3 3

  
4 4
/* 
5
*  read_line 0 = Sensor 1
6
*  read_line 1 = Sensor 8
7
*  read_line 2 = Sensor 4
8
*  read_line 3 = Sensor 6
9
*  read_line 4 = Sensor 2
10
*  read_line 5 = Sensor 7
11
*  read_line 6 = Sensor 3
12
*  read_line 7 = Sensor 5
13
*/
5
 *  read_line 0 = Sensor 1
6
 *  read_line 1 = Sensor 8
7
 *  read_line 2 = Sensor 4
8
 *  read_line 3 = Sensor 6
9
 *  read_line 4 = Sensor 2
10
 *  read_line 5 = Sensor 7
11
 *  read_line 6 = Sensor 3
12
 *  read_line 7 = Sensor 5
13
 */
14 14

  
15 15
int main(void)
16 16
{
17 17

  
18
	/* initialize components, set wireless channel */
19
	dragonfly_init(ALL_ON);
20
	lineFollow_init();
21
	int barCode;
22
	while(1)
23
	{
24
		
25
		lineFollow(200);
26 18

  
19
    /* initialize components, set wireless channel */
20
    dragonfly_init(ALL_ON);
21
    lineFollow_init();
27 22

  
23
    /*while(1)
24
    {
25
        usb_puti(read_line(0));
26
        usb_putc('\t');
27
        usb_puti(read_line(1));
28
        usb_putc('\t');
29
        usb_puti(read_line(2));
30
        usb_putc('\t');
31
        usb_puti(read_line(3));
32
        usb_putc('\t');
33
        usb_puti(read_line(4));
34
        usb_putc('\t');
35
        usb_puti(read_line(5));
36
        usb_putc('\t');
37
        usb_puti(read_line(6));
38
        usb_putc('\t');
39
        usb_puti(read_line(7));
40
        usb_putc('\t');
41
        usb_putc(13);
42
    }*/
28 43

  
29
	
30
	}
31
	return 0;
44
    int barCode;
45
    while(1)
46
    {
47

  
48
        int barcode = lineFollow(200);
49

  
50
        if(barcode != NOBARCODE)
51
        {
52
            usb_puti(barcode);
53
            usb_puts("\n");
54
        }
55

  
56
    }
57
    return 0;
32 58
}
33 59
void right()
34 60
{
35
motor_r_set(BACKWARD, 200);
36
motor_l_set(FORWARD, 200);
37
delay_ms(400);
61
    motor_r_set(BACKWARD, 200);
62
    motor_l_set(FORWARD, 200);
63
    delay_ms(400);
38 64
}
39 65

  
40 66
void straight()
41 67
{
42
motor_r_set(FORWARD, 210);
43
motor_l_set(FORWARD, 210);
44
delay_ms(200);
45
move(0,0);
46
delay_ms(2000);
68
    motor_r_set(FORWARD, 210);
69
    motor_l_set(FORWARD, 210);
70
    delay_ms(200);
71
    move(0,0);
72
    delay_ms(2000);
47 73
}
48 74

  
49 75
void left()
50 76
{
51
motor_l_set(BACKWARD, 200);
52
motor_r_set(FORWARD, 200);
53
delay_ms(400);
77
    motor_l_set(BACKWARD, 200);
78
    motor_r_set(FORWARD, 200);
79
    delay_ms(400);
54 80
}
trunk/code/projects/linefollowing/lineFollow.c
22 22
int barCode[ CODESIZE ];
23 23
int barCodePosition=0;
24 24

  
25
int duration = 0;
26
int lastColor = 0;
27
char isReset = 0;
28

  
25 29
int turnDistance=0;
26 30
//! Counts the number of full line readings before we determine an intersection
27 31
int intersectionFilter=0;
......
84 88
		intersectionFilter=0;
85 89
	}
86 90

  
87
	if(disableBarCode--)
91
    // If we're running over a line, stop reading barcodes for a sec
92
	if(disableBarCode-- > 0)
88 93
	{
89
		if(disableBarCode)return NOBARCODE;
94
        // Return intersection once we cross the line
95
		if(disableBarCode) return NOBARCODE;
90 96
		return INTERSECTION;
91 97
	}
92 98
	updateBarCode();
......
233 239

  
234 240
int getBarCode()
235 241
{
236
	if(barCodePosition!=CODESIZE) return NOBARCODE ;
237
	int temp = 0;
238
	for(int i=0; i<CODESIZE; i++)
239
		temp += (barCode[i] << i);
240
	barCodePosition = 0;
241
	return temp;
242
	if(barCodePosition != CODESIZE)
243
        return NOBARCODE ;
244
    else
245
    {
246
        int temp = 0;
247
        for(int i=0; i<CODESIZE; i++)
248
            temp += (barCode[i] << i);
249
        barCodePosition = 0;
250
        return temp;
251
    }
242 252
}
243 253

  
244 254

  
......
246 256
void updateLine(int* values)
247 257
{	
248 258
	for(int i = 0; i<5; i++)
249
		values[i] = (read_line(4-i)<150 ? LWHITE : LBLACK);
259
		values[i] = (read_line(4-i) < 150 ? LWHITE : LBLACK);
250 260
}
251 261

  
252 262

  
......
270 280
}
271 281

  
272 282

  
283
// new version by Alex
284
/*void updateBarCode()
285
{
286
    // Average the readings of the last 2 sensors
287
    int curReading = (read_line(6) + read_line(7)) / 2;
288
    int curColor = (curReading > 500) ? LBLACK : LWHITE;
289

  
290
    // USING THESE GLOBAL VARIABLES
291
    // global int duration = 0;
292
    // global int lastColor = 0;
293
    // global int barCodePosition = 0;
294
    // global char isReset = 0;
295

  
296
    // Just an error check
297
    if(barCodePosition > CODESIZE)
298
    {
299
        barCodePosition = 0;
300
    }
301

  
302
    if(curColor == lastColor)
303
    {
304
        duration++;
305
    }
306
    else
307
    {
308
        duration = 0;
309
        lastColor = curColor;
310
    }
311

  
312
    if(duration > MAX_DURATION)
313
    {
314
        // Now we assume our reading is significant - a bit, or a white space
315

  
316
        // Only read a value if we have read 0 first (isReset == 1)
317
        if(isReset && (curColor == LBLACK || curColor == LGREY) )
318
        {
319
            isReset = 0;
320

  
321
            barCode[barCodePosition++] = (curColor == LBLACK) ? 1 : 0;
322
        }
323
        else if(curColor == LWHITE)
324
        {
325
            isReset = 1;
326
        }
327
    }
328
}*/
329

  
330

  
273 331
void updateBarCode()
274 332
{
275 333
	//! Note: currently only uses one of the barcode sensors.
......
278 336
	int ports[2] = {8,1};
279 337
	int current[2];
280 338
//	current[0] = analog_get10(ports[0]);
281
	current[1] = analog_get10(ports[1]);
339
	//current[1] = analog_get10(ports[1]);
340
    current[1] = read_line(6);
282 341

  
283
	if(current[1]>500)
342
	if(current[1] > 150)
284 343
	{
285
		if(countHi++==0) 
344
		if(countHi++ == 0) 
286 345
		{
287 346
			avg = 500;
288 347
			maxAvg = 500;
......
294 353
			maxAvg = max(maxAvg, avg);
295 354
		}
296 355
	}
297
	else if(countHi>5)
356
	else if(countHi > 5)
298 357
	{
299
		if(countLo++>15)
358
		if(countLo++ > 15)
300 359
		{
301
			countHi=countLo=0;
302
			if(maxAvg>825)orb1_set_color(RED);
360
			countHi = countLo = 0;
361
			if(maxAvg > 825)
362
                orb1_set_color(RED);
303 363
			else orb1_set_color(BLUE);
304 364
			barCode[barCodePosition++] = maxAvg > 825 ? 1:0;
305 365
		}
trunk/code/projects/libdragonfly/analog.c
295 295

  
296 296

  
297 297
/** Returns the 10 bit value from the line sensors **/
298
unsigned int read_line(int which) {
298
unsigned int read_line(int which)
299
{
299 300
    int adc_h;
300 301
    int adc_l;
301 302

  
......
304 305

  
305 306
    ADMUX = ADMUX_OPT + AN1;
306 307

  
307
     which = ((which&1)<<2) + ((which&2)) + ((which&4)>>2);
308
    which = ((which&1)<<2) + ((which&2)) + ((which&4)>>2);
308 309

  
309 310
    // mask so only proper bits are possible.  
310 311
    PORTD = (PORTD & 0x1F) | ((which & 0x07) << 5);

Also available in: Unified diff