Revision 1952
Small edits to the barcode reader
With proper setup of the barcodes (printed on rather than taped), this barcode reader is working.
trunk/code/projects/linefollowing/lineFollow.h | ||
---|---|---|
22 | 22 |
//! Anything lower than this value is white |
23 | 23 |
#define GREY_THRESHOLD 100 |
24 | 24 |
//! Anything higher than this value is black |
25 |
#define BLACK_THRESHOLD 350
|
|
25 |
#define BLACK_THRESHOLD 400
|
|
26 | 26 |
|
27 | 27 |
#define LEFT_SENSOR 1 |
28 | 28 |
#define RIGHT_SENSOR 0 |
trunk/code/projects/linefollowing/main.c | ||
---|---|---|
14 | 14 |
|
15 | 15 |
int main(void) |
16 | 16 |
{ |
17 |
|
|
18 |
|
|
19 | 17 |
/* initialize components, set wireless channel */ |
20 | 18 |
dragonfly_init(ALL_ON); |
21 | 19 |
lineFollow_init(); |
trunk/code/projects/linefollowing/lineFollow.c | ||
---|---|---|
21 | 21 |
int countHi = 0; |
22 | 22 |
int countLo = 0; |
23 | 23 |
int maxAvg, avg; |
24 |
int barCode[ CODESIZE ]; |
|
25 |
int barCodePosition=0; |
|
26 | 24 |
|
27 |
int duration = 0;
|
|
28 |
int lastColor = 0;
|
|
29 |
char isReset = 0;
|
|
25 |
// Everything has a dimension of 2 for left and right readings
|
|
26 |
int barCode[2][ CODESIZE ];
|
|
27 |
int barCodePosition[2]={0};
|
|
30 | 28 |
|
29 |
int duration[2] = {0}; |
|
30 |
int lastColor[2] = {0}; |
|
31 |
char isReset[2] = {0}; |
|
32 |
|
|
31 | 33 |
int turnDistance=0; |
32 | 34 |
//! Counts the number of full line readings before we determine an intersection |
33 | 35 |
int intersectionFilter=0; |
... | ... | |
75 | 77 |
if(intersectionFilter++ > 4) |
76 | 78 |
{ |
77 | 79 |
orb2_set_color(RED); |
78 |
barCodePosition=0; |
|
80 |
barCodePosition[0]=0; |
|
81 |
barCodePosition[1]=1; |
|
79 | 82 |
disableBarCode=50; |
80 | 83 |
} |
81 | 84 |
} |
... | ... | |
241 | 244 |
|
242 | 245 |
int getBarCode() |
243 | 246 |
{ |
244 |
if(barCodePosition != CODESIZE) |
|
247 |
if(barCodePosition[0] != CODESIZE)
|
|
245 | 248 |
return NOBARCODE ; |
246 | 249 |
else |
247 | 250 |
{ |
248 | 251 |
int temp = 0; |
249 | 252 |
for(int i=0; i<CODESIZE; i++) |
250 |
temp += (barCode[i] << i); |
|
251 |
barCodePosition = 0; |
|
253 |
temp += (barCode[0][i] << i);
|
|
254 |
barCodePosition[0] = 0;
|
|
252 | 255 |
return temp; |
253 | 256 |
} |
254 | 257 |
} |
... | ... | |
291 | 294 |
// global int barCodePosition = 0; |
292 | 295 |
// global char isReset = 0; |
293 | 296 |
|
294 |
// Average the readings of the last 2 sensors |
|
295 |
if(read_line(6) - read_line(7) > 150 || read_line(6) - read_line(7) < -150) |
|
297 |
for(int i = RIGHT_SENSOR; i <= LEFT_SENSOR; i++) |
|
296 | 298 |
{ |
297 |
return; |
|
298 |
} |
|
299 |
// Add 6 to convert left (1) and right (0) to sensor 6 and 7 |
|
300 |
int curReading = read_line(i + 6); |
|
301 |
int curColor; |
|
299 | 302 |
|
300 |
int curReading = (read_line(6) + read_line(7)) / 2; |
|
301 |
int curColor; |
|
303 |
if(curReading > BLACK_THRESHOLD) |
|
304 |
curColor = LBLACK; |
|
305 |
else if(curReading < GREY_THRESHOLD) |
|
306 |
curColor = LWHITE; |
|
307 |
else |
|
308 |
curColor = LGREY; |
|
302 | 309 |
|
303 |
if(curReading > BLACK_THRESHOLD) |
|
304 |
curColor = LBLACK; |
|
305 |
else if(curReading < GREY_THRESHOLD) |
|
306 |
curColor = LWHITE; |
|
307 |
else |
|
308 |
curColor = LGREY; |
|
310 |
// Just an error check |
|
311 |
if(barCodePosition[i] > CODESIZE) |
|
312 |
{ |
|
313 |
barCodePosition[i] = 0; |
|
314 |
} |
|
309 | 315 |
|
310 |
// Just an error check |
|
311 |
if(barCodePosition > CODESIZE) |
|
312 |
{ |
|
313 |
barCodePosition = 0; |
|
314 |
} |
|
316 |
// We are only interested in consecutive color values |
|
317 |
if(curColor == lastColor[i]) |
|
318 |
{ |
|
319 |
duration[i]++; |
|
320 |
} |
|
321 |
else |
|
322 |
{ |
|
323 |
duration[i] = 0; |
|
324 |
lastColor[i] = curColor; |
|
325 |
} |
|
315 | 326 |
|
316 |
// We are only interested in consecutive color values |
|
317 |
if(curColor == lastColor) |
|
318 |
{ |
|
319 |
duration++; |
|
320 |
} |
|
321 |
else |
|
322 |
{ |
|
323 |
duration = 0; |
|
324 |
lastColor = curColor; |
|
325 |
} |
|
327 |
if(duration[i] > MAX_DURATION) |
|
328 |
{ |
|
329 |
// Now we assume our reading is significant - a bit, or a white space |
|
326 | 330 |
|
327 |
if(duration > MAX_DURATION) |
|
328 |
{ |
|
329 |
// Now we assume our reading is significant - a bit, or a white space |
|
331 |
// Only read a value if we have read 0 first (isReset == 1) |
|
332 |
if(isReset[i] && (curColor == LBLACK || curColor == LGREY) ) |
|
333 |
{ |
|
334 |
isReset[i] = 0; |
|
335 |
usb_puts("Read barcode bit: "); |
|
336 |
usb_puti(barCodePosition[i]); |
|
337 |
usb_puts(" = "); |
|
338 |
usb_puti(curColor); |
|
339 |
usb_puts(", curReading = "); |
|
340 |
usb_puti(curReading); |
|
341 |
usb_puts(".\n"); |
|
330 | 342 |
|
331 |
// Only read a value if we have read 0 first (isReset == 1) |
|
332 |
if(isReset && (curColor == LBLACK || curColor == LGREY) ) |
|
333 |
{ |
|
334 |
isReset = 0; |
|
335 |
usb_puts("Read barcode bit: "); |
|
336 |
usb_puti(barCodePosition); |
|
337 |
usb_puts(" = "); |
|
338 |
usb_puti(curColor); |
|
339 |
usb_puts(", curReading = "); |
|
340 |
usb_puti(curReading); |
|
341 |
usb_puts(".\n"); |
|
342 |
|
|
343 |
barCode[barCodePosition++] = (curColor == LBLACK) ? 1 : 0; |
|
343 |
barCode[i][barCodePosition[i]++] = (curColor == LBLACK) ? 1 : 0; |
|
344 |
} |
|
345 |
else if(curColor == LWHITE) |
|
346 |
{ |
|
347 |
isReset[i] = 1; |
|
348 |
} |
|
344 | 349 |
} |
345 |
else if(curColor == LWHITE)
|
|
350 |
if(duration[i] > TIMEOUT_DURATION && barCodePosition[i] != 0)
|
|
346 | 351 |
{ |
347 |
isReset = 1; |
|
352 |
usb_puts("TIMED OUT. BARCODE READER RESET.\n"); |
|
353 |
barCodePosition[i] = 0; |
|
354 |
duration[i] = 0; |
|
355 |
isReset[i] = 1; |
|
348 | 356 |
} |
349 | 357 |
} |
350 |
if(duration > TIMEOUT_DURATION && barCodePosition != 0) |
|
351 |
{ |
|
352 |
usb_puts("TIMED OUT. BARCODE READER RESET.\n"); |
|
353 |
barCodePosition = 0; |
|
354 |
duration = 0; |
|
355 |
} |
|
356 | 358 |
} |
357 | 359 |
|
358 | 360 |
// Dan's version |
Also available in: Unified diff