1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-20 15:42:18 +00:00
stmbl/shared/comps/stp.c

113 lines
2.8 KiB
C
Raw Permalink Normal View History

#include "stp_comp.h"
2017-07-02 17:42:53 +00:00
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(stp);
HAL_PIN(target);
2019-02-14 10:55:55 +00:00
HAL_PIN(vel_ext_cmd);
HAL_PIN(acc_ext_cmd);
2018-03-18 22:33:52 +00:00
HAL_PIN(jog);
2019-02-14 10:55:55 +00:00
HAL_PIN(pos);
HAL_PIN(mpos);
HAL_PIN(vel_cmd);
HAL_PIN(acc_cmd);
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
HAL_PIN(max_pos);
HAL_PIN(min_pos);
2017-07-02 17:42:53 +00:00
HAL_PIN(max_vel);
HAL_PIN(max_acc);
HAL_PIN(dtg);
2019-02-14 10:55:55 +00:00
HAL_PIN(ttg);
HAL_PIN(at_target);
2017-07-02 17:42:53 +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-14 10:55:55 +00:00
// struct stp_ctx_t *ctx = (struct stp_ctx_t *)ctx_ptr;
2018-03-18 22:33:52 +00:00
struct stp_pin_ctx_t *pins = (struct stp_pin_ctx_t *)pin_ptr;
2018-04-06 23:36:17 +00:00
PIN(target) = 0.0;
PIN(max_vel) = 1.0 * 2.0 * M_PI;
PIN(max_acc) = 10.0 * 2.0 * M_PI;
2019-02-14 10:55:55 +00:00
PIN(max_pos) = 10.0 * 2.0 * M_PI;
PIN(min_pos) = -10.0 * 2.0 * M_PI;
2018-03-18 22:33:52 +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-02-14 10:55:55 +00:00
// struct stp_ctx_t *ctx = (struct stp_ctx_t *)ctx_ptr;
2017-09-06 02:20:06 +00:00
struct stp_pin_ctx_t *pins = (struct stp_pin_ctx_t *)pin_ptr;
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
float max_acc = MAX(PIN(max_acc), 0.01);
float max_vel = MAX(PIN(max_vel), 0.01);
// jog input
float acc_ext_cmd = LIMIT(PIN(acc_ext_cmd), PIN(max_acc) * 0.99);
float vel_ext_cmd = LIMIT(PIN(vel_ext_cmd) + PIN(jog) * max_vel, PIN(max_vel) * 0.99);
PIN(target) += vel_ext_cmd * period + acc_ext_cmd * period * period / 2.0;
2021-09-17 19:09:32 +00:00
PIN(vel_ext_cmd) += acc_ext_cmd * period;
PIN(vel_ext_cmd) = LIMIT(PIN(vel_ext_cmd), max_vel * 0.99);
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
// pos input
float target = CLAMP(PIN(target), PIN(min_pos), PIN(max_pos));
PIN(target) = target;
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
// update
PIN(pos) += PIN(vel_cmd) * period + PIN(acc_cmd) * period * period / 2.0;
PIN(vel_cmd) += PIN(acc_cmd) * period;
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
// distance to go
float to_go = target - PIN(pos);
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
// time to go
float time_to_go = sqrtf(2.0 * ABS(to_go) / max_acc);
// real time to go
int periods_to_go = ceilf(time_to_go / period);
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
// calc new acc
float acc = 0.0;
if(periods_to_go){
acc = 2.0 * to_go / (periods_to_go * periods_to_go * period * period);
}
2017-07-02 17:42:53 +00:00
2019-02-14 10:55:55 +00:00
float vel = acc * periods_to_go * period;
vel = LIMIT(vel, max_vel);
acc = (vel - PIN(vel_cmd)) / period;
acc = LIMIT(acc, max_acc);
2017-07-02 17:42:53 +00:00
2021-09-17 19:09:32 +00:00
if(time_to_go < period && ABS(PIN(vel_cmd)) < max_acc * period){
acc = 0.0;
PIN(vel_cmd) = 0.0;
PIN(pos) = PIN(target);
}
2019-02-14 10:55:55 +00:00
PIN(acc_cmd) = acc;
PIN(dtg) = to_go;
PIN(ttg) = periods_to_go * period;
PIN(mpos) = mod(PIN(pos));
2017-09-06 02:20:06 +00:00
2019-02-14 10:55:55 +00:00
if((periods_to_go <= 1) & (ABS(PIN(vel_cmd)) < max_vel / 10000.0)){
PIN(at_target) = 1.0;
}
else{
PIN(at_target) = 0.0;
}
2017-07-02 17:42:53 +00:00
}
const hal_comp_t stp_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "stp",
.nrt = 0,
.rt = rt_func,
.frt = 0,
2018-03-18 22:33:52 +00:00
.nrt_init = nrt_init,
2017-09-06 02:20:06 +00:00
.hw_init = 0,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
2019-02-14 10:55:55 +00:00
.ctx_size = 0,
2017-09-06 02:20:06 +00:00
.pin_count = sizeof(struct stp_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-07-02 17:42:53 +00:00
};