SVPWM算法c代码

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;
    }
  }

  • 0
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值