7 #include <movehelper.h>
14 #include "corns_configs.h"
15 #include "actuators.h"
17 #include "match-timing.h"
18 #include "eb2010misc.h"
21 #include "common-states.h"
23 /************************************************************************
24 * Functions used in and called from all the (almost identical)
25 * "wait for start" states in particular strategies.
26 ************************************************************************/
28 static void set_initial_position()
30 robot_set_est_pos_trans(ROBOT_AXIS_TO_FRONT_M,
31 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2),
35 static void actuators_home()
37 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
43 robot.check_turn_safety = false;
44 pthread_create(&thid, NULL, timing_thread, NULL);
48 // We set initial position periodically in order for it to be updated
49 // on the display if the team color is changed during waiting for
53 set_initial_position();
54 if (robot.start_state == START_PLUGGED_IN)
60 sem_post(&robot.start);
62 set_initial_position();
67 robot.corns = get_all_corns(robot.corns_conf_side, robot.corns_conf_center);
70 /************************************************************************
71 * Trajectory constraints used; They are initialized in the main() function in competition.cc
72 ************************************************************************/
74 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
76 #define VIDLE_TIMEOUT 2000
78 /************************************************************************
79 * States that form the "collect some oranges" subautomaton. Calling automaton
80 * SHOULD ALWAYS call the "approach_the_slope" state.
81 ************************************************************************/
83 bool inline is_ready_to_climb_the_slope(enum which_side which_slope, double x, double y) {
85 if (which_slope == MINE) {
86 ret = x < 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
87 } else if (which_slope == OPPONENTS) {
88 ret = x > 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
90 printf("ERROR: unknown side;");
91 #warning Remove the next line
97 static struct slope_approach_style *slope_approach_style_p;
99 /* assures that the robot is near the slope rim; if outside our starting area, approach the slope first */
100 FSM_STATE(approach_the_slope)
104 struct slope_approach_style *p = (struct slope_approach_style *) FSM_EVENT_PTR;
105 slope_approach_style_p = new slope_approach_style(*p); // use copy constructor
106 delete(p); // delete given data
107 if (slope_approach_style_p == NULL) {
108 printf("it is not allowed to call the approach_the_slope state with NULL data\n!");
109 #warning remove the next line
113 robot_get_est_pos_trans(&x, &y, &phi);
115 bool ready_to_climb_the_slope = is_ready_to_climb_the_slope(slope_approach_style_p->which_side, x, y);
116 /* if necessary, approach the slope */
117 if (ready_to_climb_the_slope) {
118 FSM_TRANSITION(climb_the_slope);
122 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.3, slope_approach_style_p->which_side),
123 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05,
124 ARRIVE_FROM(DEG2RAD(0),0.05),
126 x_coord(0.3, slope_approach_style_p->which_side),
127 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
128 ARRIVE_FROM(DEG2RAD(0), 0.02),
134 FSM_TRANSITION(climb_the_slope);
140 case EV_MOTION_ERROR:
141 DBG_PRINT_EVENT("unhandled event");
147 void inline enable_switches(bool enabled)
149 robot.use_left_switch = enabled;
150 robot.use_right_switch = enabled;
151 robot.use_back_switch = enabled;
154 FSM_STATE(climb_the_slope)
158 // disables using side switches on bumpers when going up
159 enable_switches(false);
160 act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
161 robot.ignore_hokuyo = true;
162 /* create the trajectory and go */
163 robot_trajectory_new_backward(&tcSlow);
164 robot_trajectory_add_point_trans(
165 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
166 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
167 robot_trajectory_add_final_point_trans(
168 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
169 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
174 SUBFSM_TRANSITION(load_oranges, NULL);
177 FSM_TRANSITION(sledge_down);
182 case EV_MOTION_ERROR:
183 DBG_PRINT_EVENT("unhandled event");
189 /* one-state-subautomaton to load oranges in two stages */
190 FSM_STATE(load_oranges)
195 act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
198 // FIXME: respond to VIDLE EVENT
199 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
206 case EV_MOTION_ERROR:
207 DBG_PRINT_EVENT("unhandled event");
213 FSM_STATE(sledge_down)
217 robot_trajectory_new(&tcFast);
218 robot_trajectory_add_point_trans(
219 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
220 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
222 robot_trajectory_add_point_trans(
223 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, slope_approach_style_p->which_side),
224 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
225 robot_trajectory_add_final_point_trans(
226 x_coord(0.50, slope_approach_style_p->which_side),
227 PLAYGROUND_HEIGHT_M - 0.6,
230 robot_trajectory_add_point_trans(
231 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.20, slope_approach_style_p->which_side),
232 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.08);
233 robot_trajectory_add_final_point_trans(
234 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, slope_approach_style_p->which_side),
235 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.14,
239 /* just for sure, try to close it one more time */
240 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
247 case EV_MOTION_ERROR:
248 DBG_PRINT_EVENT("unhandled event");
250 // enables using side switches on bumpers
251 enable_switches(true);
252 robot.ignore_hokuyo = false;
253 robot.check_turn_safety = true;
259 /************************************************************************
260 * The "unload our oranges" subautomaton
261 ************************************************************************/
263 FSM_STATE(to_cntainer_and_unld)
268 if (slope_approach_style_p->which_side == MINE) {
269 robot_trajectory_new(&tcFast);
270 // face the rim with front of the robot
271 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
272 // face the rim with back of the robot
273 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M-0.6, 0.35);
274 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05));
276 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
280 FSM_TIMER(3000); // FIXME: test this
281 act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
284 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
290 case EV_MOTION_ERROR:
291 DBG_PRINT_EVENT("unhandled event");
297 /************************************************************************
298 * The "collect corns" subautomaton
299 ************************************************************************/
301 static enum where_to_go {
306 } where_to_go = CORN;
308 static struct corn *corn_to_get;
310 FSM_STATE(rush_corns_decider)
314 if (where_to_go == CORN) {
315 FSM_TRANSITION(approach_next_corn);
316 } else if (where_to_go == CONTAINER) {
317 FSM_TRANSITION(rush_the_corn);
318 } else if (where_to_go == TURN_AROUND) {
319 FSM_TRANSITION(turn_around);
320 } else /* NO_MORE_CORN */ {
328 case EV_MOTION_ERROR:
329 DBG_PRINT_EVENT("unhandled event");
336 FSM_STATE(approach_next_corn)
341 robot_get_est_pos(&x, &y, &phi);
342 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
345 corn_to_get = choose_next_corn();
347 Pos *p = get_corn_approach_position(corn_to_get);
348 corn_to_get->was_collected = true;
349 //robot_trajectory_new(&tcFast);
350 //robot_trajectory_add_final_point_trans(robot_trajectory_add_final_point_trans(p->x, p->y, TURN(p->phi));
351 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
353 where_to_go = CONTAINER;
355 where_to_go = NO_MORE_CORN;
361 FSM_TRANSITION(rush_corns_decider);
367 case EV_MOTION_ERROR:
368 DBG_PRINT_EVENT("unhandled event");
374 FSM_STATE(rush_the_corn)
379 if (robot.team_color == BLUE) {
380 x = corn_to_get->position.x;
382 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
384 remove_wall_around_corn(x, corn_to_get->position.y);
385 robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcFast);
386 where_to_go = TURN_AROUND;
389 FSM_TRANSITION(rush_corns_decider);
395 case EV_MOTION_ERROR:
396 DBG_PRINT_EVENT("unhandled event");
402 // used to perform the maneuvre
403 FSM_STATE(turn_around)
407 robot_trajectory_new_backward(&tcFast);
408 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
412 FSM_TRANSITION(rush_corns_decider);
418 case EV_MOTION_ERROR:
419 DBG_PRINT_EVENT("unhandled event");