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);
88 robot_set_est_pos_trans(0.16,
89 PLAYGROUND_HEIGHT_M - 0.16,
91 FSM_TIMER(2000); // wait for the localization to settle down
95 FSM_TRANSITION(approach_first_puck);
98 case EV_GOAL_NOT_REACHABLE:
99 case EV_SHORT_TIME_TO_END:
102 case EV_ACTION_ERROR:
103 case EV_PUCK_REACHABLE:
105 DBG_PRINT_EVENT("unhandled event");
113 /************************************************************************
115 ************************************************************************/
117 FSM_STATE(approach_first_puck)
121 robot_move_by(0.42, NO_TURN(), &tcSlow);
123 case EV_MOTION_DONE: {
125 if (PUCK_REACHABLE(robot.puck_distance)) {
126 printf("Puck reachable, telling act to grasp the puck\n");
127 //SUBFSM_TRANSITION(grasp_the_puck, NULL); // FIXME
131 printf("making transition to look_around_for_puck state\n");
132 SUBFSM_TRANSITION(look_around_for_puck, NULL);
135 printf("telling the robot to move by 5 cm and try again\n");
136 robot_move_by(0.05, NO_TURN(), &tcSlow);
143 case EV_PUCK_REACHABLE:
145 FSM_TRANSITION(simple_construction_zone_approach);
150 case EV_GOAL_NOT_REACHABLE:
151 case EV_SHORT_TIME_TO_END:
154 case EV_ACTION_ERROR:
155 DBG_PRINT_EVENT("unhandled event");
162 FSM_STATE(simple_construction_zone_approach)
166 robot_trajectory_new(&tcFast);
167 robot_trajectory_add_point_trans(0.9, 1);
168 robot_trajectory_add_final_point_trans(0.9, ROBOT_AXIS_TO_FRONT_M + 0.00, NO_TURN());
171 //FSM_SIGNAL(ACT, EV_UNLOAD_THE_PUCK, NULL);
174 FSM_TRANSITION(get_out_of_the_construction_zone);
179 case EV_GOAL_NOT_REACHABLE:
180 case EV_SHORT_TIME_TO_END:
182 case EV_ACTION_ERROR:
183 case EV_PUCK_REACHABLE:
185 DBG_PRINT_EVENT("unhandled event");
192 FSM_STATE(get_out_of_the_construction_zone)
196 robot_move_by(-0.15, NO_TURN(), &tcSlow);
204 case EV_GOAL_NOT_REACHABLE:
205 case EV_SHORT_TIME_TO_END:
207 case EV_ACTION_ERROR:
208 case EV_PUCK_REACHABLE:
210 DBG_PRINT_EVENT("unhandled event");
217 /************************************************************************
218 * STATES HANDLING ACT's ACTIONS (to be used as SUB FSMs)
219 ************************************************************************/
221 FSM_STATE(look_around_for_puck)
223 static int lfp_status = 0;
224 const static int scatter_angle = 20;
225 static struct robot_pos_type orig_position;
229 orig_position = robot.ref_pos;
230 ROBOT_UNLOCK(ref_pos);
231 //printf("original angle of rotation of the robot: %f degrees\n", RAD2DEG(orig_position.phi));
232 robot_move_by(0, TURN_CW(DEG2RAD(RAD2DEG(orig_position.phi)-scatter_angle)), &tcSlow);
233 //printf("lfp_status: %d\n", lfp_status);
236 switch (lfp_status) {
238 //printf("2. original angle of rotation of the robot: %f degrees\n", RAD2DEG(orig_position.phi));
239 //printf("robot.fer_pos angle of rotation of the robot: %f degrees\n", RAD2DEG(robot.ref_pos.phi));
240 //printf("--- robot move by ... turn cw\n");
241 robot_move_by(0, TURN_CCW(DEG2RAD(RAD2DEG(orig_position.phi)+scatter_angle)), &tcSlow);
245 robot_move_by(0, TURN(orig_position.phi), &tcSlow);
248 case 2: // puck not found
249 SUBFSM_RET((void *)false);
252 //printf("lfp_status: %d\n", lfp_status);
254 case EV_PUCK_REACHABLE: // puck found
256 SUBFSM_RET((void *)true);
259 case EV_ACTION_ERROR: // look for puck does not send this event
263 case EV_GOAL_NOT_REACHABLE:
264 case EV_SHORT_TIME_TO_END:
267 DBG_PRINT_EVENT("unhandled event");
274 /************************************************************************
276 ************************************************************************/
283 if (rv) error(1, errno, "robot_init() returned %d\n", rv);
285 robot.obstacle_avoidance_enabled = true;
287 robot.fsm.main.debug_states = 1;
288 robot.fsm.motion.debug_states = 1;
289 robot.fsm.act.debug_states = 1;
291 robot.fsm.main.state = &fsm_state_main_init;
292 //robot.fsm.main.transition_callback = trans_callback;
293 //robot.fsm.motion.transition_callback = move_trans_callback;
296 if (rv) error(1, errno, "robot_start() returned %d\n", rv);