Revision 198
Recharging updated.
wl_token_ring.c | ||
---|---|---|
6 | 6 |
#include <wl_defs.h> |
7 | 7 |
#include <wireless.h> |
8 | 8 |
#include <sensor_matrix.h> |
9 |
#include <queue.h> |
|
10 | 9 |
|
11 | 10 |
#ifdef ROBOT |
12 | 11 |
#ifndef FIREFLY |
... | ... | |
44 | 43 |
|
45 | 44 |
/*Packet Handling Routines*/ |
46 | 45 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
47 |
void wl_token_interrupt_request_receive(int source, int robot); |
|
48 |
void wl_token_interrupt_pass_receive(int source, int robot); |
|
49 | 46 |
void wl_token_bom_on_receive(int source); |
50 | 47 |
void wl_token_join_receive(int source); |
51 | 48 |
void wl_token_join_accept_receive(int source); |
... | ... | |
69 | 66 |
int deathDelay = -1; |
70 | 67 |
//the counter for joining, before we form our own token ring |
71 | 68 |
int joinDelay = -1; |
72 |
//queue containing ids of interruption requests |
|
73 |
Queue* interrupting = NULL; |
|
74 | 69 |
|
75 | 70 |
//current robot to check in the iterator |
76 | 71 |
int iteratorCount = 0; |
77 | 72 |
|
73 |
// the amount of time a robot has had its BOM on for |
|
74 |
int bom_on_count = 0; |
|
75 |
|
|
78 | 76 |
void do_nothing(void) {} |
79 | 77 |
int get_nothing(void) {return -1;} |
80 | 78 |
|
... | ... | |
116 | 114 |
} |
117 | 115 |
|
118 | 116 |
sensorMatrix = sensor_matrix_create(); |
119 |
interrupting = queue_create(); |
|
120 | 117 |
//add ourselves to the sensor matrix |
121 | 118 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
122 | 119 |
|
... | ... | |
157 | 154 |
void wl_token_ring_cleanup() |
158 | 155 |
{ |
159 | 156 |
sensor_matrix_destroy(sensorMatrix); |
160 |
queue_destroy(interrupting); |
|
161 | 157 |
} |
162 | 158 |
|
163 | 159 |
/** |
... | ... | |
176 | 172 |
WL_DEBUG_PRINT("Robot "); |
177 | 173 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
178 | 174 |
WL_DEBUG_PRINT(" has died.\r\n"); |
175 |
wl_token_next_robot = -1; |
|
176 |
deathDelay = DEATH_DELAY; |
|
179 | 177 |
} |
180 | 178 |
|
181 | 179 |
// we may have been dropped from the ring when this is received |
... | ... | |
209 | 207 |
deathDelay--; |
210 | 208 |
if (joinDelay >= 0) |
211 | 209 |
joinDelay--; |
210 |
if (bom_on_count >= 0) |
|
211 |
bom_on_count++; |
|
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
/** |
... | ... | |
249 | 249 |
//add the robot to the sensor matrix if it is not already there |
250 | 250 |
wl_token_bom_on_receive(source); |
251 | 251 |
break; |
252 |
case WL_TOKEN_INTERRUPT_REQUEST: |
|
253 |
wl_token_interrupt_request_receive(source, packet[0]); |
|
254 |
break; |
|
255 |
case WL_TOKEN_INTERRUPT_PASS: |
|
256 |
wl_token_interrupt_pass_receive(source, packet[0]); |
|
257 |
break; |
|
258 | 252 |
case WL_TOKEN_JOIN: |
259 | 253 |
wl_token_join_receive(source); |
260 | 254 |
break; |
... | ... | |
276 | 270 |
{ |
277 | 271 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
278 | 272 |
ringState = JOINING; |
279 |
joinDelay = JOIN_DELAY;
|
|
273 |
joinDelay = DEATH_DELAY * 2;
|
|
280 | 274 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, |
281 | 275 |
NULL, 0, 0); |
282 | 276 |
} |
... | ... | |
292 | 286 |
} |
293 | 287 |
|
294 | 288 |
/** |
295 |
* Requests that the specified robot be given the token and |
|
296 |
* allowed to flash its BOM. After its BOM is flashed, the |
|
297 |
* token will return to the robot who sent it. |
|
298 |
* |
|
299 |
* @param robot the ID of the robot which should flash its BOM |
|
300 |
**/ |
|
301 |
void wl_token_request(int robot) |
|
302 |
{ |
|
303 |
char buf[1]; |
|
304 |
buf[0] = robot; |
|
305 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_REQUEST, |
|
306 |
buf, 1, 0); |
|
307 |
} |
|
308 |
|
|
309 |
/** |
|
310 | 289 |
* Returns the BOM reading robot source has for robot dest. |
311 | 290 |
* |
312 | 291 |
* @param source the robot that made the BOM reading |
... | ... | |
344 | 323 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
345 | 324 |
{ |
346 | 325 |
int i, j; |
326 |
|
|
327 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining) |
|
328 |
if (source != wl_token_next_robot && bom_on_count <= DEATH_DELAY / 2 && |
|
329 |
ringState != ACCEPTED) |
|
330 |
{ |
|
331 |
WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n"); |
|
332 |
WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n"); |
|
333 |
return; |
|
334 |
} |
|
335 |
|
|
336 |
bom_on_count = -1; |
|
347 | 337 |
deathDelay = -1; |
348 |
|
|
349 | 338 |
WL_DEBUG_PRINT("Received the token from robot"); |
350 | 339 |
WL_DEBUG_PRINT_INT(source); |
351 | 340 |
WL_DEBUG_PRINT(", next robot is "); |
... | ... | |
518 | 507 |
WL_DEBUG_PRINT("Robot "); |
519 | 508 |
WL_DEBUG_PRINT_INT(source); |
520 | 509 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n"); |
510 |
|
|
511 |
bom_on_count = 0; |
|
521 | 512 |
|
522 |
//make sure we don't declare the robot dead if it's |
|
523 |
//flashing its BOM |
|
524 |
if (source == wl_token_next_robot) |
|
525 |
deathDelay = DEATH_DELAY; |
|
526 | 513 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
527 | 514 |
source, get_max_bom_function()); |
528 | 515 |
} |
... | ... | |
543 | 530 |
wl_get_xbee_id(), 1); |
544 | 531 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n"); |
545 | 532 |
ringState = MEMBER; |
533 |
joinDelay = -1; |
|
546 | 534 |
} |
547 | 535 |
|
548 | 536 |
if (ringState == LEAVING || ringState == NONMEMBER) |
... | ... | |
555 | 543 |
return; |
556 | 544 |
} |
557 | 545 |
|
558 |
//check for interruption requests |
|
559 |
if (queue_size(interrupting) > 0) |
|
560 |
{ |
|
561 |
char buf[1]; |
|
562 |
buf[0] = (char)(int)queue_remove(interrupting); |
|
563 |
|
|
564 |
//in case this robot has requested multiple times |
|
565 |
queue_remove_all(interrupting, (void*)(int)buf[0]); |
|
566 |
|
|
567 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_PASS, |
|
568 |
buf, 1, 0); |
|
569 |
|
|
570 |
deathDelay = DEATH_DELAY; |
|
571 |
wl_token_next_robot = buf[0]; |
|
572 |
return; |
|
573 |
} |
|
574 |
|
|
575 | 546 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n"); |
576 | 547 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
577 | 548 |
NULL, 0, 0); |
... | ... | |
639 | 610 |
WL_DEBUG_PRINT_INT(source); |
640 | 611 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
641 | 612 |
|
642 |
joinDelay = -1; |
|
643 |
|
|
644 | 613 |
// the token ring has not started yet |
645 | 614 |
if (sensor_matrix_get_joined(sensorMatrix) == 1) |
646 | 615 |
wl_token_pass_token(); |
... | ... | |
658 | 627 |
WL_DEBUG_PRINT("Accepted into the token ring by robot "); |
659 | 628 |
WL_DEBUG_PRINT_INT(source); |
660 | 629 |
WL_DEBUG_PRINT(".\r\n"); |
661 |
joinDelay = -1;
|
|
630 |
joinDelay = JOIN_DELAY;
|
|
662 | 631 |
ringState = ACCEPTED; |
663 | 632 |
acceptor = source; |
664 | 633 |
|
... | ... | |
667 | 636 |
} |
668 | 637 |
|
669 | 638 |
/** |
670 |
* Called when we receive a packet passing the token and interrupting |
|
671 |
* the token ring. |
|
672 |
* If the token has been passed to us, we flash our BOM |
|
673 |
* and pass it back. |
|
674 |
* |
|
675 |
* @param source the robot who sent the interrupt packet |
|
676 |
* @param robot the robot the token has been passed to |
|
677 |
**/ |
|
678 |
void wl_token_interrupt_pass_receive(int source, int robot) |
|
679 |
{ |
|
680 |
if (wl_get_xbee_id() != robot) |
|
681 |
{ |
|
682 |
queue_remove_all(interrupting, (void*)robot); |
|
683 |
wl_token_next_robot = robot; |
|
684 |
deathDelay = DEATH_DELAY + rand() / (RAND_MAX / (2 * DEATH_DELAY)); |
|
685 |
return; |
|
686 |
} |
|
687 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
|
688 |
NULL, 0, 0); |
|
689 |
|
|
690 |
bom_on_function(); |
|
691 |
#ifdef ROBOT |
|
692 |
delay_ms(BOM_DELAY); |
|
693 |
#endif |
|
694 |
bom_off_function(); |
|
695 |
|
|
696 |
//we don't include ourself, only if we are in the ring |
|
697 |
int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1); |
|
698 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
|
699 |
packetSize += 2; |
|
700 |
char* buf = (char*)malloc(packetSize * sizeof(char)); |
|
701 |
if (!buf) |
|
702 |
{ |
|
703 |
WL_DEBUG_PRINT("Out of memory - pass_receive.\r\n"); |
|
704 |
return; |
|
705 |
} |
|
706 |
|
|
707 |
//return the token to where we got it from |
|
708 |
buf[0] = source; |
|
709 |
|
|
710 |
int i = 0, j = 0; |
|
711 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
712 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
|
713 |
{ |
|
714 |
buf[2*j + 1] = i; |
|
715 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
|
716 |
j++; |
|
717 |
} |
|
718 |
|
|
719 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
|
720 |
buf, packetSize, 0); |
|
721 |
|
|
722 |
wl_token_next_robot = source; |
|
723 |
deathDelay = DEATH_DELAY; |
|
724 |
free(buf); |
|
725 |
} |
|
726 |
|
|
727 |
/** |
|
728 | 639 |
* Returns the number of robots in the token ring. |
729 | 640 |
* |
730 | 641 |
* @return the number of robots in the token ring |
... | ... | |
800 | 711 |
} |
801 | 712 |
|
802 | 713 |
/** |
803 |
* Called when we receive a request to interrupt the token ring. |
|
804 |
* We add the robot to our list of interrupt requests, |
|
805 |
* and will send the token to this robot when we next receive the |
|
806 |
* token, unless someone else does so first. |
|
714 |
* Returns the number of robots currently in the token ring. |
|
807 | 715 |
* |
808 |
* @param source the robot requesting interruption |
|
809 |
* @param robt the robot requested to interrupt the token ring |
|
716 |
* @return the number of robots in the token ring |
|
810 | 717 |
**/ |
811 |
void wl_token_interrupt_request_receive(int source, int robot)
|
|
718 |
int wl_token_get_num_robots(void)
|
|
812 | 719 |
{ |
813 |
queue_add(interrupting, (void*)robot);
|
|
720 |
return sensor_matrix_get_joined(sensorMatrix);
|
|
814 | 721 |
} |
815 | 722 |
|
816 |
int wl_token_get_num_robots(void){ |
|
817 |
return sensor_matrix_get_joined(sensorMatrix); |
|
723 |
/** |
|
724 |
* Returns the number of robots in the sensor matrix. |
|
725 |
* |
|
726 |
* @return the number of robots in the sensor matrix |
|
727 |
**/ |
|
728 |
int wl_token_get_matrix_size(void) |
|
729 |
{ |
|
730 |
return sensor_matrix_get_size(sensorMatrix); |
|
818 | 731 |
} |
819 | 732 |
|
820 |
int wl_token_get_matrix_size(void){ |
|
821 |
return sensor_matrix_get_size(sensorMatrix); |
|
822 |
} |
Also available in: Unified diff