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_en(ENGINE_EN_ON);
36 engine_A_dir(ENGINE_DIR_FW);
39 if(fsm->flags & CAN_LIFT_SWITCH_DOWN){
42 fsm->current_state = wait_for_cmd;
43 fsm->flags |= CAN_LIFT_HOMED;
44 fsm->trigger_can_send = 1;
45 fsm->can_req_homing = 0;
46 fsm->can_req_position = 0;
47 // fsm->can_req_position = 0x54E2;
48 } else if (timer_msec >= (time_start + 4000)) {
50 fsm->current_state = wait_for_cmd;
51 fsm->flags |= CAN_LIFT_TIMEOUT;
52 fsm->flags &= ~CAN_LIFT_HOMED;
53 fsm->trigger_can_send = 1;
54 fsm->can_req_homing = 0;
62 void fsm_lift_init(struct fsm *fsm, enum event event)
67 fsm->flags |= CAN_LIFT_INITIALIZING;
70 fsm->flags &= ~CAN_LIFT_INITIALIZING;
71 fsm->current_state = wait_for_cmd;
81 engine_A_en(ENGINE_EN_OFF);
86 static bool do_control(struct fsm *fsm, int P)
89 int e = fsm->req_pos - fsm->act_pos;
90 int action = (P*e) / 5; // ORIGINAL: int action = P*e;
92 engine_A_dir(action > 0);
93 action = action > 100 ? 100 : action;
97 action = action > 0 ? action : -action;
98 action = action > 40 ? 100 : 0;
100 action = action > 0 ? action : -action;
103 engine_A_pwm(action);
104 engine_A_en(ENGINE_EN_ON);
106 if (fsm->req_target > fsm->start_pos)
107 finished = fsm->act_pos > fsm->req_target - DEAD_ZONE;
109 finished = fsm->act_pos < fsm->req_target + DEAD_ZONE;
114 static void wait_for_cmd(struct fsm *fsm, enum event event)
116 static int last_can_req_pos = 0;
123 //homing if push home button or homing can msg. received
124 if ((fsm->flags & CAN_LIFT_SWITCH_HOME) || (fsm->can_req_homing)) {
125 fsm->can_req_homing = 0;
126 fsm->current_state = homing;
129 /* if lift is not homed, do not allow movement and regulation */
130 if (fsm->flags & CAN_LIFT_HOMED) {
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;
147 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
148 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
149 static void move(struct fsm *fsm, enum event event)
152 static bool lift_stopped_on_end = false;
158 fsm->time_start = timer_msec;
159 fsm->start_pos = fsm->act_pos;
161 /* check if movement starts on end switch */
162 if ((fsm->flags & CAN_LIFT_SWITCH_DOWN) || (fsm->flags & CAN_LIFT_SWITCH_UP))
163 lift_stopped_on_end = true;
165 lift_stopped_on_end = false;
167 if(fsm->req_spd == 0)
168 fsm->req_pos = fsm->req_target;
170 fsm->req_pos = fsm->start_pos;
173 /* if movement starts on end switch, ignore this, else stop movement on act position */
174 if ((fsm->flags & CAN_LIFT_SWITCH_UP) && !lift_stopped_on_end){
175 fsm->can_response = fsm->req_target;
176 fsm->current_state = wait_for_cmd;
177 fsm->req_pos = fsm->act_pos;
178 lift_stopped_on_end = true;
180 /* if movement starts on end switch, ignore this, else stop movement on act position */
181 if ((fsm->flags & CAN_LIFT_SWITCH_DOWN) && !lift_stopped_on_end) {
182 fsm->can_response = fsm->req_target;
183 fsm->current_state = wait_for_cmd;
185 fsm->req_pos = fsm->act_pos;
186 lift_stopped_on_end = true;
189 if (fsm->can_req_position != fsm->req_target) {
190 fsm->flags |= CAN_LIFT_TIMEOUT;
191 fsm->current_state = wait_for_cmd;
194 if(fsm->req_spd != 0 && counter++ >= 10)
197 if(fsm->req_target > fsm->start_pos) {
198 fsm->req_pos = XMIN(fsm->req_pos + fsm->req_spd,fsm->req_target);
200 fsm->req_pos = XMAX(fsm->req_pos - fsm->req_spd,fsm->req_target);
204 if (timer_msec - fsm->time_start > (fsm->req_spd == 0 ? 2000 : 4000)) {
205 fsm->flags |= CAN_LIFT_TIMEOUT;
206 fsm->can_response = fsm->req_target;
207 fsm->current_state = wait_for_cmd;
208 fsm->req_pos = fsm->act_pos;
211 finished = do_control(fsm, fsm->req_spd ? 2 : 2);
213 if (finished && fsm->req_pos == fsm->req_target) {
214 fsm->can_response = fsm->req_target;
215 fsm->current_state = wait_for_cmd;
220 fsm->trigger_can_send = true;;