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 void trans_callback(struct robo_fsm *fsm)
218 if(fsm->state_name!=NULL)
219 strncpy(robot.act_fsm_state_name, fsm->state_name, FSM_STATE_NAME_MAX_LEN);
222 /************************************************************************
223 * FSM STATES DECLARATION
224 ************************************************************************/
226 /* initial and starting states */
227 FSM_STATE_DECL(init);
228 FSM_STATE_DECL(wait_for_start);
229 /* movement states */
230 FSM_STATE_DECL(go_to_our_white_dispenser);
231 FSM_STATE_DECL(go_to_our_white_dispenser2);
232 FSM_STATE_DECL(go_far_from_dispenser);
233 FSM_STATE_DECL(go_from_our_dispenser);
234 FSM_STATE_DECL(go_to_container);
235 /* ball and carousel states */
236 FSM_STATE_DECL(get_balls);
237 FSM_STATE_DECL(next_carousel_position);
238 FSM_STATE_DECL(deposite_balls);
240 /************************************************************************
241 * INITIAL AND STARTING STATES
242 ************************************************************************/
246 /* start event ocurred */
249 robot_set_est_pos_trans(ROBOT_WIDTH_M/2,
250 PLAYGROUND_HEIGHT_M - ROBOT_AXIS_TO_BACK_M,
252 // robot_set_est_pos_trans(0.15, PLAYGROUND_HEIGHT_M - 0.7, DEG2RAD(180));
258 FSM_TRANSITION(wait_for_start);
259 // FSM_TRANSITION(go_to_container);
260 // FSM_TRANSITION(get_balls);
261 /* FIXME: delete after the test */
262 // robot.carousel_cnt = 5;
263 // robot.carousel_pos = 0;
264 // FSM_TRANSITION(deposite_balls);
270 FSM_STATE(wait_for_start)
273 #ifdef WAIT_FOR_START
277 /* start competition timer */
278 pthread_create(&thid, NULL, wait_for_end, NULL);
279 pthread_create(&thid, NULL, wait_to_deposition, NULL);
284 /* start to do something */
285 FSM_TRANSITION(go_to_our_white_dispenser);
291 /************************************************************************
293 ************************************************************************/
295 FSM_STATE(go_to_our_white_dispenser)
299 robot_trajectory_new_backward(NULL);
300 robot_trajectory_add_final_point_trans(0.7,
301 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
304 FSM_TRANSITION(go_to_our_white_dispenser2);
310 FSM_STATE(go_to_our_white_dispenser2)
312 struct ref_pos_type des_pos = {0.20, PLAYGROUND_HEIGHT_M - 0.65, 0};
313 static int approaching_attempts = 0;
317 robot_goto_point(des_pos);
320 if (closed_to_dispenser() || approaching_attempts++ < 2) {
321 FSM_TRANSITION(go_from_our_dispenser);
323 FSM_TRANSITION(get_balls);
324 approaching_attempts = 0;
331 FSM_STATE(go_to_our_red_dispenser)
335 robot_trajectory_new_backward(NULL);
336 robot_trajectory_add_final_point_trans(0.7,
337 PLAYGROUND_HEIGHT_M - 0.65, NO_TURN());
340 FSM_TRANSITION(go_to_our_white_dispenser2);
346 FSM_STATE(go_to_our_red_dispenser2)
348 struct ref_pos_type des_pos = {0.65, PLAYGROUND_HEIGHT_M - 0.20, 0};
349 static int approaching_attempts = 0;
353 robot_goto_point(des_pos);
356 if (closed_to_dispenser() || approaching_attempts++ < 2) {
357 FSM_TRANSITION(go_from_our_dispenser);
359 FSM_TRANSITION(get_balls);
360 approaching_attempts = 0;
367 FSM_STATE(go_far_from_dispenser)
369 struct ref_pos_type des_pos = { 0.12, PLAYGROUND_HEIGHT_M - 0.65, 0};
373 robot_goto_point(des_pos);
376 FSM_TRANSITION(get_balls);
382 FSM_STATE(go_from_our_dispenser)
386 /* FIXME: go backward by */
389 FSM_TRANSITION(go_to_our_white_dispenser2);
395 #define GO_TO_CONTAINER_TIMER 10000
397 FSM_STATE(go_to_container)
404 robot_trajectory_new_backward(NULL);
405 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M - 0.6, 1.0);
406 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M - 0.4,
408 FSM_TIMER(GO_TO_CONTAINER_TIMER);
412 if (closed_to_container()) {
413 robot.carousel_pos = 0;
414 FSM_TRANSITION(deposite_balls);
416 DBG("FIXME: go_closer_to_container\n");
423 /************************************************************************
424 * BALLS AND CAROUSEL MANIPULATION STATES
425 ************************************************************************/
427 #define MAX_GET_BALL_ATTEMPS 5
428 #define GET_BALL_TIMER 1500
429 #define WAIT_BALL_INSIDE 5000
430 #define GET_BALL_BAGR_SPEED 120
434 static int get_ball_attemps = 0;
435 static int ball_inside = 0;
441 set_bagr(GET_BALL_BAGR_SPEED);
450 if (robot.carousel_cnt >= BALL_TO_COLLECT) {
451 /* FIXME: just to test deposition */
452 robot.carousel_pos = 0;
453 FSM_TRANSITION(deposite_balls);
454 // printf("go_to_container\n");
455 // FSM_TRANSITION(go_to_container);
458 (robot.carousel_pos+2) % CAROUSEL_SIZE;
460 next_carousel_position, NULL);
462 } else if (get_ball_attemps++ < MAX_GET_BALL_ATTEMPS) {
463 FSM_TIMER(GET_BALL_TIMER);
466 DBG("go_to_container\n");
467 // FSM_TRANSITION(go_to_container);
471 /* ball is already inside */
476 robot.carousel[robot.carousel_pos % CAROUSEL_SIZE] =
477 (enum ball_color)robot.cmu.color;
478 robot.carousel_cnt++;
479 DBG("ball cnt=%d\n", robot.carousel_cnt);
480 FSM_TIMER(WAIT_BALL_INSIDE);
486 #define MAX_CAROUSEL_ATTEMPTS 3
487 #define CAROUSEL_ATTEMPT_TIMER 1000
489 FSM_STATE(next_carousel_position)
491 static int carousel_attempts;
496 carousel_attempts = 0;
497 DBG("carousel_pos = %d\n", robot.carousel_pos);
498 set_carousel(robot.carousel_pos);
499 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
503 /* check out, if the carousel reached position */
505 rv = (robot.drives.carousel_pos == robot.carousel_pos);
506 ROBOT_UNLOCK(drives);
507 if (!rv && carousel_attempts++ < MAX_CAROUSEL_ATTEMPTS) {
508 FSM_TIMER(CAROUSEL_ATTEMPT_TIMER);
510 DBG("carousel reached the position.\n");
513 DBG("FIXME: carousel lost.\n");
521 #define WAIT_FOR_DEPOSITION_TIMER 1500
523 FSM_STATE(deposite_balls)
527 if (robot.carousel_pos < CAROUSEL_SIZE) {
529 SUBFSM_TRANSITION(next_carousel_position, NULL);
531 robot.carousel_cnt = 0;
533 DBG("go_to_our_white_dispenser\n");
534 // FSM_TRANSITION(go_to_our_white_dispenser);
538 DBG("open_back_door\n");
540 FSM_TIMER(WAIT_FOR_DEPOSITION_TIMER);
543 robot.carousel_cnt--;
544 robot.carousel_pos++;
546 DBG("carousel_cnt = %d\n", robot.carousel_cnt);
547 FSM_TRANSITION(deposite_balls);
556 /* robot initialization */
559 robot.carousel_cnt = 0;
560 robot.carousel_pos = 0;
562 FSM(MAIN)->debug_states = 1;
564 robot.fsm[FSM_ID_MAIN].state = &fsm_state_main_init;
565 robot.fsm[FSM_ID_MAIN].transition_callback = trans_callback;
567 /* start threads and wait */