MC_FOC_Driver.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. IdFluxLessRef = 0;
  43. }
  44. else if( ADC1_Result[ADC1_RANK_VIN] < 3310 ) //59417mV 3310
  45. {
  46. FOC_IqLim = 1050 - (ADC1_Result[ADC1_RANK_VIN] - 3100)*5;
  47. IqFluxLessRef = Ref<FOC_IqLim ? Ref : FOC_IqLim;
  48. IdFluxLessRef -= 2;
  49. if(IdFluxLessRef<-420) IdFluxLessRef = -420;
  50. }
  51. else
  52. {
  53. IqFluxLessRef=0;
  54. IdFluxLessRef = -420;
  55. }
  56. UqVoltTmp = PID_Regulator(IqFluxLessRef,\
  57. IqFdb,\
  58. &PID_Torque_InitStructure); // 电流闭环输出q轴电压
  59. UqVoltFlt += ((UqVoltTmp << 9) - UqVoltFlt) >> 3;
  60. /*
  61. UqCal = DbSpdMotor*VMax*0.7/1020
  62. = DbSpdMotor*VMax/(1020/0.7)
  63. = DbSpdMotor*VMax/1020
  64. */
  65. #if 1 //加入前馈
  66. uint16_t Cal_K;
  67. Cal_K = (MC_MotorParam.Rate_Speed * 183) >> 7; //电机转速 * 1.43
  68. UqCal = ((int32_t)MotorSpeed * MAX_MODULE) / ((Cal_K < 1000) ? 1000 : Cal_K);
  69. UqVoltTmp = UqCal + (UqVoltFlt >> 9);
  70. #else //去掉前馈
  71. UqVoltTmp = UqVoltFlt >> 9;
  72. #endif
  73. UqVoltTmp = (UqVoltTmp > ((int32_t)MAX_MODULE)) ? MAX_MODULE : UqVoltTmp;
  74. Stat_Volt_q_d.qV_Component1 = UqVoltTmp;
  75. UdVoltTmp = PID_Regulator(IdFluxLessRef,\
  76. IdFdb,\
  77. &PID_Flux_InitStructure); // 电流闭环输出d轴电压
  78. UdVoltFlt += ((UdVoltTmp << 9) - UdVoltFlt) >> 3;
  79. Stat_Volt_q_d.qV_Component2 = UdVoltFlt >> 9;
  80. //circle limitation
  81. RevPark_Circle_Limitation(&Stat_Volt_q_d); // 电压极限圈限制? 会不会出现波动情况?
  82. /*Performs the Reverse Park transformation,
  83. i.e transforms stator voltages Vqs and Vds into Valpha and Vbeta on a
  84. stationary reference frame*/
  85. Stat_Volt_alfa_beta = Rev_Park(Stat_Volt_q_d);
  86. /*Valpha and Vbeta finally drive the power stage*/
  87. SVPWM_3ShuntCalcDutyCycles(Stat_Volt_alfa_beta); //实际的电流输出控制
  88. }
  89. void FOC_Enable(void)
  90. {
  91. FOC_Status = FOC_Status_RUN;
  92. }
  93. void FOC_Disable(void)
  94. {
  95. FOC_Status = FOC_Status_WAIT;
  96. Stat_Curr_q_d.qI_Component1 = 0;
  97. Stat_Curr_q_d.qI_Component2 = 0;
  98. Stat_Volt_q_d.qV_Component1 = 0;
  99. Stat_Volt_q_d.qV_Component2 = 0;
  100. }