2020-09-23 18:59:59 +00:00
|
|
|
#include "curpid_comp.h"
|
2017-04-15 18:54:21 +00:00
|
|
|
#include "commands.h"
|
2017-09-06 00:00:49 +00:00
|
|
|
#include "common.h"
|
2017-04-15 18:54:21 +00:00
|
|
|
#include "hal.h"
|
|
|
|
#include "math.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "angle.h"
|
|
|
|
|
|
|
|
HAL_COMP(curpid);
|
|
|
|
|
2017-08-10 19:03:17 +00:00
|
|
|
// enable
|
|
|
|
HAL_PIN(en);
|
2017-09-06 00:00:49 +00:00
|
|
|
HAL_PIN(cmd_mode);
|
2017-08-10 19:03:17 +00:00
|
|
|
|
2017-04-15 18:54:21 +00:00
|
|
|
// current command
|
|
|
|
HAL_PIN(id_cmd);
|
|
|
|
HAL_PIN(iq_cmd);
|
|
|
|
|
|
|
|
// current feedback
|
|
|
|
HAL_PIN(id_fb);
|
|
|
|
HAL_PIN(iq_fb);
|
|
|
|
|
2019-01-17 03:53:22 +00:00
|
|
|
// HAL_PIN(ac_current);
|
2017-11-04 01:26:51 +00:00
|
|
|
|
2017-04-15 18:54:21 +00:00
|
|
|
// voltage output
|
|
|
|
HAL_PIN(ud);
|
|
|
|
HAL_PIN(uq);
|
|
|
|
|
|
|
|
// maximum output current and voltage
|
|
|
|
HAL_PIN(max_cur);
|
|
|
|
HAL_PIN(pwm_volt);
|
|
|
|
|
|
|
|
// d, q resistance and inductance
|
2019-06-29 06:34:18 +00:00
|
|
|
HAL_PIN(r);
|
2017-04-15 18:54:21 +00:00
|
|
|
HAL_PIN(ld);
|
|
|
|
HAL_PIN(lq);
|
|
|
|
|
|
|
|
// torque constant
|
|
|
|
HAL_PIN(psi);
|
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
HAL_PIN(ff); // r feed forward
|
2017-04-15 18:54:21 +00:00
|
|
|
HAL_PIN(kp);
|
|
|
|
HAL_PIN(ki);
|
2019-06-29 06:34:18 +00:00
|
|
|
HAL_PIN(ksp); // predictor
|
2017-09-06 02:20:06 +00:00
|
|
|
HAL_PIN(kind); // bemf feed forward
|
2019-06-29 06:34:18 +00:00
|
|
|
HAL_PIN(kci);
|
|
|
|
|
|
|
|
HAL_PIN(scale);
|
2017-04-15 18:54:21 +00:00
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
HAL_PIN(vel); // velocity input
|
2017-04-15 18:54:21 +00:00
|
|
|
|
|
|
|
// current error outputs
|
|
|
|
HAL_PIN(id_error);
|
|
|
|
HAL_PIN(iq_error);
|
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
struct curpid_ctx_t {
|
2017-04-15 18:54:21 +00:00
|
|
|
float id_error_sum;
|
|
|
|
float iq_error_sum;
|
|
|
|
};
|
|
|
|
|
2019-04-29 20:08:54 +00:00
|
|
|
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
|
2017-04-15 18:54:21 +00:00
|
|
|
// struct curpid_ctx_t * ctx = (struct curpid_ctx_t *)ctx_ptr;
|
2017-09-06 02:20:06 +00:00
|
|
|
struct curpid_pin_ctx_t *pins = (struct curpid_pin_ctx_t *)pin_ptr;
|
|
|
|
|
2019-06-29 06:34:18 +00:00
|
|
|
PIN(r) = 0.5;
|
2017-09-06 02:20:06 +00:00
|
|
|
PIN(ld) = 0.01;
|
|
|
|
PIN(lq) = 0.01;
|
2017-04-15 18:54:21 +00:00
|
|
|
PIN(psi) = 0.05;
|
2017-09-06 02:20:06 +00:00
|
|
|
PIN(kp) = 0.1;
|
|
|
|
PIN(ki) = 0.005;
|
2019-06-29 06:34:18 +00:00
|
|
|
PIN(kci) = 500.0;
|
|
|
|
PIN(ksp) = 1.0;
|
|
|
|
PIN(scale) = 1.0;
|
2017-04-15 18:54:21 +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 curpid_ctx_t *ctx = (struct curpid_ctx_t *)ctx_ptr;
|
|
|
|
struct curpid_pin_ctx_t *pins = (struct curpid_pin_ctx_t *)pin_ptr;
|
|
|
|
|
2019-06-29 06:34:18 +00:00
|
|
|
float r = MAX(PIN(r), 0.1);
|
2017-09-06 02:20:06 +00:00
|
|
|
float ld = MAX(PIN(ld), 0.001);
|
|
|
|
float lq = MAX(PIN(lq), 0.001);
|
|
|
|
|
|
|
|
float ff = PIN(ff);
|
|
|
|
float kind = PIN(kind);
|
|
|
|
float kpd = ld * PIN(kp) / period / 2.0;
|
2019-06-29 06:34:18 +00:00
|
|
|
float kid = r * PIN(ki) / ld;
|
2017-09-06 02:20:06 +00:00
|
|
|
float kpq = lq * PIN(kp) / period / 2.0;
|
2019-06-29 06:34:18 +00:00
|
|
|
float kiq = r * PIN(ki) / lq;
|
2017-09-06 02:20:06 +00:00
|
|
|
|
2019-02-08 15:48:18 +00:00
|
|
|
float max_cur = MAX(PIN(max_cur), 0.01);
|
|
|
|
float idc = PIN(id_cmd);
|
|
|
|
float iqc = PIN(iq_cmd);
|
2017-09-06 02:20:06 +00:00
|
|
|
|
|
|
|
float max_volt = PIN(pwm_volt);
|
|
|
|
|
|
|
|
float id = PIN(id_fb);
|
|
|
|
float iq = PIN(iq_fb);
|
|
|
|
|
2019-01-17 03:53:22 +00:00
|
|
|
// float ac_current = id * id + iq * iq; //sqrtf(id * id + iq * iq);
|
|
|
|
// PIN(ac_current) = ac_current;
|
2017-11-04 01:26:51 +00:00
|
|
|
|
2019-02-08 15:48:18 +00:00
|
|
|
float abscur;
|
2019-06-29 06:34:18 +00:00
|
|
|
float absvolt;
|
|
|
|
|
2019-02-08 15:48:18 +00:00
|
|
|
if(PIN(cmd_mode) == VOLT_MODE) {
|
2019-06-29 06:34:18 +00:00
|
|
|
absvolt = idc * idc + iqc * iqc; // clamp cmd
|
|
|
|
PIN(scale) *= sqrtf(CLAMP(max_volt * max_volt / MAX(absvolt, max_volt * 0.1), 0.0, 1.0));
|
|
|
|
|
2019-02-08 15:48:18 +00:00
|
|
|
abscur = id * id + iq * iq; // clamp over fb
|
2019-06-29 06:34:18 +00:00
|
|
|
PIN(scale) += (max_cur * max_cur - abscur) * PIN(kci) * period;
|
2019-02-08 15:48:18 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
abscur = idc * idc + iqc * iqc; // clamp cmd
|
2019-06-29 06:34:18 +00:00
|
|
|
PIN(scale) = sqrtf(max_cur * max_cur / MAX(abscur, max_cur * 0.1));
|
2017-11-04 01:26:51 +00:00
|
|
|
}
|
2019-06-29 06:34:18 +00:00
|
|
|
PIN(scale) = CLAMP(PIN(scale), 0.0, 1.0);
|
|
|
|
|
|
|
|
idc *= PIN(scale);
|
|
|
|
iqc *= PIN(scale);
|
2019-02-08 15:48:18 +00:00
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
float vel = PIN(vel);
|
|
|
|
float psi_d = ld * id + PIN(psi);
|
|
|
|
float psi_q = lq * iq;
|
|
|
|
float indd = vel * psi_q;
|
|
|
|
float indq = vel * psi_d;
|
|
|
|
|
2019-06-29 06:34:18 +00:00
|
|
|
// predictor to cancel pwm delay
|
|
|
|
id += (PIN(ud) - r * id + indd) / ld * period * PIN(ksp);
|
|
|
|
iq += (PIN(uq) - r * iq - indq) / lq * period * PIN(ksp);
|
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
float id_error = idc - id;
|
|
|
|
float iq_error = iqc - iq;
|
|
|
|
|
2019-06-29 06:34:18 +00:00
|
|
|
float ud = LIMIT(ff * r * idc - kind * indd + kpd * id_error, max_volt);
|
|
|
|
float uq = LIMIT(ff * r * iqc + kind * indq + kpq * iq_error, max_volt);
|
2017-09-06 02:20:06 +00:00
|
|
|
|
|
|
|
if(kpd * kid > 0.0 && kpq * kiq > 0.0) {
|
2019-06-29 06:34:18 +00:00
|
|
|
ctx->id_error_sum = LIMIT(ctx->id_error_sum + kpd * kid * id_error * period, max_volt - ud);
|
|
|
|
ctx->iq_error_sum = LIMIT(ctx->iq_error_sum + kpq * kiq * iq_error * period, max_volt - uq);
|
2017-09-06 02:20:06 +00:00
|
|
|
} else {
|
2017-09-06 00:00:49 +00:00
|
|
|
ctx->id_error_sum = 0.0;
|
|
|
|
ctx->iq_error_sum = 0.0;
|
2017-09-06 02:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ud += ctx->id_error_sum;
|
|
|
|
uq += ctx->iq_error_sum;
|
|
|
|
|
|
|
|
if(PIN(cmd_mode) == VOLT_MODE) {
|
2019-06-29 06:34:18 +00:00
|
|
|
ud = idc;
|
|
|
|
uq = iqc;
|
2017-09-06 02:20:06 +00:00
|
|
|
ctx->id_error_sum = 0.0;
|
|
|
|
ctx->iq_error_sum = 0.0;
|
|
|
|
id_error = 0.0;
|
|
|
|
iq_error = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PIN(en) <= 0.0) {
|
|
|
|
ud = 0.0;
|
|
|
|
uq = 0.0;
|
|
|
|
ctx->id_error_sum = 0.0;
|
|
|
|
ctx->iq_error_sum = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIN(ud) = ud;
|
|
|
|
PIN(uq) = uq;
|
|
|
|
|
|
|
|
PIN(id_error) = id_error;
|
|
|
|
PIN(iq_error) = iq_error;
|
2017-04-15 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hal_comp_t curpid_comp_struct = {
|
2017-09-06 02:20:06 +00:00
|
|
|
.name = "curpid",
|
|
|
|
.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 = sizeof(struct curpid_ctx_t),
|
|
|
|
.pin_count = sizeof(struct curpid_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
|
2017-04-15 18:54:21 +00:00
|
|
|
};
|