Revision 79
tried to merge lib_additions again...
wl_token_ring.c | ||
---|---|---|
15 | 15 |
|
16 | 16 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
17 | 17 |
|
18 |
//TODO: idea: if the xbee can handle many packets at once, we could have everyone |
|
19 |
//send their BOM reading for a particular robot right after getting it. |
|
20 |
|
|
21 | 18 |
/*Ring States*/ |
22 | 19 |
|
23 | 20 |
#define NONMEMBER 0 |
... | ... | |
73 | 70 |
//queue containing ids of interruption requests |
74 | 71 |
Queue* interrupting = NULL; |
75 | 72 |
|
73 |
//current robot to check in the iterator |
|
74 |
int iteratorCount = 0; |
|
75 |
|
|
76 | 76 |
#ifdef ROBOT |
77 | 77 |
void (*bom_on_function) (void) = bom_on; |
78 | 78 |
void (*bom_off_function) (void) = bom_off; |
... | ... | |
168 | 168 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
169 | 169 |
WL_DEBUG_PRINT(" has died.\r\n"); |
170 | 170 |
} |
171 |
wl_token_pass_token(); |
|
171 |
|
|
172 |
// we may have been dropped from the ring when this is received |
|
173 |
if (ringState == MEMBER) |
|
174 |
wl_token_pass_token(); |
|
172 | 175 |
} |
173 | 176 |
|
174 | 177 |
//we must start our own token ring, no one is responding to us |
... | ... | |
377 | 380 |
|
378 | 381 |
if (i == wl_get_xbee_id() && ringState == MEMBER) |
379 | 382 |
{ |
380 |
sensor_matrix_set_in_ring(sensorMatrix, |
|
381 |
wl_get_xbee_id(), 1); |
|
382 | 383 |
ringState = NONMEMBER; |
383 | 384 |
wl_token_ring_join(); |
384 | 385 |
|
... | ... | |
535 | 536 |
{ |
536 | 537 |
WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n"); |
537 | 538 |
} |
539 |
return; |
|
538 | 540 |
} |
539 | 541 |
|
540 | 542 |
//check for interruption requests |
... | ... | |
706 | 708 |
} |
707 | 709 |
|
708 | 710 |
/** |
711 |
* Returns the number of robots in the token ring. |
|
712 |
* |
|
713 |
* @return the number of robots in the token ring |
|
714 |
**/ |
|
715 |
int wl_token_get_robots_in_ring(void) |
|
716 |
{ |
|
717 |
return sensor_matrix_get_joined(sensorMatrix); |
|
718 |
} |
|
719 |
|
|
720 |
/** |
|
721 |
* Returns true if the specified robot is in the token ring, false |
|
722 |
* otherwise. |
|
723 |
* |
|
724 |
* @param robot the robot to check for whether it is in the token ring |
|
725 |
* @return nonzero if the robot is in the token ring, zero otherwise |
|
726 |
**/ |
|
727 |
int wl_token_is_robot_in_ring(int robot) |
|
728 |
{ |
|
729 |
return sensor_matrix_get_in_ring(sensorMatrix, robot); |
|
730 |
} |
|
731 |
|
|
732 |
/** |
|
733 |
* Begins iterating through the robots in the token ring. |
|
734 |
* |
|
735 |
* @see wl_token_iterator_has_next, wl_token_iterator_next |
|
736 |
**/ |
|
737 |
void wl_token_iterator_begin(void) |
|
738 |
{ |
|
739 |
int i; |
|
740 |
iteratorCount = 0; |
|
741 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && |
|
742 |
i < sensor_matrix_get_size(sensorMatrix)) |
|
743 |
i++; |
|
744 |
if (i == sensor_matrix_get_size(sensorMatrix)) |
|
745 |
i = -1; |
|
746 |
} |
|
747 |
|
|
748 |
/** |
|
749 |
* Returns true if there are more robots in the token ring |
|
750 |
* to iterate through, and false otherwise. |
|
751 |
* |
|
752 |
* @return nonzero if there are more robots to iterate through, |
|
753 |
* zero otherwise |
|
754 |
* |
|
755 |
* @see wl_token_iterator_begin, wl_token_iterator_next |
|
756 |
**/ |
|
757 |
int wl_token_iterator_has_next(void) |
|
758 |
{ |
|
759 |
return iteratorCount == -1; |
|
760 |
} |
|
761 |
|
|
762 |
/** |
|
763 |
* Returns the next robot ID in the token ring. |
|
764 |
* |
|
765 |
* @return the next robot ID in the token ring, or -1 if none exists |
|
766 |
* |
|
767 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next |
|
768 |
**/ |
|
769 |
int wl_token_iterator_next(void) |
|
770 |
{ |
|
771 |
int result = iteratorCount; |
|
772 |
if (result < 0) |
|
773 |
return result; |
|
774 |
|
|
775 |
iteratorCount++; |
|
776 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) && |
|
777 |
iteratorCount < sensor_matrix_get_size(sensorMatrix)) |
|
778 |
iteratorCount++; |
|
779 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) |
|
780 |
iteratorCount = -1; |
|
781 |
return result; |
|
782 |
} |
|
783 |
|
|
784 |
/** |
|
709 | 785 |
* Called when we receive a request to interrupt the token ring. |
710 | 786 |
* We add the robot to our list of interrupt requests, |
711 | 787 |
* and will send the token to this robot when we next receive the |
... | ... | |
719 | 795 |
queue_add(interrupting, (void*)robot); |
720 | 796 |
} |
721 | 797 |
|
798 |
int wl_token_get_num_robots(void){ |
|
799 |
return sensor_matrix_get_joined(sensorMatrix); |
|
800 |
} |
|
801 |
|
|
802 |
int wl_token_get_matrix_size(void){ |
|
803 |
return sensor_matrix_get_size(sensorMatrix); |
|
804 |
} |
Also available in: Unified diff