6 #include <movehelper.h>
16 #include <shape_detect.h>
19 #include "actuators.h"
20 #include "match-timing.h"
21 #include "common-states.h"
22 #include "sub-states.h"
24 UL_LOG_CUST(ulogd_common_states); /* Log domain name = ulogd + name of the file */
26 /************************************************************************
27 * Functions used in and called from all the (almost identical)
28 * "wait for start" states in particular strategies.
29 ************************************************************************/
32 #define DBG_FSM_STATE(name) do { if (fsm->debug_states) ul_loginf("fsm %s %.1f: %s(%s)\n", \
33 fsm->debug_name, robot_current_time(), \
34 name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
36 /************************************************************************
37 * Trajectory constraints used; They are initialized in the main() function in competition.cc
38 ************************************************************************/
40 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
43 * Safe distance for target recognition
45 const double approach_radius = TARGET_RADIUS_M + 2.0*MAP_CELL_SIZE_M + ROBOT_DIAGONAL_RADIUS_M;
47 void set_initial_position()
49 robot_set_est_pos_trans(HOME_POS_X_M, HOME_POS_Y_M, DEG2RAD(HOME_POS_ANG_DEG));
57 /* Check if the new point is within the playground scene */
58 bool goal_is_in_playground(double goalx, double goaly)
60 if ((goalx < 0) || (goalx > PLAYGROUND_WIDTH_M) || (goaly < 0) || (goaly > PLAYGROUND_HEIGHT_M))
66 /* Check if the new point is close to the robot */
67 bool close_goal(double goalx, double goaly)
69 const double close = 0.5;
71 robot_get_est_pos(&x, &y, &phi);
73 if ((abs(goalx - x) < close) && (abs(goaly - y) < close) )
80 * FSM state for neighborhood observation.
82 * Detect targets using shape_detect.
83 * If no target detected, turn 120deg and try again.
84 * Scan all 360deg and then go back to move_around state.
86 * If target detected, go to approach_target state.
90 static char turn_cntr = 0;
95 DBG_PRINT_EVENT("survey");
96 // no target detected in this heading, turn 120°
97 robot_get_est_pos(&x, &y, &phi);
98 robot_goto_notrans(x, y, TURN(DEG2RAD(120)+phi), &tcSlow);
100 DBG_PRINT_EVENT("no target");
104 FSM_TRANSITION(move_around);
106 FSM_TRANSITION(survey);
112 case EV_MOTION_ERROR:
113 FSM_TRANSITION(move_around);
121 FSM_STATE(move_around)
128 goalx = ((rand()%PLAYGROUND_HEIGHT_MM)/1000.0);
129 goaly = ((rand()%PLAYGROUND_WIDTH_MM)/1000.0);
130 } while (!ShmapIsFreePoint(goalx, goaly) && close_goal(goalx, goaly));
132 robot_goto_notrans(goalx, goaly, NO_TURN(), &tcFast);
133 DBG_PRINT_EVENT("new survey point");
135 case EV_MOTION_ERROR:
136 DBG_PRINT_EVENT("can not access survey point");
138 FSM_TRANSITION(survey);
151 DBG_PRINT_EVENT("homing");
152 robot_goto_notrans(HOME_POS_X_M, HOME_POS_Y_M, ARRIVE_FROM(DEG2RAD(HOME_POS_ANG_DEG), 0.2), &tcFast);
154 case EV_MOTION_ERROR:
155 DBG_PRINT_EVENT("ERROR: home position is not reachable!");
159 FSM_TRANSITION(go_home);
163 DBG_PRINT_EVENT("Mission completed!");
180 case EV_MOTION_ERROR:
181 case EV_SWITCH_STRATEGY:
182 DBG_PRINT_EVENT("unhandled event");