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