MC_PID_regulators.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : MC_PID_regulators.c
  3. * Author : IMS Systems Lab
  4. * Date First Issued : 21/11/07
  5. * Description : This file contains the software implementation for the
  6. PI(D) regulators.
  7. ********************************************************************************
  8. * History:
  9. * 21/11/07 v1.0
  10. * 29/05/08 v2.0
  11. ********************************************************************************
  12. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  14. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  15. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  16. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  17. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * THIS SOURCE CODE IS PROTECTED BY A LICENSE.
  20. * FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
  21. * IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
  22. *******************************************************************************/
  23. /* Standard include ----------------------------------------------------------*/
  24. #include "MC_PID_regulators.h"
  25. PID_Struct_t PID_Flux_InitStructure; // d轴电流环闭环
  26. PID_Struct_t PID_Torque_InitStructure; // q轴电流环闭环
  27. PID_Struct_t PID_Weak_InitStructure; // 输出电压弱磁闭环
  28. PID_Struct_t PID_IMax; // 母线电流闭环
  29. PID_Struct_t PID_MotorSpd; // 电机速度闭环
  30. PID_Struct_t PID_ConstantPower; // 恒定功率
  31. /*******************************************************************************
  32. * Function Name : PID_Init
  33. * Description : Initialize PID coefficients for torque, flux and speed loop:
  34. Kp_Gain: proportional coeffcient
  35. Ki_Gain: integral coeffcient
  36. Kd_Gain: differential coeffcient
  37. * Input : Pointer 1 to Torque PI structure,
  38. Pointer 2 to Flux PI structure,
  39. Pointer 3 to Speed PI structure
  40. * Output : None
  41. * Return : None
  42. *******************************************************************************/
  43. void PID_Init (void)
  44. {
  45. //q轴电流环
  46. PID_Torque_InitStructure.hKp_Gain = 20000;
  47. PID_Torque_InitStructure.hKp_Divisor = 1024;
  48. PID_Torque_InitStructure.hKi_Gain = 3;
  49. PID_Torque_InitStructure.hKi_Divisor = 1024;
  50. PID_Torque_InitStructure.hKd_Gain = 0;
  51. PID_Torque_InitStructure.hKd_Divisor = 1024;
  52. PID_Torque_InitStructure.wPreviousError = 0;
  53. PID_Torque_InitStructure.hLower_Limit_Output = -32768;
  54. PID_Torque_InitStructure.hUpper_Limit_Output= 32767;
  55. PID_Torque_InitStructure.wLower_Limit_Integral = -32768 * 1024;
  56. PID_Torque_InitStructure.wUpper_Limit_Integral = 10000 * 1024;
  57. PID_Torque_InitStructure.wIntegral = 0;
  58. //d轴电流环
  59. PID_Flux_InitStructure.hKp_Gain = 20000;
  60. PID_Flux_InitStructure.hKp_Divisor = 1024;
  61. PID_Flux_InitStructure.hKi_Gain = 3;
  62. PID_Flux_InitStructure.hKi_Divisor = 1024;
  63. PID_Flux_InitStructure.hKd_Gain = 0;
  64. PID_Flux_InitStructure.hKd_Divisor = 1024;
  65. PID_Flux_InitStructure.wPreviousError = 0;
  66. PID_Flux_InitStructure.hLower_Limit_Output= -32768;
  67. PID_Flux_InitStructure.hUpper_Limit_Output= 32767;
  68. PID_Flux_InitStructure.wLower_Limit_Integral = -32768 * 1024;
  69. PID_Flux_InitStructure.wUpper_Limit_Integral = 32767 * 1024;
  70. PID_Flux_InitStructure.wIntegral = 0;
  71. //输出电压弱磁闭环
  72. PID_Weak_InitStructure.hKp_Gain = 64;
  73. PID_Weak_InitStructure.hKp_Divisor = 1024;
  74. PID_Weak_InitStructure.hKi_Gain = 3;
  75. PID_Weak_InitStructure.hKi_Divisor = 1024;
  76. PID_Weak_InitStructure.hKd_Gain = 0;
  77. PID_Weak_InitStructure.hKd_Divisor = 1024;
  78. PID_Weak_InitStructure.wPreviousError = 0;
  79. PID_Weak_InitStructure.hLower_Limit_Output= -262144;
  80. PID_Weak_InitStructure.hUpper_Limit_Output= 0;
  81. PID_Weak_InitStructure.wLower_Limit_Integral = -262144 * 1024;
  82. PID_Weak_InitStructure.wUpper_Limit_Integral = 0;
  83. PID_Weak_InitStructure.wIntegral = 0;
  84. //输出功率环(目前用于推行模式母线电流环)
  85. PID_ConstantPower.hKp_Gain = 30000;
  86. PID_ConstantPower.hKp_Divisor = 256;
  87. PID_ConstantPower.hKi_Gain = 300;
  88. PID_ConstantPower.hKi_Divisor = 1024;
  89. PID_ConstantPower.hKd_Gain = 0;
  90. PID_ConstantPower.hKd_Divisor = 1024;
  91. PID_ConstantPower.wPreviousError = 0;
  92. PID_ConstantPower.hLower_Limit_Output= -2100;
  93. PID_ConstantPower.hUpper_Limit_Output= 0;
  94. PID_ConstantPower.wLower_Limit_Integral = -2150400;
  95. PID_ConstantPower.wUpper_Limit_Integral = 0;
  96. PID_ConstantPower.wIntegral = 0;
  97. //电机速度环
  98. PID_MotorSpd.hKp_Gain = 4096;
  99. PID_MotorSpd.hKp_Divisor = 1024;
  100. PID_MotorSpd.hKi_Gain = 1;
  101. PID_MotorSpd.hKi_Divisor = 1024;
  102. PID_MotorSpd.hKd_Gain = 0;
  103. PID_MotorSpd.hKd_Divisor = 1024;
  104. PID_MotorSpd.wPreviousError = 0;
  105. PID_MotorSpd.hLower_Limit_Output= -1050;
  106. PID_MotorSpd.hUpper_Limit_Output= 1050;
  107. PID_MotorSpd.wLower_Limit_Integral = -358400;
  108. PID_MotorSpd.wUpper_Limit_Integral = 358400;
  109. PID_MotorSpd.wIntegral = 0;
  110. //母线电流环(目前用于力矩模式母线电流环)
  111. PID_IMax.hKp_Gain = 30000;
  112. PID_IMax.hKp_Divisor = 256;
  113. PID_IMax.hKi_Gain = 300;
  114. PID_IMax.hKi_Divisor = 1024;
  115. PID_IMax.hKd_Gain = 0;
  116. PID_IMax.hKd_Divisor = 1024;
  117. PID_IMax.wPreviousError = 0;
  118. PID_IMax.hLower_Limit_Output= -1050;
  119. PID_IMax.hUpper_Limit_Output= 0;
  120. PID_IMax.wLower_Limit_Integral = -1075200;
  121. PID_IMax.wUpper_Limit_Integral = 0;
  122. PID_IMax.wIntegral = 0;
  123. }
  124. /*******************************************************************************
  125. * Function Name : PID_Regulator
  126. * Description : Compute the PI(D) output for a PI(D) regulation.
  127. * Input : Pointer to the PID settings (*PID_Flux)
  128. Speed in int16_t format
  129. * Output : int16_t
  130. * Return : None
  131. *******************************************************************************/
  132. /******************************PID 调节函数的说明******************************/
  133. int16_t PID_Regulator(int16_t hReference, int16_t hPresentFeedback,PID_Struct_t *PID_Struct)
  134. {
  135. int16_t wError;
  136. int32_t wProportional_Term,wIntegral_Term, houtput_32;
  137. int32_t dwAux;
  138. // error computation
  139. wError= hReference - hPresentFeedback; //取得需要误差量 delta_e <Q16
  140. // Proportional term computation
  141. wProportional_Term = PID_Struct->hKp_Gain * wError; // wP = Kp * delta_e <Q32
  142. // wP 为比例总调节量
  143. // Integral term computation
  144. if(PID_Struct->hKi_Gain == 0)
  145. {
  146. PID_Struct->wIntegral = 0;
  147. }
  148. else
  149. {
  150. wIntegral_Term = PID_Struct->hKi_Gain * wError; // wI = Ki * delta_e ,本次积分项
  151. dwAux = PID_Struct->wIntegral + wIntegral_Term; // 积分累积的调节量 = 以前的积分累积量 + 本次的积分项
  152. if(dwAux > PID_Struct->wUpper_Limit_Integral) //dwAux为当前积分累积项,下面测试积分饱和度
  153. {
  154. PID_Struct->wIntegral = PID_Struct->wUpper_Limit_Integral; // 超上限
  155. }
  156. else if(dwAux < PID_Struct->wLower_Limit_Integral) //超下限
  157. {
  158. PID_Struct->wIntegral = PID_Struct->wLower_Limit_Integral;
  159. }
  160. else
  161. {
  162. PID_Struct->wIntegral = dwAux; //不超限, 更新积分累积项为dwAux <Q32
  163. }
  164. }
  165. // Differential term computation
  166. houtput_32 = (wProportional_Term/PID_Struct->hKp_Divisor) + //不含微分环节的总调节量 <Q16
  167. (PID_Struct->wIntegral/PID_Struct->hKi_Divisor);
  168. if((houtput_32<0)&&(houtput_32<PID_Struct->hLower_Limit_Output)) //超下限
  169. {
  170. return(PID_Struct->hLower_Limit_Output);
  171. }
  172. if((houtput_32>0)&&(houtput_32>=PID_Struct->hUpper_Limit_Output)) //测试输出是否饱和,超上限
  173. {
  174. return(PID_Struct->hUpper_Limit_Output);
  175. }
  176. else
  177. {
  178. return houtput_32; //不超限。输出结果 houtput_32 <Q16
  179. }
  180. }
  181. /******************** (C) COPYRIGHT 2008 STMicroelectronics *******************/