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 */
29 /* define in CFLAGS */
30 // #define COMPETITION
33 #define WAIT_FOR_START
34 #define COMPETITION_TIME_DEFAULT 90
35 #define TIME_TO_DEPOSITE_DEFAULT 60
38 #define COMPETITION_TIME_DEFAULT 900
39 #define TIME_TO_DEPOSITE_DEFAULT 60
42 /* competition time in seconds */
43 #define COMPETITION_TIME COMPETITION_TIME_DEFAULT
44 #define TIME_TO_DEPOSITE TIME_TO_DEPOSITE_DEFAULT
45 /* competition time in seconds */
53 int balls_to_collect = 0;
55 /************************************************************************
57 ************************************************************************/
60 * Convert back sharps' measured values to mm.
64 void get_back_sharp_mm(int *sharp)
67 sharp[LEFT] = (int)(robot.sharps.back_left)*1000;
68 sharp[RIGHT] = (int)(robot.sharps.back_right)*1000;
73 * Convert rear sharps' measured values to mm.
77 void get_rear_sharp_mm(int *sharp)
80 sharp[LEFT] = (int)(robot.sharps.left)*1000;
81 sharp[RIGHT] = (int)(robot.sharps.right)*1000;
86 * Convert front sharps' measured values to mm.
90 void get_front_sharp_mm(int *sharp)
93 sharp[LEFT] = (int)(robot.sharps.front_left)*1000;
94 sharp[RIGHT] = (int)(robot.sharps.front_right)*1000;
99 * Get values from front sharps.
103 void get_front_sharp_m(double *sharp)
106 sharp[LEFT] = robot.sharps.front_left;
107 sharp[RIGHT] = robot.sharps.front_right;
108 ROBOT_UNLOCK(sharps);
112 * Get values from rear sharps.
116 void get_rear_sharp_m(double *sharp)
119 sharp[LEFT] = robot.sharps.left;
120 sharp[RIGHT] = robot.sharps.right;
121 ROBOT_UNLOCK(sharps);
125 * Get values from back sharps.
129 void get_back_sharp_m(double *sharp)
132 sharp[LEFT] = robot.sharps.back_left;
133 sharp[RIGHT] = robot.sharps.back_right;
134 ROBOT_UNLOCK(sharps);
138 * Use bumpers check if we are closed to the dispenser
140 int closed_to_dispenser()
145 rv = robot.bumper.left | robot.bumper.right;
146 ROBOT_UNLOCK(bumper);
151 int closed_to_container()
156 rv = ((robot.sharps.back_left < 0.05)
157 && (robot.sharps.back_right < 0.05));
158 ROBOT_UNLOCK(sharps);
164 int get_ball_number()
166 static int cycle = 0;
189 * Competition timer. Stop robot when the timer exceeds.
192 void *wait_for_end(void *arg)
194 sleep(COMPETITION_TIME);
195 printf("%d seconds timer exceeded! exiting!\n", COMPETITION_TIME);
201 * Timer to go to tray.
204 void *wait_to_deposition(void *arg)
206 sleep(TIME_TO_DEPOSITE);
207 FSM_SIGNAL(MAIN, EV_SHORT_TIME_TO_END, NULL);
212 * Get position of the point when we know the distance and angle to turn.
214 * @param act actual position
215 * @param pos countered position
217 void get_relative_pos(struct est_pos_type *est, struct ref_pos_type *ref,
218 double l, double phi)
220 ref->x = est->x + l*cos(est->phi + phi);
221 ref->y = est->y + l*sin(est->phi + phi);
222 ref->phi = est->phi + phi;
225 void robot_goto_point(struct ref_pos_type des_pos)
227 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
230 robot_trajectory_new(&tc);
231 robot_trajectory_add_final_point_trans(des_pos.x, des_pos.y, NO_TURN());
234 void robot_go_backward_to_point(struct ref_pos_type des_pos)
236 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
239 robot_trajectory_new_backward(&tc);
240 robot_trajectory_add_final_point_notrans(des_pos.x, des_pos.y, NO_TURN());
243 void trans_callback(struct robo_fsm *fsm)
245 if(fsm->state_name!=NULL)
246 strncpy(robot.act_fsm_state_name, fsm->state_name, FSM_STATE_NAME_MAX_LEN);
250 void move_trans_callback(struct robo_fsm *fsm)
252 if(fsm->state_name!=NULL)
253 strncpy(robot.move_fsm_state_name, fsm->state_name, FSM_STATE_NAME_MAX_LEN);
256 /************************************************************************
257 * FSM STATES DECLARATION
258 ************************************************************************/
260 /* initial and starting states */
261 FSM_STATE_DECL(init);
262 FSM_STATE_DECL(wait_for_start);
263 FSM_STATE_DECL(decide_where_now);
264 /* movement states */
265 FSM_STATE_DECL(go_to_our_white_dispenser);
266 FSM_STATE_DECL(go_to_our_red_dispenser);
267 FSM_STATE_DECL(go_to_opponent_white_dispenser);
268 FSM_STATE_DECL(try_to_approach_dispenser);
269 FSM_STATE_DECL(go_to_container);
270 FSM_STATE_DECL(go_back_to_edge);
271 /* ball and carousel states */
272 FSM_STATE_DECL(get_balls);
273 FSM_STATE_DECL(next_carousel_position);
274 FSM_STATE_DECL(deposite_balls);
276 /************************************************************************
277 * INITIAL AND STARTING STATES
278 ************************************************************************/
282 /* start event ocurred */
285 robot_set_est_pos_trans(ROBOT_WIDTH_M/2,
286 PLAYGROUND_HEIGHT_M - ROBOT_AXIS_TO_BACK_M,
293 FSM_TRANSITION(wait_for_start);
294 // FSM_TRANSITION(go_to_container);
295 // FSM_TRANSITION(get_balls);
301 FSM_STATE(wait_for_start)
304 #ifdef WAIT_FOR_START
308 /* start competition timer */
309 pthread_create(&thid, NULL, wait_for_end, NULL);
310 pthread_create(&thid, NULL, wait_to_deposition, NULL);
315 robot.orte.laser_cmd.speed = LASER_DRIVE_ON;
316 ORTEPublicationSend(robot.orte.publication_laser_cmd);
318 /* start to do something */
319 FSM_TRANSITION(decide_where_now);
325 FSM_STATE(decide_where_now)
327 static int cycle = 0;
335 if (robot.carousel_cnt >= BALL_TO_COLLECT ||
336 robot.carousel_cnt >= CAROUSEL_SIZE) {
337 FSM_TRANSITION(go_to_container);
342 FSM_TRANSITION(go_to_our_white_dispenser);
346 FSM_TRANSITION(go_to_our_red_dispenser);
350 FSM_TRANSITION(go_to_container);
354 FSM_TRANSITION(go_to_opponent_white_dispenser);
358 FSM_TRANSITION(go_to_our_white_dispenser);
360 FSM_TRANSITION(go_to_container);
369 /************************************************************************
371 ************************************************************************/
373 FSM_STATE(go_to_our_white_dispenser)
377 robot_trajectory_new(NULL);
378 robot_trajectory_add_point_trans(0.7,
379 PLAYGROUND_HEIGHT_M - 0.65);
380 robot_trajectory_add_final_point_trans(0.25,
381 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
384 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
387 balls_to_collect = get_ball_number();
388 FSM_TRANSITION(get_balls);
394 FSM_STATE(go_to_our_red_dispenser)
398 robot_trajectory_new(NULL);
399 robot_trajectory_add_point_trans(0.7,
400 PLAYGROUND_HEIGHT_M - 0.45);
401 robot_trajectory_add_final_point_trans(0.7,
402 PLAYGROUND_HEIGHT_M - 0.25, NO_TURN());
405 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
408 balls_to_collect = get_ball_number();
409 FSM_TRANSITION(get_balls);
415 FSM_STATE(go_to_opponent_white_dispenser)
419 robot_trajectory_new(NULL);
420 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.7,
421 PLAYGROUND_HEIGHT_M - 0.65);
422 robot_trajectory_add_final_point_trans(
423 PLAYGROUND_WIDTH_M - 0.25,
424 PLAYGROUND_HEIGHT_M - 0.7, NO_TURN());
427 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
430 balls_to_collect = get_ball_number();
431 FSM_TRANSITION(get_balls);
437 #define APPROACHING_ATTEMPS 1
439 FSM_STATE(try_to_approach_dispenser)
441 static int approaching_attempts = 0;
442 static unsigned char backward = 1;
446 if (closed_to_dispenser()) {
448 approaching_attempts = 0;
450 robot_move_by(0.1, NO_TURN(), NULL);
455 if (closed_to_dispenser() || (approaching_attempts++ > APPROACHING_ATTEMPS)) {
459 FSM_TRANSITION(try_to_approach_dispenser);
461 robot_move_by(-0.1, NO_TURN(), NULL);
470 #define GO_TO_CONTAINER_TIMER 10000
472 FSM_STATE(go_to_container)
479 robot_trajectory_new(NULL);
480 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.6, 1.0);
481 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M - 0.4,
483 /*FSM_TIMER(GO_TO_CONTAINER_TIMER);*/
487 FSM_TRANSITION(go_back_to_edge);
493 FSM_STATE(go_back_to_edge)
497 robot_move_by(-0.4, NO_TURN(), NULL);
501 if (closed_to_container()) {
502 robot.carousel_pos = 0;
503 FSM_TRANSITION(deposite_balls);
505 DBG("FIXME: go_closer_to_container\n");
512 /************************************************************************
513 * BALLS AND CAROUSEL MANIPULATION STATES
514 ************************************************************************/
516 #define MAX_GET_BALL_ATTEMPS 4
517 #define GET_BALL_TIMER 1500
518 #define WAIT_BALL_INSIDE 5000
519 #define GET_BALL_BAGR_SPEED 120
523 static int get_ball_attemps = 0;
524 static int ball_inside = 0;
525 static int last_get = 0;
531 robot_move_by(-0.2, NO_TURN(), NULL);
534 get_ball_attemps = 0;
535 DBG("balls_to_collect = %d\n", balls_to_collect);
537 set_bagr(GET_BALL_BAGR_SPEED);
547 if (robot.carousel_cnt >= balls_to_collect) {
551 next_carousel_position, NULL);
553 (robot.carousel_pos+2) % CAROUSEL_SIZE;
554 } else if (get_ball_attemps++ < MAX_GET_BALL_ATTEMPS) {
555 FSM_TIMER(GET_BALL_TIMER);
557 robot_move_by(-0.2, NO_TURN(), NULL);
561 /* ball is already inside */
566 robot.carousel[robot.carousel_pos % CAROUSEL_SIZE] =
567 (enum ball_color)robot.cmu.color;
568 robot.carousel_cnt++;
569 DBG("collected balls = %d\n", robot.carousel_cnt);
570 FSM_TIMER(WAIT_BALL_INSIDE);
573 FSM_TRANSITION(decide_where_now);
579 #define MAX_CAROUSEL_ATTEMPTS 3
580 #define CAROUSEL_ATTEMPT_TIMER 1000
582 FSM_STATE(next_carousel_position)
584 static int carousel_attempts;
589 carousel_attempts = 0;
590 DBG("carousel_pos = %d\n", robot.carousel_pos);
591 set_carousel(robot.carousel_pos);
592 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
596 /* check out, if the carousel reached position */
598 rv = (robot.drives.carousel_pos == robot.carousel_pos);
599 ROBOT_UNLOCK(drives);
600 if (!rv && carousel_attempts++ < MAX_CAROUSEL_ATTEMPTS) {
601 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
603 DBG("carousel reached the position.\n");
606 DBG("FIXME: carousel lost.\n");
614 #define WAIT_FOR_DEPOSITION_TIMER 1500
616 FSM_STATE(deposite_balls)
620 if (robot.carousel_pos < CAROUSEL_SIZE) {
622 SUBFSM_TRANSITION(next_carousel_position, NULL);
624 robot.carousel_cnt = 0;
625 robot.carousel_pos = 0;
626 DBG("carousel_cnt = %d\n", robot.carousel_cnt);
627 FSM_TRANSITION(decide_where_now);
632 FSM_TIMER(WAIT_FOR_DEPOSITION_TIMER);
635 robot.carousel_pos++;
637 FSM_TRANSITION(deposite_balls);
646 /* robot initialization */
649 robot.carousel_cnt = 0;
650 robot.carousel_pos = 0;
652 FSM(MAIN)->debug_states = 1;
654 robot.fsm[FSM_ID_MAIN].state = &fsm_state_main_init;
655 robot.fsm[FSM_ID_MAIN].transition_callback = trans_callback;
656 robot.fsm[FSM_ID_MOTION].transition_callback = move_trans_callback;
658 /* start threads and wait */