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);
20 static void homing(struct fsm *fsm, enum event event);
21 static void stop(void);
24 static void homing(struct fsm *fsm, enum event event)
26 static uint32_t time_start=0;
31 time_start=timer_msec;
34 //homing with 8s timeout
35 engine_A_dir(ENGINE_DIR_FW);
38 if(fsm->flags & CAN_LIFT_SWITCH_DOWN){
41 fsm->current_state = wait_for_cmd;
42 fsm->flags |= CAN_LIFT_HOMED;
43 fsm->trigger_can_send=1;
44 fsm->can_req_homing=0;
45 // fsm->can_req_position = 0x54E2;
47 else if (timer_msec >= (time_start + 8000)) {
49 fsm->current_state = wait_for_cmd;
50 fsm->flags |= CAN_LIFT_TIMEOUT;
51 fsm->flags &= ~CAN_LIFT_HOMED;
52 fsm->trigger_can_send=1;
53 fsm->can_req_homing=0;
61 void fsm_lift_init(struct fsm *fsm, enum event event)
66 fsm->flags |= CAN_LIFT_INITIALIZING;
69 fsm->flags &= ~CAN_LIFT_INITIALIZING;
70 fsm->current_state = wait_for_cmd;
80 engine_A_en(ENGINE_EN_OFF);
85 static bool do_control(struct fsm *fsm, int P)
88 int e = fsm->req_pos - fsm->act_pos;
89 int action = (P*e) / 20; // ORIGINAL: int action = P*e;
91 engine_A_dir(action > 0);
92 action = action > 100 ? 100 : action;
96 action = action > 0 ? action : -action;
97 action = action > 40 ? 100 : 0;
99 action = action > 0 ? action : -action;
102 engine_A_pwm(action);
103 engine_A_en(ENGINE_EN_ON);
105 if (fsm->req_target > fsm->start_pos)
106 finished = fsm->act_pos > fsm->req_target - DEAD_ZONE;
108 finished = fsm->act_pos < fsm->req_target + DEAD_ZONE;
113 static void wait_for_cmd(struct fsm *fsm, enum event event)
115 static int last_can_req_pos = 0;
122 //homing if push home button
123 if (fsm->flags & CAN_LIFT_SWITCH_HOME){
124 fsm->current_state=homing;
127 //homing if match start, but did not homing before start
128 if ((fsm->can_req_homing) && !(fsm->flags & CAN_LIFT_HOMED)){
129 fsm->current_state=homing;
133 if (fsm->can_req_position != last_can_req_pos &&
134 fsm->can_req_position != fsm->req_target) {
135 last_can_req_pos = fsm->can_req_position;
136 fsm->req_target = fsm->can_req_position;
137 fsm->req_spd = fsm->can_req_spd;
138 fsm->current_state = move;
146 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
147 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
148 static void move(struct fsm *fsm, enum event event)
151 static bool lift_stopped_on_end = false;
157 fsm->time_start = timer_msec;
158 fsm->start_pos = fsm->act_pos;
159 if(fsm->req_spd == 0)
160 fsm->req_pos = fsm->req_target;
162 fsm->req_pos = fsm->start_pos;
165 if ((fsm->flags & CAN_LIFT_SWITCH_UP) && (fsm->can_response != fsm->req_target)){
166 if(!lift_stopped_on_end){
167 fsm->can_response = fsm->req_target;
168 fsm->current_state = wait_for_cmd;
169 fsm->req_pos = fsm->act_pos;
170 lift_stopped_on_end=true;
173 lift_stopped_on_end=false;
176 if ((fsm->flags & CAN_LIFT_SWITCH_DOWN) && (fsm->can_response != fsm->req_target)) {
177 if(!lift_stopped_on_end){
178 fsm->can_response = fsm->req_target;
179 fsm->current_state = wait_for_cmd;
181 fsm->req_pos = fsm->act_pos;
182 lift_stopped_on_end=true;
185 lift_stopped_on_end=false;
189 if (fsm->can_req_position != fsm->req_target) {
190 fsm->flags |= CAN_LIFT_TIMEOUT;
191 fsm->current_state = wait_for_cmd;
193 if(fsm->req_spd != 0 && counter++ >= 10)
196 if(fsm->req_target > fsm->start_pos) {
197 fsm->req_pos = XMIN(fsm->req_pos + fsm->req_spd,fsm->req_target);
199 fsm->req_pos = XMAX(fsm->req_pos - fsm->req_spd,fsm->req_target);
202 if (timer_msec - fsm->time_start > (fsm->req_spd == 0 ? 1000 : 3000)) {
203 fsm->flags |= CAN_LIFT_TIMEOUT;
204 fsm->can_response = fsm->req_target;
205 fsm->current_state = wait_for_cmd;
206 fsm->req_pos = fsm->act_pos;
208 finished = do_control(fsm, fsm->req_spd ? 2 : 2);
209 if (finished && fsm->req_pos == fsm->req_target) {
210 fsm->can_response = fsm->req_target;
211 fsm->current_state = wait_for_cmd;
216 fsm->trigger_can_send = true;;