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