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