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

80 lines
1.6 KiB
C
Raw Permalink Normal View History

2017-07-01 01:18:08 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(vel_int);
HAL_PIN(pos_in);
HAL_PIN(pos_out);
HAL_PIN(vel_in);
HAL_PIN(vel_out);
HAL_PIN(wd);
HAL_PIN(error);
2017-09-06 02:20:06 +00:00
struct vel_int_ctx_t {
float pos;
float counter;
2017-07-01 01:18:08 +00:00
};
2017-09-06 02:20:06 +00:00
static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
struct vel_int_ctx_t *ctx = (struct vel_int_ctx_t *)ctx_ptr;
struct vel_int_pin_ctx_t *pins = (struct vel_int_pin_ctx_t *)pin_ptr;
ctx->pos = 0.0;
2017-07-01 01:18:08 +00:00
ctx->counter = 0.0;
2017-09-06 02:20:06 +00:00
PIN(pos_in) = 0.0;
2017-07-01 01:18:08 +00:00
PIN(pos_out) = 0.0;
2017-09-06 02:20:06 +00:00
PIN(vel_in) = 0.0;
2017-07-01 01:18:08 +00:00
PIN(vel_out) = 0.0;
2017-09-06 02:20:06 +00:00
PIN(wd) = 0.002;
2017-07-01 01:18:08 +00:00
PIN(error) = 0.0;
}
2017-09-06 02:20:06 +00:00
static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
struct vel_int_ctx_t *ctx = (struct vel_int_ctx_t *)ctx_ptr;
struct vel_int_pin_ctx_t *pins = (struct vel_int_pin_ctx_t *)pin_ptr;
float p = PIN(pos_in);
float v = PIN(vel_in);
if(ctx->counter > PIN(wd) && v != 0.0) {
v = 0;
PIN(error) = 1.0;
} else {
PIN(error) = 0.0;
ctx->counter += period;
}
if(EDGE(p)) {
ctx->counter = 0.0;
ctx->pos = p;
} else {
ctx->pos += v * period;
}
ctx->pos = mod(ctx->pos);
PIN(pos_out) = ctx->pos;
PIN(vel_out) = v;
2017-07-01 01:18:08 +00:00
}
hal_comp_t vel_int_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "vel_int",
.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 = sizeof(struct vel_int_ctx_t),
.pin_count = sizeof(struct vel_int_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-07-01 01:18:08 +00:00
};