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);
129 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.3, slope_approach_style_p->which_side),
130 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05,
131 ARRIVE_FROM(DEG2RAD(0),0.05),
133 x_coord(0.3, slope_approach_style_p->which_side),
134 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
135 ARRIVE_FROM(DEG2RAD(0), 0.02),
141 FSM_TRANSITION(climb_the_slope);
147 case EV_MOTION_ERROR:
148 case EV_SWITCH_STRATEGY:
149 DBG_PRINT_EVENT("unhandled event");
155 void inline enable_switches(bool enabled)
157 robot.use_left_switch = enabled;
158 robot.use_right_switch = enabled;
159 robot.use_back_switch = enabled;
162 FSM_STATE(climb_the_slope)
164 struct TrajectoryConstraints tc;
167 // disables using side switches on bumpers when going up
168 enable_switches(false);
169 act_vidle(VIDLE_LOAD_PREPARE, 5);
170 robot.ignore_hokuyo = true;
171 /* create the trajectory and go */
174 robot_trajectory_new_backward(&tc);
175 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
176 robot_trajectory_add_point_trans(
177 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
178 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
179 robot_trajectory_add_final_point_trans(
180 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
181 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
183 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
184 robot_trajectory_add_point_trans(
185 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
186 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85));
187 robot_trajectory_add_final_point_trans(
188 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
189 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85),
195 SUBFSM_TRANSITION(load_oranges, NULL);
198 FSM_TRANSITION(sledge_down);
203 case EV_MOTION_ERROR:
204 case EV_SWITCH_STRATEGY:
205 DBG_PRINT_EVENT("unhandled event");
211 /* subautomaton to load oranges in two stages */
212 FSM_STATE_DECL(load_oranges2);
213 FSM_STATE_DECL(load_oranges3);
214 FSM_STATE(load_oranges)
219 act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
225 FSM_TRANSITION(load_oranges2);
230 case EV_MOTION_ERROR:
231 case EV_SWITCH_STRATEGY:
232 DBG_PRINT_EVENT("unhandled event");
238 FSM_STATE(load_oranges2)
242 act_vidle(VIDLE_UP, VIDLE_MEDIUM_SPEED);
246 FSM_TRANSITION(load_oranges3);
250 FSM_TRANSITION(load_oranges3);
256 case EV_MOTION_ERROR:
257 case EV_SWITCH_STRATEGY:
258 DBG_PRINT_EVENT("unhandled event");
260 act_vidle(VIDLE_UP-1, VIDLE_FAST_SPEED);
265 FSM_STATE(load_oranges3)
269 act_vidle(VIDLE_MIDDLE+50, 0);
281 case EV_MOTION_ERROR:
282 case EV_SWITCH_STRATEGY:
283 DBG_PRINT_EVENT("unhandled event");
285 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
290 FSM_STATE(sledge_down)
292 struct TrajectoryConstraints tc;
297 robot_trajectory_new(&tc);
299 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
300 robot_trajectory_add_point_trans(
301 x_coord(1.2 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
302 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
303 robot_trajectory_add_point_trans(
304 x_coord(1.0 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
305 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.01);
306 robot_trajectory_add_point_trans(
307 x_coord(0.8 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
308 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05);
309 robot_trajectory_add_point_trans(
310 x_coord(0.6 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
311 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
312 robot_trajectory_add_final_point_trans(
313 x_coord(0.5 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
314 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.17,
316 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
317 robot_trajectory_add_point_trans(
318 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
319 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85));
320 robot_trajectory_add_final_point_trans(
321 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.2, slope_approach_style_p->which_side),
322 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85),
325 /* robot_trajectory_add_point_trans(
326 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
327 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01, slope_approach_style_p->which_oranges));
328 robot_trajectory_add_point_trans(
329 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.20, slope_approach_style_p->which_side),
330 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.08, slope_approach_style_p->which_oranges));
331 robot_trajectory_add_final_point_trans(
332 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, slope_approach_style_p->which_side),
333 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.14, slope_approach_style_p->which_oranges),
337 /* just for sure, try to close it one more time */
338 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
340 delete(slope_approach_style_p);
346 case EV_MOTION_ERROR:
347 case EV_SWITCH_STRATEGY:
348 DBG_PRINT_EVENT("unhandled event");
351 // enables using side switches on bumpers
352 enable_switches(true);
353 robot.ignore_hokuyo = false;
354 robot.check_turn_safety = true;
360 /************************************************************************
361 * The "unload our oranges" subautomaton
362 ************************************************************************/
364 FSM_STATE(to_cntainer_and_unld)
369 if (slope_approach_style_p->which_side == MINE) {
370 robot_trajectory_new(&tcFast);
371 // face the rim with front of the robot
372 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
373 // face the rim with back of the robot
374 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M-0.6, 0.35);
375 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05));
377 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
381 FSM_TIMER(3000); // FIXME: test this
382 act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
385 act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
391 case EV_MOTION_ERROR:
392 case EV_SWITCH_STRATEGY:
393 DBG_PRINT_EVENT("unhandled event");
399 /************************************************************************
400 * The "collect corns" subautomaton
401 ************************************************************************/
403 static enum where_to_go {
408 } where_to_go = CORN;
410 static struct corn *corn_to_get;
412 FSM_STATE(rush_corns_decider)
416 if (where_to_go == CORN) {
417 FSM_TRANSITION(approach_next_corn);
418 } else if (where_to_go == CONTAINER) {
419 FSM_TRANSITION(rush_the_corn);
420 } else if (where_to_go == TURN_AROUND) {
421 FSM_TRANSITION(turn_around);
422 } else /* NO_MORE_CORN */ {
430 case EV_MOTION_ERROR:
431 case EV_SWITCH_STRATEGY:
432 DBG_PRINT_EVENT("unhandled event");
439 FSM_STATE(approach_next_corn)
444 robot_get_est_pos(&x, &y, &phi);
445 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
448 corn_to_get = choose_next_corn();
450 Pos *p = get_corn_approach_position(corn_to_get);
451 corn_to_get->was_collected = true;
452 //robot_trajectory_new(&tcFast);
453 //robot_trajectory_add_final_point_trans(robot_trajectory_add_final_point_trans(p->x, p->y, TURN(p->phi));
454 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
456 where_to_go = CONTAINER;
458 where_to_go = NO_MORE_CORN;
464 FSM_TRANSITION(rush_corns_decider);
470 case EV_MOTION_ERROR:
471 case EV_SWITCH_STRATEGY:
472 DBG_PRINT_EVENT("unhandled event");
478 FSM_STATE(rush_the_corn)
483 if (robot.team_color == BLUE) {
484 x = corn_to_get->position.x;
486 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
488 remove_wall_around_corn(x, corn_to_get->position.y);
489 robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcFast);
490 where_to_go = TURN_AROUND;
493 FSM_TRANSITION(rush_corns_decider);
499 case EV_MOTION_ERROR:
500 case EV_SWITCH_STRATEGY:
501 DBG_PRINT_EVENT("unhandled event");
507 // used to perform the maneuvre
508 FSM_STATE(turn_around)
512 robot_trajectory_new_backward(&tcFast);
513 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
517 FSM_TRANSITION(rush_corns_decider);
523 case EV_MOTION_ERROR:
524 case EV_SWITCH_STRATEGY:
525 DBG_PRINT_EVENT("unhandled event");