root / trunk / code / projects / linefollowing / lineFollow.c @ 1853
History | View | Annotate | Download (3.31 KB)
1 |
#include "lineFollow.h" |
---|---|
2 |
|
3 |
#define NOBARCODE -2 |
4 |
#define CODESIZE 5 |
5 |
#define INTERSECTION -25 |
6 |
#define FULL_LINE -26 |
7 |
|
8 |
int countHi = 0; |
9 |
int countLo = 0; |
10 |
int maxAvg, avg;
|
11 |
int barCode[ CODESIZE ];
|
12 |
int barCodePosition=0; |
13 |
|
14 |
int turnDistance=0; |
15 |
int intersectionFilter=0; |
16 |
int disableBarCode=0; |
17 |
|
18 |
|
19 |
void lineFollow_init()
|
20 |
{ |
21 |
analog_init(0);
|
22 |
lost = 0;
|
23 |
intersectionFilter=0;
|
24 |
disableBarCode=0;
|
25 |
} |
26 |
|
27 |
|
28 |
|
29 |
int lineFollow(int speed) |
30 |
{ |
31 |
int colors[5]; |
32 |
int position;
|
33 |
|
34 |
|
35 |
updateLine(colors); |
36 |
position = lineLocate(colors); |
37 |
|
38 |
//not on line
|
39 |
if(position == NOLINE)
|
40 |
{ |
41 |
if(lost++>20) |
42 |
{ |
43 |
orb2_set_color(GREEN); |
44 |
motors_off(); |
45 |
return LINELOST;
|
46 |
} |
47 |
} |
48 |
else if(position == FULL_LINE) |
49 |
{ |
50 |
if(intersectionFilter++>4) |
51 |
{ |
52 |
orb2_set_color(RED); |
53 |
barCodePosition=0;
|
54 |
disableBarCode=50;
|
55 |
} |
56 |
} |
57 |
//on line
|
58 |
else
|
59 |
{ |
60 |
position*=30;
|
61 |
orb2_set_color(ORB_OFF); |
62 |
motorLeft(min(speed+position, 255));
|
63 |
motorRight(min(speed-position, 255));
|
64 |
lost=0;
|
65 |
intersectionFilter=0;
|
66 |
} |
67 |
|
68 |
if(disableBarCode--)
|
69 |
{ |
70 |
if(disableBarCode)return NOBARCODE; |
71 |
return INTERSECTION;
|
72 |
} |
73 |
updateBarCode(); |
74 |
return getBarCode();
|
75 |
} |
76 |
|
77 |
|
78 |
int turnLeft()
|
79 |
{ |
80 |
motor_l_set(BACKWARD, 200);
|
81 |
motor_r_set(FORWARD, 200);
|
82 |
int colors[5]; |
83 |
updateLine(colors); |
84 |
int position = lineLocate(colors);
|
85 |
if(position>2 || position<-2)turnDistance++; |
86 |
|
87 |
if(turnDistance>1) |
88 |
{ |
89 |
if(position<3 && position>-3) |
90 |
{ |
91 |
turnDistance = 0;
|
92 |
return 0; |
93 |
} |
94 |
} |
95 |
return 1; |
96 |
} |
97 |
|
98 |
int turnRight()
|
99 |
{ |
100 |
motor_r_set(BACKWARD, 200);
|
101 |
motor_l_set(FORWARD, 200);
|
102 |
int colors[5]; |
103 |
updateLine(colors); |
104 |
int position = lineLocate(colors);
|
105 |
if(position>2 || position<-2)turnDistance++; |
106 |
if(turnDistance>1) |
107 |
{ |
108 |
if(position<3 && position>-3) |
109 |
{ |
110 |
turnDistance = 0;
|
111 |
return 0; |
112 |
} |
113 |
} |
114 |
return 1; |
115 |
} |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
int getBarCode()
|
121 |
{ |
122 |
if(barCodePosition!=CODESIZE) return NOBARCODE ; |
123 |
int temp = 0; |
124 |
int i;
|
125 |
for(i=0; i<CODESIZE; i++) |
126 |
temp += (barCode[i] << i); |
127 |
barCodePosition = 0;
|
128 |
return temp;
|
129 |
} |
130 |
|
131 |
|
132 |
|
133 |
void updateLine(int* values) |
134 |
{ |
135 |
int ports[5] = {13, 12, 3, 2, 9}; |
136 |
for(int i = 0; i<5; i++) |
137 |
values[i] = analog_get10(ports[i])<150 ? WHITE : BLACK;
|
138 |
} |
139 |
|
140 |
|
141 |
|
142 |
int lineLocate(int* colors) |
143 |
{ |
144 |
int i;
|
145 |
int wsum = 0; |
146 |
int count=0; |
147 |
|
148 |
for(i = 0; i<5; i++) |
149 |
{ |
150 |
count += colors[i]/2;
|
151 |
wsum += (i)*colors[i]; |
152 |
} |
153 |
if(count==0) |
154 |
return NOLINE;
|
155 |
if(count==5) |
156 |
return FULL_LINE;
|
157 |
return (wsum/count)-4; |
158 |
} |
159 |
|
160 |
|
161 |
void updateBarCode()
|
162 |
{ |
163 |
//maps the sensors to the analog input ports
|
164 |
int ports[2] = {8,1}; |
165 |
int current[2]; |
166 |
// current[0] = analog_get10(8);
|
167 |
current[1] = analog_get10(1); |
168 |
// usb_puti(analog_get10(8));
|
169 |
// usb_putc('\t');
|
170 |
// usb_puti(analog_get10(1));
|
171 |
// usb_putc('\n');
|
172 |
|
173 |
if(current[1]>500) |
174 |
{ |
175 |
if(countHi++==0) |
176 |
{ |
177 |
avg = 500;
|
178 |
maxAvg = 500;
|
179 |
} |
180 |
else
|
181 |
{ |
182 |
avg = 3*avg + current[1]; |
183 |
avg/=4;
|
184 |
maxAvg = max(maxAvg, avg); |
185 |
} |
186 |
} |
187 |
else if(countHi>5) |
188 |
{ |
189 |
if(countLo++>15) |
190 |
{ |
191 |
countHi=countLo=0;
|
192 |
if(maxAvg>825)orb1_set_color(RED); |
193 |
else orb1_set_color(BLUE);
|
194 |
barCode[barCodePosition++] = maxAvg > 825 ? 1:0; |
195 |
} |
196 |
} |
197 |
else countHi/=2; |
198 |
if(countHi==0)countLo=0; |
199 |
} |
200 |
|
201 |
|
202 |
int min(int x, int y){return x>y ? y : x;} |
203 |
int max(int x, int y){return x<y ? y : x;} |
204 |
|
205 |
void motorLeft(int speed){ |
206 |
((speed-=127)>=0)?motor_l_set(FORWARD, 160+speed*95/128):motor_l_set(BACKWARD, 160-speed*95/127); |
207 |
} |
208 |
|
209 |
void motorRight(int speed){ |
210 |
((speed-=127)>=0)?motor_r_set(FORWARD, 160+speed*95/128):motor_r_set(BACKWARD, 160-speed*95/127); |
211 |
} |
212 |
|