2 * homologation.cc 08/04/29
4 * Robot's control program intended for homologation (approval phase) on Eurobot 2009.
6 * Copyright: (c) 2009 CTU Dragons
7 * CTU FEE - Department of Control Engineering
21 #include <movehelper.h>
29 /************************************************************************
30 * Trajectory constraints used, are initialized in the init state
31 ************************************************************************/
33 struct TrajectoryConstraints tcFast, tcSlow;
35 /************************************************************************
36 * FSM STATES DECLARATION
37 ************************************************************************/
39 /* initial and starting states */
41 FSM_STATE_DECL(wait_for_start);
43 FSM_STATE_DECL(approach_first_puck);
44 FSM_STATE_DECL(simple_construction_zone_approach);
45 FSM_STATE_DECL(get_out_of_the_construction_zone);
47 FSM_STATE_DECL(look_around_for_puck);
49 /************************************************************************
50 * INITIAL AND STARTING STATES
51 ************************************************************************/
57 tcFast = trajectoryConstraintsDefault;
59 tcSlow = trajectoryConstraintsDefault;
61 FSM_TRANSITION(wait_for_start);
68 FSM_STATE(wait_for_start)
71 printf("COMPETITION mode set");
80 /* start competition timer */
81 pthread_create(&thid, NULL, wait_for_end, NULL);
82 pthread_create(&thid, NULL, wait_to_deposition, NULL);
89 robot_set_est_pos_trans(0.16,
90 PLAYGROUND_HEIGHT_M - 0.16,
92 FSM_TIMER(2000); // wait for the localization to settle down
96 FSM_TRANSITION(approach_first_puck);
99 case EV_GOAL_NOT_REACHABLE:
100 case EV_SHORT_TIME_TO_END:
103 case EV_ACTION_ERROR:
104 case EV_PUCK_REACHABLE:
106 DBG_PRINT_EVENT("unhandled event");
114 /************************************************************************
116 ************************************************************************/
118 FSM_STATE(approach_first_puck)
122 robot_move_by(0.42, NO_TURN(), &tcSlow);
124 case EV_MOTION_DONE: {
126 if (PUCK_REACHABLE(robot.puck_distance)) {
127 printf("Puck reachable, telling act to grasp the puck\n");
128 //SUBFSM_TRANSITION(grasp_the_puck, NULL); // FIXME
132 printf("making transition to look_around_for_puck state\n");
133 SUBFSM_TRANSITION(look_around_for_puck, NULL);
136 printf("telling the robot to move by 5 cm and try again\n");
137 robot_move_by(0.05, NO_TURN(), &tcSlow);
144 case EV_PUCK_REACHABLE:
146 FSM_TRANSITION(simple_construction_zone_approach);
151 case EV_GOAL_NOT_REACHABLE:
152 case EV_SHORT_TIME_TO_END:
155 case EV_ACTION_ERROR:
156 DBG_PRINT_EVENT("unhandled event");
163 FSM_STATE(simple_construction_zone_approach)
167 robot_trajectory_new(&tcFast);
168 robot_trajectory_add_point_trans(0.9, 1);
169 robot_trajectory_add_final_point_trans(0.9, ROBOT_AXIS_TO_FRONT_M + 0.00, NO_TURN());
172 //FSM_SIGNAL(ACT, EV_UNLOAD_THE_PUCK, NULL);
175 FSM_TRANSITION(get_out_of_the_construction_zone);
180 case EV_GOAL_NOT_REACHABLE:
181 case EV_SHORT_TIME_TO_END:
183 case EV_ACTION_ERROR:
184 case EV_PUCK_REACHABLE:
186 DBG_PRINT_EVENT("unhandled event");
193 FSM_STATE(get_out_of_the_construction_zone)
197 robot_move_by(-0.15, NO_TURN(), &tcSlow);
205 case EV_GOAL_NOT_REACHABLE:
206 case EV_SHORT_TIME_TO_END:
208 case EV_ACTION_ERROR:
209 case EV_PUCK_REACHABLE:
211 DBG_PRINT_EVENT("unhandled event");
218 /************************************************************************
219 * STATES HANDLING ACT's ACTIONS (to be used as SUB FSMs)
220 ************************************************************************/
222 FSM_STATE(look_around_for_puck)
224 static int lfp_status = 0;
225 const static int scatter_angle = 20;
226 static struct ref_pos_type orig_position;
230 orig_position = robot.ref_pos;
231 ROBOT_UNLOCK(ref_pos);
232 //printf("original angle of rotation of the robot: %f degrees\n", RAD2DEG(orig_position.phi));
233 robot_move_by(0, TURN_CW(DEG2RAD(RAD2DEG(orig_position.phi)-scatter_angle)), &tcSlow);
234 //printf("lfp_status: %d\n", lfp_status);
237 switch (lfp_status) {
239 //printf("2. original angle of rotation of the robot: %f degrees\n", RAD2DEG(orig_position.phi));
240 //printf("robot.fer_pos angle of rotation of the robot: %f degrees\n", RAD2DEG(robot.ref_pos.phi));
241 //printf("--- robot move by ... turn cw\n");
242 robot_move_by(0, TURN_CCW(DEG2RAD(RAD2DEG(orig_position.phi)+scatter_angle)), &tcSlow);
246 robot_move_by(0, TURN(orig_position.phi), &tcSlow);
249 case 2: // puck not found
250 SUBFSM_RET((void *)false);
253 //printf("lfp_status: %d\n", lfp_status);
255 case EV_PUCK_REACHABLE: // puck found
257 SUBFSM_RET((void *)true);
260 case EV_ACTION_ERROR: // look for puck does not send this event
264 case EV_GOAL_NOT_REACHABLE:
265 case EV_SHORT_TIME_TO_END:
268 DBG_PRINT_EVENT("unhandled event");
275 /************************************************************************
277 ************************************************************************/
284 if (rv) error(1, errno, "robot_init() returned %d\n", rv);
286 robot.obstacle_avoidance_enabled = true;
288 robot.fsm.main.debug_states = 1;
289 robot.fsm.motion.debug_states = 1;
290 robot.fsm.act.debug_states = 1;
292 robot.fsm.main.state = &fsm_state_main_init;
293 //robot.fsm.main.transition_callback = trans_callback;
294 //robot.fsm.motion.transition_callback = move_trans_callback;
297 if (rv) error(1, errno, "robot_start() returned %d\n", rv);