Revision 188
Removed interrupt packets.
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; |
... | ... | |
116 | 111 |
} |
117 | 112 |
|
118 | 113 |
sensorMatrix = sensor_matrix_create(); |
119 |
interrupting = queue_create(); |
|
120 | 114 |
//add ourselves to the sensor matrix |
121 | 115 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
122 | 116 |
|
... | ... | |
157 | 151 |
void wl_token_ring_cleanup() |
158 | 152 |
{ |
159 | 153 |
sensor_matrix_destroy(sensorMatrix); |
160 |
queue_destroy(interrupting); |
|
161 | 154 |
} |
162 | 155 |
|
163 | 156 |
/** |
... | ... | |
251 | 244 |
//add the robot to the sensor matrix if it is not already there |
252 | 245 |
wl_token_bom_on_receive(source); |
253 | 246 |
break; |
254 |
case WL_TOKEN_INTERRUPT_REQUEST: |
|
255 |
wl_token_interrupt_request_receive(source, packet[0]); |
|
256 |
break; |
|
257 |
case WL_TOKEN_INTERRUPT_PASS: |
|
258 |
wl_token_interrupt_pass_receive(source, packet[0]); |
|
259 |
break; |
|
260 | 247 |
case WL_TOKEN_JOIN: |
261 | 248 |
wl_token_join_receive(source); |
262 | 249 |
break; |
... | ... | |
294 | 281 |
} |
295 | 282 |
|
296 | 283 |
/** |
297 |
* Requests that the specified robot be given the token and |
|
298 |
* allowed to flash its BOM. After its BOM is flashed, the |
|
299 |
* token will return to the robot who sent it. |
|
300 |
* |
|
301 |
* @param robot the ID of the robot which should flash its BOM |
|
302 |
**/ |
|
303 |
void wl_token_request(int robot) |
|
304 |
{ |
|
305 |
char buf[1]; |
|
306 |
buf[0] = robot; |
|
307 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_REQUEST, |
|
308 |
buf, 1, 0); |
|
309 |
} |
|
310 |
|
|
311 |
/** |
|
312 | 284 |
* Returns the BOM reading robot source has for robot dest. |
313 | 285 |
* |
314 | 286 |
* @param source the robot that made the BOM reading |
... | ... | |
557 | 529 |
return; |
558 | 530 |
} |
559 | 531 |
|
560 |
//check for interruption requests |
|
561 |
if (queue_size(interrupting) > 0) |
|
562 |
{ |
|
563 |
char buf[1]; |
|
564 |
buf[0] = (char)(int)queue_remove(interrupting); |
|
565 |
|
|
566 |
//in case this robot has requested multiple times |
|
567 |
queue_remove_all(interrupting, (void*)(int)buf[0]); |
|
568 |
|
|
569 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_PASS, |
|
570 |
buf, 1, 0); |
|
571 |
|
|
572 |
deathDelay = DEATH_DELAY; |
|
573 |
wl_token_next_robot = buf[0]; |
|
574 |
return; |
|
575 |
} |
|
576 |
|
|
577 | 532 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n"); |
578 | 533 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
579 | 534 |
NULL, 0, 0); |
... | ... | |
669 | 624 |
} |
670 | 625 |
|
671 | 626 |
/** |
672 |
* Called when we receive a packet passing the token and interrupting |
|
673 |
* the token ring. |
|
674 |
* If the token has been passed to us, we flash our BOM |
|
675 |
* and pass it back. |
|
676 |
* |
|
677 |
* @param source the robot who sent the interrupt packet |
|
678 |
* @param robot the robot the token has been passed to |
|
679 |
**/ |
|
680 |
void wl_token_interrupt_pass_receive(int source, int robot) |
|
681 |
{ |
|
682 |
if (wl_get_xbee_id() != robot) |
|
683 |
{ |
|
684 |
queue_remove_all(interrupting, (void*)robot); |
|
685 |
wl_token_next_robot = robot; |
|
686 |
deathDelay = DEATH_DELAY + rand() / (RAND_MAX / (2 * DEATH_DELAY)); |
|
687 |
return; |
|
688 |
} |
|
689 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
|
690 |
NULL, 0, 0); |
|
691 |
|
|
692 |
bom_on_function(); |
|
693 |
#ifdef ROBOT |
|
694 |
delay_ms(BOM_DELAY); |
|
695 |
#endif |
|
696 |
bom_off_function(); |
|
697 |
|
|
698 |
//we don't include ourself, only if we are in the ring |
|
699 |
int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1); |
|
700 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
|
701 |
packetSize += 2; |
|
702 |
char* buf = (char*)malloc(packetSize * sizeof(char)); |
|
703 |
if (!buf) |
|
704 |
{ |
|
705 |
WL_DEBUG_PRINT("Out of memory - pass_receive.\r\n"); |
|
706 |
return; |
|
707 |
} |
|
708 |
|
|
709 |
//return the token to where we got it from |
|
710 |
buf[0] = source; |
|
711 |
|
|
712 |
int i = 0, j = 0; |
|
713 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
714 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
|
715 |
{ |
|
716 |
buf[2*j + 1] = i; |
|
717 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
|
718 |
j++; |
|
719 |
} |
|
720 |
|
|
721 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
|
722 |
buf, packetSize, 0); |
|
723 |
|
|
724 |
wl_token_next_robot = source; |
|
725 |
deathDelay = DEATH_DELAY; |
|
726 |
free(buf); |
|
727 |
} |
|
728 |
|
|
729 |
/** |
|
730 | 627 |
* Returns the number of robots in the token ring. |
731 | 628 |
* |
732 | 629 |
* @return the number of robots in the token ring |
... | ... | |
802 | 699 |
} |
803 | 700 |
|
804 | 701 |
/** |
805 |
* Called when we receive a request to interrupt the token ring. |
|
806 |
* We add the robot to our list of interrupt requests, |
|
807 |
* and will send the token to this robot when we next receive the |
|
808 |
* token, unless someone else does so first. |
|
702 |
* Returns the number of robots currently in the token ring. |
|
809 | 703 |
* |
810 |
* @param source the robot requesting interruption |
|
811 |
* @param robt the robot requested to interrupt the token ring |
|
704 |
* @return the number of robots in the token ring |
|
812 | 705 |
**/ |
813 |
void wl_token_interrupt_request_receive(int source, int robot)
|
|
706 |
int wl_token_get_num_robots(void)
|
|
814 | 707 |
{ |
815 |
queue_add(interrupting, (void*)robot);
|
|
708 |
return sensor_matrix_get_joined(sensorMatrix);
|
|
816 | 709 |
} |
817 | 710 |
|
818 |
int wl_token_get_num_robots(void){ |
|
819 |
return sensor_matrix_get_joined(sensorMatrix); |
|
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); |
|
820 | 719 |
} |
821 | 720 |
|
822 |
int wl_token_get_matrix_size(void){ |
|
823 |
return sensor_matrix_get_size(sensorMatrix); |
|
824 |
} |
Also available in: Unified diff