3 #include <system_def.h>
5 #include <can_msg_def.h>
13 #define DBG_ENTRY() do { \
14 send_rs_str(__func__); \
15 send_rs_str(": entry\n"); \
18 static void wait_for_cmd(struct fsm *fsm, enum event event);
19 static void move(struct fsm *fsm, enum event event);
22 void fsm_jaws_init(struct fsm *fsm, enum event event)
27 fsm->can_req_position = fsm->act_pos;
28 fsm->flags |= CAN_JAWS_INITIALIZING;
32 fsm->flags &= ~CAN_JAWS_INITIALIZING;
33 fsm->current_state = wait_for_cmd;
43 engine_A_en(ENGINE_EN_OFF);
46 engine_B_en(ENGINE_EN_OFF);
51 static bool do_control(struct fsm *fsm, int P)
53 bool finished, finished_left, finished_right;
54 int e_left = fsm->req_pos - fsm->act_pos_left;
55 int e_right = fsm->req_pos - fsm->act_pos_right;
56 int action_left = (P*e) / 10; // ORIGINAL: int action = P*e;
57 int action_right = (P*e) / 10;
59 engine_A_dir(action_left< 0);
60 engine_B_dir(action_right < 0);
63 action_left = action_left > 0 ? action_left : -action_left;
64 action_left = action_left > 40 ? 100 : 0;
66 action_right = action_right > 0 ? action_right : -action_right;
67 action_right = action_right > 40 ? 100 : 0;
69 action_left = action_left > 0 ? action_left : -action_left;
70 action_right= action_right > 0 ? action_right : -action_right;
72 engine_A_pwm(action_left);
73 engine_A_en(ENGINE_EN_ON);
75 engine_B_pwm(action_right);
76 engine_B_en(ENGINE_EN_ON);
78 if (fsm->req_target > fsm->start_pos){
79 finished_left = fsm->act_pos_left > fsm->req_target - DEAD_ZONE;
80 finished_right = fsm->act_pos_right > fsm->req_target - DEAD_ZONE;
83 finished_left = fsm->act_pos_left < fsm->req_target + DEAD_ZONE;
84 finished_right = fsm->act_pos_right < fsm->req_target + DEAD_ZONE;
87 // if (fsm->req_target > fsm->start_pos)
88 // finished = fsm->act_pos > fsm->req_target - DEAD_ZONE;
90 // finished = fsm->act_pos < fsm->req_target + DEAD_ZONE;
94 finished=finished_left && finished_right;
99 static void wait_for_cmd(struct fsm *fsm, enum event event)
101 static int last_can_req_pos = 0;
109 if (fsm->can_req_position != last_can_req_pos &&
110 fsm->can_req_position != fsm->req_target) {
111 last_can_req_pos = fsm->can_req_position;
112 fsm->req_target = fsm->can_req_position;
113 fsm->req_spd = fsm->can_req_spd;
114 fsm->current_state = move;
122 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
123 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
124 static void move(struct fsm *fsm, enum event event)
132 fsm->time_start = timer_msec;
133 fsm->start_pos = fsm->act_pos;
134 if(fsm->req_spd == 0)
135 fsm->req_pos = fsm->req_target;
137 fsm->req_pos = fsm->start_pos;
140 if (fsm->can_req_position != fsm->req_target) {
141 fsm->flags |= CAN_VIDLE_TIMEOUT;
142 fsm->current_state = wait_for_cmd;
144 if(fsm->req_spd != 0 && counter++ >= 10)
147 if(fsm->req_target > fsm->start_pos) {
148 fsm->req_pos = XMIN(fsm->req_pos + fsm->req_spd,fsm->req_target);
150 fsm->req_pos = XMAX(fsm->req_pos - fsm->req_spd,fsm->req_target);
153 if (timer_msec - fsm->time_start > (fsm->req_spd == 0 ? 1000 : 3000)) {
154 fsm->flags |= CAN_VIDLE_TIMEOUT;
155 fsm->can_response = fsm->req_target;
156 fsm->current_state = wait_for_cmd;
157 fsm->req_pos = fsm->act_pos;
159 finished = do_control(fsm, fsm->req_spd ? 2 : 2);
160 if (finished && fsm->req_pos == fsm->req_target) {
161 fsm->can_response = fsm->req_target;
162 fsm->current_state = wait_for_cmd;
167 fsm->trigger_can_send = true;;