stmbl/shared/comps/idq.c

103 lines
1.8 KiB
C
Raw Permalink Normal View History

2017-04-15 23:38:58 +00:00
#include "commands.h"
2017-09-06 00:00:49 +00:00
#include "common.h"
2017-04-15 23:38:58 +00:00
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(idq);
2017-09-03 03:00:21 +00:00
HAL_PIN(mode);
2017-04-15 23:38:58 +00:00
//d,q inputs
HAL_PIN(d);
HAL_PIN(q);
//rotor position
HAL_PIN(pos);
2017-09-06 02:20:06 +00:00
HAL_PIN(polecount); //1
2017-04-15 23:38:58 +00:00
//a,b output
HAL_PIN(a);
HAL_PIN(b);
//U V W output
HAL_PIN(u);
HAL_PIN(v);
HAL_PIN(w);
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 idq_ctx_t * ctx = (struct idq_ctx_t *)ctx_ptr;
struct idq_pin_ctx_t *pins = (struct idq_pin_ctx_t *)pin_ptr;
2017-04-15 23:38:58 +00:00
float d = PIN(d);
float q = PIN(q);
2017-09-06 02:20:06 +00:00
float p = (int)MAX(PIN(polecount), 1.0);
2017-04-15 23:38:58 +00:00
float pos = PIN(pos) * p;
float si = 0.0;
float co = 0.0;
sincos_fast(pos, &si, &co);
//inverse park transformation
float a = d * co - q * si;
float b = d * si + q * co;
2017-09-06 02:20:06 +00:00
2017-04-15 23:38:58 +00:00
//inverse clarke transformation
2017-09-03 03:00:21 +00:00
float u, v, w;
2017-09-06 02:20:06 +00:00
switch((int)PIN(mode)) {
case PHASE_90_3PH: // 90°
2017-09-03 03:00:21 +00:00
u = a;
v = 0.0;
w = b;
break;
2017-09-06 02:20:06 +00:00
case PHASE_120_3PH: // 120°
2017-09-06 00:00:49 +00:00
u = a;
2017-09-06 02:20:06 +00:00
v = -a / 2.0 + b / 2.0 * M_SQRT3;
w = -a / 2.0 - b / 2.0 * M_SQRT3;
2017-09-03 03:00:21 +00:00
break;
2017-09-06 02:20:06 +00:00
case PHASE_180_2PH: // 180°
2017-09-03 03:00:21 +00:00
u = b / 2.0;
v = 0.0;
2017-09-06 02:20:06 +00:00
w = -b / 2.0;
2017-09-03 03:00:21 +00:00
break;
2017-09-06 00:00:49 +00:00
2017-09-06 02:20:06 +00:00
case PHASE_180_3PH: // 180°
2017-09-06 00:00:49 +00:00
u = b / 2.0;
v = a;
2017-09-06 02:20:06 +00:00
w = -b / 2.0;
2017-09-06 00:00:49 +00:00
break;
2017-09-06 02:20:06 +00:00
2017-09-03 03:00:21 +00:00
default:
u = 0.0;
v = 0.0;
w = 0.0;
2017-09-06 02:20:06 +00:00
}
2017-04-15 23:38:58 +00:00
PIN(a) = a;
PIN(b) = b;
PIN(u) = u;
PIN(v) = v;
PIN(w) = w;
}
hal_comp_t idq_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "idq",
.nrt = 0,
.rt = rt_func,
.frt = 0,
.nrt_init = 0,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = 0,
.pin_count = sizeof(struct idq_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-04-15 23:38:58 +00:00
};