Revision 1942
Updated barcode code to use the new line sensor, and to work better.
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 |
} |
Also available in: Unified diff