root / trunk / code / projects / traffic_navigation / lineFollow.c @ 1846
History | View | Annotate | Download (2.39 KB)
1 |
|
---|---|
2 |
#include <lineFollow.h> |
3 |
|
4 |
#define NOBARCODE -2 |
5 |
#define CODESIZE 5 |
6 |
|
7 |
int countHi = 0; |
8 |
int countLo = 0; |
9 |
int maxAvg, avg;
|
10 |
int barCode[ CODESIZE ];
|
11 |
int barCodePosition=0; |
12 |
|
13 |
void lineFollow_init()
|
14 |
{ |
15 |
analog_init(0);
|
16 |
lost = 0;
|
17 |
} |
18 |
|
19 |
|
20 |
|
21 |
int lineFollow()
|
22 |
{ |
23 |
int colors[6]; |
24 |
int position;
|
25 |
|
26 |
|
27 |
updateLine(colors); |
28 |
position = lineLocate(colors); |
29 |
|
30 |
//not on line
|
31 |
if(position == NOLINE)
|
32 |
{ |
33 |
if(lost++>20) |
34 |
{ |
35 |
orb2_set_color(GREEN); |
36 |
motors_off(); |
37 |
return LINELOST;
|
38 |
} |
39 |
} |
40 |
//on line
|
41 |
else
|
42 |
{ |
43 |
position*=30;
|
44 |
orb2_set_color(ORB_OFF); |
45 |
motorLeft(min(SPEED+position, 150));
|
46 |
motorRight(min(SPEED-position, 150));
|
47 |
lost=0;
|
48 |
} |
49 |
updateBarCode(); |
50 |
return getBarCode();
|
51 |
} |
52 |
|
53 |
int getBarCode(){
|
54 |
if(barCodePosition!=CODESIZE) return NOBARCODE ; |
55 |
int temp = 0; |
56 |
int i;
|
57 |
for(i=0; i<CODESIZE; i++) |
58 |
temp += (barCode[i] << i); |
59 |
barCodePosition = 0;
|
60 |
return temp;
|
61 |
} |
62 |
|
63 |
|
64 |
|
65 |
void updateLine(int* values) |
66 |
{ |
67 |
for(int i = 0; i<5; i++) |
68 |
values[i] = assignColor(i, updateIR(i)); |
69 |
} |
70 |
|
71 |
|
72 |
int updateIR(int n) |
73 |
{ |
74 |
//maps the sensors to the analog input ports
|
75 |
int ports[5] = {13, 12, 3, 2, 9}; |
76 |
|
77 |
return analog_get10(ports[n]);
|
78 |
} |
79 |
|
80 |
|
81 |
|
82 |
int assignColor(int port, int analog) |
83 |
{ |
84 |
if (analog < 150) |
85 |
return 0; |
86 |
else
|
87 |
return 2; |
88 |
} |
89 |
|
90 |
|
91 |
int lineLocate(int* colors) |
92 |
{ |
93 |
int i;
|
94 |
int wsum = 0; |
95 |
int count=0; |
96 |
|
97 |
for(i = 0; i<5; i++) |
98 |
{ |
99 |
count += colors[i]/2;
|
100 |
wsum += (i)*colors[i]; |
101 |
} |
102 |
if(count==0) |
103 |
return NOLINE;
|
104 |
|
105 |
return (wsum/count)-4; |
106 |
} |
107 |
|
108 |
|
109 |
void updateBarCode()
|
110 |
{ |
111 |
//maps the sensors to the analog input ports
|
112 |
int ports[2] = {8,1}; |
113 |
int current[2]; |
114 |
// current[0] = analog_get10(8);
|
115 |
current[1] = analog_get10(1); |
116 |
// usb_puti(analog_get10(8));
|
117 |
// usb_putc('\t');
|
118 |
// usb_puti(analog_get10(1));
|
119 |
// usb_putc('\n');
|
120 |
|
121 |
if(current[1]>500) |
122 |
{ |
123 |
if(countHi++==0) |
124 |
{ |
125 |
avg = 500;
|
126 |
maxAvg = 500;
|
127 |
} |
128 |
else
|
129 |
{ |
130 |
avg = 3*avg + current[1]; |
131 |
avg/=4;
|
132 |
maxAvg = max(maxAvg, avg); |
133 |
} |
134 |
} |
135 |
else if(countHi>5) |
136 |
{ |
137 |
if(countLo++>15) |
138 |
{ |
139 |
countHi=countLo=0;
|
140 |
if(maxAvg>825)orb1_set_color(RED); |
141 |
else orb1_set_color(BLUE);
|
142 |
barCode[barCodePosition++] = (maxAvg > 825 ? 2:1)-1; |
143 |
} |
144 |
} |
145 |
else countHi/=2; |
146 |
if(countHi==0)countLo=0; |
147 |
} |
148 |
|
149 |
|
150 |
int min(int x, int y){return x>y ? y : x;} |
151 |
int max(int x, int y){return x<y ? y : x;} |
152 |
|
153 |
void motorLeft(int speed){ |
154 |
((speed-=127)>=0)?motor_l_set(FORWARD, 160+speed*95/128):motor_l_set(BACKWARD, 160-speed*95/127); |
155 |
} |
156 |
|
157 |
void motorRight(int speed){ |
158 |
((speed-=127)>=0)?motor_r_set(FORWARD, 160+speed*95/128):motor_r_set(BACKWARD, 160-speed*95/127); |
159 |
} |
160 |
|