00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #define FSM_MAIN
00012 #include "robot.h"
00013 #include <fsm.h>
00014 #include "fsmmain.h"
00015 #include <unistd.h>
00016 #include "fsmmove.h"
00017 #include "serva.h"
00018 #include <math.h>
00019 #include "trgen.h"
00020 #include "movehelper.h"
00021
00022 #define WAIT_TRANSPORTER_FORWARD_MS 1000
00023 #define WAIT_TRANSPORTER_BACKWARD_MS 1000
00024 #define WAIT_TRANSPORTER_STOP_MS 1000
00025
00026 #define WAIT_WASTE_PULLED_IN_MS 100
00027
00028 #define WAIT_DOOR_OPEN_MS 1000
00029 #define WAIT_PREPARE_FLUSHING_MS 1000
00030 #define WAIT_FLUSHING_BOTTLE_MS 1000
00031 #define WAIT_FLUSHING_CAN_MS 1000
00032
00033 enum {
00034 BOTTLE = 0,
00035 CAN,
00036
00037 FT_STOP,
00038 FT_FORWARD,
00039 FT_BACKWARD,
00040 FD_UP,
00041
00042 WASTE_NOT_PULLED_IN,
00043 WASTE_WAIT_PULLING_IN,
00044 WASTE_PULLED_IN
00045 };
00046
00047 void *wait_for_end(void *arg)
00048 {
00049
00050
00051 return NULL;
00052 }
00053
00054 int fsm_main_init(struct robo_fsm *fsm, int ret_code);
00055
00056
00057
00058 #define DEMO
00059
00060 FSM_STATE_DECL(plan_go);
00064 FSM_STATE(main_init) {
00065 pthread_t thid;
00066
00067 if ( FSM_EVENT == EV_START) {
00068 pthread_create(&thid, NULL, wait_for_end, NULL);
00069 releaseFront();
00070 frontTransporterForward();
00071 FSM_SIGNAL(PICKUP, EV_PICKUP_START);
00072
00073
00074 robot.act_pos.x = (double)ROBOT_AXIS_TO_BACK_MM/1000.0;
00075 robot.act_pos.y = ((double)PLAYGROUND_HEIGHT_MM - ROBOT_WIDTH_MM/2.0)/1000.0;
00076 robot.act_pos.phi = -90.0/180*M_PI;
00077 printf("fsmmain going to plan_go\n");
00078 FSM_TRANSITION(plan_go);
00079 }
00080 }
00081
00082
00083 FSM_STATE_DECL(plan_go);
00084
00085
00086
00087
00088 void avoid_enemy()
00089 {
00090 robot_trajectory_stop();
00091 ROBOT_LOCK(act_pos);
00092 double x = robot.act_pos.x, y = robot.act_pos.y;
00093 double heading = robot.act_pos.phi;
00094 ROBOT_UNLOCK(act_pos);
00095
00096 y+=0.3*sin(heading);
00097
00098 robot_trajectory_new(NULL);
00099 robot_trajectory_add_point(x + 0.5*cos(heading-M_PI/2.0),
00100 y + 0.5*sin(heading-M_PI/2.0));
00101 robot_trajectory_add_point(x + 0.7*cos(heading-M_PI/4.0),
00102 y + 0.7*sin(heading-M_PI/4.0));
00103 robot_trajectory_add_final_point(target_x, target_y, NAN);
00104 }
00105
00106 void follow_bottle()
00107 {
00108 robot_trajectory_stop();
00109
00110 }
00111
00112
00113 FSM_STATE(enemy_avoidance)
00114 {
00115 switch (FSM_EVENT) {
00116 case EV_DETECTED_BOTTLE:
00117 follow_bottle();
00118 break;
00119 case EV_ENEMY_AHEAD:
00120 avoid_enemy();
00121 break;
00122 case EV_GOAL_NOT_REACHABLE:
00123 case EV_MOTION_DONE:
00124 SUBFSM_RET();
00125 default:
00126 break;
00127 }
00128 }
00129
00130
00131 FSM_STATE_DECL(main_wait);
00132
00133 FSM_STATE(plan_go)
00134 {
00135 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
00136 switch (FSM_EVENT) {
00137 case EV_STATE_ENTERED:
00138 tc.maxv *= 1.5;
00139
00140
00141
00142
00143
00144
00145
00146 break;
00147 case EV_RETURN:
00148 break;
00149 default: break;
00150 }
00151 }
00152
00153 FSM_STATE_DECL(main_follow_bott);
00154
00155 FSM_STATE(main_wait)
00156 {
00157 switch (FSM_EVENT) {
00158 case EV_STATE_ENTERED:
00159 break;
00160 case EV_DETECTED_BOTTLE:
00161 robot_trajectory_stop();
00162 FSM_TRANSITION(main_follow_bott);
00163 break;
00164 case EV_GOAL_NOT_REACHABLE:
00165 case EV_MOTION_DONE:
00166 FSM_TRANSITION(plan_go);
00167 break;
00168 default: break;
00169 }
00170 }
00171
00172 FSM_STATE_DECL(main_grab);
00173 FSM_STATE(main_follow_bott)
00174 {
00175 FSM_TIMEOUT(200);
00176 int i = 0;
00177 switch (FSM_EVENT) {
00178 case EV_STATE_ENTERED:
00179 case EV_TIMEOUT:
00180
00181 if(robot.bott_sens.sens1==CLOSE) i++;
00182 if(robot.bott_sens.sens2==CLOSE) i++;
00183 if(robot.bott_sens.sens3==CLOSE) i++;
00184 if(robot.bott_sens.sens4==CLOSE) i++;
00185
00186
00187 if(robot.bott_sens.sens1==FAR) robot_send_speed(0.05,0.1);
00188 else if(robot.bott_sens.sens4==FAR) robot_send_speed(0.1,0.05);
00189 else if(robot.bott_sens.sens2==FAR) robot_send_speed(0.1,0.1);
00190 else if(robot.bott_sens.sens3==FAR) robot_send_speed(0.1,0.1);
00191 else if(i >1) FSM_TRANSITION(main_grab);
00192 else FSM_TRANSITION(plan_go);
00193 break;
00194 default: break;
00195 }
00196 }
00197
00198 FSM_STATE(main_grab)
00199 {
00200 FSM_TIMEOUT(200);
00201
00202 switch (FSM_EVENT) {
00203 case EV_STATE_ENTERED:
00204 robot_send_speed(0.0,0.0);
00205 FSM_SIGNAL(PICKUP, EV_PICKUP_TRY);
00206 break;
00207 case EV_PICKUP_DONE:
00208 FSM_TRANSITION(plan_go);
00209 break;
00210 case EV_PICKUP_FAILED:
00211 FSM_TRANSITION(main_follow_bott);
00212 break;
00213 default: break;
00214 }
00215 }