1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2025-01-01 13:32:10 +00:00
stmbl/shared/comps/home.c

126 lines
2.8 KiB
C
Raw Permalink Normal View History

#include "home_comp.h"
2019-03-03 11:48:48 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(home);
HAL_PIN(home_vel);
HAL_PIN(home_acc);
HAL_PIN(pos_in);
HAL_PIN(pos_out);
2019-12-11 09:22:44 +00:00
HAL_PIN(vel);
2019-03-03 11:48:48 +00:00
HAL_PIN(home_in);
HAL_PIN(home_polarity);
2019-12-11 09:22:44 +00:00
HAL_PIN(offset);
HAL_PIN(home_offset);
2020-02-24 21:32:15 +00:00
HAL_PIN(home_pos);
2019-03-03 11:48:48 +00:00
HAL_PIN(state);
2019-12-11 09:22:44 +00:00
HAL_PIN(en_in);
2019-03-03 11:48:48 +00:00
HAL_PIN(en_out);
HAL_PIN(re_home);
2019-04-29 20:08:54 +00:00
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr){
2019-03-03 11:48:48 +00:00
struct home_pin_ctx_t *pins = (struct home_pin_ctx_t *)pin_ptr;
PIN(state) = 0;
PIN(re_home) = 1;
PIN(home_polarity) = 1.0;
2019-12-11 09:22:44 +00:00
PIN(home_vel) = 2.0 * M_PI;
PIN(home_acc) = 2.0 * M_PI / 0.1;
2019-03-03 11:48:48 +00:00
}
2019-04-29 20:08:54 +00:00
static void rt_func(float period, void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2019-03-03 11:48:48 +00:00
//struct home_ctx_t *ctx = (struct home_ctx_t *)ctx_ptr;
struct home_pin_ctx_t *pins = (struct home_pin_ctx_t *)pin_ptr;
float vel = 0.0;
2019-12-11 09:22:44 +00:00
if(PIN(en_in) <= 0.0){
2019-03-03 11:48:48 +00:00
if(PIN(re_home) > 0.0){
PIN(state) = 0;
}
PIN(en_out) = 0;
}
switch((int)PIN(state)){
case 0: // not homed
2019-12-11 09:22:44 +00:00
if(PIN(en_in) > 0.0){
2019-03-03 11:48:48 +00:00
PIN(state) = 1;
PIN(en_out) = 0;
2019-12-11 09:22:44 +00:00
PIN(offset) = 0.0;
PIN(home_offset) = 0.0;
PIN(vel) = 0.0;
2019-03-03 11:48:48 +00:00
}
break;
case 1: // search home rising
if(PIN(home_polarity) > 0.0 && PIN(home_in) > 0.0){
PIN(state) = 2;
}
else if(PIN(home_polarity) <= 0.0 && PIN(home_in) <= 0.0){
PIN(state) = 2;
}
else{
vel = PIN(home_vel);
}
break;
case 2: // search home falling
if(PIN(home_polarity) <= 0.0 && PIN(home_in) > 0.0){
PIN(state) = 3;
2020-02-24 21:32:15 +00:00
PIN(home_offset) = PIN(offset) + PIN(home_pos);
2019-03-03 11:48:48 +00:00
}
else if(PIN(home_polarity) > 0.0 && PIN(home_in) <= 0.0){
PIN(state) = 3;
2020-02-24 21:32:15 +00:00
PIN(home_offset) = PIN(offset) + PIN(home_pos);
2019-03-03 11:48:48 +00:00
}
else{
vel = -PIN(home_vel);
}
break;
2020-02-25 03:38:03 +00:00
case 3: // go to home pos
2019-12-11 09:22:44 +00:00
vel = SIGN(PIN(home_offset) - PIN(offset)) * sqrtf(ABS(PIN(home_offset) - PIN(offset)) * 2.0 * PIN(home_acc));
2019-03-03 11:48:48 +00:00
2020-02-25 03:38:03 +00:00
if(ABS(PIN(offset) - PIN(home_offset)) < 0.01 && PIN(vel) < PIN(home_vel) * 0.01){
2019-12-11 09:22:44 +00:00
PIN(state) = 4;
2019-03-03 11:48:48 +00:00
}
break;
2019-12-11 09:22:44 +00:00
case 4: // homed
2019-03-03 11:48:48 +00:00
PIN(en_out) = 1;
2019-12-11 09:22:44 +00:00
PIN(offset) = PIN(home_offset);
PIN(vel) = 0.0;
2019-03-03 11:48:48 +00:00
break;
}
vel = LIMIT(vel, ABS(PIN(home_vel)));
2019-12-11 09:22:44 +00:00
PIN(vel) = CLAMP(vel, PIN(vel) - PIN(home_acc) * period, PIN(vel) + PIN(home_acc) * period);
PIN(offset) += PIN(vel) * period;
2019-03-03 11:48:48 +00:00
2020-02-25 03:38:03 +00:00
PIN(pos_out) = mod(PIN(pos_in) + mod(PIN(offset)));
2019-03-03 11:48:48 +00:00
}
hal_comp_t home_comp_struct = {
.name = "home",
.nrt = 0,
.rt = rt_func,
.frt = 0,
.nrt_init = nrt_init,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = 0,
.pin_count = sizeof(struct home_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
};