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 < (-70.0 / 180.0 * M_PI)) || ((beta > (70.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 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 double alpha, minimum;
118 DBG_PRINT_EVENT("turn");
119 enable_obstacle_avoidance(false);
120 get_hokuyo_min(&alpha, &minimum);
121 robot_get_est_pos(&x, &y, &phi);
122 robot_move_by(0, TURN(alpha+phi), &tcVerySlow);
127 FSM_TRANSITION(get_target_touch);
129 case EV_MOTION_ERROR:
130 enable_obstacle_avoidance(true);
138 FSM_STATE(get_target_touch)
140 double alpha, minimum;
145 DBG_PRINT_EVENT("touch");
146 get_hokuyo_min(&alpha, &minimum);
147 robot_move_by(minimum, NO_TURN(), &tcVerySlow);
152 FSM_TRANSITION(get_target_load);
154 case EV_MOTION_ERROR:
155 enable_obstacle_avoidance(true);
163 FSM_STATE(get_target_load)
165 static int direction = 0;
171 act_crane(CRANE_DOWN);
174 robot.target_loaded = true;
175 FSM_TRANSITION(get_target_back);
178 if (direction == 0) {
189 FSM_STATE(get_target_back)
193 robot_move_by(-0.5, NO_TURN(), &tcVerySlow);
200 case EV_MOTION_ERROR:
204 enable_obstacle_avoidance(true);
220 case EV_MOTION_ERROR:
221 case EV_SWITCH_STRATEGY:
222 DBG_PRINT_EVENT("unhandled event");