1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2025-01-01 13:32:10 +00:00
stmbl/shared/comps/acim_ttc.c

186 lines
4.1 KiB
C
Raw Permalink Normal View History

#include "acim_ttc_comp.h"
2017-09-03 03:01:39 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(acim_ttc);
2019-06-03 21:20:33 +00:00
HAL_PIN(mode); // 0 = slip, 1 = mtpa, 2 = u/f
HAL_PIN(sensorless);
2017-09-04 13:59:07 +00:00
2017-09-03 03:01:39 +00:00
// motor values
HAL_PIN(torque_n);
HAL_PIN(cur_n);
2019-06-03 22:01:10 +00:00
HAL_PIN(slip_n);
2017-09-03 03:01:39 +00:00
HAL_PIN(polecount);
2019-05-09 17:05:25 +00:00
HAL_PIN(freq_n);
2019-06-03 21:20:33 +00:00
HAL_PIN(vel_n);
2019-05-09 17:05:25 +00:00
HAL_PIN(u_n);
HAL_PIN(u_boost);
2019-06-03 21:20:33 +00:00
HAL_PIN(t_boost);
HAL_PIN(s_boost);
2017-09-03 03:01:39 +00:00
// torque cmd in
HAL_PIN(torque);
2019-06-03 21:20:33 +00:00
HAL_PIN(vel_m);
2017-09-03 03:01:39 +00:00
// cur cmd out
2019-05-09 17:05:25 +00:00
HAL_PIN(d_cmd);
HAL_PIN(q_cmd);
HAL_PIN(cmd_mode);
2017-09-03 03:01:39 +00:00
HAL_PIN(pos);
2019-06-03 21:20:33 +00:00
HAL_PIN(vel_e);
HAL_PIN(slip);
HAL_PIN(t_min);
HAL_PIN(t_max);
2019-06-27 20:41:57 +00:00
HAL_PIN(scale);
HAL_PIN(ki);
HAL_PIN(duty);
HAL_PIN(duty_setpoint);
2019-06-03 21:20:33 +00:00
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
struct acim_ttc_pin_ctx_t *pins = (struct acim_ttc_pin_ctx_t *)pin_ptr;
PIN(polecount) = 2;
PIN(vel_n) = 1745.0 / 60.0 * 2.0 * M_PI;
PIN(torque_n) = 23.0;
PIN(cur_n) = 17.0;
PIN(freq_n) = 60.0;
PIN(u_n) = 80.0;
PIN(u_boost) = 7.0;
PIN(t_boost) = 1.3;
PIN(s_boost) = 2.5;
PIN(mode) = 0;
PIN(sensorless) = 0;
2019-06-27 20:41:57 +00:00
PIN(scale) = 1.0;
PIN(ki) = 50.0;
PIN(duty_setpoint) = 0.9;
2019-06-03 21:20:33 +00:00
}
2017-09-03 03:01:39 +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 acim_ttc_ctx_t * ctx = (struct acim_ttc_ctx_t *)ctx_ptr;
struct acim_ttc_pin_ctx_t *pins = (struct acim_ttc_pin_ctx_t *)pin_ptr;
2017-09-03 03:01:39 +00:00
2017-09-06 02:20:06 +00:00
float poles = MAX(PIN(polecount), 1.0);
2019-06-03 21:20:33 +00:00
float vel_n = PIN(vel_n) * poles;
float t_n = MAX(PIN(torque_n), 0.001);
2019-05-09 17:05:25 +00:00
float freq_n = MAX(PIN(freq_n), 1.0);
2019-06-27 20:41:57 +00:00
float slip_n = freq_n * 2.0 * M_PI - vel_n;
2019-06-03 21:20:33 +00:00
float cur_n = PIN(cur_n);
2019-05-09 17:05:25 +00:00
float u_n = PIN(u_n);
float u_boost = PIN(u_boost);
2019-06-03 21:20:33 +00:00
float t_boost = PIN(t_boost);
float torque = PIN(torque);
float vel = 0.0;
if(PIN(sensorless) > 0.0){
vel = PIN(vel_e);
}
else{
vel = PIN(vel_m) * poles;
}
2017-09-04 13:59:07 +00:00
2019-06-03 21:20:33 +00:00
float d_cmd = 0.0;
float q_cmd = 0.0;
float slip = 0.0;
2019-05-09 17:05:25 +00:00
float cmd_mode = 0;
2017-09-04 13:59:07 +00:00
2019-06-03 21:20:33 +00:00
float id_n = cur_n / sqrtf(2.0);
2019-06-27 20:41:57 +00:00
PIN(scale) += (PIN(duty_setpoint) - PIN(duty)) * PIN(ki) * period;
PIN(scale) = CLAMP(PIN(scale), 0.01, 1);
2017-09-06 02:20:06 +00:00
switch((int)PIN(mode)) {
2019-05-09 17:05:25 +00:00
case 0: // slip control
cmd_mode = 1.0; // cur cmd
2019-06-27 20:41:57 +00:00
// d_cmd = MIN(id_n, id_n * freq_n * 2.0 * M_PI * v_boost / vel); // constant flux
d_cmd = id_n * PIN(scale);
q_cmd = id_n / t_n * torque / PIN(scale);
slip = slip_n * q_cmd / d_cmd;
// id = id_n
// slip = slip_n * iq / id
// torque = 3/2 * p * K * iq * id
// id = id_n * scale
// // iq = toruqe / t_n * id_n * id_n / id
// iq = toruqe / t_n * id_n / scale
// slip = slip_n * iq / id
// torque = 3/2 * p * K * iq * id
// torque = t_n / id_n / id_n * id * iq
// iq = toruqe / t_n * id_n * id_n / id
2017-09-04 13:59:07 +00:00
break;
2019-05-09 17:05:25 +00:00
case 1: // mtpa
cmd_mode = 1.0; // cur cmd
2019-06-27 20:41:57 +00:00
d_cmd = 0.0;
q_cmd = cur_n / t_n * torque;
2017-09-06 02:20:06 +00:00
slip = slip_n * SIGN(torque); // constant slip
break;
2017-09-04 13:59:07 +00:00
2019-06-03 21:20:33 +00:00
case 2: // u/f slip
2019-05-09 17:05:25 +00:00
cmd_mode = 0; // volt cmd
2019-06-03 21:20:33 +00:00
slip = slip_n / t_n * torque;
d_cmd = MAX(u_n / freq_n * ABS(vel / 2.0 / M_PI), u_boost);
2019-05-09 17:05:25 +00:00
q_cmd = 0.0;
break;
2017-09-06 02:20:06 +00:00
default:
2019-05-09 17:05:25 +00:00
cmd_mode = 1.0; // cur cmd
2019-06-27 20:41:57 +00:00
d_cmd = 0;
q_cmd = 0;
2017-09-06 02:20:06 +00:00
slip = 0.0;
}
2017-09-04 13:59:07 +00:00
2019-06-03 21:20:33 +00:00
float t_min = 0;
float t_max = 0;
if(PIN(vel_m) > 0.0){
2019-06-27 20:41:57 +00:00
t_max = t_n * t_boost * PIN(scale);
2019-06-29 06:34:59 +00:00
t_min = -t_max;
2019-06-03 21:20:33 +00:00
}
else{
2019-06-27 20:41:57 +00:00
t_min = -t_n * t_boost * PIN(scale);
2019-06-29 06:34:59 +00:00
t_max = -t_min;
2019-06-03 21:20:33 +00:00
}
2019-06-27 20:41:57 +00:00
slip = LIMIT(slip, slip_n * PIN(s_boost));
2019-06-03 21:20:33 +00:00
if(PIN(sensorless) > 0.0){
vel -= slip;
PIN(vel_m) = vel * poles;
}
else{
vel += slip;
PIN(vel_e) = vel;
}
PIN(t_min) = t_min;
PIN(t_max) = t_max;
2017-09-03 03:01:39 +00:00
2019-05-09 17:05:25 +00:00
PIN(cmd_mode) = cmd_mode;
PIN(d_cmd) = d_cmd;
PIN(q_cmd) = q_cmd;
2019-06-03 21:20:33 +00:00
2019-06-03 22:01:10 +00:00
PIN(slip_n) = slip_n;
2017-09-06 02:20:06 +00:00
PIN(slip) = slip;
2019-06-03 21:20:33 +00:00
PIN(pos) = mod(PIN(pos) + vel * period);
2017-09-03 03:01:39 +00:00
}
hal_comp_t acim_ttc_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "acim_ttc",
.nrt = 0,
.rt = rt_func,
.frt = 0,
2019-06-03 21:20:33 +00:00
.nrt_init = nrt_init,
2017-09-06 02:20:06 +00:00
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = 0,
.pin_count = sizeof(struct acim_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-09-03 03:01:39 +00:00
};