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_lift_init(struct fsm *fsm, enum event event)
27 fsm->can_req_position = fsm->act_pos;
28 fsm->flags |= CAN_LIFT_INITIALIZING;
31 //homing, TODO add timeout
32 while(IO0PIN & (1<<END_SWITCH_UP)){
37 fsm->flags &= ~CAN_LIFT_INITIALIZING;
38 fsm->current_state = wait_for_cmd;
48 engine_A_en(ENGINE_EN_OFF);
53 static bool do_control(struct fsm *fsm, int P)
56 int e = fsm->req_pos - fsm->act_pos;
57 int action = (P*e) / 10; // ORIGINAL: int action = P*e;
59 engine_A_dir(action < 0);
62 action = action > 0 ? action : -action;
63 action = action > 40 ? 100 : 0;
65 action = action > 0 ? action : -action;
67 if((IO0PIN & (1<<END_SWITCH_UP))&&(IO0PIN & (1<<END_SWITCH_DOWN))){
69 engine_A_en(ENGINE_EN_ON);
74 if (fsm->req_target > fsm->start_pos)
75 finished = fsm->act_pos > fsm->req_target - DEAD_ZONE;
77 finished = fsm->act_pos < fsm->req_target + DEAD_ZONE;
82 static void wait_for_cmd(struct fsm *fsm, enum event event)
84 static int last_can_req_pos = 0;
92 if (fsm->can_req_position != last_can_req_pos &&
93 fsm->can_req_position != fsm->req_target) {
94 last_can_req_pos = fsm->can_req_position;
95 fsm->req_target = fsm->can_req_position;
96 fsm->req_spd = fsm->can_req_spd;
97 fsm->current_state = move;
105 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
106 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
107 static void move(struct fsm *fsm, enum event event)
115 fsm->time_start = timer_msec;
116 fsm->start_pos = fsm->act_pos;
117 if(fsm->req_spd == 0)
118 fsm->req_pos = fsm->req_target;
120 fsm->req_pos = fsm->start_pos;
123 if (fsm->can_req_position != fsm->req_target) {
124 fsm->flags |= CAN_LIFT_TIMEOUT;
125 fsm->current_state = wait_for_cmd;
127 if(fsm->req_spd != 0 && counter++ >= 10)
130 if(fsm->req_target > fsm->start_pos) {
131 fsm->req_pos = XMIN(fsm->req_pos + fsm->req_spd,fsm->req_target);
133 fsm->req_pos = XMAX(fsm->req_pos - fsm->req_spd,fsm->req_target);
136 if (timer_msec - fsm->time_start > (fsm->req_spd == 0 ? 1000 : 3000)) {
137 fsm->flags |= CAN_LIFT_TIMEOUT;
138 fsm->can_response = fsm->req_target;
139 fsm->current_state = wait_for_cmd;
140 fsm->req_pos = fsm->act_pos;
142 finished = do_control(fsm, fsm->req_spd ? 2 : 2);
143 if (finished && fsm->req_pos == fsm->req_target) {
144 fsm->can_response = fsm->req_target;
145 fsm->current_state = wait_for_cmd;
150 fsm->trigger_can_send = true;;