root / branches / lemmings / code / behaviors / smart_run_around_fsm / orbit_fsm.c @ 496
History | View | Annotate | Download (2.61 KB)
1 |
#include <dragonfly_lib.h> |
---|---|
2 |
#include <wireless.h> |
3 |
#include <wl_token_ring.h> |
4 |
#include "orbit_fsm.h" |
5 |
|
6 |
/* Used to orbit a robot
|
7 |
|
8 |
Must be within BOM range of robot before activating
|
9 |
|
10 |
Assumes:
|
11 |
both robots are already in a token ring
|
12 |
*/
|
13 |
|
14 |
|
15 |
void orbit_init(int robot) { |
16 |
range_init(); |
17 |
analog_init(); |
18 |
motors_init(); |
19 |
orb_init(); |
20 |
orb_enable(); |
21 |
//usb_init();
|
22 |
|
23 |
/*Start in the start state, SEEK */
|
24 |
state = SEEK; |
25 |
|
26 |
otherRobot = robot; // set which robot to seek and orbit
|
27 |
|
28 |
pControl=0;
|
29 |
bom = 0;
|
30 |
|
31 |
/*Initialize distances to zero.*/
|
32 |
d1=1000; d2=1000; d3=1000; d4=1000; d5=1000; |
33 |
|
34 |
} |
35 |
|
36 |
/*The main function, call this to update states as frequently as possible.*/
|
37 |
void orbit_fsm(void) { |
38 |
|
39 |
/*The following lines ensure that undefined (-1) values
|
40 |
will not update the distances.*/
|
41 |
int temp;
|
42 |
|
43 |
wl_do(); // update wireless
|
44 |
|
45 |
temp=range_read_distance(IR1); |
46 |
d1=(temp == -1) ? d1 : temp;
|
47 |
|
48 |
temp=range_read_distance(IR2); |
49 |
d2=(temp == -1) ? d2 : temp;
|
50 |
|
51 |
temp=range_read_distance(IR3); |
52 |
d3=(temp == -1) ? d3 : temp;
|
53 |
|
54 |
temp=range_read_distance(IR4); |
55 |
d4=(temp == -1) ? d4 : temp;
|
56 |
|
57 |
temp=range_read_distance(IR5); |
58 |
d5=(temp == -1) ? d5 : temp;
|
59 |
|
60 |
// get bom reading
|
61 |
temp = wl_token_get_my_sensor_reading(otherRobot); |
62 |
bom = (temp == -1) ? bom : temp;
|
63 |
|
64 |
// modify bom reading so right is negative, left is positive
|
65 |
if (bom <= 12) |
66 |
bom -= 4;
|
67 |
else
|
68 |
bom -= 20;
|
69 |
|
70 |
|
71 |
if (state == SEEK && d2 < ORBIT_DISTANCE)
|
72 |
state = TO_ORBIT; // begin to orbit
|
73 |
if (state == TO_ORBIT && bom == ORBIT_DIRECTION)
|
74 |
state = ORBIT; // orbit achieved
|
75 |
if (state == ORBIT && d2 < ORBIT_DISTANCE)
|
76 |
state = STOP; // orbit obstructed
|
77 |
|
78 |
// evaluate state
|
79 |
evaluate_state(); |
80 |
} |
81 |
|
82 |
|
83 |
//Acts on state change.
|
84 |
void evaluate_state(){
|
85 |
switch(state){
|
86 |
case(SEEK): orb_set_color(RED);
|
87 |
// move towards robot
|
88 |
pControl = bom*10;
|
89 |
move(STRAIGHT_SPEED,pControl); |
90 |
break;
|
91 |
|
92 |
case(TO_ORBIT): orb_set_color(GREEN);
|
93 |
// rotate into orbit, perpendicular to other robot
|
94 |
pControl = -ORBIT_DIRECTION*10;
|
95 |
move(STRAIGHT_SPEED/3,pControl);
|
96 |
break;
|
97 |
|
98 |
case(ORBIT): orb_set_color(BLUE);
|
99 |
// go straight with slight rotation
|
100 |
if (bom == ORBIT_DIRECTION)
|
101 |
pControl = ORBIT_DIRECTION*2;
|
102 |
else if (bom < ORBIT_DIRECTION) |
103 |
pControl = ORBIT_DIRECTION-10;
|
104 |
else
|
105 |
pControl = ORBIT_DIRECTION+10;
|
106 |
|
107 |
move(STRAIGHT_SPEED,pControl); |
108 |
break;
|
109 |
|
110 |
case(STOP): orb_set_color(YELLOW);
|
111 |
move(0,0); |
112 |
break;
|
113 |
|
114 |
default: orb_set_color(YELLOW);
|
115 |
/*Should never get here, so stop.*/
|
116 |
move(0,0); |
117 |
break;
|
118 |
} |
119 |
} |
120 |
|
121 |
|