232 |
232 |
**/
|
233 |
233 |
static void wl_token_ring_timeout_handler()
|
234 |
234 |
{
|
235 |
|
usb_putc('t');
|
236 |
235 |
//someone is not responding, assume they are dead
|
237 |
236 |
if (deathDelay == 0)
|
238 |
237 |
{
|
... | ... | |
240 |
239 |
//also, declare that person dead, as long as it isn't us
|
241 |
240 |
if (wl_token_next_robot != wl_get_xbee_id())
|
242 |
241 |
{
|
243 |
|
usb_puts("Death.\n\r");
|
244 |
|
usb_puti(wl_token_next_robot);
|
245 |
|
usb_puts("\n\r");
|
246 |
242 |
sensor_matrix_set_in_ring(wl_token_next_robot, 0);
|
247 |
243 |
WL_DEBUG_PRINT("Robot ");
|
248 |
244 |
WL_DEBUG_PRINT_INT(wl_token_next_robot);
|
... | ... | |
262 |
258 |
{
|
263 |
259 |
if (sensor_matrix_get_joined() == 0)
|
264 |
260 |
{
|
265 |
|
usb_puts("Gave up.\n\r");
|
266 |
261 |
WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n");
|
267 |
262 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
|
268 |
263 |
ringState = MEMBER;
|
... | ... | |
319 |
314 |
switch (type)
|
320 |
315 |
{
|
321 |
316 |
case WL_TOKEN_PASS:
|
322 |
|
usb_puts("Got token.\n\r");
|
323 |
317 |
wl_token_pass_receive(source);
|
324 |
318 |
break;
|
325 |
319 |
case WL_TOKEN_SENSOR_MATRIX:
|
326 |
|
usb_puts("Got sensor matrix.\n\r");
|
327 |
320 |
wl_token_sensor_matrix_receive(source, packet, length);
|
328 |
321 |
break;
|
329 |
322 |
case WL_TOKEN_BOM_ON:
|
330 |
|
usb_puts("Got BOM on.\n\r");
|
331 |
323 |
//add the robot to the sensor matrix if it is not already there
|
332 |
324 |
wl_token_bom_on_receive(source);
|
333 |
325 |
break;
|
334 |
326 |
case WL_TOKEN_JOIN:
|
335 |
|
usb_puts("Got join request.\n\r");
|
336 |
327 |
wl_token_join_receive(source);
|
337 |
328 |
break;
|
338 |
329 |
case WL_TOKEN_JOIN_ACCEPT:
|
339 |
|
usb_puts("Got join accept.\n\r");
|
340 |
330 |
wl_token_join_accept_receive(source);
|
341 |
331 |
break;
|
342 |
332 |
default:
|
... | ... | |
515 |
505 |
WL_DEBUG_PRINT_INT(wl_token_next_robot);
|
516 |
506 |
WL_DEBUG_PRINT(".\n");
|
517 |
507 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining)
|
518 |
|
usb_puti(source); usb_putc(' '); usb_puti(wl_token_next_robot); usb_putc(' '); usb_puti(wl_get_xbee_id());
|
519 |
|
usb_putc(' '); usb_puti(bom_on_count); usb_putc(' '); usb_puti(ringState); usb_putc(' '); usb_puts("\n\r");
|
520 |
508 |
if ((source != wl_token_next_robot && wl_get_xbee_id() != wl_token_next_robot) && bom_on_count <= DEATH_DELAY / 2 &&
|
521 |
509 |
ringState != ACCEPTED)
|
522 |
510 |
{
|
... | ... | |
705 |
693 |
}
|
706 |
694 |
else
|
707 |
695 |
{
|
708 |
|
usb_puts("Accepting.\n\r");
|
709 |
696 |
WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n");
|
710 |
697 |
//add a new robot to the token ring
|
711 |
698 |
sensor_matrix_set_in_ring(accepted, 1);
|
... | ... | |
724 |
711 |
buf[2*j] = i;
|
725 |
712 |
buf[2*j + 1] = sensor_matrix_get_reading(wl_get_xbee_id(), i);
|
726 |
713 |
j++;
|
727 |
|
usb_puti(i);
|
728 |
|
usb_puts("\n\r");
|
729 |
714 |
}
|
730 |
715 |
}
|
731 |
|
usb_puts("\n\r");
|
732 |
716 |
int packetSize = 2 * j * sizeof(char);
|
733 |
717 |
WL_DEBUG_PRINT("Passing the token to robot ");
|
734 |
718 |
WL_DEBUG_PRINT_INT(nextRobot);
|
735 |
719 |
WL_DEBUG_PRINT(".\r\n");
|
736 |
|
usb_puts("Passing token ");
|
737 |
|
usb_puti(nextRobot);
|
738 |
|
usb_puts("\n\r");
|
739 |
720 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_SENSOR_MATRIX, buf, packetSize, 0) != 0)
|
740 |
721 |
return -1;
|
741 |
722 |
if (wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME))
|
742 |
723 |
return -1;
|
743 |
724 |
|
744 |
725 |
wl_token_next_robot = nextRobot;
|
745 |
|
usb_puti(nextRobot);
|
746 |
726 |
deathDelay = DEATH_DELAY;
|
747 |
727 |
|
748 |
728 |
return 0;
|
... | ... | |
799 |
779 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0);
|
800 |
780 |
|
801 |
781 |
//bom_on_function();
|
802 |
|
//#ifdef ROBOT
|
803 |
|
usb_puts("BOM ON!\n\r");
|
|
782 |
#ifdef ROBOT
|
804 |
783 |
delay_ms(BOM_DELAY);
|
805 |
|
usb_puts("BOM OFF!\n\r");
|
806 |
|
//#endif
|
807 |
|
//bom_off_function();
|
|
784 |
#endif
|
|
785 |
bom_off_function();
|
808 |
786 |
|
809 |
787 |
if (!sensor_matrix_get_in_ring(wl_get_xbee_id()))
|
810 |
788 |
{
|
811 |
|
usb_puts("FAIL\n\r");
|
812 |
789 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
|
813 |
790 |
return;
|
814 |
791 |
}
|