Revision 1552
Converted Circle formation to a state machine
circle.c | ||
---|---|---|
1 | 1 |
#include <dragonfly_lib.h> |
2 |
#include <wl_basic.h> |
|
2 | 3 |
#include <encoders.h> |
4 |
#include "circle.h" |
|
3 | 5 |
|
6 |
int EDGE = 0; |
|
7 |
int BEACON = 1; |
|
8 |
|
|
4 | 9 |
/* |
5 | 10 |
This program is used to make robots target a center (leader) robot using the BOM, |
6 | 11 |
then drive toward it and stop at a certain distance away. |
... | ... | |
10 | 15 |
With adjustment, the leader robot will be able to turn and use its standardized |
11 | 16 |
rangefinder to reposition or space the robots evenly. |
12 | 17 |
|
13 |
AuTHORS: Niko, Alex, Reva, Echo |
|
18 |
AuTHORS: Niko, Alex, Reva, Echo, Steve
|
|
14 | 19 |
*/ |
15 | 20 |
|
16 | 21 |
|
... | ... | |
130 | 135 |
go_straight(); |
131 | 136 |
} |
132 | 137 |
|
138 |
|
|
139 |
|
|
133 | 140 |
int main(void) |
134 | 141 |
{ |
135 |
|
|
136 | 142 |
/* Initialize dragonfly board */ |
137 | 143 |
dragonfly_init(ALL_ON); |
144 |
xbee_init(); |
|
138 | 145 |
encoders_init(); |
146 |
/* Initialize the basic wireless library */ |
|
147 |
wl_basic_init_default(); |
|
148 |
/* Set the XBee channel to your assigned channel */ |
|
149 |
wl_set_channel(12); |
|
150 |
|
|
151 |
int robotid = get_robotid(); |
|
152 |
char send_buffer[2]; |
|
153 |
int data_length; |
|
154 |
unsigned char *packet_data=wl_basic_do_default(&data_length); |
|
139 | 155 |
|
156 |
|
|
157 |
int state = EDGE; |
|
158 |
if(wheel()<100) |
|
159 |
{ |
|
160 |
state=EDGE; |
|
161 |
} |
|
162 |
else |
|
163 |
{ |
|
164 |
state=BEACON; |
|
165 |
} |
|
166 |
|
|
140 | 167 |
int distance=1000; // how far away to stop. |
141 | 168 |
int onefoot=300, speed=200; // one foot is 490 for bot 1; one foot is 200 for bot6 |
142 |
orient(); |
|
143 |
forward(speed); |
|
144 |
range_init(); |
|
145 | 169 |
|
146 |
orb_set_color(YELLOW); |
|
147 |
|
|
148 |
distance = get_distance(); |
|
149 |
while (distance>=onefoot || distance==0){ |
|
150 |
distance = get_distance(); |
|
151 |
orient2(); |
|
152 |
forward(speed); |
|
153 |
delay_ms(14); |
|
170 |
while(1) { |
|
171 |
switch(state) { |
|
172 |
case 0: // EDGE |
|
173 |
|
|
174 |
orb_set_color(GREEN); |
|
175 |
while(1) |
|
176 |
{ |
|
177 |
orb_set_color(YELLOW); |
|
178 |
packet_data=wl_basic_do_default(&data_length); |
|
179 |
if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_ACTION_EXIST) |
|
180 |
{ |
|
181 |
send_buffer[0]=CIRCLE_ACTION_ACK; |
|
182 |
send_buffer[1]=get_robotid(); |
|
183 |
|
|
184 |
wl_basic_send_global_packet(42,send_buffer,2); |
|
185 |
} |
|
186 |
|
|
187 |
if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_ACTION_ACK) |
|
188 |
{ |
|
189 |
break; |
|
190 |
} |
|
191 |
} |
|
192 |
orb_set_color(GREEN); |
|
193 |
|
|
194 |
orient(); |
|
195 |
forward(speed); |
|
196 |
range_init(); |
|
197 |
|
|
198 |
orb_set_color(YELLOW); |
|
199 |
|
|
200 |
distance = get_distance(); |
|
201 |
while (distance>=onefoot || distance==0) |
|
202 |
{ |
|
203 |
distance = get_distance(); |
|
204 |
orient2(); |
|
205 |
forward(speed); |
|
206 |
delay_ms(14); |
|
207 |
} |
|
208 |
stop(); |
|
209 |
orient(); |
|
210 |
|
|
211 |
//button1_wait (); // code for lab1. |
|
212 |
//go_straight_onefoot(); |
|
213 |
stop(); |
|
214 |
break; |
|
215 |
|
|
216 |
|
|
217 |
case 1: // BEACON |
|
218 |
bom_on(); |
|
219 |
orb_set_color(PURPLE); |
|
220 |
|
|
221 |
int numrobots = 0; |
|
222 |
|
|
223 |
while(!button1_click()) |
|
224 |
{ } |
|
225 |
|
|
226 |
send_buffer[0]=CIRCLE_ACTION_EXIST; |
|
227 |
send_buffer[1]=get_robotid(); |
|
228 |
|
|
229 |
wl_basic_send_global_packet(42,send_buffer,2); |
|
230 |
|
|
231 |
packet_data=wl_basic_do_default(&data_length); |
|
232 |
while(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_ACTION_ACK) |
|
233 |
{ |
|
234 |
// IF NEEDED: a good place to collect and store the robot id. |
|
235 |
numrobots++; |
|
236 |
} |
|
237 |
|
|
238 |
send_buffer[0]=CIRCLE_ACTION_ACK; |
|
239 |
wl_basic_send_global_packet(42,send_buffer,2); |
|
240 |
|
|
241 |
|
|
242 |
break; |
|
243 |
} |
|
154 | 244 |
} |
155 |
stop(); |
|
156 |
orient(); |
|
157 |
|
|
158 |
//button1_wait (); // code for lab1. |
|
159 |
//go_straight_onefoot(); |
|
160 |
stop(); |
|
161 |
/* ****** CODE HERE ******* */ |
|
162 | 245 |
|
246 |
orb_set_color(RED); |
|
163 | 247 |
while(1); /* END HERE */ |
164 | 248 |
|
165 | 249 |
return 0; |
Also available in: Unified diff