MC_PID_regulators.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. #include "string.h"
  26. /*根据型号设置不同的PID参数*/
  27. //Volans PID参数
  28. const PID_Struct_t PID_Torque_InitStructure_Volans =
  29. {
  30. (int16_t)20000, //hKp_Gain
  31. (uint16_t)1024, //hKp_Divisor
  32. (int16_t)3, //hKi_Gain
  33. (uint16_t)1024, //hKi_Divisor
  34. (int16_t)0, //hKd_Gain
  35. (uint16_t)1024, //hKd_Divisor
  36. (int32_t)0, //wPreviousError
  37. (int32_t)-32768, //hLower_Limit_Output
  38. (int32_t)32767, //hUpper_Limit_Output
  39. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  40. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  41. (int32_t)0 //wIntegral
  42. };
  43. const PID_Struct_t PID_Flux_InitStructure_Volans =
  44. {
  45. (int16_t)20000, //hKp_Gain
  46. (uint16_t)1024, //hKp_Divisor
  47. (int16_t)3, //hKi_Gain
  48. (uint16_t)1024, //hKi_Divisor
  49. (int16_t)0, //hKd_Gain
  50. (uint16_t)1024, //hKd_Divisor
  51. (int32_t)0, //wPreviousError
  52. (int32_t)-32768, //hLower_Limit_Output
  53. (int32_t)32767, //hUpper_Limit_Output
  54. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  55. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  56. (int32_t)0 //wIntegral
  57. };
  58. const PID_Struct_t PID_Weak_InitStructure_Volans =
  59. {
  60. (int16_t)64, //hKp_Gain
  61. (uint16_t)1024, //hKp_Divisor
  62. (int16_t)3, //hKi_Gain
  63. (uint16_t)1024, //hKi_Divisor
  64. (int16_t)0, //hKd_Gain
  65. (uint16_t)1024, //hKd_Divisor
  66. (int32_t)0, //wPreviousError
  67. (int32_t)-262144, //hLower_Limit_Output
  68. (int32_t)0, //hUpper_Limit_Output
  69. (int32_t)-268435456, //wLower_Limit_Integral
  70. (int32_t)0, //wUpper_Limit_Integral
  71. (int32_t)0 //wIntegral
  72. };
  73. const PID_Struct_t PID_ConstantPower_Volans =
  74. {
  75. (int16_t)30000, //hKp_Gain
  76. (uint16_t)256, //hKp_Divisor
  77. (int16_t)300, //hKi_Gain
  78. (uint16_t)1024, //hKi_Divisor
  79. (int16_t)0, //hKd_Gain
  80. (uint16_t)1024, //hKd_Divisor
  81. (int32_t)0, //wPreviousError
  82. (int32_t)-2100, //hLower_Limit_Output
  83. (int32_t)0, //hUpper_Limit_Output
  84. (int32_t)-2150400, //wLower_Limit_Integral
  85. (int32_t)0, //wUpper_Limit_Integral
  86. (int32_t)0 //wIntegral
  87. };
  88. const PID_Struct_t PID_MotorSpd_Volans =
  89. {
  90. (int16_t)4096, //hKp_Gain
  91. (uint16_t)1024, //hKp_Divisor
  92. (int16_t)1, //hKi_Gain
  93. (uint16_t)1024, //hKi_Divisor
  94. (int16_t)0, //hKd_Gain
  95. (uint16_t)1024, //hKd_Divisor
  96. (int32_t)0, //wPreviousError
  97. (int32_t)-1050, //hLower_Limit_Output
  98. (int32_t)1050, //hUpper_Limit_Output
  99. (int32_t)-358400, //wLower_Limit_Integral
  100. (int32_t)358400, //wUpper_Limit_Integral
  101. (int32_t)0 //wIntegral
  102. };
  103. const PID_Struct_t PID_IMax_Volans =
  104. {
  105. (int16_t)30000, //hKp_Gain
  106. (uint16_t)1024, //hKp_Divisor
  107. (int16_t)300, //hKi_Gain
  108. (uint16_t)1024, //hKi_Divisor
  109. (int16_t)0, //hKd_Gain
  110. (uint16_t)1024, //hKd_Divisor
  111. (int32_t)0, //wPreviousError
  112. (int32_t)-1050, //hLower_Limit_Output
  113. (int32_t)0, //hUpper_Limit_Output
  114. (int32_t)-1075200, //wLower_Limit_Integral
  115. (int32_t)0, //wUpper_Limit_Integral
  116. (int32_t)0 //wIntegral
  117. };
  118. //Volans_Plus PID参数
  119. const PID_Struct_t PID_Torque_InitStructure_Volans_Plus =
  120. {
  121. (int16_t)20000, //hKp_Gain
  122. (uint16_t)1024, //hKp_Divisor
  123. (int16_t)3, //hKi_Gain
  124. (uint16_t)1024, //hKi_Divisor
  125. (int16_t)0, //hKd_Gain
  126. (uint16_t)1024, //hKd_Divisor
  127. (int32_t)0, //wPreviousError
  128. (int32_t)-32768, //hLower_Limit_Output
  129. (int32_t)32767, //hUpper_Limit_Output
  130. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  131. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  132. (int32_t)0 //wIntegral
  133. };
  134. const PID_Struct_t PID_Flux_InitStructure_Volans_Plus =
  135. {
  136. (int16_t)20000, //hKp_Gain
  137. (uint16_t)1024, //hKp_Divisor
  138. (int16_t)3, //hKi_Gain
  139. (uint16_t)1024, //hKi_Divisor
  140. (int16_t)0, //hKd_Gain
  141. (uint16_t)1024, //hKd_Divisor
  142. (int32_t)0, //wPreviousError
  143. (int32_t)-32768, //hLower_Limit_Output
  144. (int32_t)32767, //hUpper_Limit_Output
  145. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  146. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  147. (int32_t)0 //wIntegral
  148. };
  149. const PID_Struct_t PID_Weak_InitStructure_Volans_Plus =
  150. {
  151. (int16_t)64, //hKp_Gain
  152. (uint16_t)1024, //hKp_Divisor
  153. (int16_t)3, //hKi_Gain
  154. (uint16_t)1024, //hKi_Divisor
  155. (int16_t)0, //hKd_Gain
  156. (uint16_t)1024, //hKd_Divisor
  157. (int32_t)0, //wPreviousError
  158. (int32_t)-262144, //hLower_Limit_Output
  159. (int32_t)0, //hUpper_Limit_Output
  160. (int32_t)-268435456, //wLower_Limit_Integral
  161. (int32_t)0, //wUpper_Limit_Integral
  162. (int32_t)0 //wIntegral
  163. };
  164. const PID_Struct_t PID_ConstantPower_Volans_Plus =
  165. {
  166. (int16_t)30000, //hKp_Gain
  167. (uint16_t)256, //hKp_Divisor
  168. (int16_t)300, //hKi_Gain
  169. (uint16_t)1024, //hKi_Divisor
  170. (int16_t)0, //hKd_Gain
  171. (uint16_t)1024, //hKd_Divisor
  172. (int32_t)0, //wPreviousError
  173. (int32_t)-2100, //hLower_Limit_Output
  174. (int32_t)0, //hUpper_Limit_Output
  175. (int32_t)-2150400, //wLower_Limit_Integral
  176. (int32_t)0, //wUpper_Limit_Integral
  177. (int32_t)0 //wIntegral
  178. };
  179. const PID_Struct_t PID_MotorSpd_Volans_Plus =
  180. {
  181. (int16_t)4096, //hKp_Gain
  182. (uint16_t)1024, //hKp_Divisor
  183. (int16_t)1, //hKi_Gain
  184. (uint16_t)1024, //hKi_Divisor
  185. (int16_t)0, //hKd_Gain
  186. (uint16_t)1024, //hKd_Divisor
  187. (int32_t)0, //wPreviousError
  188. (int32_t)-1050, //hLower_Limit_Output
  189. (int32_t)1050, //hUpper_Limit_Output
  190. (int32_t)-358400, //wLower_Limit_Integral
  191. (int32_t)358400, //wUpper_Limit_Integral
  192. (int32_t)0 //wIntegral
  193. };
  194. const PID_Struct_t PID_IMax_Volans_Plus =
  195. {
  196. (int16_t)30000, //hKp_Gain
  197. (uint16_t)1024, //hKp_Divisor
  198. (int16_t)300, //hKi_Gain
  199. (uint16_t)1024, //hKi_Divisor
  200. (int16_t)0, //hKd_Gain
  201. (uint16_t)1024, //hKd_Divisor
  202. (int32_t)0, //wPreviousError
  203. (int32_t)-1050, //hLower_Limit_Output
  204. (int32_t)0, //hUpper_Limit_Output
  205. (int32_t)-1075200, //wLower_Limit_Integral
  206. (int32_t)0, //wUpper_Limit_Integral
  207. (int32_t)0 //wIntegral
  208. };
  209. //Pegasi PID参数
  210. const PID_Struct_t PID_Torque_InitStructure_Pegasi =
  211. {
  212. (int16_t)20000, //hKp_Gain
  213. (uint16_t)1024, //hKp_Divisor
  214. (int16_t)3, //hKi_Gain
  215. (uint16_t)1024, //hKi_Divisor
  216. (int16_t)0, //hKd_Gain
  217. (uint16_t)1024, //hKd_Divisor
  218. (int32_t)0, //wPreviousError
  219. (int32_t)-32768, //hLower_Limit_Output
  220. (int32_t)32767, //hUpper_Limit_Output
  221. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  222. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  223. (int32_t)0 //wIntegral
  224. };
  225. const PID_Struct_t PID_Flux_InitStructure_Pegasi =
  226. {
  227. (int16_t)20000, //hKp_Gain
  228. (uint16_t)1024, //hKp_Divisor
  229. (int16_t)3, //hKi_Gain
  230. (uint16_t)1024, //hKi_Divisor
  231. (int16_t)0, //hKd_Gain
  232. (uint16_t)1024, //hKd_Divisor
  233. (int32_t)0, //wPreviousError
  234. (int32_t)-32768, //hLower_Limit_Output
  235. (int32_t)32767, //hUpper_Limit_Output
  236. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  237. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  238. (int32_t)0 //wIntegral
  239. };
  240. const PID_Struct_t PID_Weak_InitStructure_Pegasi =
  241. {
  242. (int16_t)64, //hKp_Gain
  243. (uint16_t)1024, //hKp_Divisor
  244. (int16_t)3, //hKi_Gain
  245. (uint16_t)1024, //hKi_Divisor
  246. (int16_t)0, //hKd_Gain
  247. (uint16_t)1024, //hKd_Divisor
  248. (int32_t)0, //wPreviousError
  249. (int32_t)-262144, //hLower_Limit_Output
  250. (int32_t)0, //hUpper_Limit_Output
  251. (int32_t)-268435456, //wLower_Limit_Integral
  252. (int32_t)0, //wUpper_Limit_Integral
  253. (int32_t)0 //wIntegral
  254. };
  255. const PID_Struct_t PID_ConstantPower_Pegasi =
  256. {
  257. (int16_t)30000, //hKp_Gain
  258. (uint16_t)256, //hKp_Divisor
  259. (int16_t)300, //hKi_Gain
  260. (uint16_t)1024, //hKi_Divisor
  261. (int16_t)0, //hKd_Gain
  262. (uint16_t)1024, //hKd_Divisor
  263. (int32_t)0, //wPreviousError
  264. (int32_t)-2100, //hLower_Limit_Output
  265. (int32_t)0, //hUpper_Limit_Output
  266. (int32_t)-2150400, //wLower_Limit_Integral
  267. (int32_t)0, //wUpper_Limit_Integral
  268. (int32_t)0 //wIntegral
  269. };
  270. const PID_Struct_t PID_MotorSpd_Pegasi =
  271. {
  272. (int16_t)4096, //hKp_Gain
  273. (uint16_t)1024, //hKp_Divisor
  274. (int16_t)1, //hKi_Gain
  275. (uint16_t)1024, //hKi_Divisor
  276. (int16_t)0, //hKd_Gain
  277. (uint16_t)1024, //hKd_Divisor
  278. (int32_t)0, //wPreviousError
  279. (int32_t)-1050, //hLower_Limit_Output
  280. (int32_t)1050, //hUpper_Limit_Output
  281. (int32_t)-358400, //wLower_Limit_Integral
  282. (int32_t)358400, //wUpper_Limit_Integral
  283. (int32_t)0 //wIntegral
  284. };
  285. const PID_Struct_t PID_IMax_Pegasi =
  286. {
  287. (int16_t)30000, //hKp_Gain
  288. (uint16_t)1024, //hKp_Divisor
  289. (int16_t)300, //hKi_Gain
  290. (uint16_t)1024, //hKi_Divisor
  291. (int16_t)0, //hKd_Gain
  292. (uint16_t)1024, //hKd_Divisor
  293. (int32_t)0, //wPreviousError
  294. (int32_t)-1050, //hLower_Limit_Output
  295. (int32_t)0, //hUpper_Limit_Output
  296. (int32_t)-1075200, //wLower_Limit_Integral
  297. (int32_t)0, //wUpper_Limit_Integral
  298. (int32_t)0 //wIntegral
  299. };
  300. //Pegasi_Plus PID参数
  301. const PID_Struct_t PID_Torque_InitStructure_Pegasi_Plus =
  302. {
  303. (int16_t)20000, //hKp_Gain
  304. (uint16_t)1024, //hKp_Divisor
  305. (int16_t)3, //hKi_Gain
  306. (uint16_t)1024, //hKi_Divisor
  307. (int16_t)0, //hKd_Gain
  308. (uint16_t)1024, //hKd_Divisor
  309. (int32_t)0, //wPreviousError
  310. (int32_t)-32768, //hLower_Limit_Output
  311. (int32_t)32767, //hUpper_Limit_Output
  312. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  313. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  314. (int32_t)0 //wIntegral
  315. };
  316. const PID_Struct_t PID_Flux_InitStructure_Pegasi_Plus =
  317. {
  318. (int16_t)20000, //hKp_Gain
  319. (uint16_t)1024, //hKp_Divisor
  320. (int16_t)3, //hKi_Gain
  321. (uint16_t)1024, //hKi_Divisor
  322. (int16_t)0, //hKd_Gain
  323. (uint16_t)1024, //hKd_Divisor
  324. (int32_t)0, //wPreviousError
  325. (int32_t)-32768, //hLower_Limit_Output
  326. (int32_t)32767, //hUpper_Limit_Output
  327. (int32_t)(-32768 * 1024), //wLower_Limit_Integral
  328. (int32_t)(32767 * 1024), //wUpper_Limit_Integral
  329. (int32_t)0 //wIntegral
  330. };
  331. const PID_Struct_t PID_Weak_InitStructure_Pegasi_Plus =
  332. {
  333. (int16_t)64, //hKp_Gain
  334. (uint16_t)1024, //hKp_Divisor
  335. (int16_t)3, //hKi_Gain
  336. (uint16_t)1024, //hKi_Divisor
  337. (int16_t)0, //hKd_Gain
  338. (uint16_t)1024, //hKd_Divisor
  339. (int32_t)0, //wPreviousError
  340. (int32_t)-262144, //hLower_Limit_Output
  341. (int32_t)0, //hUpper_Limit_Output
  342. (int32_t)-268435456, //wLower_Limit_Integral
  343. (int32_t)0, //wUpper_Limit_Integral
  344. (int32_t)0 //wIntegral
  345. };
  346. const PID_Struct_t PID_ConstantPower_Pegasi_Plus =
  347. {
  348. (int16_t)30000, //hKp_Gain
  349. (uint16_t)256, //hKp_Divisor
  350. (int16_t)300, //hKi_Gain
  351. (uint16_t)1024, //hKi_Divisor
  352. (int16_t)0, //hKd_Gain
  353. (uint16_t)1024, //hKd_Divisor
  354. (int32_t)0, //wPreviousError
  355. (int32_t)-2100, //hLower_Limit_Output
  356. (int32_t)0, //hUpper_Limit_Output
  357. (int32_t)-2150400, //wLower_Limit_Integral
  358. (int32_t)0, //wUpper_Limit_Integral
  359. (int32_t)0 //wIntegral
  360. };
  361. const PID_Struct_t PID_MotorSpd_Pegasi_Plus =
  362. {
  363. (int16_t)4096, //hKp_Gain
  364. (uint16_t)1024, //hKp_Divisor
  365. (int16_t)1, //hKi_Gain
  366. (uint16_t)1024, //hKi_Divisor
  367. (int16_t)0, //hKd_Gain
  368. (uint16_t)1024, //hKd_Divisor
  369. (int32_t)0, //wPreviousError
  370. (int32_t)-1050, //hLower_Limit_Output
  371. (int32_t)1050, //hUpper_Limit_Output
  372. (int32_t)-358400, //wLower_Limit_Integral
  373. (int32_t)358400, //wUpper_Limit_Integral
  374. (int32_t)0 //wIntegral
  375. };
  376. const PID_Struct_t PID_IMax_Pegasi_Plus =
  377. {
  378. (int16_t)30000, //hKp_Gain
  379. (uint16_t)1024, //hKp_Divisor
  380. (int16_t)300, //hKi_Gain
  381. (uint16_t)1024, //hKi_Divisor
  382. (int16_t)0, //hKd_Gain
  383. (uint16_t)1024, //hKd_Divisor
  384. (int32_t)0, //wPreviousError
  385. (int32_t)-1050, //hLower_Limit_Output
  386. (int32_t)0, //hUpper_Limit_Output
  387. (int32_t)-1075200, //wLower_Limit_Integral
  388. (int32_t)0, //wUpper_Limit_Integral
  389. (int32_t)0 //wIntegral
  390. };
  391. /* 根据型号设置不同的PID参数 END */
  392. //PID控制变量定义
  393. PID_Struct_t PID_Torque_InitStructure; // q轴电流环闭环
  394. PID_Struct_t PID_Flux_InitStructure; // d轴电流环闭环
  395. PID_Struct_t PID_Weak_InitStructure; // 输出电压弱磁闭环
  396. PID_Struct_t PID_ConstantPower; // 恒定功率
  397. PID_Struct_t PID_MotorSpd; // 电机速度闭环
  398. PID_Struct_t PID_IMax; // 母线电流闭环
  399. /*******************************************************************************
  400. * Function Name : PID_Init
  401. * Description : Initialize PID coefficients for torque, flux and speed loop:
  402. Kp_Gain: proportional coeffcient
  403. Ki_Gain: integral coeffcient
  404. Kd_Gain: differential coeffcient
  405. * Input : Pointer 1 to Torque PI structure,
  406. Pointer 2 to Flux PI structure,
  407. Pointer 3 to Speed PI structure
  408. * Output : None
  409. * Return : None
  410. *******************************************************************************/
  411. void PID_Init (uint8_t SerialNum)
  412. {
  413. switch(SerialNum)
  414. {
  415. case 1://Volans
  416. {
  417. memcpy(&PID_Torque_InitStructure.hKp_Gain, &PID_Torque_InitStructure_Volans.hKp_Gain, sizeof(PID_Torque_InitStructure_Volans));
  418. memcpy(&PID_Flux_InitStructure.hKp_Gain, &PID_Flux_InitStructure_Volans.hKp_Gain, sizeof(PID_Flux_InitStructure_Volans));
  419. memcpy(&PID_Weak_InitStructure.hKp_Gain, &PID_Weak_InitStructure_Volans.hKp_Gain, sizeof(PID_Weak_InitStructure_Volans));
  420. memcpy(&PID_ConstantPower.hKp_Gain, &PID_ConstantPower_Volans.hKp_Gain, sizeof(PID_ConstantPower_Volans));
  421. memcpy(&PID_MotorSpd.hKp_Gain, &PID_MotorSpd_Volans.hKp_Gain, sizeof(PID_MotorSpd_Volans));
  422. memcpy(&PID_IMax.hKp_Gain, &PID_IMax_Volans.hKp_Gain, sizeof(PID_IMax_Volans));
  423. break;
  424. }
  425. case 2://Volans Plus
  426. {
  427. memcpy(&PID_Torque_InitStructure.hKp_Gain, &PID_Torque_InitStructure_Volans_Plus.hKp_Gain, sizeof(PID_Torque_InitStructure_Volans_Plus));
  428. memcpy(&PID_Flux_InitStructure.hKp_Gain, &PID_Flux_InitStructure_Volans_Plus.hKp_Gain, sizeof(PID_Flux_InitStructure_Volans_Plus));
  429. memcpy(&PID_Weak_InitStructure.hKp_Gain, &PID_Weak_InitStructure_Volans_Plus.hKp_Gain, sizeof(PID_Weak_InitStructure_Volans_Plus));
  430. memcpy(&PID_ConstantPower.hKp_Gain, &PID_ConstantPower_Volans_Plus.hKp_Gain, sizeof(PID_ConstantPower_Volans_Plus));
  431. memcpy(&PID_MotorSpd.hKp_Gain, &PID_MotorSpd_Volans_Plus.hKp_Gain, sizeof(PID_MotorSpd_Volans_Plus));
  432. memcpy(&PID_IMax.hKp_Gain, &PID_IMax_Volans_Plus.hKp_Gain, sizeof(PID_IMax_Volans_Plus));
  433. break;
  434. }
  435. case 3://Pegasi
  436. {
  437. memcpy(&PID_Torque_InitStructure.hKp_Gain, &PID_Torque_InitStructure_Pegasi.hKp_Gain, sizeof(PID_Torque_InitStructure_Pegasi));
  438. memcpy(&PID_Flux_InitStructure.hKp_Gain, &PID_Flux_InitStructure_Pegasi.hKp_Gain, sizeof(PID_Flux_InitStructure_Pegasi));
  439. memcpy(&PID_Weak_InitStructure.hKp_Gain, &PID_Weak_InitStructure_Pegasi.hKp_Gain, sizeof(PID_Weak_InitStructure_Pegasi));
  440. memcpy(&PID_ConstantPower.hKp_Gain, &PID_ConstantPower_Pegasi.hKp_Gain, sizeof(PID_ConstantPower_Pegasi));
  441. memcpy(&PID_MotorSpd.hKp_Gain, &PID_MotorSpd_Pegasi.hKp_Gain, sizeof(PID_MotorSpd_Pegasi));
  442. memcpy(&PID_IMax.hKp_Gain, &PID_IMax_Pegasi.hKp_Gain, sizeof(PID_IMax_Pegasi));
  443. break;
  444. }
  445. case 4://Pegasi Plus
  446. {
  447. memcpy(&PID_Torque_InitStructure.hKp_Gain, &PID_Torque_InitStructure_Pegasi_Plus.hKp_Gain, sizeof(PID_Torque_InitStructure_Pegasi_Plus));
  448. memcpy(&PID_Flux_InitStructure.hKp_Gain, &PID_Flux_InitStructure_Pegasi_Plus.hKp_Gain, sizeof(PID_Flux_InitStructure_Pegasi_Plus));
  449. memcpy(&PID_Weak_InitStructure.hKp_Gain, &PID_Weak_InitStructure_Pegasi_Plus.hKp_Gain, sizeof(PID_Weak_InitStructure_Pegasi_Plus));
  450. memcpy(&PID_ConstantPower.hKp_Gain, &PID_ConstantPower_Pegasi_Plus.hKp_Gain, sizeof(PID_ConstantPower_Pegasi_Plus));
  451. memcpy(&PID_MotorSpd.hKp_Gain, &PID_MotorSpd_Pegasi_Plus.hKp_Gain, sizeof(PID_MotorSpd_Pegasi_Plus));
  452. memcpy(&PID_IMax.hKp_Gain, &PID_IMax_Pegasi_Plus.hKp_Gain, sizeof(PID_IMax_Pegasi_Plus));
  453. break;
  454. }
  455. default://默认处理
  456. {
  457. memcpy(&PID_Torque_InitStructure.hKp_Gain, &PID_Torque_InitStructure_Volans.hKp_Gain, sizeof(PID_Torque_InitStructure_Volans));
  458. memcpy(&PID_Flux_InitStructure.hKp_Gain, &PID_Flux_InitStructure_Volans.hKp_Gain, sizeof(PID_Flux_InitStructure_Volans));
  459. memcpy(&PID_Weak_InitStructure.hKp_Gain, &PID_Weak_InitStructure_Volans.hKp_Gain, sizeof(PID_Weak_InitStructure_Volans));
  460. memcpy(&PID_ConstantPower.hKp_Gain, &PID_ConstantPower_Volans.hKp_Gain, sizeof(PID_ConstantPower_Volans));
  461. memcpy(&PID_MotorSpd.hKp_Gain, &PID_MotorSpd_Volans.hKp_Gain, sizeof(PID_MotorSpd_Volans));
  462. memcpy(&PID_IMax.hKp_Gain, &PID_IMax_Volans.hKp_Gain, sizeof(PID_IMax_Volans));
  463. break;
  464. }
  465. }
  466. }
  467. /*******************************************************************************
  468. * Function Name : PID_Regulator
  469. * Description : Compute the PI(D) output for a PI(D) regulation.
  470. * Input : Pointer to the PID settings (*PID_Flux)
  471. Speed in int16_t format
  472. * Output : int16_t
  473. * Return : None
  474. *******************************************************************************/
  475. /******************************PID 调节函数的说明******************************/
  476. int16_t PID_Regulator(int16_t hReference, int16_t hPresentFeedback,PID_Struct_t *PID_Struct)
  477. {
  478. int16_t wError;
  479. int32_t wProportional_Term,wIntegral_Term, houtput_32;
  480. int32_t dwAux;
  481. // error computation
  482. wError= hReference - hPresentFeedback; //取得需要误差量 delta_e <Q16
  483. // Proportional term computation
  484. wProportional_Term = PID_Struct->hKp_Gain * wError; // wP = Kp * delta_e <Q32
  485. // wP 为比例总调节量
  486. // Integral term computation
  487. if(PID_Struct->hKi_Gain == 0)
  488. {
  489. PID_Struct->wIntegral = 0;
  490. }
  491. else
  492. {
  493. wIntegral_Term = PID_Struct->hKi_Gain * wError; // wI = Ki * delta_e ,本次积分项
  494. dwAux = PID_Struct->wIntegral + wIntegral_Term; // 积分累积的调节量 = 以前的积分累积量 + 本次的积分项
  495. if(dwAux > PID_Struct->wUpper_Limit_Integral) //dwAux为当前积分累积项,下面测试积分饱和度
  496. {
  497. PID_Struct->wIntegral = PID_Struct->wUpper_Limit_Integral; // 超上限
  498. }
  499. else if(dwAux < PID_Struct->wLower_Limit_Integral) //超下限
  500. {
  501. PID_Struct->wIntegral = PID_Struct->wLower_Limit_Integral;
  502. }
  503. else
  504. {
  505. PID_Struct->wIntegral = dwAux; //不超限, 更新积分累积项为dwAux <Q32
  506. }
  507. }
  508. // Differential term computation
  509. houtput_32 = (wProportional_Term/PID_Struct->hKp_Divisor) + //不含微分环节的总调节量 <Q16
  510. (PID_Struct->wIntegral/PID_Struct->hKi_Divisor);
  511. if((houtput_32<0)&&(houtput_32<PID_Struct->hLower_Limit_Output)) //超下限
  512. {
  513. return(PID_Struct->hLower_Limit_Output);
  514. }
  515. if((houtput_32>0)&&(houtput_32>=PID_Struct->hUpper_Limit_Output)) //测试输出是否饱和,超上限
  516. {
  517. return(PID_Struct->hUpper_Limit_Output);
  518. }
  519. else
  520. {
  521. return houtput_32; //不超限。输出结果 houtput_32 <Q16
  522. }
  523. }
  524. /******************** (C) COPYRIGHT 2008 STMicroelectronics *******************/