Revision 668
Updated port of wireless library to bay boards.
wl_token_ring.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (c) 2007 Colony Project |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Permission is hereby granted, free of charge, to any person |
5 | 5 |
* obtaining a copy of this software and associated documentation |
6 | 6 |
* files (the "Software"), to deal in the Software without |
... | ... | |
9 | 9 |
* copies of the Software, and to permit persons to whom the |
10 | 10 |
* Software is furnished to do so, subject to the following |
11 | 11 |
* conditions: |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* The above copyright notice and this permission notice shall be |
14 | 14 |
* included in all copies or substantial portions of the Software. |
15 |
*
|
|
15 |
* |
|
16 | 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 | 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
... | ... | |
64 | 64 |
/*Function Prototypes*/ |
65 | 65 |
|
66 | 66 |
/*Wireless Library Prototypes*/ |
67 |
void wl_token_ring_timeout_handler(void); |
|
68 |
void wl_token_ring_response_handler(int frame, int received); |
|
69 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
|
70 |
int length); |
|
71 |
void wl_token_ring_cleanup(void); |
|
67 |
static void wl_token_ring_timeout_handler(void); |
|
68 |
static void wl_token_ring_response_handler(int frame, int received); |
|
69 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length); |
|
70 |
static void wl_token_ring_cleanup(void); |
|
72 | 71 |
|
73 | 72 |
/*Helper Functions*/ |
74 |
void wl_token_pass_token(void);
|
|
75 |
int get_token_distance(int robot1, int robot2); |
|
76 |
void wl_token_get_token(void); |
|
73 |
static int wl_token_pass_token(void);
|
|
74 |
static int get_token_distance(int robot1, int robot2);
|
|
75 |
static void wl_token_get_token(void);
|
|
77 | 76 |
|
78 | 77 |
/*Packet Handling Routines*/ |
79 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
|
80 |
void wl_token_bom_on_receive(int source); |
|
81 |
void wl_token_join_receive(int source); |
|
82 |
void wl_token_join_accept_receive(int source); |
|
78 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength);
|
|
79 |
static void wl_token_bom_on_receive(int source);
|
|
80 |
static void wl_token_join_receive(int source);
|
|
81 |
static void wl_token_join_accept_receive(int source);
|
|
83 | 82 |
|
84 | 83 |
/*Global Variables*/ |
85 | 84 |
|
86 | 85 |
//the sensor matrix |
87 |
SensorMatrix* sensorMatrix; |
|
86 |
static SensorMatrix* sensorMatrix;
|
|
88 | 87 |
|
89 | 88 |
//the robot we are waiting to say it has received the token. -1 if unspecified |
90 |
int wl_token_next_robot = -1; |
|
89 |
static int wl_token_next_robot = -1;
|
|
91 | 90 |
|
92 | 91 |
//true if the robot should be in the token ring, 0 otherwise |
93 |
int ringState = NONMEMBER; |
|
92 |
static int ringState = NONMEMBER;
|
|
94 | 93 |
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state |
95 |
int acceptor = -1; |
|
94 |
static int acceptor = -1;
|
|
96 | 95 |
//id of the robot we are accepting |
97 |
int accepted = -1; |
|
96 |
static int accepted = -1;
|
|
98 | 97 |
|
99 | 98 |
//the counter for when we assume a robot is dead |
100 |
int deathDelay = -1; |
|
99 |
static int deathDelay = -1;
|
|
101 | 100 |
//the counter for joining, before we form our own token ring |
102 |
int joinDelay = -1; |
|
101 |
static int joinDelay = -1;
|
|
103 | 102 |
|
104 | 103 |
//current robot to check in the iterator |
105 |
int iteratorCount = 0; |
|
104 |
static int iteratorCount = 0;
|
|
106 | 105 |
|
107 | 106 |
// the amount of time a robot has had its BOM on for |
108 |
int bom_on_count = 0; |
|
107 |
static int bom_on_count = 0;
|
|
109 | 108 |
|
110 |
void do_nothing(void) {} |
|
111 |
int get_nothing(void) {return -1;} |
|
109 |
static void do_nothing(void) {}
|
|
110 |
static int get_nothing(void) {return -1;}
|
|
112 | 111 |
|
113 | 112 |
#ifdef ROBOT |
114 | 113 |
#ifndef FIREFLY |
115 |
void (*bom_on_function) (void) = bom_on; |
|
116 |
void (*bom_off_function) (void) = bom_off; |
|
117 |
int (*get_max_bom_function) (void) = get_max_bom; |
|
114 |
static void (*bom_on_function) (void) = bom_on;
|
|
115 |
static void (*bom_off_function) (void) = bom_off;
|
|
116 |
static int (*get_max_bom_function) (void) = get_max_bom;
|
|
118 | 117 |
#else |
119 |
void (*bom_on_function) (void) = do_nothing; |
|
120 |
void (*bom_off_function) (void) = do_nothing; |
|
121 |
int (*get_max_bom_function) (void) = get_nothing; |
|
118 |
static void (*bom_on_function) (void) = do_nothing;
|
|
119 |
static void (*bom_off_function) (void) = do_nothing;
|
|
120 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
122 | 121 |
#endif |
123 | 122 |
#else |
124 |
void (*bom_on_function) (void) = do_nothing; |
|
125 |
void (*bom_off_function) (void) = do_nothing; |
|
126 |
int (*get_max_bom_function) (void) = get_nothing; |
|
123 |
static void (*bom_on_function) (void) = do_nothing;
|
|
124 |
static void (*bom_off_function) (void) = do_nothing;
|
|
125 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
127 | 126 |
#endif |
128 | 127 |
|
129 |
PacketGroupHandler wl_token_ring_handler = |
|
130 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
|
|
128 |
static PacketGroupHandler wl_token_ring_handler =
|
|
129 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler, |
|
131 | 130 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
132 | 131 |
wl_token_ring_cleanup}; |
133 | 132 |
|
134 | 133 |
/** |
134 |
* Causes the robot to join an existing token ring, or create one |
|
135 |
* if no token ring exists. The token ring uses global and robot to robot |
|
136 |
* packets, and does not rely on any PAN. |
|
137 |
**/ |
|
138 |
int wl_token_ring_join() |
|
139 |
{ |
|
140 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
|
141 |
|
|
142 |
ringState = JOINING; |
|
143 |
joinDelay = DEATH_DELAY * 2; |
|
144 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) { |
|
145 |
return -1; |
|
146 |
} |
|
147 |
|
|
148 |
return 0; |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* Causes the robot to leave the token ring. The robot stops |
|
153 |
* alerting others of its location, but continues storing the |
|
154 |
* locations of other robots. |
|
155 |
**/ |
|
156 |
void wl_token_ring_leave() |
|
157 |
{ |
|
158 |
ringState = LEAVING; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
135 | 162 |
* Initialize the token ring packet group and register it with the |
136 | 163 |
* wireless library. The robot will not join a token ring. |
137 | 164 |
**/ |
138 |
void wl_token_ring_register()
|
|
165 |
int wl_token_ring_register()
|
|
139 | 166 |
{ |
140 | 167 |
if (wl_get_xbee_id() > 0xFF) |
141 | 168 |
{ |
... | ... | |
144 | 171 |
WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is "); |
145 | 172 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
146 | 173 |
WL_DEBUG_PRINT(".\r\n"); |
147 |
return; |
|
174 |
return -1;
|
|
148 | 175 |
} |
149 |
|
|
176 |
|
|
150 | 177 |
sensorMatrix = sensor_matrix_create(); |
151 | 178 |
//add ourselves to the sensor matrix |
152 | 179 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
153 | 180 |
|
154 | 181 |
wl_register_packet_group(&wl_token_ring_handler); |
182 |
|
|
183 |
return 0; |
|
155 | 184 |
} |
156 | 185 |
|
157 | 186 |
/** |
... | ... | |
164 | 193 |
|
165 | 194 |
/** |
166 | 195 |
* Sets the functions that are called when the BOM ought to be |
167 |
* turned on or off. This could be used for things such as
|
|
196 |
* turned on or off. This could be used for things such as |
|
168 | 197 |
* charging stations, which have multiple BOMs. |
169 | 198 |
* |
170 | 199 |
* @param on_function the function to be called when the BOM |
... | ... | |
185 | 214 |
/** |
186 | 215 |
* Called to cleanup the token ring packet group. |
187 | 216 |
**/ |
188 |
void wl_token_ring_cleanup() |
|
217 |
static void wl_token_ring_cleanup()
|
|
189 | 218 |
{ |
190 | 219 |
sensor_matrix_destroy(sensorMatrix); |
191 | 220 |
} |
... | ... | |
193 | 222 |
/** |
194 | 223 |
* Called approximately every quarter second by the wireless library. |
195 | 224 |
**/ |
196 |
void wl_token_ring_timeout_handler() |
|
225 |
static void wl_token_ring_timeout_handler()
|
|
197 | 226 |
{ |
198 | 227 |
//someone is not responding, assume they are dead |
199 | 228 |
if (deathDelay == 0) |
... | ... | |
209 | 238 |
wl_token_next_robot = -1; |
210 | 239 |
deathDelay = DEATH_DELAY; |
211 | 240 |
} |
212 |
|
|
241 |
|
|
213 | 242 |
// we may have been dropped from the ring when this is received |
214 |
if (ringState == MEMBER) |
|
243 |
if (ringState == MEMBER) {
|
|
215 | 244 |
wl_token_pass_token(); |
245 |
} |
|
216 | 246 |
} |
217 | 247 |
|
218 | 248 |
//we must start our own token ring, no one is responding to us |
... | ... | |
237 | 267 |
} |
238 | 268 |
} |
239 | 269 |
|
240 |
if (deathDelay >= 0) |
|
270 |
if (deathDelay >= 0) {
|
|
241 | 271 |
deathDelay--; |
242 |
if (joinDelay >= 0) |
|
272 |
} |
|
273 |
|
|
274 |
if (joinDelay >= 0) { |
|
243 | 275 |
joinDelay--; |
244 |
if (bom_on_count >= 0) |
|
276 |
} |
|
277 |
|
|
278 |
if (bom_on_count >= 0) { |
|
245 | 279 |
bom_on_count++; |
280 |
} |
|
246 | 281 |
} |
247 | 282 |
|
248 | 283 |
/** |
249 | 284 |
* Called when the XBee tells us if a packet we sent has been received. |
250 |
*
|
|
285 |
* |
|
251 | 286 |
* @param frame the frame number assigned when the packet was sent |
252 | 287 |
* @param received 1 if the packet was received, 0 otherwise |
253 | 288 |
**/ |
254 |
void wl_token_ring_response_handler(int frame, int received) |
|
289 |
static void wl_token_ring_response_handler(int frame, int received)
|
|
255 | 290 |
{ |
256 | 291 |
if (!received) |
257 | 292 |
{ |
... | ... | |
266 | 301 |
* @param packet the data in the packet |
267 | 302 |
* @param length the length of the packet in bytes |
268 | 303 |
**/ |
269 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
|
270 |
int length) |
|
304 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length) |
|
271 | 305 |
{ |
272 | 306 |
switch (type) |
273 | 307 |
{ |
... | ... | |
296 | 330 |
} |
297 | 331 |
|
298 | 332 |
/** |
299 |
* Causes the robot to join an existing token ring, or create one |
|
300 |
* if no token ring exists. The token ring uses global and robot to robot |
|
301 |
* packets, and does not rely on any PAN. |
|
302 |
**/ |
|
303 |
void wl_token_ring_join() |
|
304 |
{ |
|
305 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
|
306 |
ringState = JOINING; |
|
307 |
joinDelay = DEATH_DELAY * 2; |
|
308 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, |
|
309 |
NULL, 0, 0); |
|
310 |
} |
|
311 |
|
|
312 |
/** |
|
313 |
* Causes the robot to leave the token ring. The robot stops |
|
314 |
* alerting others of its location, but continues storing the |
|
315 |
* locations of other robots. |
|
316 |
**/ |
|
317 |
void wl_token_ring_leave() |
|
318 |
{ |
|
319 |
ringState = LEAVING; |
|
320 |
} |
|
321 |
|
|
322 |
/** |
|
323 | 333 |
* Returns the BOM reading robot source has for robot dest. |
324 | 334 |
* |
325 | 335 |
* @param source the robot that made the BOM reading |
... | ... | |
331 | 341 |
int wl_token_get_sensor_reading(int source, int dest) |
332 | 342 |
{ |
333 | 343 |
if (wl_token_is_robot_in_ring(dest) && |
334 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) |
|
344 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) {
|
|
335 | 345 |
return sensor_matrix_get_reading(sensorMatrix, source, dest); |
346 |
} |
|
347 |
|
|
336 | 348 |
return -1; |
337 | 349 |
} |
338 | 350 |
|
339 | 351 |
/** |
340 | 352 |
* Returns the BOM reading we have for robot dest. |
341 |
*
|
|
353 |
* |
|
342 | 354 |
* @param dest the robot whose relative location is returned |
343 | 355 |
* |
344 | 356 |
* @return a BOM reading from us to robot dest, in the range |
... | ... | |
349 | 361 |
return wl_token_get_sensor_reading(wl_get_xbee_id(), dest); |
350 | 362 |
} |
351 | 363 |
|
364 |
|
|
352 | 365 |
/** |
366 |
* Returns the number of robots in the token ring. |
|
367 |
* |
|
368 |
* @return the number of robots in the token ring |
|
369 |
**/ |
|
370 |
int wl_token_get_robots_in_ring(void) |
|
371 |
{ |
|
372 |
return sensor_matrix_get_joined(sensorMatrix); |
|
373 |
} |
|
374 |
|
|
375 |
/** |
|
376 |
* Returns true if the specified robot is in the token ring, false |
|
377 |
* otherwise. |
|
378 |
* |
|
379 |
* @param robot the robot to check for whether it is in the token ring |
|
380 |
* @return nonzero if the robot is in the token ring, zero otherwise |
|
381 |
**/ |
|
382 |
int wl_token_is_robot_in_ring(int robot) |
|
383 |
{ |
|
384 |
return sensor_matrix_get_in_ring(sensorMatrix, robot); |
|
385 |
} |
|
386 |
|
|
387 |
/** |
|
388 |
* Begins iterating through the robots in the token ring. |
|
389 |
* |
|
390 |
* @see wl_token_iterator_has_next, wl_token_iterator_next |
|
391 |
**/ |
|
392 |
void wl_token_iterator_begin(void) |
|
393 |
{ |
|
394 |
int i = 0; |
|
395 |
|
|
396 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) { |
|
397 |
i++; |
|
398 |
} |
|
399 |
|
|
400 |
if (i == sensor_matrix_get_size(sensorMatrix)) { |
|
401 |
i = -1; |
|
402 |
} |
|
403 |
|
|
404 |
iteratorCount = i; |
|
405 |
} |
|
406 |
|
|
407 |
/** |
|
408 |
* Returns true if there are more robots in the token ring |
|
409 |
* to iterate through, and false otherwise. |
|
410 |
* |
|
411 |
* @return nonzero if there are more robots to iterate through, |
|
412 |
* zero otherwise |
|
413 |
* |
|
414 |
* @see wl_token_iterator_begin, wl_token_iterator_next |
|
415 |
**/ |
|
416 |
int wl_token_iterator_has_next(void) |
|
417 |
{ |
|
418 |
return iteratorCount != -1; |
|
419 |
} |
|
420 |
|
|
421 |
/** |
|
422 |
* Returns the next robot ID in the token ring. |
|
423 |
* |
|
424 |
* @return the next robot ID in the token ring, or -1 if none exists |
|
425 |
* |
|
426 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next |
|
427 |
**/ |
|
428 |
int wl_token_iterator_next(void) |
|
429 |
{ |
|
430 |
int result = iteratorCount; |
|
431 |
if (result < 0) { |
|
432 |
return result; |
|
433 |
} |
|
434 |
|
|
435 |
iteratorCount++; |
|
436 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) |
|
437 |
&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) { |
|
438 |
iteratorCount++; |
|
439 |
} |
|
440 |
|
|
441 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) { |
|
442 |
iteratorCount = -1; |
|
443 |
} |
|
444 |
|
|
445 |
return result; |
|
446 |
} |
|
447 |
|
|
448 |
/** |
|
449 |
* Returns the number of robots currently in the token ring. |
|
450 |
* |
|
451 |
* @return the number of robots in the token ring |
|
452 |
**/ |
|
453 |
int wl_token_get_num_robots(void) |
|
454 |
{ |
|
455 |
return sensor_matrix_get_joined(sensorMatrix); |
|
456 |
} |
|
457 |
|
|
458 |
/** |
|
459 |
* Returns the number of robots in the sensor matrix. |
|
460 |
* |
|
461 |
* @return the number of robots in the sensor matrix |
|
462 |
**/ |
|
463 |
int wl_token_get_matrix_size(void) |
|
464 |
{ |
|
465 |
return sensor_matrix_get_size(sensorMatrix); |
|
466 |
} |
|
467 |
|
|
468 |
/** |
|
353 | 469 |
* This method is called when we receive a token pass packet. |
354 | 470 |
* @param source is the robot it came from |
355 | 471 |
* @param nextRobot is the robot the token was passed to |
... | ... | |
357 | 473 |
* reading for that robot, repeated for sensorDataLength bytes |
358 | 474 |
* @param sensorDataLength the length in bytes of sensorData |
359 | 475 |
*/ |
360 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
|
476 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength)
|
|
361 | 477 |
{ |
362 | 478 |
int i, j; |
363 | 479 |
|
... | ... | |
384 | 500 |
j = 0; |
385 | 501 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
386 | 502 |
{ |
387 |
if (i == source) |
|
503 |
if (i == source) {
|
|
388 | 504 |
continue; |
389 |
|
|
505 |
} |
|
506 |
|
|
390 | 507 |
//set the sensor information we receive |
391 | 508 |
if (j < sensorDataLength / 2 && sensorData[2 * j] == i) |
392 | 509 |
{ |
... | ... | |
419 | 536 |
{ |
420 | 537 |
ringState = NONMEMBER; |
421 | 538 |
wl_token_ring_join(); |
422 |
|
|
539 |
|
|
423 | 540 |
WL_DEBUG_PRINT("We have been removed from the ring "); |
424 | 541 |
WL_DEBUG_PRINT("and are rejoining.\r\n"); |
425 | 542 |
} |
426 |
|
|
543 |
|
|
427 | 544 |
//the person who accepted us is dead... let's ask again |
428 | 545 |
if (i == acceptor) |
429 | 546 |
{ |
... | ... | |
437 | 554 |
} |
438 | 555 |
|
439 | 556 |
wl_token_next_robot = nextRobot; |
440 |
|
|
557 |
|
|
441 | 558 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
442 |
|
|
559 |
|
|
443 | 560 |
//we have the token |
444 |
if (wl_token_next_robot == wl_get_xbee_id()) |
|
561 |
if (wl_token_next_robot == wl_get_xbee_id()) {
|
|
445 | 562 |
wl_token_get_token(); |
563 |
} |
|
446 | 564 |
} |
447 | 565 |
|
448 | 566 |
/** |
... | ... | |
454 | 572 |
* @return the number of passes before the token is expected |
455 | 573 |
* to reach robot2 from robot1 |
456 | 574 |
**/ |
457 |
int get_token_distance(int robot1, int robot2) |
|
575 |
static int get_token_distance(int robot1, int robot2)
|
|
458 | 576 |
{ |
459 | 577 |
int curr = robot1 + 1; |
460 | 578 |
int count = 1; |
... | ... | |
474 | 592 |
/** |
475 | 593 |
* Passes the token to the next robot in the token ring. |
476 | 594 |
**/ |
477 |
void wl_token_pass_token()
|
|
595 |
static int wl_token_pass_token()
|
|
478 | 596 |
{ |
479 | 597 |
char nextRobot; |
480 | 598 |
int i = wl_get_xbee_id() + 1; |
... | ... | |
482 | 600 |
{ |
483 | 601 |
while (1) |
484 | 602 |
{ |
485 |
if (i == sensor_matrix_get_size(sensorMatrix)) |
|
603 |
if (i == sensor_matrix_get_size(sensorMatrix)) {
|
|
486 | 604 |
i = 0; |
605 |
} |
|
606 |
|
|
487 | 607 |
if (sensor_matrix_get_in_ring(sensorMatrix, i)) |
488 | 608 |
{ |
489 | 609 |
nextRobot = (char)i; |
490 | 610 |
break; |
491 | 611 |
} |
612 |
|
|
492 | 613 |
i++; |
493 | 614 |
} |
494 | 615 |
} |
... | ... | |
508 | 629 |
{ |
509 | 630 |
WL_DEBUG_PRINT_INT(packetSize); |
510 | 631 |
WL_DEBUG_PRINT("Out of memory - pass token.\r\n"); |
511 |
return; |
|
632 |
free(buf); |
|
633 |
return -1; |
|
512 | 634 |
} |
513 | 635 |
buf[0] = nextRobot; |
514 | 636 |
|
515 | 637 |
int j = 0; |
516 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
638 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) {
|
|
517 | 639 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
518 | 640 |
{ |
519 | 641 |
buf[2*j + 1] = i; |
520 | 642 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
521 | 643 |
j++; |
522 | 644 |
} |
523 |
|
|
645 |
} |
|
646 |
|
|
524 | 647 |
WL_DEBUG_PRINT("Passing the token to robot "); |
525 | 648 |
WL_DEBUG_PRINT_INT(buf[0]); |
526 | 649 |
WL_DEBUG_PRINT(".\r\n"); |
527 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
|
528 |
buf, packetSize, 3); |
|
650 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, buf, packetSize, 3) != 0) { |
|
651 |
free(buf); |
|
652 |
return -1; |
|
653 |
} |
|
529 | 654 |
|
530 | 655 |
wl_token_next_robot = nextRobot; |
531 | 656 |
deathDelay = DEATH_DELAY; |
532 | 657 |
free(buf); |
658 |
|
|
659 |
return 0; |
|
533 | 660 |
} |
534 | 661 |
|
535 | 662 |
/** |
... | ... | |
539 | 666 |
* |
540 | 667 |
* @param source the robot whose BOM is on |
541 | 668 |
**/ |
542 |
void wl_token_bom_on_receive(int source) |
|
669 |
static void wl_token_bom_on_receive(int source)
|
|
543 | 670 |
{ |
544 | 671 |
WL_DEBUG_PRINT("Robot "); |
545 | 672 |
WL_DEBUG_PRINT_INT(source); |
546 | 673 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n"); |
547 |
|
|
674 |
|
|
548 | 675 |
bom_on_count = 0; |
549 | 676 |
|
550 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(),
|
|
677 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
|
551 | 678 |
source, get_max_bom_function()); |
552 | 679 |
} |
553 | 680 |
|
... | ... | |
555 | 682 |
* This method is called when we receive the token. Upon receiving |
556 | 683 |
* the token, we must send a BOM_ON packet, flash the BOM, and send |
557 | 684 |
* the token to the next robot. |
558 |
*
|
|
685 |
* |
|
559 | 686 |
* If there is a pending request for the token, this is processed first. |
560 | 687 |
**/ |
561 |
void wl_token_get_token() |
|
688 |
static void wl_token_get_token()
|
|
562 | 689 |
{ |
563 | 690 |
WL_DEBUG_PRINT("We have the token.\r\n"); |
564 | 691 |
if (ringState == ACCEPTED) |
565 | 692 |
{ |
566 |
sensor_matrix_set_in_ring(sensorMatrix, |
|
567 |
wl_get_xbee_id(), 1); |
|
693 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1); |
|
568 | 694 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n"); |
569 | 695 |
ringState = MEMBER; |
570 | 696 |
joinDelay = -1; |
... | ... | |
579 | 705 |
} |
580 | 706 |
return; |
581 | 707 |
} |
582 |
|
|
708 |
|
|
583 | 709 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n"); |
584 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
|
585 |
NULL, 0, 0); |
|
710 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0); |
|
586 | 711 |
|
587 | 712 |
bom_on_function(); |
588 | 713 |
#ifdef ROBOT |
589 | 714 |
delay_ms(BOM_DELAY); |
590 | 715 |
#endif |
591 | 716 |
bom_off_function(); |
592 |
|
|
717 |
|
|
593 | 718 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
594 | 719 |
{ |
595 | 720 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n"); |
596 | 721 |
return; |
597 | 722 |
} |
598 |
|
|
723 |
|
|
599 | 724 |
wl_token_pass_token(); |
600 | 725 |
} |
601 | 726 |
|
... | ... | |
607 | 732 |
* |
608 | 733 |
* @param source the robot who requested to join |
609 | 734 |
**/ |
610 |
void wl_token_join_receive(int source) |
|
735 |
static void wl_token_join_receive(int source)
|
|
611 | 736 |
{ |
612 | 737 |
WL_DEBUG_PRINT("Received joining request from robot "); |
613 | 738 |
WL_DEBUG_PRINT_INT(source); |
... | ... | |
622 | 747 |
//we can only accept one request at a time |
623 | 748 |
if (accepted != -1) |
624 | 749 |
return; |
625 |
|
|
750 |
|
|
626 | 751 |
//check if we are the preceding robot in the token ring |
627 | 752 |
int i = source - 1; |
628 | 753 |
while (1) |
... | ... | |
642 | 767 |
accepted = source; |
643 | 768 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT, |
644 | 769 |
NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME); |
645 |
|
|
770 |
|
|
646 | 771 |
WL_DEBUG_PRINT("Accepting robot "); |
647 | 772 |
WL_DEBUG_PRINT_INT(source); |
648 | 773 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
... | ... | |
659 | 784 |
* |
660 | 785 |
* @param source the robot who accepted us |
661 | 786 |
**/ |
662 |
void wl_token_join_accept_receive(int source) |
|
787 |
static void wl_token_join_accept_receive(int source)
|
|
663 | 788 |
{ |
664 | 789 |
WL_DEBUG_PRINT("Accepted into the token ring by robot "); |
665 | 790 |
WL_DEBUG_PRINT_INT(source); |
... | ... | |
671 | 796 |
//add ourselves to the token ring |
672 | 797 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1); |
673 | 798 |
} |
674 |
|
|
675 |
/** |
|
676 |
* Returns the number of robots in the token ring. |
|
677 |
* |
|
678 |
* @return the number of robots in the token ring |
|
679 |
**/ |
|
680 |
int wl_token_get_robots_in_ring(void) |
|
681 |
{ |
|
682 |
return sensor_matrix_get_joined(sensorMatrix); |
|
683 |
} |
|
684 |
|
|
685 |
/** |
|
686 |
* Returns true if the specified robot is in the token ring, false |
|
687 |
* otherwise. |
|
688 |
* |
|
689 |
* @param robot the robot to check for whether it is in the token ring |
|
690 |
* @return nonzero if the robot is in the token ring, zero otherwise |
|
691 |
**/ |
|
692 |
int wl_token_is_robot_in_ring(int robot) |
|
693 |
{ |
|
694 |
return sensor_matrix_get_in_ring(sensorMatrix, robot); |
|
695 |
} |
|
696 |
|
|
697 |
/** |
|
698 |
* Begins iterating through the robots in the token ring. |
|
699 |
* |
|
700 |
* @see wl_token_iterator_has_next, wl_token_iterator_next |
|
701 |
**/ |
|
702 |
void wl_token_iterator_begin(void) |
|
703 |
{ |
|
704 |
int i = 0; |
|
705 |
iteratorCount = 0; |
|
706 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && |
|
707 |
i < sensor_matrix_get_size(sensorMatrix)) |
|
708 |
i++; |
|
709 |
if (i == sensor_matrix_get_size(sensorMatrix)) |
|
710 |
i = -1; |
|
711 |
iteratorCount = i; |
|
712 |
} |
|
713 |
|
|
714 |
/** |
|
715 |
* Returns true if there are more robots in the token ring |
|
716 |
* to iterate through, and false otherwise. |
|
717 |
* |
|
718 |
* @return nonzero if there are more robots to iterate through, |
|
719 |
* zero otherwise |
|
720 |
* |
|
721 |
* @see wl_token_iterator_begin, wl_token_iterator_next |
|
722 |
**/ |
|
723 |
int wl_token_iterator_has_next(void) |
|
724 |
{ |
|
725 |
return iteratorCount != -1; |
|
726 |
} |
|
727 |
|
|
728 |
/** |
|
729 |
* Returns the next robot ID in the token ring. |
|
730 |
* |
|
731 |
* @return the next robot ID in the token ring, or -1 if none exists |
|
732 |
* |
|
733 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next |
|
734 |
**/ |
|
735 |
int wl_token_iterator_next(void) |
|
736 |
{ |
|
737 |
int result = iteratorCount; |
|
738 |
if (result < 0) |
|
739 |
return result; |
|
740 |
|
|
741 |
iteratorCount++; |
|
742 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) && |
|
743 |
iteratorCount < sensor_matrix_get_size(sensorMatrix)) |
|
744 |
iteratorCount++; |
|
745 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) |
|
746 |
iteratorCount = -1; |
|
747 |
return result; |
|
748 |
} |
|
749 |
|
|
750 |
/** |
|
751 |
* Returns the number of robots currently in the token ring. |
|
752 |
* |
|
753 |
* @return the number of robots in the token ring |
|
754 |
**/ |
|
755 |
int wl_token_get_num_robots(void) |
|
756 |
{ |
|
757 |
return sensor_matrix_get_joined(sensorMatrix); |
|
758 |
} |
|
759 |
|
|
760 |
/** |
|
761 |
* Returns the number of robots in the sensor matrix. |
|
762 |
* |
|
763 |
* @return the number of robots in the sensor matrix |
|
764 |
**/ |
|
765 |
int wl_token_get_matrix_size(void) |
|
766 |
{ |
|
767 |
return sensor_matrix_get_size(sensorMatrix); |
|
768 |
} |
|
769 |
|
Also available in: Unified diff