1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-20 15:42:18 +00:00
stmbl/shared/comps/dq.c

105 lines
1.8 KiB
C
Raw Permalink Normal View History

#include "dq_comp.h"
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(dq);
2017-09-03 03:00:21 +00:00
HAL_PIN(mode);
2017-04-15 23:38:58 +00:00
//U V W inputs
HAL_PIN(u);
HAL_PIN(v);
HAL_PIN(w);
//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,gamma output
HAL_PIN(a);
HAL_PIN(b);
HAL_PIN(y);
//d,q output
HAL_PIN(d);
HAL_PIN(q);
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 dq_ctx_t * ctx = (struct dq_ctx_t *)ctx_ptr;
struct dq_pin_ctx_t *pins = (struct dq_pin_ctx_t *)pin_ptr;
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
float u = PIN(u);
float v = PIN(v);
float w = PIN(w);
//clarke transformation
2017-09-03 03:00:21 +00:00
float a, b, y;
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
a = u - v;
b = w - v;
y = u / 3.0 + v / 3.0 + w / 3.0;
2017-09-06 02:20:06 +00:00
break;
2017-09-03 03:00:21 +00:00
2017-09-06 02:20:06 +00:00
case PHASE_120_3PH: // 120°
2017-09-03 03:00:21 +00:00
a = u * 2.0 / 3.0 - v / 3.0 - w / 3.0;
b = v / M_SQRT3 - w / M_SQRT3;
y = u / 3.0 + v / 3.0 + w / 3.0;
2017-09-06 02:20:06 +00:00
break;
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
case PHASE_180_2PH: // 180°
2017-09-03 03:00:21 +00:00
a = 0;
b = (u - w) / 2.0;
y = (u + w) / 2.0;
2017-09-06 02:20:06 +00:00
break;
case PHASE_180_3PH: // 180°
2017-09-06 00:00:49 +00:00
a = v;
b = (u - w) / 2.0;
y = (u + w) / 2.0;
2017-09-06 02:20:06 +00:00
break;
2017-09-03 03:00:21 +00:00
default:
a = 0.0;
b = 0.0;
y = 0.0;
2017-09-06 02:20:06 +00:00
}
float p = (int)MAX(PIN(polecount), 1.0);
float pos = PIN(pos) * p;
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
float si = 0.0;
float co = 0.0;
sincos_fast(pos, &si, &co);
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
//park transformation
float d = a * co + b * si;
float q = -a * si + b * co;
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
PIN(a) = a;
PIN(b) = b;
PIN(y) = y;
2017-04-15 23:38:58 +00:00
2017-09-06 02:20:06 +00:00
PIN(d) = d;
PIN(q) = q;
2017-04-15 23:38:58 +00:00
}
hal_comp_t dq_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "dq",
.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 dq_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-04-15 23:38:58 +00:00
};