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

76 lines
2.0 KiB
C
Raw Permalink Normal View History

#include "pmsm_ttc_comp.h"
2017-06-30 19:41:32 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
2017-07-01 03:21:03 +00:00
HAL_COMP(pmsm_ttc);
2017-06-30 19:41:32 +00:00
// motor values
HAL_PIN(psi);
HAL_PIN(polecount);
// cogging torque ripple, constant
2018-04-06 23:36:17 +00:00
HAL_PIN(ac); // amplitude
HAL_PIN(pc); // phase
HAL_PIN(nc); // frequency
2017-12-11 14:38:15 +00:00
// electrical torque ripple, linear with current
2018-04-06 23:36:17 +00:00
HAL_PIN(ae); // amplitude
HAL_PIN(pe); // phase
HAL_PIN(ne); // frequency
HAL_PIN(pos_in);
HAL_PIN(pos_out);
2018-04-06 23:36:17 +00:00
HAL_PIN(t); // compensation torque
HAL_PIN(g); // compensation gain
HAL_PIN(block_gain); // block commutation gain
2017-12-11 14:38:15 +00:00
2017-06-30 19:41:32 +00:00
// torque cmd in
HAL_PIN(torque);
// cur cmd out
HAL_PIN(cur);
2019-04-29 20:08:54 +00:00
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2017-06-30 19:41:32 +00:00
// struct sim_ctx_t * ctx = (struct sim_ctx_t *)ctx_ptr;
2017-09-06 02:20:06 +00:00
struct pmsm_ttc_pin_ctx_t *pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr;
2018-04-06 23:36:17 +00:00
PIN(nc) = 1.0;
PIN(ne) = 1.0;
PIN(block_gain) = 0.0;
2017-06-30 19:41:32 +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 pmsm_ttc_ctx_t * ctx = (struct pmsm_ttc_ctx_t *)ctx_ptr;
struct pmsm_ttc_pin_ctx_t *pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr;
float p = MAX(PIN(polecount), 1.0);
float psi_m = MAX(PIN(psi), 0.01);
float torque = PIN(torque);
2018-04-06 23:36:17 +00:00
float pos = PIN(pos_in);
float tc = PIN(ac) * sinf(PIN(pc) + pos * PIN(nc));
float te = torque * PIN(ae) * sinf(PIN(pe) + pos * PIN(ne));
2017-09-06 02:20:06 +00:00
PIN(pos_out) = pos * (1.0 - PIN(block_gain)) + (((int)((pos / 2.0 / M_PI + 0.5) * 6 + 0.5)) / 6.0 * 2.0 * M_PI - M_PI) * PIN(block_gain);
2018-04-06 23:36:17 +00:00
PIN(t) = tc + te;
PIN(cur) = (torque + PIN(g) * (tc + te)) / 3.0 * 2.0 / p / psi_m;
2017-06-30 19:41:32 +00:00
}
2017-07-01 03:21:03 +00:00
hal_comp_t pmsm_ttc_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "pmsm_ttc",
.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 = 0,
.pin_count = sizeof(struct pmsm_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-06-30 19:41:32 +00:00
};