MC_FOC_Driver.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "MC_FOC_Driver.h"
  2. #include "MC_type.h"
  3. #include "MC_Globals.h"
  4. #include "MC_PID_regulators.h"
  5. #include "MC_Clarke_park.h"
  6. #include "stm32f10x_svpwm_3shunt.h"
  7. #define FluxWeak_Control 0
  8. //全局变量定义
  9. int32_t IqFdbFlt = 0;
  10. int32_t IdFdbFlt = 0;
  11. int32_t VoltSquareFlt = 0;
  12. int32_t UqVoltFlt = 0;
  13. int32_t UdVoltFlt = 0;
  14. void FOC_Model(int16_t Ref, int16_t MotorSpeed, uint16_t SVM_Angle) //电流环处理函数,严格按照框图理解
  15. {
  16. int32_t UqVoltTmp;
  17. int16_t UdVoltTmp;
  18. static int16_t IqFdb = 0;
  19. static int16_t IdFdb = 0;
  20. #if FluxWeak_Control
  21. int32_t VoltSquare = 0; //弱磁控制预留
  22. int32_t VoltMax = 0;
  23. #endif
  24. static int32_t IdFluxLessRef = 0, IqFluxLessRef = 0;
  25. int32_t UqCal = 0;
  26. static int32_t FOC_IqLim = 0;
  27. Curr_Components Stat_Curr_a_b; // Stator currents Ia,Ib
  28. Curr_Components Stat_Curr_alfa_beta; // Ialpha & Ibeta, Clarke's transformations of Ia & Ib
  29. Volt_Components Stat_Volt_alfa_beta; // Valpha & Vbeta, RevPark transformations of Vq & Vd
  30. /**********STARTS THE VECTOR CONTROL *********************** */
  31. Stat_Curr_a_b = SVPWM_3ShuntGetPhaseCurrentValues(); //读取2相的电流值
  32. Stat_Curr_alfa_beta = Clarke(Stat_Curr_a_b); // 得到Ialpha和Ibeta,Clark变换
  33. Stat_Curr_q_d = Park(Stat_Curr_alfa_beta, SVM_Angle); // Stat_Curr_q_d为当前的Id和Iq值//SVM_Angle
  34. // 给定值为 Stat_Curr_q_d_ref_ref
  35. IqFdbFlt += (((int32_t)(Stat_Curr_q_d.qI_Component1 << 10)) - IqFdbFlt)>>10; // KFlt = 2ms
  36. IqFdb = IqFdbFlt >> 10;
  37. IdFdbFlt += (((int32_t)(Stat_Curr_q_d.qI_Component2 << 10)) - IdFdbFlt)>>10; // KFlt = 2ms
  38. IdFdb = IdFdbFlt >> 10;
  39. if( ADC1_Result[ADC1_RANK_VIN] < 3100) //55648mV 17.951 3100
  40. {
  41. IqFluxLessRef = Ref;
  42. if(IdFluxLessRef<(-4))
  43. {
  44. IdFluxLessRef += 4;
  45. }
  46. else IdFluxLessRef = 0;
  47. }
  48. else if( ADC1_Result[ADC1_RANK_VIN] < 3310 ) //59417mV 3310
  49. {
  50. FOC_IqLim = 1050 - (ADC1_Result[ADC1_RANK_VIN] - 3100)*5;
  51. IqFluxLessRef = Ref<FOC_IqLim ? Ref : FOC_IqLim;
  52. IdFluxLessRef -= 2;
  53. if(IdFluxLessRef<-420) IdFluxLessRef = -420;
  54. }
  55. else
  56. {
  57. IqFluxLessRef=0;
  58. IdFluxLessRef = -420;
  59. }
  60. UqVoltTmp = PID_Regulator(IqFluxLessRef,\
  61. IqFdb,\
  62. &PID_Torque_InitStructure); // 电流闭环输出q轴电压
  63. UqVoltFlt += ((UqVoltTmp << 9) - UqVoltFlt) >> 3;
  64. /*
  65. UqCal = DbSpdMotor*VMax*0.7/1020
  66. = DbSpdMotor*VMax/(1020/0.7)
  67. = DbSpdMotor*VMax/1020
  68. */
  69. #if 1 //加入前馈
  70. uint16_t Cal_K;
  71. Cal_K = (MC_MotorParam.Rate_Speed * 183) >> 7; //电机转速 * 1.43
  72. UqCal = ((int32_t)MotorSpeed * MAX_MODULE) / ((Cal_K < 1000) ? 1000 : Cal_K);
  73. UqVoltTmp = UqCal + (UqVoltFlt >> 9);
  74. #else //去掉前馈
  75. UqVoltTmp = UqVoltFlt >> 9;
  76. #endif
  77. UqVoltTmp = (UqVoltTmp > ((int32_t)MAX_MODULE)) ? MAX_MODULE : UqVoltTmp;
  78. Stat_Volt_q_d.qV_Component1 = UqVoltTmp;
  79. UdVoltTmp = PID_Regulator(IdFluxLessRef,\
  80. IdFdb,\
  81. &PID_Flux_InitStructure); // 电流闭环输出d轴电压
  82. UdVoltFlt += ((UdVoltTmp << 9) - UdVoltFlt) >> 3;
  83. Stat_Volt_q_d.qV_Component2 = UdVoltFlt >> 9;
  84. //circle limitation
  85. RevPark_Circle_Limitation(&Stat_Volt_q_d); // 电压极限圈限制? 会不会出现波动情况?
  86. /*Performs the Reverse Park transformation,
  87. i.e transforms stator voltages Vqs and Vds into Valpha and Vbeta on a
  88. stationary reference frame*/
  89. Stat_Volt_alfa_beta = Rev_Park(Stat_Volt_q_d);
  90. /*Valpha and Vbeta finally drive the power stage*/
  91. SVPWM_3ShuntCalcDutyCycles(Stat_Volt_alfa_beta); //实际的电流输出控制
  92. }
  93. void FOC_Enable(void)
  94. {
  95. FOC_Status = FOC_Status_RUN;
  96. }
  97. void FOC_Disable(void)
  98. {
  99. FOC_Status = FOC_Status_WAIT;
  100. Stat_Curr_q_d.qI_Component1 = 0;
  101. Stat_Curr_q_d.qI_Component2 = 0;
  102. Stat_Volt_q_d.qV_Component1 = 0;
  103. Stat_Volt_q_d.qV_Component2 = 0;
  104. }