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()
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;"); robot_exit();
95 static enum which_side which_slope;
97 /* assures that the robot is near the slope rim; if outside our starting area, approach the slope first */
98 FSM_STATE(approach_the_slope)
102 which_slope = (enum which_side) FSM_EVENT_INT;
104 robot_get_est_pos_trans(&x, &y, &phi);
106 bool ready_to_climb_the_slope = is_ready_to_climb_the_slope(which_slope, x, y);
107 /* if necessary, approach the slope */
108 if (ready_to_climb_the_slope) {
109 FSM_TRANSITION(climb_the_slope);
112 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.05, which_slope),
113 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05,
114 ARRIVE_FROM(DEG2RAD(0),0.05),
120 FSM_TRANSITION(climb_the_slope);
126 case EV_ACTION_ERROR:
127 case EV_GOAL_NOT_REACHABLE:
128 DBG_PRINT_EVENT("unhandled event");
134 void inline enable_switches(bool enabled)
136 robot.use_left_switch = enabled;
137 robot.use_right_switch = enabled;
138 robot.use_back_switch = enabled;
141 FSM_STATE(climb_the_slope)
145 // disables using side switches on bumpers when going up
146 enable_switches(false);
147 act_vidle(VIDLE_DOWN);
148 robot.ignore_hokuyo = true;
149 /* create the trajectory and go */
150 robot_trajectory_new_backward(&tcSlow);
151 robot_trajectory_add_point_trans(
152 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, which_slope),
153 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
154 robot_trajectory_add_final_point_trans(
155 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, which_slope),
156 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
161 FSM_TIMER(VIDLE_TIMEOUT);
166 // FIXME: respond to VIDLE EVENT
167 FSM_TRANSITION(sledge_down);
171 case EV_ACTION_ERROR:
172 case EV_GOAL_NOT_REACHABLE:
173 DBG_PRINT_EVENT("unhandled event");
179 FSM_STATE(sledge_down)
183 robot_trajectory_new(&tcFast);
184 robot_trajectory_add_point_trans(
185 x_coord(1 - ROBOT_AXIS_TO_BACK_M, which_slope),
186 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
188 robot_trajectory_add_point_trans(
189 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, which_slope),
190 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
191 robot_trajectory_add_final_point_trans(
192 x_coord(0.50, which_slope),
193 PLAYGROUND_HEIGHT_M - 0.6,
196 robot_trajectory_add_point_trans(
197 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.20, which_slope),
198 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.08);
199 robot_trajectory_add_final_point_trans(
200 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, which_slope),
201 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.14,
205 /* just for sure, try to close it one more time */
213 case EV_ACTION_ERROR:
214 case EV_GOAL_NOT_REACHABLE:
215 DBG_PRINT_EVENT("unhandled event");
217 // enables using side switches on bumpers
218 enable_switches(true);
219 robot.ignore_hokuyo = false;
220 robot.check_turn_safety = true;
226 /************************************************************************
227 * The "unload our oranges" subautomaton
228 ************************************************************************/
230 FSM_STATE(to_cntainer_and_unld)
235 if (which_slope == MINE) {
236 robot_trajectory_new(&tcFast);
237 // face the rim with front of the robot
238 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
239 // face the rim with back of the robot
240 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M-0.6, 0.35);
241 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05));
243 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
247 FSM_TIMER(3000); // FIXME: test this
248 act_vidle(VIDLE_VYSIP);
257 case EV_ACTION_ERROR:
258 case EV_GOAL_NOT_REACHABLE:
259 DBG_PRINT_EVENT("unhandled event");
265 /************************************************************************
266 * The "collect corns" subautomaton
267 ************************************************************************/
269 static enum where_to_go {
274 } where_to_go = CORN;
276 static struct corn *corn_to_get;
278 FSM_STATE(rush_corns_decider)
282 if (where_to_go == CORN) {
283 FSM_TRANSITION(approach_next_corn);
284 } else if (where_to_go == CONTAINER) {
285 FSM_TRANSITION(rush_the_corn);
286 } else if (where_to_go == TURN_AROUND) {
287 FSM_TRANSITION(turn_around);
288 } else /* NO_MORE_CORN */ {
295 case EV_ACTION_ERROR:
297 case EV_GOAL_NOT_REACHABLE:
298 DBG_PRINT_EVENT("unhandled event");
305 FSM_STATE(approach_next_corn)
310 robot_get_est_pos(&x, &y, &phi);
311 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
314 corn_to_get = choose_next_corn();
316 Pos *p = get_corn_approach_position(corn_to_get);
317 corn_to_get->was_collected = true;
318 //robot_trajectory_new(&tcFast);
319 //robot_trajectory_add_final_point_trans(robot_trajectory_add_final_point_trans(p->x, p->y, TURN(p->phi));
320 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
322 where_to_go = CONTAINER;
324 where_to_go = NO_MORE_CORN;
330 FSM_TRANSITION(rush_corns_decider);
336 case EV_ACTION_ERROR:
337 case EV_GOAL_NOT_REACHABLE:
338 DBG_PRINT_EVENT("unhandled event");
344 FSM_STATE(rush_the_corn)
349 if (robot.team_color == BLUE) {
350 x = corn_to_get->position.x;
352 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
354 remove_wall_around_corn(x, corn_to_get->position.y);
355 robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcFast);
356 where_to_go = TURN_AROUND;
359 FSM_TRANSITION(rush_corns_decider);
365 case EV_ACTION_ERROR:
366 case EV_GOAL_NOT_REACHABLE:
367 DBG_PRINT_EVENT("unhandled event");
373 // used to perform the maneuvre
374 FSM_STATE(turn_around)
378 robot_trajectory_new_backward(&tcFast);
379 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
383 FSM_TRANSITION(rush_corns_decider);
389 case EV_ACTION_ERROR:
390 case EV_GOAL_NOT_REACHABLE:
391 DBG_PRINT_EVENT("unhandled event");