root / branches / lemmings / code / behaviors / smart_run_around_fsm / orbit_fsm.c @ 536
History | View | Annotate | Download (3.74 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 |
orbit_theta_stop requires encoders, and is incomplete
|
11 |
|
12 |
Assumes:
|
13 |
both robots are already in a token ring
|
14 |
*/
|
15 |
|
16 |
|
17 |
/* private function prototype */
|
18 |
void evaluate_state(void); |
19 |
|
20 |
|
21 |
/* primary init */
|
22 |
void orbit_init(int robot) { |
23 |
range_init(); |
24 |
analog_init(1);
|
25 |
motors_init(); |
26 |
orb_init(); |
27 |
orb_enable(); |
28 |
//usb_init();
|
29 |
|
30 |
/*Start in the start state, ORBIT_SEEK */
|
31 |
orbit_state = ORBIT_SEEK; |
32 |
|
33 |
orbit_otherRobot = robot; // set which robot to seek and orbit
|
34 |
|
35 |
orbit_pControl=0;
|
36 |
orbit_bom = 0;
|
37 |
orbit_theta = 0;
|
38 |
orbit_theta_stop = -1;
|
39 |
|
40 |
/*Initialize distances to zero.*/
|
41 |
orbit_d1=1000; orbit_d2=1000; orbit_d3=1000; orbit_d4=1000; orbit_d5=1000; |
42 |
|
43 |
} |
44 |
/* secondary init */
|
45 |
void orbit_init_theta(int robot,int theta) { |
46 |
range_init(); |
47 |
analog_init(1);
|
48 |
motors_init(); |
49 |
orb_init(); |
50 |
orb_enable(); |
51 |
//usb_init();
|
52 |
|
53 |
/*Start in the start state, ORBIT_SEEK */
|
54 |
orbit_state = ORBIT_SEEK; |
55 |
|
56 |
orbit_otherRobot = robot; // set which robot to seek and orbit
|
57 |
|
58 |
orbit_pControl=0;
|
59 |
orbit_bom = 0;
|
60 |
orbit_theta = 0;
|
61 |
orbit_theta_stop = (theta>0)?theta:-1; // check theta_stop |
62 |
|
63 |
/*Initialize distances to zero.*/
|
64 |
orbit_d1=1000; orbit_d2=1000; orbit_d3=1000; orbit_d4=1000; orbit_d5=1000; |
65 |
|
66 |
} |
67 |
|
68 |
/*The main function, call this to update states as frequently as possible.*/
|
69 |
void orbit_fsm(void) { |
70 |
|
71 |
/*The following lines ensure that undefined (-1) values
|
72 |
will not update the distances.*/
|
73 |
int temp;
|
74 |
|
75 |
temp=range_read_distance(IR1); |
76 |
orbit_d1=(temp == -1) ? orbit_d1 : temp;
|
77 |
|
78 |
temp=range_read_distance(IR2); |
79 |
orbit_d2=(temp == -1) ? orbit_d2 : temp;
|
80 |
|
81 |
temp=range_read_distance(IR3); |
82 |
orbit_d3=(temp == -1) ? orbit_d3 : temp;
|
83 |
|
84 |
temp=range_read_distance(IR4); |
85 |
orbit_d4=(temp == -1) ? orbit_d4 : temp;
|
86 |
|
87 |
temp=range_read_distance(IR5); |
88 |
orbit_d5=(temp == -1) ? orbit_d5 : temp;
|
89 |
|
90 |
wl_do(); // update wireless
|
91 |
|
92 |
// get bom reading
|
93 |
temp = wl_token_get_my_sensor_reading(orbit_otherRobot); |
94 |
orbit_bom = (temp == -1) ? orbit_bom : temp;
|
95 |
|
96 |
// modify bom reading so right is negative, left is positive
|
97 |
if (orbit_bom <= 12) |
98 |
orbit_bom -= 4;
|
99 |
else
|
100 |
orbit_bom -= 20;
|
101 |
|
102 |
|
103 |
if (orbit_state == ORBIT_SEEK && orbit_d2 < ORBIT_DISTANCE)
|
104 |
orbit_state = ORBIT_INSERTION; // begin to orbit
|
105 |
if (orbit_state == ORBIT_INSERTION && orbit_bom == ORBIT_DIRECTION)
|
106 |
orbit_state = ORBIT_ORBITING; // orbit achieved
|
107 |
if (orbit_state == ORBIT_ORBITING && ((orbit_d2 < ORBIT_DISTANCE)
|
108 |
|| (orbit_theta >= orbit_theta_stop))) |
109 |
orbit_state = ORBIT_STOP; // orbit obstructed
|
110 |
|
111 |
// evaluate state
|
112 |
evaluate_state(); |
113 |
} |
114 |
|
115 |
|
116 |
//Acts on state change.
|
117 |
void evaluate_state(){
|
118 |
switch(orbit_state){
|
119 |
case(ORBIT_SEEK): orb_set_color(RED);
|
120 |
// move towards robot
|
121 |
orbit_pControl = orbit_bom*10;
|
122 |
move(ORBIT_STRAIGHT_SPEED,orbit_pControl); |
123 |
break;
|
124 |
|
125 |
case(ORBIT_INSERTION): orb_set_color(GREEN);
|
126 |
// rotate into orbit, perpendicular to other robot
|
127 |
orbit_pControl = -ORBIT_DIRECTION*10;
|
128 |
move(ORBIT_STRAIGHT_SPEED/3,orbit_pControl);
|
129 |
break;
|
130 |
|
131 |
case(ORBIT_ORBITING): orb_set_color(BLUE);
|
132 |
// go straight with slight rotation
|
133 |
if (orbit_bom == ORBIT_DIRECTION)
|
134 |
orbit_pControl = ORBIT_DIRECTION; |
135 |
else if (orbit_bom < ORBIT_DIRECTION) |
136 |
orbit_pControl = ORBIT_DIRECTION-5;
|
137 |
else
|
138 |
orbit_pControl = ORBIT_DIRECTION+5;
|
139 |
|
140 |
move(ORBIT_STRAIGHT_SPEED,orbit_pControl); |
141 |
break;
|
142 |
|
143 |
case(ORBIT_STOP): orb_set_color(YELLOW);
|
144 |
move(0,0); |
145 |
break;
|
146 |
|
147 |
default: orb_set_color(YELLOW);
|
148 |
/*Should never get here, so stop.*/
|
149 |
move(0,0); |
150 |
break;
|
151 |
} |
152 |
} |
153 |
|
154 |
|