4 * Robot's main control program (Eurobot 2008).
6 * Copyright: (c) 2008 CTU Dragons
7 * CTU FEE - Department of Control Engineering
17 #include <robot_eb2008.h>
20 #include <servos_eb2008.h>
23 #include <movehelper_eb2008.h>
28 /* define this macro */
32 #define WAIT_FOR_START
33 #define COMPETITION_TIME_DEFAULT 90
34 #define TIME_TO_DEPOSITE_DEFAULT 60
37 #define COMPETITION_TIME_DEFAULT 900
38 #define TIME_TO_DEPOSITE_DEFAULT 60
41 /* competition time in seconds */
42 #define COMPETITION_TIME COMPETITION_TIME_DEFAULT
43 #define TIME_TO_DEPOSITE TIME_TO_DEPOSITE_DEFAULT
44 /* competition time in seconds */
52 /************************************************************************
54 ************************************************************************/
57 * Convert back sharps' measured values to mm.
61 void get_back_sharp_mm(int *sharp)
64 sharp[LEFT] = (int)(robot.sharps.back_left)*1000;
65 sharp[RIGHT] = (int)(robot.sharps.back_right)*1000;
70 * Convert rear sharps' measured values to mm.
74 void get_rear_sharp_mm(int *sharp)
77 sharp[LEFT] = (int)(robot.sharps.left)*1000;
78 sharp[RIGHT] = (int)(robot.sharps.right)*1000;
83 * Convert front sharps' measured values to mm.
87 void get_front_sharp_mm(int *sharp)
90 sharp[LEFT] = (int)(robot.sharps.front_left)*1000;
91 sharp[RIGHT] = (int)(robot.sharps.front_right)*1000;
96 * Get values from front sharps.
100 void get_front_sharp_m(double *sharp)
103 sharp[LEFT] = robot.sharps.front_left;
104 sharp[RIGHT] = robot.sharps.front_right;
105 ROBOT_UNLOCK(sharps);
109 * Get values from rear sharps.
113 void get_rear_sharp_m(double *sharp)
116 sharp[LEFT] = robot.sharps.left;
117 sharp[RIGHT] = robot.sharps.right;
118 ROBOT_UNLOCK(sharps);
122 * Get values from back sharps.
126 void get_back_sharp_m(double *sharp)
129 sharp[LEFT] = robot.sharps.back_left;
130 sharp[RIGHT] = robot.sharps.back_right;
131 ROBOT_UNLOCK(sharps);
135 * Use bumpers check if we are closed to the dispenser
137 int closed_to_dispenser()
142 rv = robot.bumper.left | robot.bumper.right;
143 ROBOT_UNLOCK(bumper);
148 int closed_to_container()
153 rv = ((robot.sharps.back_left < 0.05)
154 && (robot.sharps.back_right < 0.05));
155 ROBOT_UNLOCK(sharps);
162 * Competition timer. Stop robot when the timer exceeds.
165 void *wait_for_end(void *arg)
167 sleep(COMPETITION_TIME);
168 printf("%d seconds timer exceeded! exiting!\n", COMPETITION_TIME);
174 * Timer to go to tray.
177 void *wait_to_deposition(void *arg)
179 sleep(TIME_TO_DEPOSITE);
180 FSM_SIGNAL(MAIN, EV_SHORT_TIME_TO_END, NULL);
185 * Get position of the point when we know the distance and angle to turn.
187 * @param act actual position
188 * @param pos countered position
190 void get_relative_pos(struct est_pos_type *est, struct ref_pos_type *ref,
191 double l, double phi)
193 ref->x = est->x + l*cos(est->phi + phi);
194 ref->y = est->y + l*sin(est->phi + phi);
195 ref->phi = est->phi + phi;
198 void robot_goto_point(struct ref_pos_type des_pos)
200 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
203 robot_trajectory_new(&tc);
204 robot_trajectory_add_final_point_trans(des_pos.x, des_pos.y, NO_TURN());
207 void robot_go_backward_to_point(struct ref_pos_type des_pos)
209 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
212 robot_trajectory_new_backward(&tc);
213 robot_trajectory_add_final_point_notrans(des_pos.x, des_pos.y, NO_TURN());
216 /************************************************************************
217 * FSM STATES DECLARATION
218 ************************************************************************/
220 /* initial and starting states */
221 FSM_STATE_DECL(init);
222 FSM_STATE_DECL(wait_for_start);
223 /* movement states */
224 FSM_STATE_DECL(go_to_our_white_dispenser);
225 FSM_STATE_DECL(go_to_our_white_dispenser2);
226 FSM_STATE_DECL(go_far_from_dispenser);
227 FSM_STATE_DECL(go_from_our_dispenser);
228 FSM_STATE_DECL(go_to_container);
229 /* ball and carousel states */
230 FSM_STATE_DECL(get_balls);
231 FSM_STATE_DECL(next_carousel_position);
232 FSM_STATE_DECL(deposite_balls);
234 /************************************************************************
235 * INITIAL AND STARTING STATES
236 ************************************************************************/
240 /* start event ocurred */
243 robot_set_est_pos_trans(ROBOT_WIDTH_M/2,
244 PLAYGROUND_HEIGHT_M - ROBOT_AXIS_TO_BACK_M,
246 // robot_set_est_pos_trans(0.15, PLAYGROUND_HEIGHT_M - 0.7, DEG2RAD(180));
252 FSM_TRANSITION(wait_for_start);
253 // FSM_TRANSITION(go_to_container);
254 // FSM_TRANSITION(get_balls);
255 /* FIXME: delete after the test */
256 // robot.carousel_cnt = 5;
257 // robot.carousel_pos = 0;
258 // FSM_TRANSITION(deposite_balls);
264 FSM_STATE(wait_for_start)
267 #ifdef WAIT_FOR_START
271 /* start competition timer */
272 pthread_create(&thid, NULL, wait_for_end, NULL);
273 pthread_create(&thid, NULL, wait_to_deposition, NULL);
278 /* start to do something */
279 FSM_TRANSITION(go_to_our_white_dispenser);
285 /************************************************************************
287 ************************************************************************/
289 FSM_STATE(go_to_our_white_dispenser)
293 robot_trajectory_new_backward(NULL);
294 robot_trajectory_add_final_point_trans(0.7,
295 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
298 FSM_TRANSITION(go_to_our_white_dispenser2);
304 FSM_STATE(go_to_our_white_dispenser2)
306 struct ref_pos_type des_pos = {0.20, PLAYGROUND_HEIGHT_M - 0.65, 0};
307 static int approaching_attempts = 0;
311 robot_goto_point(des_pos);
314 if (closed_to_dispenser() || approaching_attempts++ < 2) {
315 FSM_TRANSITION(go_from_our_dispenser);
317 FSM_TRANSITION(get_balls);
318 approaching_attempts = 0;
325 FSM_STATE(go_to_our_red_dispenser)
329 robot_trajectory_new_backward(NULL);
330 robot_trajectory_add_final_point_trans(0.7,
331 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
334 FSM_TRANSITION(go_to_our_white_dispenser2);
340 FSM_STATE(go_to_our_red_dispenser2)
342 struct ref_pos_type des_pos = {0.65, PLAYGROUND_HEIGHT_M - 0.20, 0};
343 static int approaching_attempts = 0;
347 robot_goto_point(des_pos);
350 if (closed_to_dispenser() || approaching_attempts++ < 2) {
351 FSM_TRANSITION(go_from_our_dispenser);
353 FSM_TRANSITION(get_balls);
354 approaching_attempts = 0;
361 FSM_STATE(go_far_from_dispenser)
363 struct ref_pos_type des_pos = { 0.12, PLAYGROUND_HEIGHT_M - 0.65, 0};
367 robot_goto_point(des_pos);
370 FSM_TRANSITION(get_balls);
376 FSM_STATE(go_from_our_dispenser)
380 /* FIXME: go backward by */
383 FSM_TRANSITION(go_to_our_white_dispenser2);
389 #define GO_TO_CONTAINER_TIMER 10000
391 FSM_STATE(go_to_container)
398 robot_trajectory_new_backward(NULL);
399 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.6, 1.0);
400 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M - 0.4,
402 FSM_TIMER(GO_TO_CONTAINER_TIMER);
406 if (closed_to_container()) {
407 robot.carousel_pos = 0;
408 FSM_TRANSITION(deposite_balls);
410 DBG("FIXME: go_closer_to_container\n");
417 /************************************************************************
418 * BALLS AND CAROUSEL MANIPULATION STATES
419 ************************************************************************/
421 #define MAX_GET_BALL_ATTEMPS 5
422 #define GET_BALL_TIMER 1500
423 #define WAIT_BALL_INSIDE 5000
424 #define GET_BALL_BAGR_SPEED 120
428 static int get_ball_attemps = 0;
429 static int ball_inside = 0;
435 set_bagr(GET_BALL_BAGR_SPEED);
444 if (robot.carousel_cnt >= BALL_TO_COLLECT) {
445 /* FIXME: just to test deposition */
446 robot.carousel_pos = 0;
447 FSM_TRANSITION(deposite_balls);
448 // printf("go_to_container\n");
449 // FSM_TRANSITION(go_to_container);
452 (robot.carousel_pos+2) % CAROUSEL_SIZE;
454 next_carousel_position, NULL);
456 } else if (get_ball_attemps++ < MAX_GET_BALL_ATTEMPS) {
457 FSM_TIMER(GET_BALL_TIMER);
460 DBG("go_to_container\n");
461 // FSM_TRANSITION(go_to_container);
465 /* ball is already inside */
470 robot.carousel[robot.carousel_pos % CAROUSEL_SIZE] =
471 (enum ball_color)robot.cmu.color;
472 robot.carousel_cnt++;
473 DBG("ball cnt=%d\n", robot.carousel_cnt);
474 FSM_TIMER(WAIT_BALL_INSIDE);
480 #define MAX_CAROUSEL_ATTEMPTS 3
481 #define CAROUSEL_ATTEMPT_TIMER 1000
483 FSM_STATE(next_carousel_position)
485 static int carousel_attempts;
490 carousel_attempts = 0;
491 DBG("carousel_pos = %d\n", robot.carousel_pos);
492 set_carousel(robot.carousel_pos);
493 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
497 /* check out, if the carousel reached position */
499 rv = (robot.drives.carousel_pos == robot.carousel_pos);
500 ROBOT_UNLOCK(drives);
501 if (!rv && carousel_attempts++ < MAX_CAROUSEL_ATTEMPTS) {
502 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
504 DBG("carousel reached the position.\n");
507 DBG("FIXME: carousel lost.\n");
515 #define WAIT_FOR_DEPOSITION_TIMER 1500
517 FSM_STATE(deposite_balls)
521 if (robot.carousel_pos < CAROUSEL_SIZE) {
523 SUBFSM_TRANSITION(next_carousel_position, NULL);
525 robot.carousel_cnt = 0;
527 DBG("go_to_our_white_dispenser\n");
528 // FSM_TRANSITION(go_to_our_white_dispenser);
532 DBG("open_back_door\n");
534 FSM_TIMER(WAIT_FOR_DEPOSITION_TIMER);
537 robot.carousel_cnt--;
538 robot.carousel_pos++;
540 DBG("carousel_cnt = %d\n", robot.carousel_cnt);
541 FSM_TRANSITION(deposite_balls);
550 /* robot initialization */
553 robot.carousel_cnt = 0;
554 robot.carousel_pos = 0;
556 FSM(MAIN)->debug_states = 1;
558 robot.fsm[FSM_ID_MAIN].state = &fsm_state_main_init;
560 /* start threads and wait */