6 #include <movehelper.h>
16 #include <shape_detect.h>
19 #include "actuators.h"
20 #include "match-timing.h"
21 #include "common-states.h"
23 UL_LOG_CUST(ulogd_common_states); /* Log domain name = ulogd + name of the file */
25 /************************************************************************
26 * Functions used in and called from all the (almost identical)
27 * "wait for start" states in particular strategies.
28 ************************************************************************/
31 #define DBG_FSM_STATE(name) do { if (fsm->debug_states) ul_loginf("fsm %s %.1f: %s(%s)\n", \
32 fsm->debug_name, robot_current_time(), \
33 name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
35 /************************************************************************
36 * Trajectory constraints used; They are initialized in the main() function in competition.cc
37 ************************************************************************/
39 struct TrajectoryConstraints tcSlow, tcVerySlow;
42 void set_initial_position()
44 robot_set_est_pos_trans(HOME_POS_X_M, HOME_POS_Y_M, DEG2RAD(HOME_POS_ANG_DEG));
53 /* Check if the new point is within the playground scene */
54 bool goal_is_in_playground(double goalx, double goaly)
56 if ((goalx < 0) || (goalx > PLAYGROUND_WIDTH_M) || (goaly < 0) || (goaly > PLAYGROUND_HEIGHT_M))
62 /* Check if the new point is close to the robot */
63 bool close_goal(double goalx, double goaly)
66 robot_get_est_pos(&x, &y, &phi);
68 if ((abs(goalx - x) < 0.5) && (abs(goaly - y) < 0.5) )
74 static bool detect_target()
76 // run shape detection
77 // return TRUE when any circle detected
79 struct hokuyo_scan_type hokuyo = robot.hokuyo;
82 std::vector<Shape_detect::Arc> arcs;
83 sd.prepare(hokuyo.data);
86 if (arcs.size() > 0) {
88 sharp_pos hok, target;
90 hok.x = HOKUYO_CENTER_OFFSET_M;
93 robot_get_est_pos(&e.x, &e.y, &e.phi);
95 //TODO save all detected targets and
96 Shape_detect::Arc * a = &arcs[0];
97 hok.x += (double)a->center.x / 1000.0;
98 hok.y = (double)a->center.y / 1000.0;
100 /* transform target position which is relative to Hokuyo center to absolute position in space */
101 robot.target_pos.x = (hok.x * cos(e.phi)) - (hok.y * sin(e.phi)) + e.x;
102 robot.target_pos.y = (hok.x * sin(e.phi)) + (hok.y * cos(e.phi)) + e.y;
110 /* automaton to survey neighborhood in two stages */
113 static char turn_cntr = 0;
118 /* run shape detection
119 - if detection is positive go to the target
120 - if no target detected turn 120° to the left */
121 if (detect_target() & 0) {
122 // target detected, go to the target
123 robot.target_detected = true;
124 FSM_TRANSITION(approach_target);
125 DBG_PRINT_EVENT("Target detected!");
127 // no target detected in this heading, turn 120°
128 robot.target_detected = false;
129 robot_get_est_pos(&x, &y, &phi);
130 robot_goto_notrans(x, y, TURN(DEG2RAD(120)+phi), &tcSlow);
140 FSM_TRANSITION(move_around);
142 FSM_TRANSITION(survey);
151 case EV_MOTION_ERROR:
152 case EV_SWITCH_STRATEGY:
153 DBG_PRINT_EVENT("unhandled event");
154 FSM_TRANSITION(move_around);
161 FSM_STATE(approach_target)
167 x = robot.target_pos.x;
168 y = robot.target_pos.y;
170 if (goal_is_in_playground(x, y)) {
172 1. calculate position for approach
173 2. approach the target to 30cm distance
174 3. then use camera? to validate the target
175 4. approach the target closely - using hokuyo and sharp?
177 6. move backwards 0.5m
178 7. transport cargo to the home position
180 //ShmapSetCircleFlag(x, y, 0.1, MAP_FLAG_TARGET, 0);
181 //ShmapSetCircleFlag(x, y, 0.1, MAP_FLAG_IGNORE_OBST, NULL);
182 //robot_goto_notrans(x, y, NO_TURN(), &tcSlow);
183 printf("regular target x:%f y:%f\n",x,y);
185 DBG_PRINT_EVENT("Target out of range!");
186 printf("false target x:%f y:%f\n",x,y);
193 DBG_PRINT_EVENT("Mission completed :)");
201 case EV_MOTION_ERROR:
202 case EV_SWITCH_STRATEGY:
203 DBG_PRINT_EVENT("unhandled event");
209 FSM_STATE(move_around)
211 double goalx, goaly, phi;
212 static int survey_cnt = 0;
216 /* TODO upravit generovani nahodne pozice tak aby se lepe filtrovaly
217 uz prozkomane body a prekazky i body mimo hriste.
218 Oznacit v mape vsechny oblasti prozkoumane pomoci hokuya na kterych nedoslo k detekci??
221 goalx = ((rand()%PLAYGROUND_HEIGHT_MM)/1000.0);
222 goaly += ((rand()%PLAYGROUND_WIDTH_MM)/1000.0);
223 } while (!ShmapIsFreePoint(goalx, goaly) && close_goal(goalx, goaly));
225 robot_goto_notrans(goalx, goaly, NO_TURN(), &tcSlow);
228 case EV_MOTION_ERROR:
229 ul_logdeb("Goal is not reachable\n");
233 FSM_TRANSITION(survey);
236 FSM_TRANSITION(move_around);
255 robot_goto_notrans(HOME_POS_X_M, HOME_POS_Y_M, ARRIVE_FROM(0, 0.5), &tcSlow);
257 case EV_MOTION_ERROR:
258 ul_logdeb("Goal is not reachable\n");
264 FSM_TRANSITION(go_home);
288 case EV_MOTION_ERROR:
289 case EV_SWITCH_STRATEGY:
290 DBG_PRINT_EVENT("unhandled event");