7 #include <movehelper.h>
14 #include "actuators.h"
17 #include "match-timing.h"
21 UL_LOG_CUST(ulogd_common_states); /* Log domain name = ulogd + name of the file */
23 #include "common-states.h"
25 /************************************************************************
26 * Functions used in and called from all the (almost identical)
27 * "wait for start" states in particular strategies.
28 ************************************************************************/
31 #define DBG_FSM_STATE(name) do { if (fsm->debug_states) ul_loginf("fsm %s %.1f: %s(%s)\n", \
32 fsm->debug_name, robot_current_time(), \
33 name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
36 void set_initial_position()
38 robot_set_est_pos_trans(ROBOT_START_X_M,
40 DEG2RAD(ROBOT_START_ANGLE_DEG));
47 bool pokus = robot.fsm.motion.state_name == "wait_and_try_again";
48 // drive lift to home position
50 // unset the homing request
57 robot.check_turn_safety = false;
58 pthread_create(&thid, NULL, timing_thread, NULL);
62 // We set initial position periodically in order for it to be updated
63 // on the display if the team color is changed during waiting for
67 set_initial_position();
68 if (robot.start_state == START_PLUGGED_IN)
74 sem_post(&robot.start);
76 set_initial_position();
86 int sharp_data = robot.orte.jaws_status.act_pos.left;
88 sharp_dist = s_gp2y0a21_ir2mmLong(sharp_data);
89 printf("sharp data: %dmm\n", sharp_dist);
90 return (sharp_dist <= 150 ? true : false);
93 void inline enable_bumpers(bool enabled)
95 robot.use_left_bumper = enabled;
96 robot.use_right_bumper = enabled;
97 robot.use_back_bumpers = enabled;
100 void enable_my_square_walls(bool enabled)
102 for (int i = 0; i < 15; i++) {
103 if (robot.team_color == RED)
104 ShmapSetCircleFlag(red_sq[i].x, red_sq[i].y, 0.2, enabled*MAP_FLAG_WALL, (!enabled)*MAP_FLAG_WALL);
106 ShmapSetCircleFlag(blue_sq[i].x, blue_sq[i].y, 0.2,enabled*MAP_FLAG_WALL, (!enabled)*MAP_FLAG_WALL);
110 /************************************************************************
111 * Trajectory constraints used; They are initialized in the main() function in competition.cc
112 ************************************************************************/
114 struct TrajectoryConstraints tcFast, tcVeryFast, tcSlow, tcVerySlow;
117 bool bonus_placed = false;
119 const double load_dist = 0.04; // TODO distance from side when loading green figures
120 const double app_dist = 0.04; // TODO distance from green figures when approach
122 /* fields with numbers of opponent squares, we have five variations of movement style across opp. squares*/
123 const int move_around[][8] = {{12, 14, 11, 13, 11, 8, 6, 9},
124 {6, 8, 11, 13, 11, 14, 12, 9},
125 {11, 13, 11, 8, 11, 14, 12, 9},
126 {6, 8, 11, 13, 11, 14, 12, 9},
127 {11, 8, 11, 13, 11, 14, 12, 9}};
129 /** generate "random" positions on oponent squares and goto this position */
130 FSM_STATE(move_around)
132 static int next_sq = 0;
133 static int max_wait = 0;
134 // choose randomly one of five move_around strategies
135 static int strategy = rand() % 5;
137 double goal_x, goal_y;
138 static bool entry = true;
144 robot.use_left_bumper = true;
145 robot.use_right_bumper = true;
146 robot.use_back_bumpers = true;
147 robot.ignore_hokuyo = false;
151 enable_my_square_walls(true);
153 srand((int)(robot_current_time()*10000));
155 // save next square number where to go to
156 goal = move_around[strategy][next_sq];
158 // pick position on opponent squares with goal index
159 if (robot.team_color == RED) {
160 goal_x = blue_sq[goal].x;
161 goal_y = blue_sq[goal].y;
163 goal_x = red_sq[goal].x;
164 goal_y = red_sq[goal].y;
167 robot_goto_notrans(goal_x, goal_y, NO_TURN(), &tcFast);
168 printf("strategy: %d, next_sq: %d, goal: %d\n",strategy, next_sq, goal);
169 next_sq = (next_sq + 1 < sizeof(move_around[strategy]) ? next_sq + 1 : next_sq);
173 // every two seconds check if short time to end
175 if (robot.short_time_to_end == true) {
176 // if short time to end - send stop signal to motion FSM and return to main FSM
177 FSM_SIGNAL(MOTION, EV_MOVE_STOP, NULL);
179 printf("return from move around state\n");
180 } else if (robot.fsm.motion.state_name == "wait_and_try_again") {
181 // if goal position unawailable (obstacle) try max. X-times
182 if (++max_wait >= 3) {
183 // if goal not awailable, send stop signal to motion FSM and generate new target trom move_around
184 printf("go to next square!\n");
186 next_sq = (next_sq + 1 < sizeof(move_around[strategy]) ? next_sq + 1 : next_sq);
187 FSM_SIGNAL(MOTION, EV_MOVE_STOP, NULL);
188 FSM_TRANSITION(move_around);
190 printf("waiting for opponent! %d\n", max_wait);
196 if (robot.short_time_to_end == true)
199 FSM_TRANSITION(move_around);
201 case EV_MOTION_ERROR:
207 /** pick figure from opponent bonus square */
208 FSM_STATE(approach_opp_bonus_figure)
212 robot.use_left_bumper = true;
213 robot.use_right_bumper = true;
214 robot.use_back_bumpers = true;
215 robot.ignore_hokuyo = false;
218 0.45 + 2*0.35 + 0.175,
220 TURN(DEG2RAD(-50)), &tcFast);
224 FSM_TRANSITION(load_opp_bonus_figure);
229 case EV_MOTION_ERROR:
230 case EV_SWITCH_STRATEGY:
231 DBG_PRINT_EVENT("unhandled event");
237 FSM_STATE(load_opp_bonus_figure)
241 enable_my_square_walls(false);
242 robot.use_left_bumper = true;
243 robot.use_right_bumper = true;
244 robot.use_back_bumpers = true;
245 robot.ignore_hokuyo = true;
247 //robot_trajectory_new(&tcSlow);
248 robot_move_by(0.35, NO_TURN(), &tcSlow);
252 FSM_TRANSITION(place_opp_bonus_figure);
257 case EV_MOTION_ERROR:
258 case EV_SWITCH_STRATEGY:
259 DBG_PRINT_EVENT("unhandled event");
265 FSM_STATE(place_opp_bonus_figure)
269 robot.use_left_bumper = true;
270 robot.use_right_bumper = true;
271 robot.use_back_bumpers = true;
272 robot.ignore_hokuyo = true;
274 robot_trajectory_new_backward(&tcSlow);
275 robot_trajectory_add_point_trans(
276 0.45 + 2*0.35 + 0.175,
278 robot_trajectory_add_final_point_trans(
279 0.45 + 2*0.35 + 0.175,
280 2*0.35 + 0.175 + ROBOT_AXIS_TO_FIGURE_CENTER_M,
281 ARRIVE_FROM(DEG2RAD(90), 0.1));
285 FSM_TRANSITION(leave_opp_bonus_figure);
290 case EV_MOTION_ERROR:
291 case EV_SWITCH_STRATEGY:
292 DBG_PRINT_EVENT("unhandled event");
298 FSM_STATE(leave_opp_bonus_figure)
302 robot.use_left_bumper = true;
303 robot.use_right_bumper = true;
304 robot.use_back_bumpers = true;
305 robot.ignore_hokuyo = true;
307 robot_trajectory_new_backward(&tcSlow);
308 robot_trajectory_add_final_point_trans(
309 0.45 + 2*0.35 + 0.175,
320 case EV_MOTION_ERROR:
321 case EV_SWITCH_STRATEGY:
322 DBG_PRINT_EVENT("unhandled event");
328 /** securely bypass firt figure in front of starting area */
329 FSM_STATE(bypass_figure_in_front_of_start)
333 robot.use_left_bumper = true;
334 robot.use_right_bumper = true;
335 robot.use_back_bumpers = false;
336 robot.ignore_hokuyo = false;
338 robot_trajectory_new(&tcFast);
339 robot_trajectory_add_point_trans(
341 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2));
342 robot_trajectory_add_final_point_trans(
353 case EV_MOTION_ERROR:
354 case EV_SWITCH_STRATEGY:
355 DBG_PRINT_EVENT("unhandled event");
361 /** pick second figure from green area */
362 FSM_STATE(approach_second_green_figure)
366 robot.use_left_bumper = true;
367 robot.use_right_bumper = true;
368 robot.use_back_bumpers = true;
369 robot.ignore_hokuyo = false;
371 robot_trajectory_new(&tcFast);
372 robot_trajectory_add_final_point_trans(
373 0.45 + 0.3 - app_dist,
382 FSM_TRANSITION(load_second_green_figure);
385 case EV_MOTION_ERROR:
391 FSM_STATE(load_second_green_figure)
395 robot.use_left_bumper = true;
396 robot.use_right_bumper = true;
397 robot.use_back_bumpers = true;
398 robot.ignore_hokuyo = true;
400 robot_trajectory_new(&tcSlow);
401 robot_trajectory_add_final_point_trans(
402 ROBOT_AXIS_TO_FRONT_M + ROBOT_JAWS_LENGHT_M + load_dist,
404 ARRIVE_FROM(DEG2RAD(180), 0.10));
411 FSM_TRANSITION(go_out_second_green_figure);
414 case EV_MOTION_ERROR:
420 FSM_STATE(go_out_second_green_figure)
424 robot.use_left_bumper = true;
425 robot.use_right_bumper = true;
426 robot.use_back_bumpers = true;
427 robot.ignore_hokuyo = true;
429 robot_trajectory_new_backward(&tcFast);
430 robot_trajectory_add_final_point_trans(
437 FSM_TRANSITION(place_figure_to_bonus_area);
439 FSM_TRANSITION(place_figure_to_protected_block);
446 case EV_MOTION_ERROR:
452 FSM_STATE(place_figure_to_protected_block)
456 robot.use_left_bumper = true;
457 robot.use_right_bumper = true;
458 robot.use_back_bumpers = true;
459 robot.ignore_hokuyo = false;
461 robot_trajectory_new(&tcSlow);
462 robot_trajectory_add_final_point_trans(
464 0.12 + 0.08 + ROBOT_AXIS_TO_FIGURE_CENTER_M,
465 ARRIVE_FROM(DEG2RAD(-90), 0.20));
469 FSM_TRANSITION(leave_protected_figure);
476 case EV_MOTION_ERROR:
477 case EV_SWITCH_STRATEGY:
478 DBG_PRINT_EVENT("unhandled event");
484 FSM_STATE(leave_protected_figure)
488 robot.use_left_bumper = true;
489 robot.use_right_bumper = true;
490 robot.use_back_bumpers = true;
491 robot.ignore_hokuyo = false;
493 robot_trajectory_new_backward(&tcFast);
494 robot_trajectory_add_point_trans(
497 robot_trajectory_add_final_point_trans(
510 case EV_MOTION_ERROR:
516 /** pick third figure from green area */
517 FSM_STATE(approach_third_green_figure)
521 robot.use_left_bumper = true;
522 robot.use_right_bumper = true;
523 robot.use_back_bumpers = true;
524 robot.ignore_hokuyo = false;
526 robot_trajectory_new(&tcFast);
527 robot_trajectory_add_final_point_trans(
528 0.45 + 0.3 - app_dist,
537 FSM_TRANSITION(load_third_green_figure);
540 case EV_MOTION_ERROR:
546 FSM_STATE(load_third_green_figure)
550 robot.use_left_bumper = true;
551 robot.use_right_bumper = true;
552 robot.use_back_bumpers = true;
553 robot.ignore_hokuyo = true;
555 robot_trajectory_new(&tcSlow);
556 robot_trajectory_add_final_point_trans(
557 ROBOT_AXIS_TO_FRONT_M + ROBOT_JAWS_LENGHT_M + load_dist,
559 ARRIVE_FROM(DEG2RAD(180), 0.20));
566 FSM_TRANSITION(go_out_third_green_figure);
569 case EV_MOTION_ERROR:
575 FSM_STATE(go_out_third_green_figure)
579 robot.use_left_bumper = true;
580 robot.use_right_bumper = true;
581 robot.use_back_bumpers = true;
582 robot.ignore_hokuyo = true;
584 robot_trajectory_new_backward(&tcFast);
585 robot_trajectory_add_final_point_trans(
592 FSM_TRANSITION(place_figure_to_near_area);
594 FSM_TRANSITION(place_figure_to_bonus_area);
600 case EV_MOTION_ERROR:
602 robot.ignore_hokuyo = false;
607 FSM_STATE(place_figure_to_near_area)
611 robot.use_left_bumper = true;
612 robot.use_right_bumper = true;
613 robot.use_back_bumpers = true;
614 robot.ignore_hokuyo = false;
616 robot_trajectory_new(&tcFast);
617 robot_trajectory_add_final_point_trans(
619 0.35 + 0.175 + ROBOT_AXIS_TO_FIGURE_CENTER_M,
627 FSM_TRANSITION(leave_near_figure);
631 case EV_MOTION_ERROR:
637 FSM_STATE(leave_near_figure)
641 robot.use_left_bumper = true;
642 robot.use_right_bumper = true;
643 robot.use_back_bumpers = true;
644 robot.ignore_hokuyo = false;
646 robot_trajectory_new_backward(&tcFast);
647 robot_trajectory_add_final_point_trans(
661 case EV_MOTION_ERROR:
667 FSM_STATE(place_figure_to_bonus_area)
671 robot.use_left_bumper = true;
672 robot.use_right_bumper = true;
673 robot.use_back_bumpers = true;
674 robot.ignore_hokuyo = false;
676 robot_trajectory_new(&tcSlow);
677 robot_trajectory_add_final_point_trans(
679 0.08 + ROBOT_AXIS_TO_FIGURE_CENTER_M,
680 ARRIVE_FROM(DEG2RAD(-90), 0.3));
684 FSM_TRANSITION(leave_bonus_figure);
692 case EV_MOTION_ERROR:
693 case EV_SWITCH_STRATEGY:
694 DBG_PRINT_EVENT("unhandled event");
700 FSM_STATE(leave_bonus_figure)
704 robot.use_left_bumper = true;
705 robot.use_right_bumper = true;
706 robot.use_back_bumpers = true;
707 robot.ignore_hokuyo = true;
709 robot_trajectory_new_backward(&tcFast);
710 robot_trajectory_add_final_point_trans(
721 act_lift(DOWN, 0, 0);
724 case EV_MOTION_ERROR:
730 /** pick fourth green figure from green area */
731 FSM_STATE(approach_fourth_green_figure)
735 robot.use_left_bumper = true;
736 robot.use_right_bumper = true;
737 robot.use_back_bumpers = true;
738 robot.ignore_hokuyo = false;
740 robot_trajectory_new(&tcFast);
741 robot_trajectory_add_final_point_trans(
742 0.45 + 0.3 - app_dist,
747 tower = read_sharp();
752 FSM_TRANSITION(load_fourth_green_figure);
755 case EV_MOTION_ERROR:
761 FSM_STATE(load_fourth_green_figure)
765 robot.use_left_bumper = true;
766 robot.use_right_bumper = true;
767 robot.use_back_bumpers = true;
768 robot.ignore_hokuyo = true;
770 robot_trajectory_new(&tcSlow);
771 robot_trajectory_add_final_point_trans(
772 ROBOT_AXIS_TO_FRONT_M + ROBOT_JAWS_LENGHT_M + load_dist,
774 ARRIVE_FROM(DEG2RAD(180), 0.20));
781 FSM_TRANSITION(go_out_fourth_green_figure);
784 case EV_MOTION_ERROR:
790 FSM_STATE(go_out_fourth_green_figure)
794 robot.use_left_bumper = true;
795 robot.use_right_bumper = true;
796 robot.use_back_bumpers = true;
797 robot.ignore_hokuyo = true;
799 robot_trajectory_new_backward(&tcFast);
800 robot_trajectory_add_final_point_trans(
806 FSM_TRANSITION(place_figure_to_red_square);
812 case EV_MOTION_ERROR:
818 FSM_STATE(place_figure_to_red_square)
822 robot.use_left_bumper = true;
823 robot.use_right_bumper = true;
824 robot.use_back_bumpers = true;
825 robot.ignore_hokuyo = false;
826 robot_trajectory_new(&tcSlow);
827 robot_trajectory_add_final_point_trans(
829 0.7 + 0.15 + ROBOT_AXIS_TO_FIGURE_CENTER_M,
830 ARRIVE_FROM(DEG2RAD(-90), 0.05));
834 FSM_TRANSITION(leave_red_square_figure);
841 case EV_MOTION_ERROR:
842 case EV_SWITCH_STRATEGY:
843 DBG_PRINT_EVENT("unhandled event");
849 FSM_STATE(leave_red_square_figure)
853 robot.use_left_bumper = true;
854 robot.use_right_bumper = true;
855 robot.use_back_bumpers = true;
856 robot.ignore_hokuyo = false;
858 robot_trajectory_new_backward(&tcFast);
859 // robot_trajectory_add_point_trans(
862 robot_trajectory_add_final_point_trans(
875 case EV_MOTION_ERROR:
881 /** pick fifth green figure from green area */
882 FSM_STATE(approach_fifth_green_figure)
886 robot.use_left_bumper = true;
887 robot.use_right_bumper = true;
888 robot.use_back_bumpers = true;
889 robot.ignore_hokuyo = true;
891 robot_trajectory_new(&tcFast);
892 robot_trajectory_add_final_point_trans(
893 0.45 + 0.3 - app_dist,
902 FSM_TRANSITION(load_fifth_green_figure);
905 case EV_MOTION_ERROR:
911 FSM_STATE(load_fifth_green_figure)
915 robot.use_left_bumper = true;
916 robot.use_right_bumper = true;
917 robot.use_back_bumpers = true;
918 robot.ignore_hokuyo = true;
920 robot_trajectory_new(&tcSlow);
921 robot_trajectory_add_final_point_trans(
922 ROBOT_AXIS_TO_FRONT_M + ROBOT_JAWS_LENGHT_M + load_dist,
924 ARRIVE_FROM(DEG2RAD(180), 0.20));
931 FSM_TRANSITION(go_out_fifth_green_figure);
934 case EV_MOTION_ERROR:
940 FSM_STATE(go_out_fifth_green_figure)
944 robot.use_left_bumper = true;
945 robot.use_right_bumper = true;
946 robot.use_back_bumpers = true;
947 robot.ignore_hokuyo = true;
949 robot_trajectory_new_backward(&tcFast);
950 robot_trajectory_add_final_point_trans(
951 0.45 + 2*0.35 + 0.175,
961 case EV_MOTION_ERROR:
967 /** pick center figure */
968 FSM_STATE(approach_center_figure)
972 robot.use_left_bumper = true;
973 robot.use_right_bumper = true;
974 robot.use_back_bumpers = false;
975 robot.ignore_hokuyo = false;
977 robot_trajectory_new(&tcVeryFast);
978 robot_trajectory_add_point_trans(
981 robot_trajectory_add_final_point_trans(
989 robot.use_back_bumpers = true;
993 FSM_TRANSITION(load_center_figure);
995 case EV_MOTION_ERROR:
1001 FSM_STATE(load_center_figure)
1006 robot.use_left_bumper = true;
1007 robot.use_right_bumper = true;
1008 robot.use_back_bumpers = true;
1009 robot.ignore_hokuyo = false;
1011 robot_trajectory_new(&tcFast);
1012 robot_trajectory_add_final_point_trans(
1015 ARRIVE_FROM(DEG2RAD(-90), 0.1));
1021 case EV_MOTION_DONE:
1023 FSM_TRANSITION(place_figure_to_bonus_area);
1025 case EV_MOTION_ERROR: