Revision 1997
Added auto-calibration for barcode reading.
lineFollow.c | ||
---|---|---|
23 | 23 |
* 1 - print out buckets |
24 | 24 |
* 2 - print out line sensor readings |
25 | 25 |
*/ |
26 |
#define DBG_LINEFOLLOW 0
|
|
26 |
#define DBG_LINEFOLLOW 2
|
|
27 | 27 |
|
28 |
//! Anything lower than this value is white |
|
29 |
int GREY_THRESHOLD = 300; |
|
30 |
//! Anything higher than this value is black |
|
31 |
int BLACK_THRESHOLD = 750; |
|
32 |
|
|
28 | 33 |
int countHi = 0; |
29 | 34 |
int countLo = 0; |
30 | 35 |
int maxAvg, avg; |
... | ... | |
53 | 58 |
|
54 | 59 |
void lineFollow_init() |
55 | 60 |
{ |
56 |
int i, j; |
|
61 |
int i, j, curReading; |
|
62 |
int lowGrey = 1000, highGrey = 0, lowBlack = 1000, |
|
63 |
highBlack = 0; |
|
57 | 64 |
|
58 | 65 |
analog_init(0); |
59 | 66 |
encoders_init(); |
... | ... | |
69 | 76 |
} |
70 | 77 |
isReset[i] = 1; |
71 | 78 |
} |
79 |
|
|
80 |
// Calibrate thresholds |
|
81 |
orb_set_color(YELLOW); |
|
82 |
delay_ms(2000); |
|
72 | 83 |
|
84 |
orb_set_color(BLUE); |
|
85 |
|
|
86 |
for(i=0; i<100; i++) |
|
87 |
{ |
|
88 |
curReading = read_line(LEFT_SENSOR + 6); |
|
89 |
if(curReading < lowGrey) |
|
90 |
lowGrey = curReading; |
|
91 |
if(curReading > highGrey) |
|
92 |
highGrey = curReading; |
|
93 |
|
|
94 |
delay_ms(20); |
|
95 |
} |
|
96 |
|
|
97 |
orb_set_color(YELLOW); |
|
98 |
delay_ms(2000); |
|
99 |
|
|
100 |
orb_set_color(GREEN); |
|
101 |
|
|
102 |
for(i=0; i<100; i++) |
|
103 |
{ |
|
104 |
curReading = read_line(LEFT_SENSOR + 6); |
|
105 |
if(curReading < lowBlack) |
|
106 |
lowBlack = curReading; |
|
107 |
if(curReading > highBlack) |
|
108 |
highBlack = curReading; |
|
109 |
|
|
110 |
delay_ms(20); |
|
111 |
} |
|
112 |
|
|
113 |
orbs_set(0,0,0,0,0,0); |
|
114 |
|
|
115 |
GREY_THRESHOLD = lowGrey / 2; |
|
116 |
BLACK_THRESHOLD = (highGrey + lowBlack) / 2; |
|
117 |
|
|
118 |
usb_puts("Grey: "); |
|
119 |
usb_puti(lowGrey); |
|
120 |
usb_puts(", "); |
|
121 |
usb_puti(highGrey); |
|
122 |
usb_puts("\nBlack: "); |
|
123 |
usb_puti(lowBlack); |
|
124 |
usb_puts(", "); |
|
125 |
usb_puti(highBlack); |
|
126 |
usb_puts("\nThresholds: "); |
|
127 |
usb_puti(GREY_THRESHOLD); |
|
128 |
usb_puts(", "); |
|
129 |
usb_puti(BLACK_THRESHOLD); |
|
130 |
usb_puts("\n\n"); |
|
131 |
|
|
132 |
delay_ms(1500); |
|
133 |
|
|
73 | 134 |
//numLast = { {0, 0, 0, NUM_READINGS}, {0, 0, 0, NUM_READINGS} }; |
74 | 135 |
} |
75 | 136 |
|
Also available in: Unified diff