root / trunk / code / behaviors / formation_control / circle2 / circle.c @ 1552
History | View | Annotate | Download (5.42 KB)
1 |
#include <dragonfly_lib.h> |
---|---|
2 |
#include <wl_basic.h> |
3 |
#include <encoders.h> |
4 |
#include "circle.h" |
5 |
|
6 |
int EDGE = 0; |
7 |
int BEACON = 1; |
8 |
|
9 |
/*
|
10 |
This program is used to make robots target a center (leader) robot using the BOM,
|
11 |
then drive toward it and stop at a certain distance away.
|
12 |
|
13 |
The distance will eventually be adjustable.
|
14 |
|
15 |
With adjustment, the leader robot will be able to turn and use its standardized
|
16 |
rangefinder to reposition or space the robots evenly.
|
17 |
|
18 |
AuTHORS: Niko, Alex, Reva, Echo, Steve
|
19 |
*/
|
20 |
|
21 |
|
22 |
/*
|
23 |
TODO:
|
24 |
Check BOM more often and before final stop
|
25 |
Make BOM check more comprehensively
|
26 |
Use the center bot to check distances
|
27 |
Count them ("spam" method)
|
28 |
Use beacon to find relative positions
|
29 |
Beacon tells them how to move to be at the right distance
|
30 |
Wireless communication, initialization
|
31 |
*/
|
32 |
|
33 |
void forward(int speed){ // set the motors to this forward speed. |
34 |
motor_l_set(FORWARD,speed); |
35 |
motor_r_set(FORWARD,speed); |
36 |
} |
37 |
void left(int speed){ // turn left at this speed. |
38 |
motor_l_set(FORWARD,speed); |
39 |
motor_r_set(BACKWARD,speed); |
40 |
} |
41 |
void right(int speed){ |
42 |
motor_l_set(BACKWARD,speed); |
43 |
motor_r_set(FORWARD,speed); |
44 |
} |
45 |
void stop(void){ // could be set to motors_off(), or just use this as an alternative. |
46 |
motor_l_set(BACKWARD,0);
|
47 |
motor_r_set(FORWARD,0);
|
48 |
} |
49 |
void setforward(int spd1, int spd2){ |
50 |
motor_l_set(FORWARD,spd1); |
51 |
motor_r_set(FORWARD,spd2); |
52 |
} |
53 |
void backward(int speed){ |
54 |
motor_l_set(BACKWARD, speed); |
55 |
motor_r_set(BACKWARD, speed); |
56 |
} |
57 |
int get_distance(void){ // takes an averaged reading of the front rangefinder |
58 |
int temp,distance,kk=5; // kk sets this to 5 readings. |
59 |
distance =0;
|
60 |
for (int i=0; i<kk; i++){ |
61 |
temp = range_read_distance(IR2); |
62 |
if (temp == -1) {temp=0; kk--;} |
63 |
distance+= temp; |
64 |
delay_ms(3);
|
65 |
} |
66 |
if (kk>0) |
67 |
return (int)(distance/kk); |
68 |
else
|
69 |
return 0; |
70 |
} |
71 |
void turn_to_beacon(int max){ |
72 |
if (max>-1 && max<16){ |
73 |
int index = (max+12)%16; |
74 |
if (index==0) { |
75 |
stop(); |
76 |
} |
77 |
else if (index<8) right(170); |
78 |
else left(170); |
79 |
} |
80 |
} |
81 |
|
82 |
void turn_to_beacon2(int max){ // like the previous but no stop() call |
83 |
if (max>-1 && max<16){ |
84 |
int index = (max+12)%16; |
85 |
if (index==0) { |
86 |
|
87 |
} |
88 |
else if (index<8) right(170); |
89 |
else left(170); |
90 |
} |
91 |
} |
92 |
void orient(void){ |
93 |
int max_index = -1; |
94 |
while (max_index!=4) { |
95 |
/* Refresh and make sure the table is updated */
|
96 |
bom_refresh(BOM_ALL); |
97 |
max_index = bom_get_max(); |
98 |
turn_to_beacon(max_index); |
99 |
delay_ms(22);
|
100 |
} |
101 |
} |
102 |
void orient2(void){ |
103 |
int max_index = -1; |
104 |
while (max_index!=4) { |
105 |
/* Refresh and make sure the table is updated */
|
106 |
bom_refresh(BOM_ALL); |
107 |
max_index = bom_get_max(); |
108 |
turn_to_beacon2(max_index); |
109 |
delay_ms(22);
|
110 |
} |
111 |
} |
112 |
void go_straight(void){ // drives forward a hardcoded distance. May not be useful. |
113 |
forward(200);
|
114 |
encoder_rst_dx(LEFT); |
115 |
encoder_rst_dx(RIGHT); |
116 |
delay_ms(100);
|
117 |
int x_left = encoder_get_x(LEFT), x_right = encoder_get_x(RIGHT);
|
118 |
int count = 0; |
119 |
int d;
|
120 |
while (count<25){ //count = 25 when bot6; count <12 |
121 |
x_left = encoder_get_x(LEFT); |
122 |
x_right = encoder_get_x(RIGHT); |
123 |
d = x_right-x_left; |
124 |
if (d>13 || d<-13){ |
125 |
if(d<50 && d>-50){ |
126 |
d = round(1.0*d/4); |
127 |
setforward(200+d, 200-d); |
128 |
} |
129 |
} |
130 |
delay_ms(32);
|
131 |
count++; |
132 |
} |
133 |
} |
134 |
void go_straight_onefoot(void){ // essentially, another name for the above. Should be removed. |
135 |
go_straight(); |
136 |
} |
137 |
|
138 |
|
139 |
|
140 |
int main(void) |
141 |
{ |
142 |
/* Initialize dragonfly board */
|
143 |
dragonfly_init(ALL_ON); |
144 |
xbee_init(); |
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); |
155 |
|
156 |
|
157 |
int state = EDGE;
|
158 |
if(wheel()<100) |
159 |
{ |
160 |
state=EDGE; |
161 |
} |
162 |
else
|
163 |
{ |
164 |
state=BEACON; |
165 |
} |
166 |
|
167 |
int distance=1000; // how far away to stop. |
168 |
int onefoot=300, speed=200; // one foot is 490 for bot 1; one foot is 200 for bot6 |
169 |
|
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 |
} |
244 |
} |
245 |
|
246 |
orb_set_color(RED); |
247 |
while(1); /* END HERE */ |
248 |
|
249 |
return 0; |
250 |
} |