1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-29 20:12:10 +00:00
stmbl/shared/comps/idx_home.c

66 lines
1.5 KiB
C
Raw Permalink Normal View History

#include "idx_home_comp.h"
2017-10-23 16:17:59 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(idx_home);
HAL_PIN(mot_state); // 0 = disabled, 1 = inc, 2 = start abs, 3 = abs
HAL_PIN(fb);
HAL_PIN(fb_abs);
HAL_PIN(index_en);
HAL_PIN(index_clear);
HAL_PIN(pos_out);
struct idx_home_ctx_t {
int state;
int lastq;
int waitabs;
};
2019-04-29 20:08:54 +00:00
static void rt_func(float period, void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2017-10-23 16:17:59 +00:00
struct idx_home_ctx_t *ctx = (struct idx_home_ctx_t *)ctx_ptr;
struct idx_home_pin_ctx_t *pins = (struct idx_home_pin_ctx_t *)pin_ptr;
2017-10-29 21:42:31 +00:00
2017-10-23 16:17:59 +00:00
uint8_t q = 0;
2017-10-29 21:42:31 +00:00
if(PIN(index_en) > 0) {
if(PIN(mot_state) == 3) {
2017-10-23 16:17:59 +00:00
q = quadrant(PIN(fb_abs));
2017-10-29 21:42:31 +00:00
if(((q == 1 && ctx->lastq == 4) || (q == 4 && ctx->lastq == 1)) || ctx->waitabs == 1) {
2017-10-23 16:17:59 +00:00
PIN(index_clear) = 1;
2017-10-29 21:42:31 +00:00
ctx->state = 1;
2017-10-23 16:17:59 +00:00
}
ctx->lastq = q;
2017-10-29 21:42:31 +00:00
} else { //index requested, but index not seen
2017-10-23 16:17:59 +00:00
ctx->waitabs = 1;
}
}
2017-10-29 21:42:31 +00:00
if(!(PIN(index_en) > 0) && ctx->state == 1) {
2017-10-23 16:17:59 +00:00
PIN(index_clear) = 0;
}
2017-10-29 21:42:31 +00:00
if(ctx->state == 1) {
2017-10-23 16:17:59 +00:00
PIN(pos_out) = PIN(fb_abs);
2017-10-29 21:42:31 +00:00
} else {
2017-10-23 16:17:59 +00:00
PIN(pos_out) = PIN(fb);
}
}
hal_comp_t idx_home_comp_struct = {
.name = "idx_home",
.nrt = 0,
.rt = rt_func,
.frt = 0,
.nrt_init = 0,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = sizeof(struct idx_home_ctx_t),
.pin_count = sizeof(struct idx_home_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
};