1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2025-01-01 21:42:14 +00:00
stmbl/shared/comps/vel_int.c

93 lines
1.9 KiB
C
Raw Permalink Normal View History

#include "vel_int_comp.h"
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);
2018-05-06 21:36:38 +00:00
HAL_PIN(cmd_freq);
HAL_PIN(real_cmd_freq);
2017-07-01 01:18:08 +00:00
HAL_PIN(wd);
HAL_PIN(error);
2017-09-06 02:20:06 +00:00
struct vel_int_ctx_t {
float pos;
float counter;
2018-05-06 21:36:38 +00:00
float cmd_freq;
2017-07-01 01:18:08 +00:00
};
2019-04-29 20:08:54 +00:00
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2017-09-06 02:20:06 +00:00
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;
2018-05-06 21:36:38 +00:00
PIN(cmd_freq) = 1000.0;
ctx->cmd_freq = PIN(cmd_freq);
2017-07-01 01:18:08 +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) {
2017-09-06 02:20:06 +00:00
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;
2018-05-06 21:36:38 +00:00
ctx->cmd_freq += 1.0;
2017-09-06 02:20:06 +00:00
} else {
ctx->pos += v * period;
}
2018-05-06 21:36:38 +00:00
ctx->cmd_freq -= ctx->cmd_freq * period;
PIN(real_cmd_freq) = ctx->cmd_freq * 0.001 + PIN(real_cmd_freq) * 0.999;
2017-09-06 02:20:06 +00:00
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
};