stmbl/shared/comps/pmsm_limits.c

106 lines
2.9 KiB
C
Raw Permalink Normal View History

2017-06-30 19:41:32 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
2016-02-21 23:10:32 +00:00
HAL_COMP(pmsm_limits);
2015-09-13 21:38:15 +00:00
// motor values
2017-06-30 19:41:32 +00:00
HAL_PIN(psi);
HAL_PIN(r);
HAL_PIN(ld);
HAL_PIN(lq);
HAL_PIN(polecount);
2015-09-13 21:38:15 +00:00
// sys limit
2017-06-30 19:41:32 +00:00
HAL_PIN(ac_volt);
2015-09-13 21:38:15 +00:00
// next min max out -> pid, curpid
2017-06-30 19:41:32 +00:00
HAL_PIN(next_max_cur);
HAL_PIN(next_max_torque);
HAL_PIN(next_min_cur);
HAL_PIN(next_min_torque);
2015-09-13 21:38:15 +00:00
// min max out @ current vel
2017-06-30 19:41:32 +00:00
HAL_PIN(max_cur);
HAL_PIN(max_torque);
HAL_PIN(min_cur);
HAL_PIN(min_torque);
2015-09-13 21:38:15 +00:00
// abs max out
2017-06-30 19:41:32 +00:00
HAL_PIN(abs_max_cur);
HAL_PIN(abs_max_torque);
HAL_PIN(abs_max_vel);
2015-09-13 21:38:15 +00:00
// pmsm feedback
2017-06-30 19:41:32 +00:00
HAL_PIN(iq);
HAL_PIN(indq);
2017-09-06 02:20:06 +00:00
static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
2017-06-30 19:41:32 +00:00
// struct pmsm_limits_ctx_t * ctx = (struct pmsm_limits_ctx_t *)ctx_ptr;
2017-09-06 02:20:06 +00:00
struct pmsm_limits_pin_ctx_t *pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr;
PIN(psi) = 0.01;
PIN(r) = 1.0;
PIN(ld) = 0.001;
PIN(lq) = 0.001;
2017-06-30 19:41:32 +00:00
PIN(polecount) = 1.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 pmsm_limits_ctx_t * ctx = (struct pmsm_limits_ctx_t *)ctx_ptr;
struct pmsm_limits_pin_ctx_t *pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr;
float p = (int)MAX(PIN(polecount), 1.0);
//float ld = MAX(PIN(ld), 0.0001);
float lq = MAX(PIN(lq), 0.0001);
float psi = MAX(PIN(psi), 0.01);
float r = MAX(PIN(r), 0.001);
float iq = PIN(iq); // TODO id
float indq = PIN(indq); // TODO vel * (psi + id ld)
float volt = PIN(ac_volt);
float abs_max_cur = volt / r;
float abs_max_vel = volt / psi / p;
float abs_max_torque = 3.0 / 2.0 * p * psi * abs_max_cur;
float next_max_cur = iq + (volt - r * iq - indq) / lq * period * 2.0 / 3.0;
float next_min_cur = iq + (-volt - r * iq - indq) / lq * period * 2.0 / 3.0;
float next_max_torque = 3.0 / 2.0 * p * (psi * next_max_cur);
float next_min_torque = 3.0 / 2.0 * p * (psi * next_min_cur);
float max_cur = (volt - indq) / r;
float min_cur = (-volt - indq) / r;
float max_torque = 3.0 / 2.0 * p * (psi * max_cur);
float min_torque = 3.0 / 2.0 * p * (psi * min_cur);
PIN(abs_max_cur) = abs_max_cur;
PIN(abs_max_vel) = abs_max_vel;
PIN(abs_max_torque) = abs_max_torque;
PIN(next_max_cur) = next_max_cur;
PIN(next_min_cur) = next_min_cur;
PIN(next_max_torque) = next_max_torque;
PIN(next_min_torque) = next_min_torque;
PIN(max_cur) = max_cur;
PIN(min_cur) = min_cur;
PIN(max_torque) = max_torque;
PIN(min_torque) = min_torque;
2017-06-30 19:41:32 +00:00
}
2015-09-13 21:38:15 +00:00
2017-06-30 19:41:32 +00:00
hal_comp_t pmsm_limits_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "pmsm_limits",
.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_limits_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-06-30 19:41:32 +00:00
};