root / trunk / code / projects / libwireless / lib / wl_token_ring.c @ 188
History | View | Annotate | Download (18.2 KB)
1 |
#include <wl_token_ring.h> |
---|---|
2 |
|
3 |
#include <stdlib.h> |
4 |
#include <stdio.h> |
5 |
|
6 |
#include <wl_defs.h> |
7 |
#include <wireless.h> |
8 |
#include <sensor_matrix.h> |
9 |
|
10 |
#ifdef ROBOT
|
11 |
#ifndef FIREFLY
|
12 |
#include <bom.h> |
13 |
#endif
|
14 |
#include <time.h> |
15 |
#endif
|
16 |
|
17 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
18 |
|
19 |
/*Ring States*/
|
20 |
|
21 |
#define NONMEMBER 0 |
22 |
#define MEMBER 1 |
23 |
#define JOINING 2 |
24 |
#define ACCEPTED 3 |
25 |
#define LEAVING 4 |
26 |
|
27 |
/*Frame Types*/
|
28 |
#define TOKEN_JOIN_ACCEPT_FRAME 1 |
29 |
|
30 |
/*Function Prototypes*/
|
31 |
|
32 |
/*Wireless Library Prototypes*/
|
33 |
void wl_token_ring_timeout_handler(void); |
34 |
void wl_token_ring_response_handler(int frame, int received); |
35 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
36 |
int length);
|
37 |
void wl_token_ring_cleanup(void); |
38 |
|
39 |
/*Helper Functions*/
|
40 |
void wl_token_pass_token(void); |
41 |
int get_token_distance(int robot1, int robot2); |
42 |
void wl_token_get_token(void); |
43 |
|
44 |
/*Packet Handling Routines*/
|
45 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
46 |
void wl_token_bom_on_receive(int source); |
47 |
void wl_token_join_receive(int source); |
48 |
void wl_token_join_accept_receive(int source); |
49 |
|
50 |
/*Global Variables*/
|
51 |
|
52 |
//the sensor matrix
|
53 |
SensorMatrix* sensorMatrix; |
54 |
|
55 |
//the robot we are waiting to say it has received the token. -1 if unspecified
|
56 |
int wl_token_next_robot = -1; |
57 |
|
58 |
//true if the robot should be in the token ring, 0 otherwise
|
59 |
int ringState = NONMEMBER;
|
60 |
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state
|
61 |
int acceptor = -1; |
62 |
//id of the robot we are accepting
|
63 |
int accepted = -1; |
64 |
|
65 |
//the counter for when we assume a robot is dead
|
66 |
int deathDelay = -1; |
67 |
//the counter for joining, before we form our own token ring
|
68 |
int joinDelay = -1; |
69 |
|
70 |
//current robot to check in the iterator
|
71 |
int iteratorCount = 0; |
72 |
|
73 |
void do_nothing(void) {} |
74 |
int get_nothing(void) {return -1;} |
75 |
|
76 |
#ifdef ROBOT
|
77 |
#ifndef FIREFLY
|
78 |
void (*bom_on_function) (void) = bom_on; |
79 |
void (*bom_off_function) (void) = bom_off; |
80 |
int (*get_max_bom_function) (void) = get_max_bom; |
81 |
#else
|
82 |
void (*bom_on_function) (void) = do_nothing; |
83 |
void (*bom_off_function) (void) = do_nothing; |
84 |
int (*get_max_bom_function) (void) = get_nothing; |
85 |
#endif
|
86 |
#else
|
87 |
void (*bom_on_function) (void) = do_nothing; |
88 |
void (*bom_off_function) (void) = do_nothing; |
89 |
int (*get_max_bom_function) (void) = get_nothing; |
90 |
#endif
|
91 |
|
92 |
PacketGroupHandler wl_token_ring_handler = |
93 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler, |
94 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
95 |
wl_token_ring_cleanup}; |
96 |
|
97 |
/**
|
98 |
* Initialize the token ring packet group and register it with the
|
99 |
* wireless library. The robot will not join a token ring.
|
100 |
**/
|
101 |
void wl_token_ring_register()
|
102 |
{ |
103 |
if (wl_get_xbee_id() > 0xFF) |
104 |
{ |
105 |
//Note: if this becomes an issue (unlikely), we could limit sensor information
|
106 |
//to half a byte and use 12 bits for the id
|
107 |
WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is ");
|
108 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
109 |
WL_DEBUG_PRINT(".\r\n");
|
110 |
return;
|
111 |
} |
112 |
|
113 |
sensorMatrix = sensor_matrix_create(); |
114 |
//add ourselves to the sensor matrix
|
115 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
|
116 |
|
117 |
wl_register_packet_group(&wl_token_ring_handler); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Removes the packet group from the wireless library.
|
122 |
**/
|
123 |
void wl_token_ring_unregister()
|
124 |
{ |
125 |
wl_unregister_packet_group(&wl_token_ring_handler); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* Sets the functions that are called when the BOM ought to be
|
130 |
* turned on or off. This could be used for things such as
|
131 |
* charging stations, which have multiple BOMs.
|
132 |
*
|
133 |
* @param on_function the function to be called when the BOM
|
134 |
* should be turned on
|
135 |
* @param off_function the function to be called when the BOM
|
136 |
* should be turned off
|
137 |
* @param max_bom_function the function to be called when a
|
138 |
* measurement of the maximum BOM reading is needed.
|
139 |
**/
|
140 |
void wl_token_ring_set_bom_functions(void (*on_function) (void), |
141 |
void (*off_function) (void), int (*max_bom_function) (void)) |
142 |
{ |
143 |
bom_on_function = on_function; |
144 |
bom_off_function = off_function; |
145 |
get_max_bom_function = max_bom_function; |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Called to cleanup the token ring packet group.
|
150 |
**/
|
151 |
void wl_token_ring_cleanup()
|
152 |
{ |
153 |
sensor_matrix_destroy(sensorMatrix); |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Called approximately every quarter second by the wireless library.
|
158 |
**/
|
159 |
void wl_token_ring_timeout_handler()
|
160 |
{ |
161 |
//someone is not responding, assume they are dead
|
162 |
if (deathDelay == 0) |
163 |
{ |
164 |
//pass the token to the next robot if we think someone has died
|
165 |
//also, declare that person dead, as long as it isn't us
|
166 |
if (wl_token_next_robot != wl_get_xbee_id())
|
167 |
{ |
168 |
sensor_matrix_set_in_ring(sensorMatrix, wl_token_next_robot, 0);
|
169 |
WL_DEBUG_PRINT("Robot ");
|
170 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
171 |
WL_DEBUG_PRINT(" has died.\r\n");
|
172 |
wl_token_next_robot = -1;
|
173 |
deathDelay = DEATH_DELAY; |
174 |
} |
175 |
|
176 |
// we may have been dropped from the ring when this is received
|
177 |
if (ringState == MEMBER)
|
178 |
wl_token_pass_token(); |
179 |
} |
180 |
|
181 |
//we must start our own token ring, no one is responding to us
|
182 |
if (joinDelay == 0) |
183 |
{ |
184 |
if (sensor_matrix_get_joined(sensorMatrix) == 0) |
185 |
{ |
186 |
WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n");
|
187 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
|
188 |
ringState = MEMBER; |
189 |
//this will make us pass the token to ourself
|
190 |
//repeatedly, and other robots when they join
|
191 |
deathDelay = DEATH_DELAY; |
192 |
wl_token_next_robot = wl_get_xbee_id(); |
193 |
} |
194 |
else
|
195 |
{ |
196 |
WL_DEBUG_PRINT("Attempting to join the token ring again.\r\n");
|
197 |
//attempt to rejoin with a random delay
|
198 |
wl_token_ring_join(); |
199 |
joinDelay = rand() / (RAND_MAX / JOIN_DELAY) + 1;
|
200 |
} |
201 |
} |
202 |
|
203 |
if (deathDelay >= 0) |
204 |
deathDelay--; |
205 |
if (joinDelay >= 0) |
206 |
joinDelay--; |
207 |
} |
208 |
|
209 |
/**
|
210 |
* Called when the XBee tells us if a packet we sent has been received.
|
211 |
*
|
212 |
* @param frame the frame number assigned when the packet was sent
|
213 |
* @param received 1 if the packet was received, 0 otherwise
|
214 |
**/
|
215 |
void wl_token_ring_response_handler(int frame, int received) |
216 |
{ |
217 |
if (!received)
|
218 |
{ |
219 |
WL_DEBUG_PRINT("FAILED.\r\n");
|
220 |
} |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Called when we recieve a token ring packet.
|
225 |
* @param type the type of the packet
|
226 |
* @param source the id of the robot who sent the packet
|
227 |
* @param packet the data in the packet
|
228 |
* @param length the length of the packet in bytes
|
229 |
**/
|
230 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
231 |
int length)
|
232 |
{ |
233 |
switch (type)
|
234 |
{ |
235 |
case WL_TOKEN_PASS:
|
236 |
if (length < 1) |
237 |
{ |
238 |
WL_DEBUG_PRINT("Malformed Token Pass packet received.\r\n");
|
239 |
return;
|
240 |
} |
241 |
wl_token_pass_receive(source, packet[0], packet + 1, length - 1); |
242 |
break;
|
243 |
case WL_TOKEN_BOM_ON:
|
244 |
//add the robot to the sensor matrix if it is not already there
|
245 |
wl_token_bom_on_receive(source); |
246 |
break;
|
247 |
case WL_TOKEN_JOIN:
|
248 |
wl_token_join_receive(source); |
249 |
break;
|
250 |
case WL_TOKEN_JOIN_ACCEPT:
|
251 |
wl_token_join_accept_receive(source); |
252 |
break;
|
253 |
default:
|
254 |
WL_DEBUG_PRINT("Unimplemented token ring packet received.\r\n");
|
255 |
break;
|
256 |
} |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Causes the robot to join an existing token ring, or create one
|
261 |
* if no token ring exists. The token ring uses global and robot to robot
|
262 |
* packets, and does not rely on any PAN.
|
263 |
**/
|
264 |
void wl_token_ring_join()
|
265 |
{ |
266 |
WL_DEBUG_PRINT("Joining the token ring.\r\n");
|
267 |
ringState = JOINING; |
268 |
joinDelay = JOIN_DELAY; |
269 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, |
270 |
NULL, 0, 0); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Causes the robot to leave the token ring. The robot stops
|
275 |
* alerting others of its location, but continues storing the
|
276 |
* locations of other robots.
|
277 |
**/
|
278 |
void wl_token_ring_leave()
|
279 |
{ |
280 |
ringState = LEAVING; |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Returns the BOM reading robot source has for robot dest.
|
285 |
*
|
286 |
* @param source the robot that made the BOM reading
|
287 |
* @param dest the robot whose relative location is returned
|
288 |
*
|
289 |
* @return a BOM reading from robot source to robot dest,
|
290 |
* in the range 0-15, or -1 if it is unknown
|
291 |
**/
|
292 |
int wl_token_get_sensor_reading(int source, int dest) |
293 |
{ |
294 |
return sensor_matrix_get_reading(sensorMatrix, source, dest);
|
295 |
} |
296 |
|
297 |
/**
|
298 |
* Returns the BOM reading we have for robot dest.
|
299 |
*
|
300 |
* @param dest the robot whose relative location is returned
|
301 |
*
|
302 |
* @return a BOM reading from us to robot dest, in the range
|
303 |
* 0-15, or -1 if it is unkown
|
304 |
**/
|
305 |
int wl_token_get_my_sensor_reading(int dest) |
306 |
{ |
307 |
return wl_token_get_sensor_reading(wl_get_xbee_id(), dest);
|
308 |
} |
309 |
|
310 |
/**
|
311 |
* This method is called when we receive a token pass packet.
|
312 |
* @param source is the robot it came from
|
313 |
* @param nextRobot is the robot the token was passed to
|
314 |
* @param sensorData a char with an id followed by a char with the sensor
|
315 |
* reading for that robot, repeated for sensorDataLength bytes
|
316 |
* @param sensorDataLength the length in bytes of sensorData
|
317 |
*/
|
318 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
319 |
{ |
320 |
int i, j;
|
321 |
deathDelay = -1;
|
322 |
|
323 |
WL_DEBUG_PRINT("Received the token from robot");
|
324 |
WL_DEBUG_PRINT_INT(source); |
325 |
WL_DEBUG_PRINT(", next robot is ");
|
326 |
WL_DEBUG_PRINT_INT((int)nextRobot);
|
327 |
WL_DEBUG_PRINT(" \r\n");
|
328 |
sensor_matrix_set_in_ring(sensorMatrix, source, 1);
|
329 |
|
330 |
//with this packet, we are passed the id of the next robot in the ring
|
331 |
//and the sensor matrix, a list of id and sensor reading pairs (two bytes for both)
|
332 |
j = 0;
|
333 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
334 |
{ |
335 |
if (i == source)
|
336 |
continue;
|
337 |
|
338 |
//set the sensor information we receive
|
339 |
if (j < sensorDataLength / 2 && sensorData[2 * j] == i) |
340 |
{ |
341 |
//the robot we were going to accept has already been accepted
|
342 |
if (accepted == i)
|
343 |
{ |
344 |
accepted = -1;
|
345 |
WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n");
|
346 |
} |
347 |
sensor_matrix_set_reading(sensorMatrix, source, i, |
348 |
sensorData[2 * j + 1]); |
349 |
sensor_matrix_set_in_ring(sensorMatrix, i, 1);
|
350 |
j++; |
351 |
} |
352 |
else
|
353 |
{ |
354 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
355 |
{ |
356 |
WL_DEBUG_PRINT("Robot ");
|
357 |
WL_DEBUG_PRINT_INT(i); |
358 |
WL_DEBUG_PRINT(" has been removed from the sensor matrix of robot ");
|
359 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
360 |
WL_DEBUG_PRINT(" due to a packet from robot ");
|
361 |
WL_DEBUG_PRINT_INT(source); |
362 |
WL_DEBUG_PRINT(".\r\n");
|
363 |
sensor_matrix_set_in_ring(sensorMatrix, i, 0);
|
364 |
} |
365 |
|
366 |
if (i == wl_get_xbee_id() && ringState == MEMBER)
|
367 |
{ |
368 |
ringState = NONMEMBER; |
369 |
wl_token_ring_join(); |
370 |
|
371 |
WL_DEBUG_PRINT("We have been removed from the ring ");
|
372 |
WL_DEBUG_PRINT("and are rejoining.\r\n");
|
373 |
} |
374 |
|
375 |
//the person who accepted us is dead... let's ask again
|
376 |
if (i == acceptor)
|
377 |
{ |
378 |
sensor_matrix_set_in_ring(sensorMatrix, |
379 |
wl_get_xbee_id(), 1);
|
380 |
ringState = NONMEMBER; |
381 |
acceptor = -1;
|
382 |
wl_token_ring_join(); |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
wl_token_next_robot = nextRobot; |
388 |
|
389 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
390 |
|
391 |
//we have the token
|
392 |
if (wl_token_next_robot == wl_get_xbee_id())
|
393 |
wl_token_get_token(); |
394 |
} |
395 |
|
396 |
/**
|
397 |
* Gets the distance in the token ring between two robots.
|
398 |
*
|
399 |
* @param robot1 the first robot
|
400 |
* @param robot2 the second robot
|
401 |
*
|
402 |
* @return the number of passes before the token is expected
|
403 |
* to reach robot2 from robot1
|
404 |
**/
|
405 |
int get_token_distance(int robot1, int robot2) |
406 |
{ |
407 |
int curr = robot1 + 1; |
408 |
int count = 1; |
409 |
while (1) |
410 |
{ |
411 |
if (curr == sensor_matrix_get_size(sensorMatrix))
|
412 |
curr = 0;
|
413 |
if (curr == robot2)
|
414 |
break;
|
415 |
if (sensor_matrix_get_in_ring(sensorMatrix, curr))
|
416 |
count++; |
417 |
curr++; |
418 |
} |
419 |
return count;
|
420 |
} |
421 |
|
422 |
/**
|
423 |
* Passes the token to the next robot in the token ring.
|
424 |
**/
|
425 |
void wl_token_pass_token()
|
426 |
{ |
427 |
char nextRobot;
|
428 |
int i = wl_get_xbee_id() + 1; |
429 |
if (accepted == -1) |
430 |
{ |
431 |
while (1) |
432 |
{ |
433 |
if (i == sensor_matrix_get_size(sensorMatrix))
|
434 |
i = 0;
|
435 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
436 |
{ |
437 |
nextRobot = (char)i;
|
438 |
break;
|
439 |
} |
440 |
i++; |
441 |
} |
442 |
} |
443 |
else
|
444 |
{ |
445 |
WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n");
|
446 |
//add a new robot to the token ring
|
447 |
sensor_matrix_set_in_ring(sensorMatrix, accepted, 1);
|
448 |
nextRobot = accepted; |
449 |
accepted = -1;
|
450 |
} |
451 |
|
452 |
//we don't include ourself
|
453 |
int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1); |
454 |
char* buf = (char*)malloc(packetSize * sizeof(char)); |
455 |
if (!buf)
|
456 |
{ |
457 |
WL_DEBUG_PRINT_INT(packetSize); |
458 |
WL_DEBUG_PRINT("Out of memory - pass token.\r\n");
|
459 |
return;
|
460 |
} |
461 |
buf[0] = nextRobot;
|
462 |
|
463 |
int j = 0; |
464 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
465 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id())
|
466 |
{ |
467 |
buf[2*j + 1] = i; |
468 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
469 |
j++; |
470 |
} |
471 |
|
472 |
WL_DEBUG_PRINT("Passing the token to robot ");
|
473 |
WL_DEBUG_PRINT_INT(buf[0]);
|
474 |
WL_DEBUG_PRINT(".\r\n");
|
475 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
476 |
buf, packetSize, 3);
|
477 |
|
478 |
wl_token_next_robot = nextRobot; |
479 |
deathDelay = DEATH_DELAY; |
480 |
free(buf); |
481 |
} |
482 |
|
483 |
/**
|
484 |
* Called when a packet is received stating that another robot has turned
|
485 |
* its BOM on. Our BOM is then read, and the data is added to the sensor
|
486 |
* matrix.
|
487 |
*
|
488 |
* @param source the robot whose BOM is on
|
489 |
**/
|
490 |
void wl_token_bom_on_receive(int source) |
491 |
{ |
492 |
WL_DEBUG_PRINT("Robot ");
|
493 |
WL_DEBUG_PRINT_INT(source); |
494 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n");
|
495 |
|
496 |
//make sure we don't declare the robot dead if it's
|
497 |
//flashing its BOM
|
498 |
if (source == wl_token_next_robot)
|
499 |
deathDelay = DEATH_DELAY; |
500 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
501 |
source, get_max_bom_function()); |
502 |
} |
503 |
|
504 |
/**
|
505 |
* This method is called when we receive the token. Upon receiving
|
506 |
* the token, we must send a BOM_ON packet, flash the BOM, and send
|
507 |
* the token to the next robot.
|
508 |
*
|
509 |
* If there is a pending request for the token, this is processed first.
|
510 |
**/
|
511 |
void wl_token_get_token()
|
512 |
{ |
513 |
WL_DEBUG_PRINT("We have the token.\r\n");
|
514 |
if (ringState == ACCEPTED)
|
515 |
{ |
516 |
sensor_matrix_set_in_ring(sensorMatrix, |
517 |
wl_get_xbee_id(), 1);
|
518 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n");
|
519 |
ringState = MEMBER; |
520 |
} |
521 |
|
522 |
if (ringState == LEAVING || ringState == NONMEMBER)
|
523 |
{ |
524 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
|
525 |
if (ringState == NONMEMBER)
|
526 |
{ |
527 |
WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n");
|
528 |
} |
529 |
return;
|
530 |
} |
531 |
|
532 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
|
533 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
534 |
NULL, 0, 0); |
535 |
|
536 |
bom_on_function(); |
537 |
#ifdef ROBOT
|
538 |
delay_ms(BOM_DELAY); |
539 |
#endif
|
540 |
bom_off_function(); |
541 |
|
542 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id()))
|
543 |
{ |
544 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
|
545 |
return;
|
546 |
} |
547 |
|
548 |
wl_token_pass_token(); |
549 |
} |
550 |
|
551 |
/**
|
552 |
* Called when a request to join the token ring is received.
|
553 |
* If we are the robot preceding the requester in the ring,
|
554 |
* we respond with a JOIN_ACCEPT packet and pass the token to
|
555 |
* this robot when we receive the token.
|
556 |
*
|
557 |
* @param source the robot who requested to join
|
558 |
**/
|
559 |
void wl_token_join_receive(int source) |
560 |
{ |
561 |
WL_DEBUG_PRINT("Received joining request from robot ");
|
562 |
WL_DEBUG_PRINT_INT(source); |
563 |
WL_DEBUG_PRINT(".\r\n");
|
564 |
|
565 |
//we cannot accept the request if we are not a member
|
566 |
if (ringState != MEMBER)
|
567 |
return;
|
568 |
//if they didn't get our response, see if we should respond again
|
569 |
if (accepted == source)
|
570 |
accepted = -1;
|
571 |
//we can only accept one request at a time
|
572 |
if (accepted != -1) |
573 |
return;
|
574 |
|
575 |
//check if we are the preceding robot in the token ring
|
576 |
int i = source - 1; |
577 |
while (1) |
578 |
{ |
579 |
if (i < 0) |
580 |
i = sensor_matrix_get_size(sensorMatrix) - 1;
|
581 |
//we must send a join acceptance
|
582 |
if (i == wl_get_xbee_id())
|
583 |
break;
|
584 |
|
585 |
//another robot will handle it
|
586 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
587 |
return;
|
588 |
i--; |
589 |
} |
590 |
|
591 |
accepted = source; |
592 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT, |
593 |
NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME); |
594 |
|
595 |
WL_DEBUG_PRINT("Accepting robot ");
|
596 |
WL_DEBUG_PRINT_INT(source); |
597 |
WL_DEBUG_PRINT(" into the token ring.\r\n");
|
598 |
|
599 |
joinDelay = -1;
|
600 |
|
601 |
// the token ring has not started yet
|
602 |
if (sensor_matrix_get_joined(sensorMatrix) == 1) |
603 |
wl_token_pass_token(); |
604 |
} |
605 |
|
606 |
/**
|
607 |
* Called when we receive a JOIN_ACCEPT packet in attempting to join
|
608 |
* the token ring.
|
609 |
* Our attempt to join the ring is stopped, and we wait for the token.
|
610 |
*
|
611 |
* @param source the robot who accepted us
|
612 |
**/
|
613 |
void wl_token_join_accept_receive(int source) |
614 |
{ |
615 |
WL_DEBUG_PRINT("Accepted into the token ring by robot ");
|
616 |
WL_DEBUG_PRINT_INT(source); |
617 |
WL_DEBUG_PRINT(".\r\n");
|
618 |
joinDelay = -1;
|
619 |
ringState = ACCEPTED; |
620 |
acceptor = source; |
621 |
|
622 |
//add ourselves to the token ring
|
623 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
|
624 |
} |
625 |
|
626 |
/**
|
627 |
* Returns the number of robots in the token ring.
|
628 |
*
|
629 |
* @return the number of robots in the token ring
|
630 |
**/
|
631 |
int wl_token_get_robots_in_ring(void) |
632 |
{ |
633 |
return sensor_matrix_get_joined(sensorMatrix);
|
634 |
} |
635 |
|
636 |
/**
|
637 |
* Returns true if the specified robot is in the token ring, false
|
638 |
* otherwise.
|
639 |
*
|
640 |
* @param robot the robot to check for whether it is in the token ring
|
641 |
* @return nonzero if the robot is in the token ring, zero otherwise
|
642 |
**/
|
643 |
int wl_token_is_robot_in_ring(int robot) |
644 |
{ |
645 |
return sensor_matrix_get_in_ring(sensorMatrix, robot);
|
646 |
} |
647 |
|
648 |
/**
|
649 |
* Begins iterating through the robots in the token ring.
|
650 |
*
|
651 |
* @see wl_token_iterator_has_next, wl_token_iterator_next
|
652 |
**/
|
653 |
void wl_token_iterator_begin(void) |
654 |
{ |
655 |
int i = 0; |
656 |
iteratorCount = 0;
|
657 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) &&
|
658 |
i < sensor_matrix_get_size(sensorMatrix)) |
659 |
i++; |
660 |
if (i == sensor_matrix_get_size(sensorMatrix))
|
661 |
i = -1;
|
662 |
iteratorCount = i; |
663 |
} |
664 |
|
665 |
/**
|
666 |
* Returns true if there are more robots in the token ring
|
667 |
* to iterate through, and false otherwise.
|
668 |
*
|
669 |
* @return nonzero if there are more robots to iterate through,
|
670 |
* zero otherwise
|
671 |
*
|
672 |
* @see wl_token_iterator_begin, wl_token_iterator_next
|
673 |
**/
|
674 |
int wl_token_iterator_has_next(void) |
675 |
{ |
676 |
return iteratorCount != -1; |
677 |
} |
678 |
|
679 |
/**
|
680 |
* Returns the next robot ID in the token ring.
|
681 |
*
|
682 |
* @return the next robot ID in the token ring, or -1 if none exists
|
683 |
*
|
684 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next
|
685 |
**/
|
686 |
int wl_token_iterator_next(void) |
687 |
{ |
688 |
int result = iteratorCount;
|
689 |
if (result < 0) |
690 |
return result;
|
691 |
|
692 |
iteratorCount++; |
693 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) &&
|
694 |
iteratorCount < sensor_matrix_get_size(sensorMatrix)) |
695 |
iteratorCount++; |
696 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix))
|
697 |
iteratorCount = -1;
|
698 |
return result;
|
699 |
} |
700 |
|
701 |
/**
|
702 |
* Returns the number of robots currently in the token ring.
|
703 |
*
|
704 |
* @return the number of robots in the token ring
|
705 |
**/
|
706 |
int wl_token_get_num_robots(void) |
707 |
{ |
708 |
return sensor_matrix_get_joined(sensorMatrix);
|
709 |
} |
710 |
|
711 |
/**
|
712 |
* Returns the number of robots in the sensor matrix.
|
713 |
*
|
714 |
* @return the number of robots in the sensor matrix
|
715 |
**/
|
716 |
int wl_token_get_matrix_size(void) |
717 |
{ |
718 |
return sensor_matrix_get_size(sensorMatrix);
|
719 |
} |
720 |
|