Project

General

Profile

Revision 536

removed any conflict with other fsm variables.
added tentative orbit stop, which needs encoders to be accurate.

View differences:

branches/lemmings/code/behaviors/smart_run_around_fsm/orbit_fsm.c
7 7

  
8 8
  Must be within BOM range of robot before activating
9 9
  
10
  orbit_theta_stop requires encoders, and is incomplete
11
  
10 12
  Assumes:
11 13
    both robots are already in a token ring
12 14
*/
13 15

  
14 16

  
17
/* private function prototype */
18
void evaluate_state(void);
19

  
20

  
21
/* primary init */
15 22
void orbit_init(int robot) {
16 23
  range_init();
17
  analog_init();
24
  analog_init(1);
18 25
  motors_init();
19 26
  orb_init();
20 27
  orb_enable();
21 28
  //usb_init();
22 29
 
23
  /*Start in the start state, SEEK */ 
24
  state = SEEK;
30
  /*Start in the start state, ORBIT_SEEK */ 
31
  orbit_state = ORBIT_SEEK;
25 32
  
26
  otherRobot = robot; // set which robot to seek and orbit
33
  orbit_otherRobot = robot; // set which robot to seek and orbit
27 34
  
28
  pControl=0;
29
  bom = 0;
35
  orbit_pControl=0;
36
  orbit_bom = 0;
37
  orbit_theta = 0;
38
  orbit_theta_stop = -1;
30 39
  
31 40
  /*Initialize distances to zero.*/ 
32
  d1=1000; d2=1000; d3=1000; d4=1000; d5=1000;
41
  orbit_d1=1000; orbit_d2=1000; orbit_d3=1000; orbit_d4=1000; orbit_d5=1000;
33 42
  
34 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
}
35 67

  
36 68
/*The main function, call this to update states as frequently as possible.*/
37 69
void orbit_fsm(void) {
......
39 71
  /*The following lines ensure that undefined (-1) values
40 72
  will not update the distances.*/ 
41 73
  int temp;  
42
  
43
  wl_do(); // update wireless
44
  
74
    
45 75
  temp=range_read_distance(IR1);
46
  d1=(temp == -1) ? d1 : temp;
76
  orbit_d1=(temp == -1) ? orbit_d1 : temp;
47 77
  
48 78
  temp=range_read_distance(IR2);
49
  d2=(temp == -1) ? d2 : temp;
79
  orbit_d2=(temp == -1) ? orbit_d2 : temp;
50 80
  
51 81
  temp=range_read_distance(IR3);
52
  d3=(temp == -1) ? d3 : temp;
82
  orbit_d3=(temp == -1) ? orbit_d3 : temp;
53 83
  
54 84
  temp=range_read_distance(IR4);
55
  d4=(temp == -1) ? d4 : temp;
85
  orbit_d4=(temp == -1) ? orbit_d4 : temp;
56 86
  
57 87
  temp=range_read_distance(IR5);
58
  d5=(temp == -1) ? d5 : temp;
88
  orbit_d5=(temp == -1) ? orbit_d5 : temp;
59 89
  
90
  wl_do(); // update wireless
91
  
60 92
  // get bom reading
61
  temp = wl_token_get_my_sensor_reading(otherRobot);
62
  bom = (temp == -1) ? bom : temp;
93
  temp = wl_token_get_my_sensor_reading(orbit_otherRobot);
94
  orbit_bom = (temp == -1) ? orbit_bom : temp;
63 95
  
64 96
  // modify bom reading so right is negative, left is positive
65
  if (bom <= 12)
66
    bom -= 4;
97
  if (orbit_bom <= 12)
98
    orbit_bom -= 4;
67 99
  else
68
    bom -= 20;
100
    orbit_bom -= 20;
69 101
  
70 102
    
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
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
77 110
  
78 111
  // evaluate state
79 112
  evaluate_state();
......
82 115

  
83 116
//Acts on state change.
84 117
void evaluate_state(){
85
    switch(state){
86
    case(SEEK): orb_set_color(RED);
118
    switch(orbit_state){
119
    case(ORBIT_SEEK): orb_set_color(RED);
87 120
      // move towards robot
88
      pControl = bom*10;      
89
      move(STRAIGHT_SPEED,pControl);
121
      orbit_pControl = orbit_bom*10;      
122
      move(ORBIT_STRAIGHT_SPEED,orbit_pControl);
90 123
      break;
91 124
    
92
    case(TO_ORBIT): orb_set_color(GREEN);
125
    case(ORBIT_INSERTION): orb_set_color(GREEN);
93 126
      // rotate into orbit, perpendicular to other robot
94
      pControl = -ORBIT_DIRECTION*10;
95
      move(STRAIGHT_SPEED/3,pControl);
127
      orbit_pControl = -ORBIT_DIRECTION*10;
128
      move(ORBIT_STRAIGHT_SPEED/3,orbit_pControl);
96 129
      break;
97 130
      
98
    case(ORBIT): orb_set_color(BLUE);
131
    case(ORBIT_ORBITING): orb_set_color(BLUE);
99 132
      // 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;
133
      if (orbit_bom == ORBIT_DIRECTION)
134
        orbit_pControl = ORBIT_DIRECTION;
135
      else if (orbit_bom < ORBIT_DIRECTION)
136
        orbit_pControl = ORBIT_DIRECTION-5;
104 137
      else
105
        pControl = ORBIT_DIRECTION+10;
138
        orbit_pControl = ORBIT_DIRECTION+5;
106 139
      
107
      move(STRAIGHT_SPEED,pControl);
140
      move(ORBIT_STRAIGHT_SPEED,orbit_pControl);
108 141
      break;
109 142
      
110
    case(STOP): orb_set_color(YELLOW);
143
    case(ORBIT_STOP): orb_set_color(YELLOW);
111 144
      move(0,0);
112 145
      break;
113 146
      
branches/lemmings/code/behaviors/smart_run_around_fsm/orbit_fsm.h
5 5
#define _ORBIT_FSM_H_
6 6

  
7 7
//The States: 
8
#define SEEK 12           //Move straight towards robot
9
#define TO_ORBIT 13       //Rotate into orbit
10
#define ORBIT 15          //Orbit robot
11
#define STOP 16           //Stop.  The default state, (Something broke).
8
#define ORBIT_SEEK      12           //Move straight towards robot
9
#define ORBIT_INSERTION 13           //Rotate into orbit
10
#define ORBIT_ORBITING  15           //Orbit robot
11
#define ORBIT_STOP      16           //Stop.  The default and ending state
12 12

  
13 13

  
14
#define STRAIGHT_SPEED 160
14
#define ORBIT_STRAIGHT_SPEED 160
15 15

  
16 16

  
17 17
#define ORBIT_DIRECTION 4     // if 4, then orbit to the right; if -4, then orbit to the left
......
19 19
#define ORBIT_DISTANCE 250 // orbit distance as measured by rangefinders
20 20

  
21 21

  
22
int state;    /*State machine variable.*/
22
int orbit_state;    /*State machine variable.*/
23 23

  
24
int otherRobot; /* must be set prior to running FSM */
24
int orbit_otherRobot; /* must be set prior to running FSM */
25 25

  
26 26

  
27
int pControl;		/*Proportional control variable, determines turn direction.*/
28
int d1,d2,d3,d4,d5;	/*The five distances taken in by IR.*/
29
int bom; /* bom data */
27
int orbit_pControl;		/*Proportional control variable, determines turn direction.*/
28
int orbit_d1,orbit_d2,orbit_d3,orbit_d4,orbit_d5;	/*The five distances taken in by IR.*/
29
int orbit_bom; /* bom data */
30
int orbit_theta; /* number of degrees traveled in orbit */
31
int orbit_theta_stop; /* stop condition in number of degrees traveled */
30 32

  
33
/* orbit_init
34
   argument: robot_id that you want to orbit
35
   notes: must call before orbit_fsm
36
*/
31 37
void orbit_init(int robot);
38

  
39
/* orbit_init_theta
40
   argument: robot_id that you want to orbit
41
             theta that you want to stop orbiting at
42
   notes: must call before orbit_fsm
43
*/
44
void orbit_init_theta(int robot,int theta);
45

  
46
/* orbit_fsm
47
   argument: none
48
   notes: call in a while loop to perform FSM action
49
*/
32 50
void orbit_fsm(void);
33
void evaluate_state(void);
34 51

  
35 52
#endif

Also available in: Unified diff