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

87 lines
1.9 KiB
C
Raw Permalink Normal View History

#include "ramp_comp.h"
2017-10-16 00:07:28 +00:00
#include "commands.h"
#include "hal.h"
#include "defines.h"
HAL_COMP(ramp);
2017-11-24 03:37:11 +00:00
// input
HAL_PIN(vel_ext_cmd);
2017-10-16 00:07:28 +00:00
2019-02-08 15:48:31 +00:00
HAL_PIN(en);
2019-06-05 22:37:39 +00:00
HAL_PIN(scale);
2019-02-08 15:48:31 +00:00
HAL_PIN(max_vel);
2017-10-16 00:07:28 +00:00
HAL_PIN(max_acc);
2019-02-08 15:48:31 +00:00
HAL_PIN(at_speed_th);
2017-10-16 00:07:28 +00:00
2017-11-24 03:37:11 +00:00
// output
HAL_PIN(vel_cmd);
2019-02-08 15:48:31 +00:00
HAL_PIN(at_speed);
HAL_PIN(en_out);
2019-06-05 22:37:39 +00:00
HAL_PIN(en_timer);
HAL_PIN(en_delay);
2019-02-08 15:48:31 +00:00
2019-04-29 20:08:54 +00:00
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2019-02-08 15:48:31 +00:00
// struct ramp_ctx_t *ctx = (struct ramp_ctx_t *)ctx_ptr;
struct ramp_pin_ctx_t *pins = (struct ramp_pin_ctx_t *)pin_ptr;
PIN(at_speed_th) = 0.01;
2019-06-05 22:37:39 +00:00
PIN(en_delay) = 0.25;
PIN(scale) = 1.0;
2019-02-08 15:48:31 +00:00
}
2017-10-16 00:07:28 +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-11-24 03:37:11 +00:00
// struct ramp_ctx_t *ctx = (struct ramp_ctx_t *)ctx_ptr;
2017-10-16 00:07:28 +00:00
struct ramp_pin_ctx_t *pins = (struct ramp_pin_ctx_t *)pin_ptr;
2018-09-11 17:54:40 +00:00
2019-02-08 15:48:31 +00:00
float vel_ext_cmd = LIMIT(PIN(vel_ext_cmd), PIN(max_vel));
if(PIN(en) <= 0.0){
vel_ext_cmd = 0.0;
}
2017-10-16 00:07:28 +00:00
2018-09-11 17:54:40 +00:00
float vel_error = vel_ext_cmd - PIN(vel_cmd);
2019-06-05 22:37:39 +00:00
float max_acc = PIN(max_acc) * PIN(scale);
if(PIN(en_timer) >= PIN(en_delay) * 0.9){
PIN(vel_cmd) += LIMIT(vel_error, max_acc * period);
}
2019-02-08 15:48:31 +00:00
if(ABS(PIN(vel_ext_cmd) - PIN(vel_cmd)) < PIN(max_vel) * PIN(at_speed_th)){
PIN(at_speed) = 1;
}
else{
PIN(at_speed) = 0;
}
2018-09-11 17:54:40 +00:00
2019-05-29 22:03:47 +00:00
if((ABS(PIN(vel_cmd)) > 0.01) | (ABS(PIN(vel_ext_cmd)) > 0.01)){
2019-06-05 22:37:39 +00:00
PIN(en_timer) = CLAMP(PIN(en_timer) + period, 0, PIN(en_delay));
}
else{
PIN(en_timer) = CLAMP(PIN(en_timer) - period, 0, PIN(en_delay));
}
if(PIN(en_timer) > 0.0){
2019-02-08 15:48:31 +00:00
PIN(en_out) = 1;
2018-09-11 17:54:40 +00:00
}
else{
2019-02-08 15:48:31 +00:00
PIN(en_out) = 0;
2018-09-11 17:54:40 +00:00
}
2017-10-16 00:07:28 +00:00
}
hal_comp_t ramp_comp_struct = {
2017-12-04 15:54:51 +00:00
.name = "ramp",
.nrt = 0,
.rt = rt_func,
.frt = 0,
2019-02-08 15:48:31 +00:00
.nrt_init = nrt_init,
2017-12-04 15:54:51 +00:00
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = 0,
.pin_count = sizeof(struct ramp_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2019-06-05 22:37:39 +00:00
};