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