6 #include <movehelper.h>
18 #include "actuators.h"
19 #include "match-timing.h"
20 #include "common-states.h"
21 #include "sub-states.h"
23 UL_LOG_CUST(ulogd_sub_states); /* Log domain name = ulogd + name of the file */
25 extern struct TrajectoryConstraints tcSlow, tcVerySlow;
27 /************************************************************************
28 * Functions used in and called from all the (almost identical)
29 * "wait for start" states in particular strategies.
30 ************************************************************************/
33 #define DBG_FSM_STATE(name) do { if (fsm->debug_states) ul_loginf("fsm %s %.1f: %s(%s)\n", \
34 fsm->debug_name, robot_current_time(), \
35 name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
38 #define HOK_MIN_MM (ROBOT_AXIS_TO_FRONT_MM - HOKUYO_CENTER_OFFSET_MM)
39 #define HOK_MIN_M (HOK_MIN_MM/1000.0)
40 #define HOK_MAX_MM (1000)
43 * Count the angle to turn by to see the minimum distance in the robot axis.
44 * @param alpha poiner to the angle to turn by
45 * @param minimum pointer to the minimum distance from hokuyo
47 void get_hokuyo_min(double *alpha, double *minimum)
53 struct hokuyo_scan_type scan = robot.hokuyo;
54 uint16_t *data = scan.data;
56 for (int i = 0; i < HOKUYO_ARRAY_SIZE; i++) {
57 beta = HOKUYO_INDEX_TO_RAD(i);
58 if((beta < (-50.0 / 180.0 * M_PI)) || ((beta > (50.0 / 180.0 * M_PI))))
61 if(data[i] > HOK_MIN_MM && data[i] < HOK_MAX_MM) {
66 // printf("min: %f, beta: %f\n", min, beta);
70 beta = HOKUYO_INDEX_TO_RAD(min_i);
71 *alpha = atan2((min * sin(beta)), (HOKUYO_CENTER_OFFSET_M + min * cos(beta)));
72 *minimum = min - HOK_MIN_M;
74 printf("alpha: %f, beta: %f, minimum: %f\n", *alpha, beta, *minimum);
77 void enable_obstacle_avoidance(bool enable)
79 robot.obstacle_avoidance_enabled = enable;
80 robot.ignore_hokuyo = !enable;
81 robot.check_turn_safety = enable;
88 act_camera_recognize(robot.target_color);
89 DBG_PRINT_EVENT("recognition");
97 if (robot.target_valid) {
98 DBG_PRINT_EVENT("camera: Target valid!");
99 FSM_TRANSITION(get_target_turn);
101 DBG_PRINT_EVENT("camera: Target not valid!");
102 robot.target_loaded = false;
111 FSM_STATE(get_target_turn)
113 const double close = 0.05;
114 double alpha, minimum;
119 DBG_PRINT_EVENT("turn");
120 enable_obstacle_avoidance(false);
121 get_hokuyo_min(&alpha, &minimum);
122 robot_get_est_pos(&x, &y, &phi);
123 robot_move_by(0, TURN(alpha+phi), &tcVerySlow);
128 get_hokuyo_min(&alpha, &minimum);
129 if (minimum < close) {
130 FSM_TRANSITION(get_target_load);
132 FSM_TRANSITION(get_target_touch);
135 case EV_MOTION_ERROR:
136 enable_obstacle_avoidance(true);
144 FSM_STATE(get_target_touch)
146 const double step = 0.02;
151 DBG_PRINT_EVENT("touch");
152 robot_move_by(step, NO_TURN(), &tcVerySlow);
157 FSM_TRANSITION(get_target_turn);
159 case EV_MOTION_ERROR:
160 enable_obstacle_avoidance(true);
168 FSM_STATE(get_target_load)
170 static int direction = 0;
179 robot.target_loaded = true;
180 FSM_TRANSITION(get_target_back);
183 if (direction == 0) {
193 FSM_STATE(get_target_back)
197 robot_move_by(-0.5, NO_TURN(), &tcVerySlow);
204 case EV_MOTION_ERROR:
208 enable_obstacle_avoidance(true);
224 case EV_MOTION_ERROR:
225 case EV_SWITCH_STRATEGY:
226 DBG_PRINT_EVENT("unhandled event");