Revision 1774

trunk/code/behaviors/formation_control/circle/circle.c (revision 1774)
105 105
int EDGE_MACHINE = 3;
106 106
int BEACON_MACHINE = 4;
107 107

  
108
int COUNT = 0;
109
int CIRCLEUP = 1;
110
int ORIENT = 2;
111
int DRIVE = 3;
108 112

  
113
int currentPos = 0;
114
int state = 0;
115

  
116

  
109 117
int timeout = 0;
110 118
int sending = 0;
111 119
int stop2 = 0;
......
272 280
	orb_set_color(ORB_OFF);
273 281
}
274 282

  
283
void order(int action)
284
{
285
	currentPos++;
286
	send2(CIRCLE_EXECUTE, action);
287
	state = 20 + action;
288
}
275 289

  
290
void terminate(void)
291
{
292
	send2(CIRCLE_EXECUTE, 100);
293
	orb_set_color(GREEN);
294
	orb2_set_color(WHITE);
295
	while(1) ;
296
}
276 297

  
277 298

  
278 299

  
300

  
279 301
//*****************************************************************************************************************************************************************************************
280 302
//*****************************************************************************************************************************************************************************************
281 303
//*****************************************************************************************************************************************************************************************
......
302 324
	int data_length;		// keeps track of the length of wireless packets received.
303 325
	unsigned char *packet_data=wl_basic_do_default(&data_length);
304 326
	
305
	int state = WAITINGSTATE;
306 327
	int beacon_State=0;		// these variables keep track of the inner state machines in the procedural MACHINE states.
307 328
	int edge_State=0;
308 329

  
......
342 363
			*/
343 364
			case 0:
344 365

  
366
				orb_set_color(YELLOW);
345 367
				packet_data=wl_basic_do_default(&data_length);
346 368
				if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_CLAIM_CENTER)
347 369
				{
......
371 393
			*/
372 394

  
373 395
			case 1:
396
				orb_set_color(CYAN);
397
				orb1_set_color(YELLOW);
374 398

  
399
				int command = -1;
400
				
401
				packet_data=wl_basic_do_default(&data_length);
402
				if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_EXECUTE)
403
				{
404
					command = packet_data[1];
405
				}
406

  
407
				if(command != -1)
408
				{
409
					switch(command)
410
					{
411
						case 0:
412
							state = 10; break;
413

  
414
						case 1:
415
							state = 11; break;
416

  
417
						case 2:
418
							state = 12; break;
419

  
420
						case 3:
421
							state = 13; break;
422

  
423
						case 100:
424
							terminate(); break;
425
					}
426
				}
427

  
375 428
			break;
376 429

  
377 430

  
......
385 438
				commands, for a robot to follow if it is set to a BEACON.
386 439
			*/
387 440
			case 2:
441
				orb_set_color(PURPLE);
388 442

  
443
				
444
				switch(currentPos)
445
				{
446
					case 0:
447
						order(COUNT);	break;
448

  
449
					case 1:
450
						order(CIRCLEUP); break;
451

  
452
					case 2:
453
						order(ORIENT); break;
454

  
455
					case 3:
456
						order(DRIVE); break;
457

  
458
					case 4:
459
						terminate(); break;
460
				}
461
				
389 462
			break;
390 463

  
391 464

  
......
393 466
//***********************************************************************************************************************************************************************************
394 467

  
395 468

  
469
			/* The following states are MACHINE states for the EDGE robot. */
470

  
396 471
			/*
397
				The MACHINE for the EDGE state.
472
				EDGE on COUNT
398 473
			*/
399
			case 3:	
474
			case 10:	
400 475

  
476

  
477
				edge_State = 0;
478

  
401 479
				switch(edge_State)
402 480
				{
403 481
					/*
......
406 484
						2. After they receive the package, they send ACK package to center.
407 485
						3. Done for now: display green.
408 486
					*/
409
					case 0:   
487
					case 0:
410 488
						bom_off();
411 489
						orb1_set_color(YELLOW);
412 490
						orb2_set_color(CYAN);
......
444 522
						packet_data=wl_basic_do_default(&data_length);
445 523
						if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_ACTION_DONE && packet_data[1] == centerid)
446 524
						{
447
							edge_State=3;
525
							state = 1;
448 526
						}
449 527
					break;
528
				}
529

  
530
			break;
531

  
532
			/* The CIRCLEUP command for EDGE */
533

  
534
			case 11:
535

  
536
				edge_State = 0;
537
				switch(edge_State)
538
				{
450 539
					
451
					case 3:
540
					case 0:
452 541
						// COLOR afer DONE ---> MAGENTA
453 542
						orb_set_color(MAGENTA);
454 543
						correctTurn();			// turn to face the beacon
......
483 572
						send2(CIRCLE_ACTION_ACK, robotid);
484 573

  
485 574
						stop();
486
						edge_State=4;
575
						state = 1;
487 576
					break;
488 577

  
578
				}
579

  
580

  
581
			break;
582
		
583
			/* An ORIENT series of steps for the EDGE robot. */
584

  
585
			case 12:
586
			
587
				edge_State = 0;
588

  
589
				switch(edge_State)
590
				{
591

  
489 592
					// waits for a packet to tell it to turn on the bom.
490
					case 4:
593
					case 0:
491 594
						packet_data=wl_basic_do_default(&data_length);
492 595
						if(packet_data != 0 && data_length==2 && packet_data[0]==CIRCLE_ACTION_GOTYOU && packet_data[1] == robotid)
493 596
						{
494 597
							bom_on();
495 598
							orb_set_color(ORANGE);
496 599
							send2(CIRCLE_ACTION_ACK,centerid);
497
							edge_State = 5;
600
							edge_State = 1;
498 601
						}
499 602
					break;
500 603

  
501 604
					// waits for a packet to tell it that it has been received.
502
					case 5:
605
					case 1:
503 606
						orb2_set_color(YELLOW);
504 607
						packet_data=wl_basic_do_default(&data_length);
505 608
						if(packet_data != 0 && data_length==3 && packet_data[0]==CIRCLE_ACTION_GOTYOU && packet_data[1] == robotid)
......
507 610
							bom_off();
508 611
							direction = packet_data[2];
509 612
							orb_set_color(YELLOW);
510
							edge_State = 6;
613
							edge_State = 2;
511 614
						}
512 615
					break;
513 616

  
514 617
					/* Wait for the center bot to send a DONE packet; then turn to face the right direction. */
515
					case 6:
618
					case 2:
516 619
						orb_set_color(GREEN);
517 620
						packet_data=wl_basic_do_default(&data_length);
518 621
						if(packet_data != 0 && data_length>=2 && packet_data[0]==CIRCLE_ACTION_DONE)
519 622
						{
520 623
							orb_set_color(WHITE);
521 624
							orb2_set_color(CYAN);
522
							edge_State = 7;
625
							edge_State = 3;
523 626
						}
524 627
					break;
525 628

  
526 629
					/* Turn until we reach the right direction (DIRECTION) */
527
					case 7:
630
					case 3:
528 631
						left(170);
529 632
						bom_refresh(BOM_ALL);
530 633
						if(bom_get_max() == direction)
531 634
						{
532 635
							stop();
533 636
							orb_set_color(YELLOW);
534
							edge_State = 8;
637
							state = 1;
535 638
						}
536 639
					break;
537 640

  
641
				}
642

  
643

  
644
			break;
645

  
646

  
647
			/* The MOVE steps for the EDGE robot */
648

  
649
			case 13:
650

  
651
				edge_State = 0;
652
				switch(edge_State)
653
				{
654
				
538 655
					/* Wait for the command to move forward. */
539
					case 8:
656
					case 0:
540 657
						packet_data=wl_basic_do_default(&data_length);
541 658
						if(packet_data != 0 && data_length>=3 && packet_data[0]==CIRCLE_ACTION_FORWARD)
542 659
						{
543 660
							orb_set_color(BLUE);
544 661
							forward(200);
545 662
							delay_ms(2000);
546
							edge_State = END;
663
							edge_State = 1;
547 664
						}
548 665
					break;
549 666

  
550 667
					/* Terminal. */
551
					case 100:
668
					case 1:
552 669
						stop();
553
						orb_set_color(GREEN);
554
						orb2_set_color(WHITE);
555
						while(1);
670
						state = 1;
556 671
					break;
557 672

  
558 673

  
......
574 689

  
575 690
			/*
576 691
			   The MACHINE for the BEACON state
577
			*/	
578
			case 4:
692
			*/
693

  
694
			/* the COUNT code for the BEACON */
695
			case 20:
696
				beacon_State = 0;
579 697
				switch(beacon_State) 
580 698
				{
581 699

  
......
583 701
					case 0:
584 702
						bom_on();
585 703
						orb_set_color(PURPLE);
704
						robotsReceived = 0;
586 705
						if(button1_click()) beacon_State=1;
587 706
					break;	
588 707

  
......
628 747
						blink(robotsReceived);
629 748
						orb_set_color(GREEN);
630 749
						send2(CIRCLE_ACTION_DONE, robotid);
631
						beacon_State=4;
750
						state = 2;
632 751
					break;
752
				}
633 753

  
754
			break;
755

  
756
			/* The CIRCLEUP method for BEACON */
757
			case 21:
758

  
759
				beacon_State = 0;
760
				switch(beacon_State)
761
				{
762

  
634 763
					/* Wait for all the robots to get to right distance/position */
635
					case 4: 
764
					case 0: 
636 765
						left(170);
637 766
						orb1_set_color(YELLOW);
638 767
						orb2_set_color(WHITE);
......
648 777
							}
649 778
						}
650 779
					
651
						beacon_State = 5;
652
					break; 
653
				
780
						state = 2;
781
					break;
782
				}
783

  
784
			break;
785

  
786

  
787
			/* The ORIENT code for the beacon */
788
			case 22:
789

  
790
				beacon_State = 0;
791
				switch(beacon_State)
792
				{
654 793
					/* Turns all the robots in the same direction */
655
					case 5:
794
					case 0:
656 795
						stop();
657 796
						bom_off();
658 797
						orb_set_color(ORANGE);
......
683 822
								delay_ms(20);
684 823
							}
685 824
						}
686
						beacon_State = 6;
825
						beacon_State = 1;
687 826
					break;
688 827
			
689 828
					/* Sends a DONE packet to signify that it has read in all the robots' directions and sent packets.
690 829
						Edge robots should now turn to face the right direction. */
691
					case 6:
830
					case 1:
692 831
						send2(CIRCLE_ACTION_DONE,robotid);
693 832
						bom_on();
694
						beacon_State = 7;
833
						state = 2;
695 834
					break;
835
				}
696 836

  
837
			break;
838

  
839

  
840
			/* The DRIVE code for the beacon */
841
			case 23:
842

  
843
				beacon_State = 0;
844
				switch(beacon_State)
845
				{
846

  
697 847
					/* Tells the robots to move forward and moves itself. */
698
					case 7:
848
					case 2:
699 849
						orb_set_color(YELLOW);
700 850
						delay_ms(5000);
701 851

  
702 852
						// format: type of ack, speed, time.
703
						send3(CIRCLE_ACTION_FORWARD,200,2000);
853
						send3(CIRCLE_ACTION_FORWARD,200,200);
704 854
						orb_set_color(BLUE);
705 855
						forward(200);
706 856
						delay_ms(2000);
707 857
						stop();
708
						beacon_State = END;
858
						beacon_State = 3;
709 859
					break;
710 860

  
711 861
					/* Terminal. */
712
					case 100:
862
					case 3:
713 863
						stop();
714
						orb_set_color(GREEN);
715
						orb2_set_color(WHITE);
716
						while(1);
864
						state = 2;
717 865
					break;
718 866
				}
719 867
			break;
trunk/code/behaviors/formation_control/circle/circle.h (revision 1774)
6 6
/**** This file should not be edited! ****/
7 7

  
8 8
/*
9
 * The packet structure is 2 bytes
9
 * The packet structure is 2 bytes (or 3!)
10 10
 * byte 0 is the action, which is one of the values below
11 11
 * byte 1 is the robot id
12 12
 */
......
18 18
#define CIRCLE_ACTION_GOTYOU 'G'
19 19
#define CIRCLE_ACTION_FORWARD 'F'
20 20
#define CIRCLE_CLAIM_CENTER 'C'
21
#define CIRCLE_EXECUTE 'X'
21 22

  
22 23

  
23 24
#endif

Also available in: Unified diff