root / trunk / code / behaviors / smart_run_around_fsm / smart_run_around_fsm.c @ 473
History | View | Annotate | Download (3.07 KB)
1 | 94 | jykong | #include "dragonfly_lib.h" |
---|---|---|---|
2 | #include "smart_run_around_fsm.h" |
||
3 | |||
4 | /*A simple behavior for navigating in an environment, i.e. avoiding walls and getting stuck.
|
||
5 | Could be better at not getting stuck.
|
||
6 | |||
7 | Latest revision only has two accessible states: move and reverse.
|
||
8 | */
|
||
9 | |||
10 | |||
11 | void run_around_init(void) |
||
12 | { |
||
13 | range_init(); |
||
14 | analog_init(); |
||
15 | motors_init(); |
||
16 | orb_init(); |
||
17 | orb_enable(); |
||
18 | usb_init(); |
||
19 | |||
20 | /*Start in the default state, MOVING*/
|
||
21 | avoid_state=MOVING; |
||
22 | /*Set timers to their maximum values.*/
|
||
23 | crazy_count=CRAZY_MAX; |
||
24 | backup_count=0;
|
||
25 | pControl=0;
|
||
26 | |||
27 | /*Initialize distances to zero.*/
|
||
28 | d1=0; d2=0; d3=0; d4=0; d5=0; |
||
29 | |||
30 | orb_set_color(GREEN); |
||
31 | |||
32 | } |
||
33 | |||
34 | /*The main function, call this to update states as frequently as possible.*/
|
||
35 | void run_around_FSM(void) { |
||
36 | /*Default to moving.*/
|
||
37 | avoid_state=MOVING; |
||
38 | |||
39 | /*The following lines ensure that undefined (-1) values
|
||
40 | will not update the distances.*/
|
||
41 | int temp;
|
||
42 | |||
43 | temp=range_read_distance(IR1); |
||
44 | d1=(temp == -1) ? d1 : temp;
|
||
45 | |||
46 | temp=range_read_distance(IR2); |
||
47 | d2=(temp == -1) ? d2 : temp;
|
||
48 | |||
49 | temp=range_read_distance(IR3); |
||
50 | d3=(temp == -1) ? d3 : temp;
|
||
51 | |||
52 | temp=range_read_distance(IR4); |
||
53 | d4=(temp == -1) ? d4 : temp;
|
||
54 | |||
55 | temp=range_read_distance(IR5); |
||
56 | d5=(temp == -1) ? d5 : temp;
|
||
57 | |||
58 | /*If the crazy count is in it's >>3 range, it acts crazy.*/
|
||
59 | if(crazy_count<=(CRAZY_MAX>>3)) |
||
60 | { |
||
61 | avoid_state=CRAZY; |
||
62 | crazy_count--; |
||
63 | |||
64 | if(crazy_count<0) crazy_count=CRAZY_MAX; |
||
65 | |||
66 | evaluate_state(); |
||
67 | return;
|
||
68 | } |
||
69 | |||
70 | //Checks the forward distance to see if it should back up, if so...state backwards.
|
||
71 | 211 | jykong | if((d2!=-1)&&(d2 < 150)){ |
72 | 94 | jykong | backup_count=BACKUP_MAX; |
73 | avoid_state=BACKWARDS; |
||
74 | evaluate_state(); |
||
75 | return;
|
||
76 | } |
||
77 | 211 | jykong | /*
|
78 | if(d1 < 120 || d3 < 120) {
|
||
79 | avoid_state = BACKWARDS;
|
||
80 | backup_count = BACKUP_MAX;
|
||
81 | evaluate_state();
|
||
82 | return;
|
||
83 | }
|
||
84 | */
|
||
85 | 94 | jykong | if(backup_count<BACKUP_MAX){
|
86 | avoid_state=BACKWARDS; |
||
87 | if(backup_count<0) |
||
88 | backup_count=BACKUP_MAX; |
||
89 | evaluate_state(); |
||
90 | return;
|
||
91 | } |
||
92 | |||
93 | /*Should evaluate an expression from -255 to 255 to pass to move.*/
|
||
94 | pControl= ((d3-d1) + (d4-d5)) >> TURN_CONSTANT; |
||
95 | |||
96 | if(pControl>PCONTROL_CRAZY_LIMIT || pControl<-PCONTROL_CRAZY_LIMIT) crazy_count--;
|
||
97 | /*i.e. if you really want to turn for an extended period of time...you're probably stuck.*/
|
||
98 | |||
99 | /*Debug stuff:*/
|
||
100 | /*usb_puts("pControl evaluating: ");
|
||
101 | usb_puti(pControl);
|
||
102 | usb_puts("\n\r");
|
||
103 | usb_puts("IR1: ");
|
||
104 | usb_puti(d1);
|
||
105 | usb_puts(" IR2: ");
|
||
106 | usb_puti(d2);
|
||
107 | usb_puts(" IR3: ");
|
||
108 | usb_puti(d3);
|
||
109 | usb_puts(" IR4: ");
|
||
110 | usb_puti(d4);
|
||
111 | usb_puts(" IR5: ");
|
||
112 | usb_puti(d5);
|
||
113 | usb_puts("\n\r");*/
|
||
114 | |||
115 | evaluate_state(); |
||
116 | } |
||
117 | |||
118 | |||
119 | //Acts on state change.
|
||
120 | void evaluate_state(){
|
||
121 | switch(avoid_state){
|
||
122 | case(MOVING): orb_set_color(GREEN);
|
||
123 | 211 | jykong | move(STRAIT_SPEED,-pControl); |
124 | 94 | jykong | break;
|
125 | |||
126 | case(BACKWARDS): orb_set_color(ORANGE);
|
||
127 | move(-STRAIT_SPEED,0);
|
||
128 | break;
|
||
129 | |||
130 | case(CRAZY): orb_set_color(RED);
|
||
131 | /*TODO: Implement a crazy state.*/
|
||
132 | 211 | jykong | move(STRAIT_SPEED,-pControl); |
133 | 94 | jykong | break;
|
134 | |||
135 | default:
|
||
136 | /*Should never get here, go strait.*/
|
||
137 | move(100,0); orb_set_color(BLUE); |
||
138 | break;
|
||
139 | } |
||
140 | } |
||
141 |