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 ************************************************************************/
29 #define DBG_FSM_STATE(name) do { if (fsm->debug_states) printf("fsm %s %.1f: %s(%s)\n", \
30 fsm->debug_name, robot_current_time(), \
31 name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
34 static void set_initial_position()
36 robot_set_est_pos_trans(ROBOT_AXIS_TO_FRONT_M,
37 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2),
41 static void actuators_home()
44 act_vidle(VIDLE_UP - tmp, VIDLE_FAST_SPEED);
45 tmp = 1 - tmp; // Force movement (we need to change the target position)
51 robot.check_turn_safety = false;
52 pthread_create(&thid, NULL, timing_thread, NULL);
57 // We set initial position periodically in order for it to be updated
58 // on the display if the team color is changed during waiting for
62 set_initial_position();
63 if (robot.start_state == START_PLUGGED_IN)
69 sem_post(&robot.start);
71 set_initial_position();
76 robot.corns = get_all_corns(robot.corns_conf_side, robot.corns_conf_center);
80 /************************************************************************
81 * Trajectory constraints used; They are initialized in the main() function in competition.cc
82 ************************************************************************/
84 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
86 #define VIDLE_TIMEOUT 2000
88 /************************************************************************
89 * States that form the "collect some oranges" subautomaton. Calling automaton
90 * SHOULD ALWAYS call the "approach_the_slope" state.
91 ************************************************************************/
93 bool inline is_ready_to_climb_the_slope(enum which_side which_slope, double x, double y) {
95 if (which_slope == MINE) {
96 ret = x < 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
97 } else if (which_slope == OPPONENTS) {
98 ret = x > 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
100 printf("ERROR: unknown side;");
101 #warning Remove the next line
107 static struct slope_approach_style *slope_approach_style_p;
109 /* assures that the robot is near the slope rim; if outside our starting area, approach the slope first */
110 FSM_STATE(approach_the_slope)
114 slope_approach_style_p = (struct slope_approach_style *) FSM_EVENT_PTR;
115 if (slope_approach_style_p == NULL) {
116 printf("\n\nit is not allowed to call the approach_the_slope state with NULL data!!\n\n");
117 #warning remove the next line
121 robot_get_est_pos_trans(&x, &y, &phi);
123 bool ready_to_climb_the_slope = is_ready_to_climb_the_slope(slope_approach_style_p->which_side, x, y);
124 /* if necessary, approach the slope */
125 if (ready_to_climb_the_slope) {
126 FSM_TRANSITION(climb_the_slope);
129 x_coord(0.3, slope_approach_style_p->which_side),
130 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
131 ARRIVE_FROM(DEG2RAD(0), 0.02),
137 FSM_TRANSITION(climb_the_slope);
143 case EV_MOTION_ERROR:
144 case EV_SWITCH_STRATEGY:
145 DBG_PRINT_EVENT("unhandled event");
151 void inline enable_switches(bool enabled)
153 robot.use_left_switch = enabled;
154 robot.use_right_switch = enabled;
155 robot.use_back_switch = enabled;
158 FSM_STATE(climb_the_slope)
160 struct TrajectoryConstraints tc;
163 // disables using side switches on bumpers when going up
164 enable_switches(false);
165 robot.ignore_hokuyo = true;
166 /* create the trajectory and go */
169 robot_trajectory_new_backward(&tc);
170 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
171 act_vidle(VIDLE_LOAD_PREPARE, 5);
172 robot_trajectory_add_point_trans(
173 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
174 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
175 robot_trajectory_add_final_point_trans(
176 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
177 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
179 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
181 robot_trajectory_add_point_trans(
182 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
183 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85));
184 robot_trajectory_add_final_point_trans(
185 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
186 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85),
192 SUBFSM_TRANSITION(load_oranges, NULL);
195 FSM_TRANSITION(sledge_down);
198 act_vidle(VIDLE_LOAD_PREPARE, 10);
202 case EV_MOTION_ERROR:
203 case EV_SWITCH_STRATEGY:
204 DBG_PRINT_EVENT("unhandled event");
210 /* subautomaton to load oranges in two stages */
211 FSM_STATE_DECL(load_oranges2);
212 FSM_STATE_DECL(load_oranges3);
213 FSM_STATE(load_oranges)
218 act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
224 FSM_TRANSITION(load_oranges2);
229 case EV_MOTION_ERROR:
230 case EV_SWITCH_STRATEGY:
231 DBG_PRINT_EVENT("unhandled event");
237 FSM_STATE(load_oranges2)
241 act_vidle(VIDLE_UP, VIDLE_MEDIUM_SPEED);
245 FSM_TRANSITION(load_oranges3);
249 FSM_TRANSITION(load_oranges3);
255 case EV_MOTION_ERROR:
256 case EV_SWITCH_STRATEGY:
257 DBG_PRINT_EVENT("unhandled event");
259 act_vidle(VIDLE_UP-1, VIDLE_FAST_SPEED);
264 FSM_STATE(load_oranges3)
268 act_vidle(VIDLE_MIDDLE+50, 0);
280 case EV_MOTION_ERROR:
281 case EV_SWITCH_STRATEGY:
282 DBG_PRINT_EVENT("unhandled event");
284 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
289 FSM_STATE(sledge_down)
291 struct TrajectoryConstraints tc;
296 robot_trajectory_new(&tc);
298 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
299 robot_trajectory_add_point_trans(
300 x_coord(1.2 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
301 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
302 robot_trajectory_add_point_trans(
303 x_coord(1.0 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
304 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.01);
305 robot_trajectory_add_point_trans(
306 x_coord(0.8 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
307 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05);
308 robot_trajectory_add_point_trans(
309 x_coord(0.6 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
310 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
311 robot_trajectory_add_final_point_trans(
312 x_coord(0.5 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
313 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.17,
315 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
316 robot_trajectory_add_point_trans(
317 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
318 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85));
319 robot_trajectory_add_final_point_trans(
320 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.2, slope_approach_style_p->which_side),
321 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85),
326 /* just for sure, try to close it one more time */
327 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
329 delete(slope_approach_style_p);
335 case EV_MOTION_ERROR:
336 case EV_SWITCH_STRATEGY:
337 DBG_PRINT_EVENT("unhandled event");
340 // enables using side switches on bumpers
341 enable_switches(true);
342 robot.ignore_hokuyo = false;
343 robot.check_turn_safety = true;
349 /************************************************************************
350 * The "unload our oranges" subautomaton
351 ************************************************************************/
353 FSM_STATE(to_cntainer_and_unld)
357 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
360 FSM_TIMER(3000); // FIXME: test this
361 act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
364 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
370 case EV_MOTION_ERROR:
371 case EV_SWITCH_STRATEGY:
372 DBG_PRINT_EVENT("unhandled event");
378 /************************************************************************
379 * The "collect corns" subautomaton
380 ************************************************************************/
382 static enum where_to_go {
387 } where_to_go = CORN;
389 static struct corn *corn_to_get;
391 FSM_STATE(rush_corns_decider)
395 if (where_to_go == CORN) {
396 FSM_TRANSITION(approach_next_corn);
397 } else if (where_to_go == CONTAINER) {
398 FSM_TRANSITION(rush_the_corn);
399 } else if (where_to_go == TURN_AROUND) {
400 FSM_TRANSITION(turn_around);
401 } else /* NO_MORE_CORN */ {
409 case EV_MOTION_ERROR:
410 case EV_SWITCH_STRATEGY:
411 DBG_PRINT_EVENT("unhandled event");
418 FSM_STATE(approach_next_corn)
423 robot_get_est_pos(&x, &y, &phi);
424 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
427 corn_to_get = choose_next_corn();
429 Pos *p = get_corn_approach_position(corn_to_get);
430 corn_to_get->was_collected = true;
431 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
433 where_to_go = CONTAINER;
435 where_to_go = NO_MORE_CORN;
441 FSM_TRANSITION(rush_corns_decider);
447 case EV_MOTION_ERROR:
448 case EV_SWITCH_STRATEGY:
449 DBG_PRINT_EVENT("unhandled event");
455 FSM_STATE(rush_the_corn)
460 if (robot.team_color == BLUE) {
461 x = corn_to_get->position.x;
463 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
465 remove_wall_around_corn(x, corn_to_get->position.y);
466 robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcSlow);
467 where_to_go = TURN_AROUND;
470 FSM_TRANSITION(rush_corns_decider);
476 case EV_MOTION_ERROR:
477 case EV_SWITCH_STRATEGY:
478 DBG_PRINT_EVENT("unhandled event");
484 // used to perform the maneuvre
485 FSM_STATE(turn_around)
489 robot_trajectory_new_backward(&tcFast);
490 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
494 FSM_TRANSITION(rush_corns_decider);
500 case EV_MOTION_ERROR:
501 case EV_SWITCH_STRATEGY:
502 DBG_PRINT_EVENT("unhandled event");