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>
28 #include "corns_configs.h"
31 /************************************************************************
32 * Trajectory constraints used, are initialized in the init state
33 ************************************************************************/
35 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
37 /************************************************************************
38 * Functions to manipulate map, compute distances, choose corns etc.
39 ************************************************************************/
41 static const Point containerPosition(PLAYGROUND_WIDTH_M - 0.25, 0.02); // blue container Position
43 // returns pointer to next real (non-fake) corn which was not yet collected
44 // TODO: note: use this for "short_time_to_end: situation only, otherwise
45 // it is better to try to rush more corns at once
46 struct corn * choose_next_corn()
50 double minDistance = 2 * PLAYGROUND_HEIGHT_M; // "infinity"
51 struct corn *minCorn = NULL, *corn;
52 // robot.corns->corns[NUM_OF_FAKE_CORNS] is first non-fake corn in the robot.corns structure
53 for(corn = &robot.corns->corns[NUM_OF_FAKE_CORNS]; corn < &robot.corns->corns[robot.corns->corns_count]; corn++) {
54 cornPosition.x = corn->position.x;
55 cornPosition.y = corn->position.y;
56 double distance = cornPosition.distanceTo(containerPosition);
58 if (distance < minDistance && corn->was_collected == false) {
59 minDistance = distance;
64 if (minCorn) printf("\tmin distance was: %.3f ", minDistance);
69 * Computes and returns line to point distance
70 * @param[in] p the point coords
71 * @param[in] lp1 coords of one of the points on the line
72 * @param[in] lp2 coords of the second point on the line
74 double get_point_to_line_distance(const Point &p, const Point &lp1, const Point &lp2)
76 double distance = fabs((lp2.x - lp1.x)*(lp1.y-p.y) - (lp1.x - p.x)*(lp2.y - lp1.y))
77 / sqrt((lp2.x - lp1.x)*(lp2.x - lp1.x) + (lp2.y - lp1.y)*(lp2.y - lp1.y));
81 Pos * get_corn_approach_position(struct corn *corn)
83 const Point approxContainerPosition(PLAYGROUND_WIDTH_M - 0.15, 0.02); // blue container Position
84 Pos *p = new Pos; // robot position result
86 Point cornPosition(corn->position.x, corn->position.y);
87 double a = approxContainerPosition.angleTo(cornPosition);
89 p->x = cornPosition.x + cos(a)*(CORN_NEIGHBOURHOOD_RADIUS_M + 0.1);
90 p->y = cornPosition.y + sin(a)*(CORN_NEIGHBOURHOOD_RADIUS_M + 0.1);
97 /************************************************************************
98 * FSM STATES DECLARATION
99 ************************************************************************/
101 /* initial and starting states */
102 FSM_STATE_DECL(init);
103 FSM_STATE_DECL(wait_for_start);
104 FSM_STATE_DECL(wait);
105 /* movement states */
106 FSM_STATE_DECL(climb_the_slope);
107 FSM_STATE_DECL(sledge_down);
108 FSM_STATE_DECL(to_container_diag);
109 FSM_STATE_DECL(to_container_ortho);
110 FSM_STATE_DECL(experiment);
112 /************************************************************************
113 * INITIAL AND STARTING STATES
114 ************************************************************************/
120 tcFast = trajectoryConstraintsDefault;
124 tcSlow = trajectoryConstraintsDefault;
127 tcVerySlow = trajectoryConstraintsDefault;
128 tcVerySlow.maxv = 0.05;
129 tcVerySlow.maxacc = 0.05;
130 FSM_TRANSITION(wait_for_start);
137 FSM_STATE(wait_for_start)
140 printf("COMPETITION mode set");
143 #ifdef WAIT_FOR_START
149 /* start competition timer */
150 pthread_create(&thid, NULL, wait_for_end, NULL);
151 pthread_create(&thid, NULL, wait_to_deposition, NULL);
158 robot_set_est_pos_trans(ROBOT_AXIS_TO_FRONT_M,
159 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2),
161 FSM_TRANSITION(climb_the_slope);
165 case EV_GOAL_NOT_REACHABLE:
167 case EV_ACTION_ERROR:
169 DBG_PRINT_EVENT("unhandled event");
172 robot.corns = get_all_corns(robot.corns_conf_side, robot.corns_conf_center);
178 /************************************************************************
180 ************************************************************************/
182 FSM_STATE(climb_the_slope)
186 robot.ignore_hokuyo = true;
187 robot_trajectory_new_backward(&tcSlow);
188 robot_trajectory_add_point_trans(
189 0.5 - ROBOT_AXIS_TO_BACK_M,
190 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2));
191 /* position for collecting oranges*/
192 robot_trajectory_add_final_point_trans(
193 SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.04,
194 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.02,
198 FSM_TRANSITION(wait);
204 case EV_ACTION_ERROR:
205 case EV_GOAL_NOT_REACHABLE:
206 DBG_PRINT_EVENT("unhandled event");
214 DBG_PRINT_EVENT("Loading oranges");
218 FSM_TRANSITION(sledge_down);
225 FSM_STATE(sledge_down)
229 robot_trajectory_new(&tcFast);
230 robot_trajectory_add_point_trans(
231 1 -ROBOT_AXIS_TO_BACK_M,
232 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.02);
233 robot_trajectory_add_point_trans(
234 SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26,
235 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
236 robot_trajectory_add_final_point_trans(0.50, PLAYGROUND_HEIGHT_M - 0.6, NO_TURN());
239 FSM_TRANSITION(to_container_diag);
240 //FSM_TRANSITION(to_container_ortho);
246 case EV_ACTION_ERROR:
247 case EV_GOAL_NOT_REACHABLE:
248 DBG_PRINT_EVENT("unhandled event");
250 robot.ignore_hokuyo = false;
255 FSM_STATE(to_container_diag)
259 robot_trajectory_new(&tcFast);
260 // face the rim with front of the robot
261 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
262 // face the rim with back of the robot
263 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M-0.6, 0.35);
264 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05));
270 case EV_ACTION_ERROR:
272 case EV_GOAL_NOT_REACHABLE:
273 DBG_PRINT_EVENT("unhandled event");
279 FSM_STATE(to_container_ortho)
283 robot_trajectory_new(&tcFast);
284 robot_trajectory_add_point_trans(
285 SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.15,
286 PLAYGROUND_HEIGHT_M - 0.355);
287 robot_trajectory_add_point_trans(0.55, PLAYGROUND_HEIGHT_M - 0.65);
288 robot_trajectory_add_point_trans(0.90, PLAYGROUND_HEIGHT_M - 0.75);
289 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.60, PLAYGROUND_HEIGHT_M - 0.7);
290 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.35, PLAYGROUND_HEIGHT_M - 0.9);
292 // face the rim with front of the robot
293 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
294 // face the rim with back of the robot
295 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.40, TURN_CCW(DEG2RAD(90)));
301 case EV_ACTION_ERROR:
303 case EV_GOAL_NOT_REACHABLE:
304 DBG_PRINT_EVENT("unhandled event");
311 FSM_STATE(experiment)
316 robot_get_est_pos(&x, &y, &phi);
317 printf("experiment: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
319 struct corn * corn = choose_next_corn();
320 Pos *p = get_corn_approach_position(corn /*&robot.corns->corns[cnt++]*/);
321 corn->was_collected = true;
322 //robot_trajectory_new(&tcFast);
323 //robot_trajectory_add_final_point_trans(robot_trajectory_add_final_point_trans(p->x, p->y, TURN(p->phi));
324 robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
329 FSM_TRANSITION(experiment);
335 case EV_ACTION_ERROR:
336 case EV_GOAL_NOT_REACHABLE:
337 DBG_PRINT_EVENT("unhandled event");
345 /************************************************************************
347 ************************************************************************/
354 if (rv) error(1, errno, "robot_init() returned %d\n", rv);
356 robot.obstacle_avoidance_enabled = true;
358 robot.fsm.main.debug_states = 1;
359 //robot.fsm.motion.debug_states = 1;
360 //robot.fsm.act.debug_states = 1;
362 robot.fsm.main.state = &fsm_state_main_init;
363 //robot.fsm.main.transition_callback = trans_callback;
364 //robot.fsm.motion.transition_callback = move_trans_callback;
367 if (rv) error(1, errno, "robot_start() returned %d\n", rv);
374 /************************************************************************
376 ************************************************************************/
388 case EV_ACTION_ERROR:
390 case EV_GOAL_NOT_REACHABLE:
391 DBG_PRINT_EVENT("unhandled event");