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);
56 // We set initial position periodically in order for it to be updated
57 // on the display if the team color is changed during waiting for
61 set_initial_position();
62 if (robot.start_state == START_PLUGGED_IN)
68 sem_post(&robot.start);
70 set_initial_position();
75 robot.corns = get_all_corns(robot.corns_conf_side, robot.corns_conf_center);
79 /************************************************************************
80 * Trajectory constraints used; They are initialized in the main() function in competition.cc
81 ************************************************************************/
83 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
85 #define VIDLE_TIMEOUT 2000
87 /************************************************************************
88 * States that form the "collect some oranges" subautomaton. Calling automaton
89 * SHOULD ALWAYS call the "approach_the_slope" state.
90 ************************************************************************/
92 bool inline is_ready_to_climb_the_slope(enum which_side which_slope, double x, double y) {
94 if (which_slope == MINE) {
95 ret = x < 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
96 } else if (which_slope == OPPONENTS) {
97 ret = x > 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
99 printf("ERROR: unknown side;");
100 #warning Remove the next line
106 static struct slope_approach_style *slope_approach_style_p;
108 /* assures that the robot is near the slope rim; if outside our starting area, approach the slope first */
109 FSM_STATE(approach_the_slope)
113 slope_approach_style_p = (struct slope_approach_style *) FSM_EVENT_PTR;
114 if (slope_approach_style_p == NULL) {
115 printf("\n\nit is not allowed to call the approach_the_slope state with NULL data!!\n\n");
116 #warning remove the next line
120 robot_get_est_pos_trans(&x, &y, &phi);
122 bool ready_to_climb_the_slope = is_ready_to_climb_the_slope(slope_approach_style_p->which_side, x, y);
123 /* if necessary, approach the slope */
124 if (ready_to_climb_the_slope) {
125 FSM_TRANSITION(climb_the_slope);
128 x_coord(0.3, slope_approach_style_p->which_side),
129 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
130 ARRIVE_FROM(DEG2RAD(0), 0.02),
136 FSM_TRANSITION(climb_the_slope);
142 case EV_MOTION_ERROR:
143 case EV_SWITCH_STRATEGY:
144 DBG_PRINT_EVENT("unhandled event");
150 void inline enable_switches(bool enabled)
152 robot.use_left_switch = enabled;
153 robot.use_right_switch = enabled;
154 robot.use_back_switch = enabled;
157 FSM_STATE(climb_the_slope)
159 struct TrajectoryConstraints tc;
162 // disables using side switches on bumpers when going up
163 enable_switches(false);
164 robot.ignore_hokuyo = true;
165 /* create the trajectory and go */
168 robot_trajectory_new_backward(&tc);
169 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
170 act_vidle(VIDLE_LOAD_PREPARE, 5);
171 robot_trajectory_add_point_trans(
172 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
173 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
174 robot_trajectory_add_final_point_trans(
175 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
176 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
178 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
180 robot_trajectory_add_point_trans(
181 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
182 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85));
183 robot_trajectory_add_final_point_trans(
184 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
185 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85),
191 SUBFSM_TRANSITION(load_oranges, NULL);
194 FSM_TRANSITION(sledge_down);
197 act_vidle(VIDLE_LOAD_PREPARE, 10);
201 case EV_MOTION_ERROR:
202 case EV_SWITCH_STRATEGY:
203 DBG_PRINT_EVENT("unhandled event");
209 /* subautomaton to load oranges in two stages */
210 FSM_STATE_DECL(load_oranges2);
211 FSM_STATE_DECL(load_oranges3);
212 FSM_STATE(load_oranges)
217 act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
223 FSM_TRANSITION(load_oranges2);
228 case EV_MOTION_ERROR:
229 case EV_SWITCH_STRATEGY:
230 DBG_PRINT_EVENT("unhandled event");
236 FSM_STATE(load_oranges2)
240 act_vidle(VIDLE_UP, VIDLE_MEDIUM_SPEED);
244 FSM_TRANSITION(load_oranges3);
248 FSM_TRANSITION(load_oranges3);
254 case EV_MOTION_ERROR:
255 case EV_SWITCH_STRATEGY:
256 DBG_PRINT_EVENT("unhandled event");
258 act_vidle(VIDLE_UP-1, VIDLE_FAST_SPEED);
263 FSM_STATE(load_oranges3)
267 act_vidle(VIDLE_MIDDLE+50, 0);
279 case EV_MOTION_ERROR:
280 case EV_SWITCH_STRATEGY:
281 DBG_PRINT_EVENT("unhandled event");
283 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
288 FSM_STATE(sledge_down)
290 struct TrajectoryConstraints tc;
295 robot_trajectory_new(&tc);
297 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
298 robot_trajectory_add_point_trans(
299 x_coord(1.2 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
300 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
301 robot_trajectory_add_point_trans(
302 x_coord(1.0 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
303 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.01);
304 robot_trajectory_add_point_trans(
305 x_coord(0.8 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
306 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05);
307 robot_trajectory_add_point_trans(
308 x_coord(0.6 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
309 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
310 robot_trajectory_add_final_point_trans(
311 x_coord(0.5 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
312 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.17,
314 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
315 robot_trajectory_add_point_trans(
316 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
317 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85));
318 robot_trajectory_add_final_point_trans(
319 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.2, slope_approach_style_p->which_side),
320 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85),
325 /* just for sure, try to close it one more time */
326 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
328 delete(slope_approach_style_p);
334 case EV_MOTION_ERROR:
335 case EV_SWITCH_STRATEGY:
336 DBG_PRINT_EVENT("unhandled event");
339 // enables using side switches on bumpers
340 enable_switches(true);
341 robot.ignore_hokuyo = false;
342 robot.check_turn_safety = true;
348 /************************************************************************
349 * The "unload our oranges" subautomaton
350 ************************************************************************/
352 FSM_STATE(to_cntainer_and_unld)
356 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
359 FSM_TIMER(3000); // FIXME: test this
360 act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
363 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
369 case EV_MOTION_ERROR:
370 case EV_SWITCH_STRATEGY:
371 DBG_PRINT_EVENT("unhandled event");
377 /************************************************************************
378 * The "collect corns" subautomaton
379 ************************************************************************/
381 static enum where_to_go {
386 } where_to_go = CORN;
388 static struct corn *corn_to_get;
390 FSM_STATE(rush_corns_decider)
394 if (where_to_go == CORN) {
395 FSM_TRANSITION(approach_next_corn);
396 } else if (where_to_go == CONTAINER) {
397 FSM_TRANSITION(rush_the_corn);
398 } else if (where_to_go == TURN_AROUND) {
399 FSM_TRANSITION(turn_around);
400 } else /* NO_MORE_CORN */ {
408 case EV_MOTION_ERROR:
409 case EV_SWITCH_STRATEGY:
410 DBG_PRINT_EVENT("unhandled event");
417 FSM_STATE(approach_next_corn)
422 robot_get_est_pos(&x, &y, &phi);
423 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
426 corn_to_get = choose_next_corn();
428 Pos *p = get_corn_approach_position(corn_to_get);
429 corn_to_get->was_collected = true;
430 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
432 where_to_go = CONTAINER;
434 where_to_go = NO_MORE_CORN;
440 FSM_TRANSITION(rush_corns_decider);
446 case EV_MOTION_ERROR:
447 case EV_SWITCH_STRATEGY:
448 DBG_PRINT_EVENT("unhandled event");
454 FSM_STATE(rush_the_corn)
459 if (robot.team_color == BLUE) {
460 x = corn_to_get->position.x;
462 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
464 remove_wall_around_corn(x, corn_to_get->position.y);
465 robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcSlow);
466 where_to_go = TURN_AROUND;
469 FSM_TRANSITION(rush_corns_decider);
475 case EV_MOTION_ERROR:
476 case EV_SWITCH_STRATEGY:
477 DBG_PRINT_EVENT("unhandled event");
483 // used to perform the maneuvre
484 FSM_STATE(turn_around)
488 robot_trajectory_new_backward(&tcFast);
489 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
493 FSM_TRANSITION(rush_corns_decider);
499 case EV_MOTION_ERROR:
500 case EV_SWITCH_STRATEGY:
501 DBG_PRINT_EVENT("unhandled event");