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