mokongking

FOC系列电机入门知识

 

原文链接:https:/

/*
 * foc.c
 *
 *  Created on: 2024年9月1日
 *      Author: 13624
 */
#include"main.h"
#include "foc.h"
//帕克变换
  float u_d;//d坐标系电压
  float u_q;//q坐标系电压
  float theta;//电角度
//克拉克变换
  float u_alpha;//
  float u_beta;//
//svpwm
  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;


  void ipark() {
      sine = sin(theta);
      cosine = cos(theta);
      u_alpha = u_d * cosine - u_q * sine;
      u_beta = u_q * cosine + u_d * sine;
    }

    void ipark2() {
      u_alpha = u_d * cosine - u_q * sine;
      u_beta = u_q * cosine + u_d * sine;
    }

    void clarke() {
      i_alpha = i_a;
      i_beta = (i_a + 2 * i_b) *0.5773502691896257;//sqrt(3) = 0.5773502691896257;
    }

    void park() {
      sine = sin(theta);
      cosine = cos(theta);
      i_d = i_alpha * cosine + i_beta * sine;
      i_q = i_beta * cosine - i_alpha * sine;
    }

    void svpwm() {
      const float ts = 1;

      float u1 = u_beta;
      float u2 = -0.8660254037844386 * u_alpha - 0.5 * u_beta;
      float u3 = 0.8660254037844386 * u_alpha - 0.5 * u_beta;

      uint8_t sector = (u1 > 0.0) + ((u2 > 0.0) << 1) + ((u3 > 0.0) << 2);

      if (sector == 5) {
        float t4 = u3;
        float t6 = u1;
        float sum = t4 + t6;
        if (sum > ts) {
          k_svpwm = ts / 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;
      } else if (sector == 1) {
        float t2 = -u3;
        float t6 = -u2;
        float sum = t2 + t6;
        if (sum > ts) {
          k_svpwm = ts / 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;
      } else if (sector == 3) {
        float t2 = u1;
        float t3 = u2;
        float sum = t2 + t3;
        if (sum > ts) {
          k_svpwm = ts / 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;
      } else if (sector == 2) {
        float t1 = -u1;
        float t3 = -u3;
        float sum = t1 + t3;
        if (sum > ts) {
          k_svpwm = ts / 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;
      } else if (sector == 6) {
        float t1 = u2;
        float t5 = u3;
        float sum = t1 + t5;
        if (sum > ts) {
          k_svpwm = ts / 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;
      } else if (sector == 4) {
        float t4 = -u2;
        float t5 = -u1;
        float sum = t4 + t5;
        if (sum > ts) {
          k_svpwm = ts / 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;
      }
    }
    void Foc_Run()
    {
         for (double theta = 0; theta < 7; theta += 1e-3) {
        foc.u_d = 0.1;
        foc.u_q = 0;
        foc.theta = theta;
        foc.ipark();
        foc.svpwm();
        double u_a = foc.t_a - 0.5 * (foc.t_b + foc.t_c);
        double u_b = foc.t_b - 0.5 * (foc.t_a + foc.t_c);
        double u_c = -(u_a + u_b);
        __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,u_a*4200);
        __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,u_b*4200);
        __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,u_c*4200);

    }

/*
* foc.h
*
* Created on: 2024年9月1日
* Author: 13624
*/

#ifndef INC_FOC_H_
#define INC_FOC_H_

//帕克变换
extern float u_d;//d坐标系电压
extern float u_q;//q坐标系电压
extern float theta;//电角度
//克拉克变换
extern float u_alpha;//
extern float u_beta;//
//svpwm
extern float t_a;
extern float t_b;
extern float t_c;

extern float i_a;
extern float i_b;
extern float i_c;

extern float i_alpha;
extern float i_beta;

float i_d;
float i_q;


void ipark() ;

void ipark2();

void clarke() ;

void park() ;

void svpwm() ;
void Foc_Run();


#endif /* INC_FOC_H_ */

/blog.csdn.net/qq_35947329/article/details/115483413

posted on 2024-09-01 22:35  虎啸岳林  阅读(14)  评论(0编辑  收藏  举报

导航