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 * Vector where all absolute positions of all detected targets are stored.
45 std::vector<robot_pos_type> detected_target;
48 * Safe distance for target recognition
50 const double approach_radius = TARGET_RADIUS_M + 2.0*MAP_CELL_SIZE_M + ROBOT_DIAGONAL_RADIUS_M;
52 void set_initial_position()
54 robot_set_est_pos_trans(HOME_POS_X_M, HOME_POS_Y_M, DEG2RAD(HOME_POS_ANG_DEG));
62 /* Check if the new point is within the playground scene */
63 bool goal_is_in_playground(double goalx, double goaly)
65 if ((goalx < 0) || (goalx > PLAYGROUND_WIDTH_M) || (goaly < 0) || (goaly > PLAYGROUND_HEIGHT_M))
71 /* Check if the new point is close to the robot */
72 bool close_goal(double goalx, double goaly)
74 const double close = 0.5;
76 robot_get_est_pos(&x, &y, &phi);
78 if ((abs(goalx - x) < close) && (abs(goaly - y) < close) )
85 * Take data from hokuyo and run shape detection on it.
87 * Absolute positions of all detected targets centers are stored in alobal variable (vector).
89 * @return True if at least one target detected, else false.
91 static bool detect_target()
93 struct hokuyo_scan_type hokuyo = robot.hokuyo;
96 std::vector<Shape_detect::Arc> arcs;
97 sd.prepare(hokuyo.data);
101 detected_target.clear();
103 if (arcs.size() > 0) {
104 robot_pos_type e, target, hok;
106 robot_get_est_pos(&e.x, &e.y, &e.phi);
108 double sinus = sin(e.phi);
109 double cosinus = cos(e.phi);
111 // save absolute positions of all detected targets
112 for (int i = 0; i < arcs.size(); i++) {
113 Shape_detect::Arc *a = &arcs[i];
115 hok.x = HOKUYO_CENTER_OFFSET_M + (double)a->center.x / 1000.0;
116 hok.y = (double)a->center.y / 1000.0;
118 /* transform target position which is relative to Hokuyo
119 center to absolute position in space */
120 target.x = (hok.x * cosinus) - (hok.y * sinus) + e.x;
121 target.y = (hok.x * sinus) + (hok.y * cosinus) + e.y;
123 // filter those targets not in playground range
124 //if (goal_is_in_playground(target.x, target.y))
125 // detected_target.push_back(target);
128 return detected_target.size();
132 * Calculates point to approach the target.
134 * @param target Position of the center of the target.
135 * @param approach Pointer to the the intersection point of circle around
136 * the target and line between robot center and target.
138 void get_approach_point(double xtarget, double ytarget, double *xapproach, double *yapproach, double *phi_approach)
140 double xrobot, yrobot, phi;
143 robot_get_est_pos(&xrobot, &yrobot, &phi);
145 delta = distance(xrobot, yrobot, xtarget, ytarget);
147 *xapproach = xtarget - (approach_radius * (xtarget - xrobot) / delta);
148 *yapproach = ytarget - (approach_radius * (ytarget - yrobot) / delta);
150 *phi_approach = get_approach_angle(xtarget, ytarget);
154 * Calculates point to approach the target.
156 * @param target Position of the center of the target.
157 * @return Angle to approach the target form.
159 double get_approach_angle(double xtarget, double ytarget)
161 double xrobot, yrobot,phi;
163 robot_get_est_pos(&xrobot, &yrobot, &phi);
165 return atan2((ytarget - yrobot), (xtarget - xrobot));
170 * FSM state for neighborhood observation.
172 * Detect targets using shape_detect.
173 * If no target detected, turn 120deg and try again.
174 * Scan all 360deg and then go back to move_around state.
176 * If target detected, go to approach_target state.
180 static char turn_cntr = 0;
185 DBG_PRINT_EVENT("survey");
186 #if 1 // FIXME just for test
187 if (detect_target()) {
190 robot_pos_type target;
191 detected_target.clear();
192 for (double i = 1; i < 5; i++) {
195 detected_target.push_back(target);
198 // target detected, go to the target
199 FSM_TRANSITION(approach_target);
200 DBG_PRINT_EVENT("Target detected!");
202 // no target detected in this heading, turn 120°
203 robot_get_est_pos(&x, &y, &phi);
204 robot_goto_notrans(x, y, TURN(DEG2RAD(120)+phi), &tcSlow);
206 DBG_PRINT_EVENT("no target");
211 FSM_TRANSITION(move_around);
213 FSM_TRANSITION(survey);
219 case EV_MOTION_ERROR:
220 FSM_TRANSITION(move_around);
229 * FSM state for approaching all detected targets.
231 * Try to approach target.
232 * If approach OK - go to subautomaton and do target recognition, touch and load.
233 * On subautomaton return check if target loaded/valid.
235 * If target loaded, go home.
236 * If target not valid, try next target if any.
237 * If approach not succesfull - go to move_around state.
239 FSM_STATE(approach_target)
241 static int target_cntr = 0;
242 int max_target = detected_target.size();
243 double x_target, y_tatget;
244 double x_approach, y_approach, phi_approach;
248 DBG_PRINT_EVENT("approaching target");
249 x_target = detected_target[target_cntr].x;
250 y_tatget = detected_target[target_cntr].y;
253 printf("target %d / %d\n", target_cntr, max_target);
255 get_approach_point(x_target, y_tatget, &x_approach, &y_approach, &phi_approach);
256 robot_goto_notrans(x_approach, y_approach, ARRIVE_FROM(phi_approach, 0.2), &tcFast);
259 DBG_PRINT_EVENT("target approached");
260 SUBFSM_TRANSITION(recognize, NULL);
263 if (robot.target_loaded) {
264 FSM_TRANSITION(go_home);
265 } else if (robot.target_valid) {
266 //FIXME target is valid but not loaded - try another approach direction
268 } else if (!robot.target_valid && (target_cntr < max_target)) {
269 // go for next target if any
270 FSM_TRANSITION(approach_target);
272 // go to new point and survey
273 FSM_TRANSITION(move_around);
276 case EV_MOTION_ERROR:
277 DBG_PRINT_EVENT("can not approach target");
278 if (target_cntr < max_target) {
279 FSM_TRANSITION(approach_target);
281 FSM_TRANSITION(move_around);
290 FSM_STATE(move_around)
297 goalx = ((rand()%PLAYGROUND_HEIGHT_MM)/1000.0);
298 goaly = ((rand()%PLAYGROUND_WIDTH_MM)/1000.0);
299 } while (!ShmapIsFreePoint(goalx, goaly) && close_goal(goalx, goaly));
301 robot_goto_notrans(goalx, goaly, NO_TURN(), &tcFast);
302 DBG_PRINT_EVENT("new survey point");
304 case EV_MOTION_ERROR:
305 DBG_PRINT_EVENT("can not access survey point");
307 FSM_TRANSITION(survey);
320 DBG_PRINT_EVENT("homing");
321 robot_goto_notrans(HOME_POS_X_M, HOME_POS_Y_M, ARRIVE_FROM(DEG2RAD(HOME_POS_ANG_DEG), 0.2), &tcFast);
323 case EV_MOTION_ERROR:
324 DBG_PRINT_EVENT("ERROR: home position is not reachable!");
328 FSM_TRANSITION(go_home);
332 DBG_PRINT_EVENT("Mission completed!");
349 case EV_MOTION_ERROR:
350 case EV_SWITCH_STRATEGY:
351 DBG_PRINT_EVENT("unhandled event");