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,
296 robot_set_est_pos_trans(ROBOT_WIDTH_M/2,
297 PLAYGROUND_HEIGHT_M - ROBOT_AXIS_TO_BACK_M,
299 robot.orte.laser_cmd.speed = LASER_DRIVE_ON;
300 ORTEPublicationSend(robot.orte.publication_laser_cmd);
301 robot.orte.laser_cmd.speed = LASER_DRIVE_ON;
302 ORTEPublicationSend(robot.orte.publication_laser_cmd);
303 FSM_TRANSITION(wait_for_start);
306 // FSM_TRANSITION(go_to_container);
307 // FSM_TRANSITION(get_balls);
313 FSM_STATE(wait_for_start)
316 #ifdef WAIT_FOR_START
320 /* start competition timer */
321 pthread_create(&thid, NULL, wait_for_end, NULL);
322 pthread_create(&thid, NULL, wait_to_deposition, NULL);
327 robot.orte.laser_cmd.speed = LASER_DRIVE_ON;
328 ORTEPublicationSend(robot.orte.publication_laser_cmd);
330 /* start to do something */
331 FSM_TRANSITION(decide_where_now);
337 FSM_STATE(decide_where_now)
339 static int cycle = 0;
347 if (robot.carousel_cnt >= BALL_TO_COLLECT ||
348 robot.carousel_cnt >= CAROUSEL_SIZE) {
349 FSM_TRANSITION(go_to_container);
354 FSM_TRANSITION(go_to_our_white_dispenser);
358 FSM_TRANSITION(go_to_our_red_dispenser);
362 FSM_TRANSITION(go_to_container);
366 FSM_TRANSITION(go_to_opponent_white_dispenser);
370 FSM_TRANSITION(go_to_our_white_dispenser);
372 FSM_TRANSITION(go_to_container);
381 /************************************************************************
383 ************************************************************************/
385 FSM_STATE(go_to_our_white_dispenser)
389 robot_trajectory_new(NULL);
390 robot_trajectory_add_point_trans(0.7,
391 PLAYGROUND_HEIGHT_M - 0.65);
392 robot_trajectory_add_final_point_trans(0.25,
393 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
396 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
399 balls_to_collect = get_ball_number();
400 FSM_TRANSITION(get_balls);
406 FSM_STATE(go_to_our_red_dispenser)
410 robot_trajectory_new(NULL);
411 robot_trajectory_add_point_trans(0.7,
412 PLAYGROUND_HEIGHT_M - 0.45);
413 robot_trajectory_add_final_point_trans(0.7,
414 PLAYGROUND_HEIGHT_M - 0.25, NO_TURN());
417 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
420 balls_to_collect = get_ball_number();
421 FSM_TRANSITION(get_balls);
427 FSM_STATE(go_to_opponent_white_dispenser)
431 robot_trajectory_new(NULL);
432 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.7,
433 PLAYGROUND_HEIGHT_M - 0.65);
434 robot_trajectory_add_final_point_trans(
435 PLAYGROUND_WIDTH_M - 0.25,
436 PLAYGROUND_HEIGHT_M - 0.7, NO_TURN());
439 SUBFSM_TRANSITION(try_to_approach_dispenser, NULL);
442 balls_to_collect = get_ball_number();
443 FSM_TRANSITION(get_balls);
449 #define APPROACHING_ATTEMPS 1
451 FSM_STATE(try_to_approach_dispenser)
453 static int approaching_attempts = 0;
454 static unsigned char backward = 1;
458 if (closed_to_dispenser()) {
460 approaching_attempts = 0;
462 robot_move_by(0.1, NO_TURN(), NULL);
467 if (closed_to_dispenser() || (approaching_attempts++ > APPROACHING_ATTEMPS)) {
471 FSM_TRANSITION(try_to_approach_dispenser);
473 robot_move_by(-0.1, NO_TURN(), NULL);
482 #define GO_TO_CONTAINER_TIMER 10000
484 FSM_STATE(go_to_container)
488 robot.obstacle_avoidance_enabled = true;
492 robot_trajectory_new(NULL);
493 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.8, 1.0);
494 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M - 0.6,
495 0.4, TURN(DEG2RAD(90)));
496 /*FSM_TIMER(GO_TO_CONTAINER_TIMER);*/
500 FSM_TRANSITION(go_back_to_edge);
506 FSM_STATE(go_back_to_edge)
510 robot_move_by(-0.4, NO_TURN(), NULL);
514 if (closed_to_container()) {
515 robot.carousel_pos = 0;
516 FSM_TRANSITION(deposite_balls);
518 DBG("FIXME: go_closer_to_container\n");
525 /************************************************************************
526 * BALLS AND CAROUSEL MANIPULATION STATES
527 ************************************************************************/
529 #define MAX_GET_BALL_ATTEMPS 4
530 #define GET_BALL_TIMER 1500
531 #define WAIT_BALL_INSIDE 5000
532 #define GET_BALL_BAGR_SPEED 130
536 static int get_ball_attemps = 0;
537 static int ball_inside = 0;
538 static int last_get = 0;
544 robot_move_by(-0.2, NO_TURN(), NULL);
547 get_ball_attemps = 0;
548 DBG("balls_to_collect = %d\n", balls_to_collect);
550 set_bagr(GET_BALL_BAGR_SPEED);
560 if (robot.carousel_cnt >= balls_to_collect) {
564 next_carousel_position, NULL);
566 (robot.carousel_pos+2) % CAROUSEL_SIZE;
567 } else if (get_ball_attemps++ < MAX_GET_BALL_ATTEMPS) {
568 FSM_TIMER(GET_BALL_TIMER);
570 robot_move_by(-0.2, NO_TURN(), NULL);
574 /* ball is already inside */
579 robot.carousel[robot.carousel_pos % CAROUSEL_SIZE] =
580 (enum ball_color)robot.cmu.color;
581 robot.carousel_cnt++;
582 DBG("collected balls = %d\n", robot.carousel_cnt);
583 FSM_TIMER(WAIT_BALL_INSIDE);
586 FSM_TRANSITION(decide_where_now);
592 #define MAX_CAROUSEL_ATTEMPTS 3
593 #define CAROUSEL_ATTEMPT_TIMER 1000
595 FSM_STATE(next_carousel_position)
597 static int carousel_attempts;
602 carousel_attempts = 0;
603 DBG("carousel_pos = %d\n", robot.carousel_pos);
604 set_carousel(robot.carousel_pos);
605 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
609 /* check out, if the carousel reached position */
611 rv = (robot.drives.carousel_pos == robot.carousel_pos);
612 ROBOT_UNLOCK(drives);
613 if (!rv && carousel_attempts++ < MAX_CAROUSEL_ATTEMPTS) {
614 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
616 DBG("carousel reached the position.\n");
619 DBG("FIXME: carousel lost.\n");
627 #define WAIT_FOR_DEPOSITION_TIMER 1500
629 FSM_STATE(deposite_balls)
633 if (robot.carousel_pos < CAROUSEL_SIZE) {
635 SUBFSM_TRANSITION(next_carousel_position, NULL);
637 robot.carousel_cnt = 0;
638 robot.carousel_pos = 0;
639 DBG("carousel_cnt = %d\n", robot.carousel_cnt);
640 FSM_TRANSITION(decide_where_now);
645 FSM_TIMER(WAIT_FOR_DEPOSITION_TIMER);
648 robot.carousel_pos++;
650 FSM_TRANSITION(deposite_balls);
659 /* robot initialization */
662 robot.carousel_cnt = 0;
663 robot.carousel_pos = 0;
665 FSM(MAIN)->debug_states = 1;
666 FSM(MOTION)->debug_states = 1;
668 robot.fsm[FSM_ID_MAIN].state = &fsm_state_main_init;
669 robot.fsm[FSM_ID_MAIN].transition_callback = trans_callback;
670 robot.fsm[FSM_ID_MOTION].transition_callback = move_trans_callback;
672 /* start threads and wait */