typedef struct {
float u_d;
float u_q;
float theta;
float u_alpha;
float u_beta;
float t_a;
float t_b;
float t_c;
float i_a;
float i_b;
float i_c;
float i_alpha;
float i_beta;
float i_d;
float i_q;
float theta;
float sine;
float cosine;
}FOC;
void ipark(FOC foc) {
foc.sine = sin(foc.theta);
foc.cosine = cos(foc.theta);
foc.u_alpha = foc.u_d * foc.cosine - foc.u_q * foc.sine;
foc.u_beta = foc.u_q * foc.cosine + foc.u_d * foc.sine;
}
void clarke(FOC foc) {
foc.i_alpha = foc.i_a;
foc.i_beta = (foc.i_a + 2 * foc.i_b) * 0.5773502691896257;
}
void park(FOC foc) {
foc.sine = sin(foc.theta);
foc.cosine = cos(foc.theta);
foc.i_d = foc.i_alpha * foc.cosine + foc.i_beta * foc.sine;
foc.i_q = foc.i_beta * foc.cosine - foc.i_alpha * foc.sine;
}
void svpwm(FOC foc) {
constexpr float ts = 1;
float k_svpwm;
float u1 = foc.u_beta;
float u2 = -0.8660254037844386 * foc.u_alpha - 0.5 * foc.u_beta;
float u3 = 0.8660254037844386 * foc.u_alpha - 0.5 * foc.u_beta;
unsigned char sector = (u1 > 0.0) + ((u2 > 0.0) << 1) + ((u3 > 0.0) << 2);
switch(sector){
case 5:
float t4 = u3;
float t6 = u1;
float t_sum = t4 + t6;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t4 = k_svpwm * t4;
t6 = k_svpwm * t6;
}
float t7 = (ts - t4 - t6) / 2;
t_a = t4 + t6 + t7;
t_b = t6 + t7;
t_c = t7;
break;
case 1:
float t2 = -u3;
float t6 = -u2;
float t_sum = t2 + t6;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t2 = k_svpwm * t2;
t6 = k_svpwm * t6;
}
float t7 = (ts - t2 - t6) / 2;
t_a = t6 + t7;
t_b = t2 + t6 + t7;
t_c = t7;
break;
case 3:
float t2 = u1;
float t3 = u2;
float t_sum = t2 + t3;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t2 = k_svpwm * t2;
t3 = k_svpwm * t3;
}
float t7 = (ts - t2 - t3) / 2;
t_a = t7;
t_b = t2 + t3 + t7;
t_c = t3 + t7;
break;
case 2:
float t1 = -u1;
float t3 = -u3;
float t_sum = t1 + t3;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t1 = k_svpwm * t1;
t3 = k_svpwm * t3;
}
float t7 = (ts - t1 - t3) / 2;
t_a = t7;
t_b = t3 + t7;
t_c = t1 + t3 + t7;
break;
case 6:
float t1 = u2;
float t5 = u3;
float t_sum = t1 + t5;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t1 = k_svpwm * t1;
t5 = k_svpwm * t5;
}
float t7 = (ts - t1 - t5) / 2;
t_a = t5 + t7;
t_b = t7;
t_c = t1 + t5 + t7;
break;
case 4:
float t4 = -u2;
float t5 = -u1;
float t_sum = t4 + t5;
if (t_sum > ts) {
k_svpwm = ts / t_sum;
t4 = k_svpwm * t4;
t5 = k_svpwm * t5;
}
float t7 = (ts - t4 - t5) / 2;
t_a = t4 + t5 + t7;
t_b = t7;
t_c = t5 + t7;
break;
}
}