spdctrmode.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: spdctrmode.c
  4. Partner Filename: spdctrmode.h
  5. Description: Speed control mode
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : GD32F3x0
  8. *************************************************************************
  9. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. ************************************************************************/
  15. /************************************************************************
  16. Beginning of File, do not put anything above here except notes
  17. Compiler Directives:
  18. *************************************************************************/
  19. #ifndef _SPDCTRMODE_C_
  20. #define _SPDCTRMODE_C_
  21. #endif
  22. /************************************************************************
  23. Included File
  24. *************************************************************************/
  25. #include "syspar.h"
  26. #include "user.h"
  27. #include "AssistCurve.h"
  28. #include "cmdgennew.h"
  29. #include "CodePara.h"
  30. #include "FSM_2nd.h"
  31. /************************************************************************
  32. Constant Table (N/A)
  33. *************************************************************************/
  34. /************************************************************************
  35. Private Variables
  36. *************************************************************************/
  37. static LPF_OUT swTmpSpdRateLpf;
  38. static SWORD swTmpSpdRate = 0;
  39. static SWORD swTmpSpdFbkPuZ1 = 0;
  40. static SWORD swSpdRateAbsPu;
  41. static SWORD swTestIqref;
  42. static UWORD DCPswitch = 0;
  43. static CRD_PARK_IN Test_U_in;
  44. static CRD_PARK_OUT Test_U_out;
  45. /*************************************************************************
  46. Exported Functions (N/A)
  47. *************************************************************************/
  48. /***************************************************************
  49. Function: scm_voSpdCtrMdInit;
  50. Description: Speed control mode initializing function
  51. Call by: rmd_voModeSchd();
  52. Input Variables: N/A
  53. Output/Return Variables: N/A
  54. Subroutine Call: ...;
  55. Reference: N/A
  56. ****************************************************************/
  57. void scm_voSpdCtrMdInit(void)
  58. {
  59. // /* PWM init */
  60. // sysctrl_voPwmInit();
  61. /*cmd handle Initial */
  62. cmd_voCmdInit();
  63. /* Current PI init */
  64. acr_voCurPIInit();
  65. /* Current decoupling init */
  66. acr_voUdqDcpInit();
  67. /* Sensorless observer init */
  68. obs_voObsInit();
  69. /* SPI position sensor init */
  70. spi_voResolverInit();
  71. /* Speed PI init */
  72. asr_voSpdPIInit();
  73. /* Flux weakening init */
  74. flx_voInit();
  75. // fw_voInit();
  76. /* Power Limit init */
  77. pwr_voPwrLimInit();
  78. /* SVPWM init */
  79. pwm_voInit();
  80. /* Dead time init */
  81. dbc_voDBCompInit();
  82. /* Contant voltage brake init */
  83. cvb_voBrakeInit();
  84. /* switchHall init */
  85. //switchhall_voInit();
  86. /* Align pos startup open2clz clzloop init */
  87. align_voInit();
  88. /* Torque observer init */
  89. torqobs_voInit();
  90. /* Global variablles init */
  91. scm_stSpdFbkLpf.slY.sw.hi = 0;
  92. scm_swSpdRefPu = 0;
  93. scm_swUalphaPu = 0; // Q14
  94. scm_swUbetaPu = 0; // Q14
  95. scm_stIqLoadLpf.slY.sl = 0;
  96. scm_stIdFbkLpf.slY.sl = 0; // Id feedback LPF
  97. scm_stIqFbkLpf.slY.sl = 0; // Iq feedback LPF
  98. scm_swIdRefPu = 0; // Q14
  99. scm_swIqRefPu = 0; // Q14
  100. scm_uwAngRefPu = 0; // Q15
  101. scm_uwAngParkPu = 0; // Q15
  102. scm_uwAngIParkPu = 0; // Q15
  103. scm_swRotateDir = 1; // Direction of motor rotate
  104. scm_ulStatCt = 0; // Status hold time count
  105. scm_uwAngManuPu = 0; // Q15, Angle given manually
  106. scm_slAngManuPu = 0;
  107. scm_slDragSpdPu = 0; // Q15, Drag speed
  108. scm_slDragSpdRefPu = 0; // Q29, intermediate Drag speed
  109. scm_blCurSwitchOvrFlg = FALSE; // Current switch over flag
  110. scm_blAngSwitchOvrFlg = FALSE; // Angle switch over flag
  111. scm_uwAngSwitchK = 0; // Angle switch weight value
  112. scm_swMotorPwrInWt = 0; // unit: w, Input power of motor
  113. scm_blCoefUpdateFlg = FALSE; // Coefficient update flag
  114. scm_stSpdFbkLpf.slY.sl = 0; // Speed feedback LPF
  115. scm_uwSpdFbkLpfAbsPu = 0; // Q15, Speed feedback LPF absolute
  116. scm_swMotorPwrInPu = 0; // Q15, Input power of motor
  117. scm_stMotoPwrInLpf.slY.sl = 0; // Input power of motor after LPF
  118. scm_swMotorPwrInLpfWt = 0; // unit: 0.1w, Input power of motor after LPF
  119. scm_uwMotorPwrInAvgPu = 0; // Q15, Input power of motor after average filter
  120. scm_swIdFdbLpfPu = 0;
  121. scm_swIqFdbLpfPu = 0;
  122. scm_swUdRefPu = 0;
  123. scm_swUqRefPu = 0;
  124. scm_swUalphaFbkPu = 0;
  125. scm_swUbetaFbkPu = 0;
  126. scm_swUalphaRefPu = 0;
  127. scm_swUbetaRefPu = 0;
  128. scm_swUalphaCompPu = 0;
  129. scm_swUbetaCompPu = 0;
  130. scm_uwHfiAngZ1Pu = 0;
  131. scm_slAngSumPu = 0;
  132. scm_slAngErrPu = 0;
  133. scm_blAngSumOvrFlg = FALSE;
  134. scm_uwRunMdSw = 1;
  135. scm_ulRunMdSwCt = 0;
  136. scm_ulCloseCt = 0;
  137. scm_uwStartMd = cp_stControlPara.swStartMode;
  138. scm_uwStartMdSw = scm_uwStartMd;
  139. scm_uwInitPosMd = cp_stControlPara.swInitPosMode;
  140. scm_uwInitPosMdSw = scm_uwInitPosMd;
  141. scm_uwHfiOvrCnt = 0;
  142. scm_slIdRefPu = 0;
  143. /* Private variables init */
  144. swTmpSpdRateLpf.slY.sl = 0;
  145. swTmpSpdRate = 0;
  146. swTmpSpdFbkPuZ1 = 0;
  147. swSpdRateAbsPu = 0;
  148. swTestIqref = 0;
  149. DCPswitch = 0;
  150. }
  151. /***************************************************************
  152. Function: scm_voSpdCtrMdCoef;
  153. Description: Speed control mode TBS scheduler
  154. Call by: tbs_voIsr();
  155. Input Variables: N/A
  156. Output/Return Variables: N/A
  157. Subroutine Call: ...;
  158. Reference: N/A
  159. ****************************************************************/
  160. void scm_voSpdCtrMdCoef(void)
  161. {
  162. ULONG ulLpfTm; // unit: us
  163. SLONG slLqPu = 0;
  164. ULONG ulAccel100rpmpsPu = USER_MOTOR_100RPMPS2PU_Q29;
  165. if (ABS(scm_swIqRefPu) < mn_swIqTurn1Pu)
  166. {
  167. scm_uwLqPu = cof_uwLqPu;
  168. }
  169. else
  170. {
  171. slLqPu = mn_slLqTurn1Pu + (((SLONG)ABS(scm_swIqRefPu) - mn_swIqTurn1Pu) * mn_swKLqSat >> 10); // Q10
  172. if (slLqPu < cof_uwLqMinPu)
  173. {
  174. scm_uwLqPu = cof_uwLqMinPu;
  175. }
  176. else if (slLqPu > cof_uwLqPu)
  177. {
  178. scm_uwLqPu = cof_uwLqPu;
  179. }
  180. else
  181. {
  182. scm_uwLqPu = (UWORD)slLqPu;
  183. }
  184. }
  185. spi_stResolverCoefIn.uwFbHz = cof_uwFbHz;
  186. spi_stResolverCoefIn.uwFreqTbcHz = FTBC_HZ;
  187. spi_stResolverCoefIn.uwSpdPllWvcHz = 30; //cp_stControlPara.swObsSpdPLLBandWidthHz; // Real Value, Unit:Hz
  188. spi_stResolverCoefIn.uwSpdPllMcoef = 2; //cp_stControlPara.swObsSpdPLLM;
  189. spi_voResolverCoef(&spi_stResolverCoefIn, &spi_stResolverCoef);
  190. /* Sensorless observer coefficient calculate */
  191. obs_stObsCoefIn.uwRbOm = cof_uwRbOm; // Real Value, unit: 0.01Ohm, Resistance base
  192. obs_stObsCoefIn.uwLbHm = cof_uwLbHm; // Real Value, unit: 0.01mH, Inductance base
  193. obs_stObsCoefIn.uwFluxbWb = cof_uwFluxbWb; // Real Value, unit: 0.01mWb, Flux linkage base
  194. obs_stObsCoefIn.uwFbHz = cof_uwFbHz; // Real Value, Unit:Hz frequency base
  195. obs_stObsCoefIn.uwRsOm = cp_stMotorPara.swRsOhm; // Real Value, unit: 0.01Ohm, Resistance base
  196. obs_stObsCoefIn.uwLqHm = (UWORD)(((ULONG)scm_uwLqPu * cof_uwLbHm) >> 10); // Real Value, unit: 0.01mH, q Inductance
  197. obs_stObsCoefIn.uwLdHm = cp_stMotorPara.swLdmH; // Real Value, unit: 0.01mH, d Inductance
  198. obs_stObsCoefIn.uwFluxWb = cp_stMotorPara.swFluxWb; // Real Value, unit: 0.01mWb, Flux linkage
  199. obs_stObsCoefIn.uwFreqTbcHz = FTBC_HZ; // Real Value, Unit:Hz Tbc
  200. obs_stObsCoefIn.uwFluxDampingRatio = cp_stControlPara.swObsFluxPIDampratio; // Real Value, unit:0.1
  201. obs_stObsCoefIn.uwFluxCrossFreqHz = cp_stControlPara.swObsFluxPICrossfreHz; // Real Value, unit:Hz
  202. obs_stObsCoefIn.uwSpdPllWvcHz = cp_stControlPara.swObsSpdPLLBandWidthHz; // Real Value, Unit:Hz
  203. obs_stObsCoefIn.uwSpdPllMcoef = cp_stControlPara.swObsSpdPLLM;
  204. obs_voObsCoef(&obs_stObsCoefIn, &obs_stObsCoefPu);
  205. /* Speed PI coefficient calculate */
  206. asr_stSpdPICoefIn.uwUbVt = VBASE;
  207. asr_stSpdPICoefIn.uwIbAp = IBASE;
  208. asr_stSpdPICoefIn.uwFbHz = FBASE;
  209. asr_stSpdPICoefIn.uwFTbsHz = FTBS_HZ;
  210. asr_stSpdPICoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
  211. asr_stSpdPICoefIn.uwMtJm = cp_stMotorPara.swJD;
  212. asr_stSpdPICoefIn.uwMtFlxWb = cp_stMotorPara.swFluxWb;
  213. asr_stSpdPICoefIn.uwMcoef = cp_stControlPara.swAsrPIM;
  214. asr_stSpdPICoefIn.uwWvcHz = cp_stControlPara.swAsrPIBandwidth;
  215. asr_stSpdPICoefIn.uwRatioJm = cp_stControlPara.swAsrSpdInerRate;
  216. asr_voSpdPICoef(&asr_stSpdPICoefIn, &asr_stSpdPICoef);
  217. /* Torque Observe coefficient calculate */
  218. torqobs_stCoefIn.uwUbVt = VBASE;
  219. torqobs_stCoefIn.uwIbAp = IBASE;
  220. torqobs_stCoefIn.uwFbHz = FBASE;
  221. torqobs_stCoefIn.uwFTbsHz = FTBS_HZ;
  222. torqobs_stCoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
  223. torqobs_stCoefIn.uwMtJm = cp_stMotorPara.swJD;
  224. torqobs_stCoefIn.uwMtFlxWb = cp_stMotorPara.swFluxWb;
  225. torqobs_stCoefIn.uwWtcHz = 50; // cp_stControlPara.swAsrPIBandwidth;
  226. torqobs_stCoefIn.uwRatioJm = cp_stControlPara.swAsrSpdInerRate;
  227. torqobs_voCoef(&torqobs_stCoefIn, &torqobs_stCoef);
  228. mth_voLPFilterCoef(1000000 / 50, FTBS_HZ, &scm_stIqLoadLpf.uwKx);
  229. /* Id PI coefficient calculate */
  230. acr_stCurIdPICoefIn.uwFbHz = FBASE;
  231. acr_stCurIdPICoefIn.uwUbVt = VBASE;
  232. acr_stCurIdPICoefIn.uwIbAp = IBASE;
  233. acr_stCurIdPICoefIn.uwLHm = cp_stMotorPara.swLdmH;
  234. acr_stCurIdPICoefIn.uwMtRsOh = cp_stMotorPara.swRsOhm;
  235. acr_stCurIdPICoefIn.uwFTbcHz = FTBC_HZ;
  236. acr_stCurIdPICoefIn.uwRaCoef = cp_stControlPara.swAcrRaCoef; // Coefficient of Active Resistance
  237. acr_stCurIdPICoefIn.uwWicHz = cp_stControlPara.swAcrPIBandwidth; // Current loop frequency bandwidth
  238. acr_voCurPICoef(&acr_stCurIdPICoefIn, &acr_stCurIdPICoef);
  239. /* Iq PI coefficient calculate */
  240. acr_stCurIqPICoefIn.uwFbHz = FBASE;
  241. acr_stCurIqPICoefIn.uwUbVt = VBASE;
  242. acr_stCurIqPICoefIn.uwIbAp = IBASE;
  243. acr_stCurIqPICoefIn.uwLHm = cp_stMotorPara.swLqmH;
  244. acr_stCurIqPICoefIn.uwMtRsOh = cp_stMotorPara.swRsOhm;
  245. acr_stCurIqPICoefIn.uwFTbcHz = FTBC_HZ;
  246. acr_stCurIqPICoefIn.uwRaCoef = cp_stControlPara.swAcrRaCoef;
  247. acr_stCurIqPICoefIn.uwWicHz = cp_stControlPara.swAcrPIBandwidth;
  248. acr_voCurPICoef(&acr_stCurIqPICoefIn, &acr_stCurIqPICoef);
  249. /* Current decoupling coefficient calculate */
  250. acr_stUdqDcpCoefIn.uwLdHm = cp_stMotorPara.swLdmH;
  251. acr_stUdqDcpCoefIn.uwLqHm = cp_stMotorPara.swLqmH;
  252. acr_stUdqDcpCoefIn.uwMtFlxWb = cp_stMotorPara.swFluxWb;
  253. acr_stUdqDcpCoefIn.uwUbVt = VBASE;
  254. acr_stUdqDcpCoefIn.uwFbHz = FBASE;
  255. acr_stUdqDcpCoefIn.uwIbAp = IBASE;
  256. acr_voUdqDcpCoef(&acr_stUdqDcpCoefIn, &acr_stUdqDcpCoef);
  257. /* Id feedback low pass filter coef */
  258. ulLpfTm = 1000000 / cp_stControlPara.swAcrCurFbLpfFre;
  259. mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stIdFbkLpf.uwKx);
  260. /* Iq feedback low pass filter coef */
  261. ulLpfTm = 1000000 / cp_stControlPara.swAcrCurFbLpfFre;
  262. mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stIqFbkLpf.uwKx);
  263. /* Coefficient update only once */
  264. if (!scm_blCoefUpdateFlg)
  265. {
  266. /* Deadband compensation coefficient calculate */
  267. dbc_stDbCompCoefIn.uwDeadBandTimeNs = cp_stControlPara.swIPMDeadTimeNs; // unit: ns, Dead band time
  268. dbc_stDbCompCoefIn.uwPosSwOnTimeNs = cp_stControlPara.swIPMTurnOnNs; // unit: ns, IPM switch-on time at positive current
  269. dbc_stDbCompCoefIn.uwPosSwOffTimeNs = cp_stControlPara.swIPMTurnOnNs; // unit: ns, IPM switch-off time at positive current
  270. dbc_stDbCompCoefIn.uwNegSwOnTimeNs = cp_stControlPara.swIPMTurnOnNs; // unit: ns, IPM switch-on time at negative current
  271. dbc_stDbCompCoefIn.uwNegSwOffTimeNs = cp_stControlPara.swIPMTurnOnNs; // unit: ns, IPM switch-off time at negative current
  272. dbc_stDbCompCoefIn.ulPWMPerUs = PWM_PERIOD_US; // unit: 0.1us, PWM period
  273. dbc_stDbCompCoefIn.uwKcoefVtPerAp = cp_stControlPara.swDbcK; // Q6, Deadband compensation slope coefficient
  274. dbc_stDbCompCoefIn.uwVBaseVt = VBASE; // Q0, Vbase
  275. dbc_stDbCompCoefIn.uwIBaseAp = IBASE; // Q0, Ibase
  276. dbc_voDBCompCoef(&dbc_stDbCompCoefIn, &dbc_stDbCompCoef);
  277. /* Flux weakening coefficient calculate */
  278. flx_stCtrlCoefIn.swIdMaxAp = (SWORD)cp_stMotorPara.swIdMaxA; // Q0,unit: 0.01A
  279. flx_stCtrlCoefIn.swIdMinAp = (SWORD)cp_stMotorPara.swIdMinA; // Q0,unit: 0.01A
  280. flx_stCtrlCoefIn.uwRsOhm = cp_stMotorPara.swRsOhm; // Q0,unit: 0.1mOhm
  281. flx_stCtrlCoefIn.swIdPIOutMinAp = (SWORD)cp_stControlPara.swFwIdPIOutMin; // Q0,unit: 0.01A
  282. flx_stCtrlCoefIn.uwCharCurCrossFreqHz = cp_stControlPara.swFwCharCurCrossFre; // Q0,unit: SQRT(1/2piR)
  283. flx_stCtrlCoefIn.uwCharCurDampRatio = cp_stControlPara.swFwCharCurDampRatio; // Q0,unit: SQRT(pi/2R)
  284. flx_stCtrlCoefIn.uwIdRegKpPu = cp_stControlPara.swFwIdKpPu; // Q16,unit: A/V2
  285. flx_stCtrlCoefIn.uwIdRegKiPu = cp_stControlPara.swFwIdKiPu; // Q16,unit: A/V2
  286. flx_stCtrlCoefIn.uwPWMDutyMax = cp_stControlPara.swFwPWMMaxDuty; // Q0,%
  287. flx_stCtrlCoefIn.uwVdcLpfFreqHz = cp_stControlPara.swFwVdcLPFFre; // Q0,unit: Hz
  288. flx_stCtrlCoefIn.uwVdcMinCalcTmMs = cp_stControlPara.swFwVdcMinCalTMms; // Q0,unit: ms
  289. flx_stCtrlCoefIn.uwFwCurLimAp = cp_stMotorPara.swIpeakMaxA; // Q0,unit: 0.01A
  290. flx_stCtrlCoefIn.uwIdMinLimRatio = cp_stControlPara.swFwIdMinLimRatio; // Q0,0.01
  291. flx_stCtrlCoefIn.uwUbVt = VBASE; // Q0,unit: 0.1V, Voltage base
  292. flx_stCtrlCoefIn.uwFreqTbcHz = FTBC_HZ; // Q0
  293. flx_stCtrlCoefIn.uwIBaseAp = IBASE; // Q0,unit: 0.01A, Base Current
  294. flx_stCtrlCoefIn.uwFBaseHz = FBASE; // Q0,unit: Hz, Base Frequency
  295. flx_voCoef(&flx_stCtrlCoefIn, &flx_stCtrlCoef);
  296. // fw_stFluxWeakeningCoefInPu
  297. // fw_voFluxWeakeningCoef(fw_stFluxWeakeningCoefInPu,flx_stCtrlCoef)
  298. /* Constant vlotage brake coefficient calculate */
  299. cvb_stBrakeCoefIn.uwVdcCvbVt = cp_stControlPara.swCvbConstantVolBrakeV;
  300. cvb_stBrakeCoefIn.uwLowSpdRpm = cp_stControlPara.swCvbConstantSpdLowRpm;
  301. cvb_stBrakeCoefIn.swIqRefMaxAp = cp_stMotorPara.swIpeakMaxA;
  302. cvb_stBrakeCoefIn.swIdRefMaxAp = cp_stMotorPara.swIdMaxA;
  303. cvb_stBrakeCoefIn.swIdRefMinAp = cp_stMotorPara.swIdMinA;
  304. cvb_stBrakeCoefIn.uwVBaseVt = VBASE;
  305. cvb_stBrakeCoefIn.uwIBaseAp = IBASE;
  306. cvb_stBrakeCoefIn.uwFBaseHz = FBASE;
  307. cvb_stBrakeCoefIn.uwMotorPairs = cp_stMotorPara.swMotrPolePairs;
  308. cvb_voBrakeCoef(&cvb_stBrakeCoefIn, &cvb_stBrakeCoef);
  309. /* Speed feedback low pass filter coef */
  310. ulLpfTm = 1000000 / cp_stControlPara.swAsrSpdFbLPFFre;
  311. mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stSpdFbkLpf.uwKx);
  312. /* Power limit coef */
  313. ulLpfTm = 1000000 / cp_stControlPara.swPwrLimitLPFFre;
  314. mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stMotoPwrInLpf.uwKx);
  315. pwr_stPwrLimCofIn.swPwrLimW = cp_stControlPara.swPwrLimitValWt; // Q0, unit: 0.1w, Power limit value
  316. pwr_stPwrLimCofIn.uwPwrErrW = cp_stControlPara.swPwrLimitErrWt; // Q0, unit: 0.1w, Start power limit when "VAL - ERR"
  317. pwr_stPwrLimCofIn.swIqMaxAp = cp_stMotorPara.swIpeakMaxA; // Q0, unit: 0.01A, Max phase current (peak value)
  318. pwr_stPwrLimCofIn.uwIBaseAp = IBASE; // Q0,unit: 0.01A, Base Current
  319. pwr_stPwrLimCofIn.uwUbVt = VBASE; // Q0,unit: 0.1V, Voltage base
  320. pwr_stPwrLimCofIn.uwPwrLimPIKp = cp_stControlPara.swPwrLimitKpPu;
  321. pwr_stPwrLimCofIn.uwPwrLimPIKi = cp_stControlPara.swPwrLimitKiPu;
  322. pwr_stPwrLimCofIn.uwPwrLimSTARTCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
  323. pwr_stPwrLimCofIn.uwPwrLimENDCe = cp_stControlPara.swAlmOverHeatCeVal;
  324. pwr_voPwrLimCof(&pwr_stPwrLimCofIn, &pwr_stPwrLimCof);
  325. /*Accelaration&Decelaration limit*/
  326. if (ABS(scm_swSpdRefPu) < USER_MOTOR_300RPM2PU)
  327. {
  328. cmd_stCmdCoefIn.ulAccelPu = ulAccel100rpmpsPu; // Q29
  329. }
  330. else
  331. {
  332. cmd_stCmdCoefIn.ulAccelPu = ulAccel100rpmpsPu; // Q29
  333. }
  334. cmd_stCmdCoefIn.ulDecelPu = ulAccel100rpmpsPu*10; // Q29
  335. cmd_stCmdCoefIn.swBrakeSpdDeltaPu = USER_MOTOR_100RPM2PU;
  336. cmd_voCmdCoef(&cmd_stCmdCoefIn, &cmd_stCmdCoef);
  337. pwm_stGenCoefIn.uwPWMDutyMax = cp_stControlPara.swPWMMaxDuty;
  338. pwm_stGenCoefIn.uwPWM7To5Duty = cp_stControlPara.swPWM7to5Duty;
  339. pwm_stGenCoefIn.uwPWMMinSample1Pu = cp_stControlPara.swPWMMinSampleDuty1;
  340. pwm_stGenCoefIn.uwPWMMinSample2Pu = cp_stControlPara.swPWMMinSampleDuty2;
  341. pwm_stGenCoefIn.uwPWMMinSample3Pu = cp_stControlPara.swPWMMinSampleDuty3;
  342. pwm_stGenCoefIn.uwSampleSteadyPu = cp_stControlPara.swPWMSampleToSteady;
  343. pwm_stGenCoefIn.uwSingelResisSamplePu = cp_stControlPara.swPWMSampleSigR;
  344. pwm_stGenCoefIn.uwOvmNo = cp_stControlPara.swPWMOverMdlMode;
  345. pwm_stGenCoefIn.uwPWMPd = HW_INIT_PWM_PERIOD;
  346. pwm_voGenCoef(&pwm_stGenCoefIn, &pwm_stGenCoef);
  347. scm_uwAcrLimCof = (UWORD)((ULONG)cp_stControlPara.swPWMMaxDuty * cp_stControlPara.uwAcrCurOutLim / 1000); // Q15
  348. scm_uwUdcpLimCof = (UWORD)((ULONG)cp_stControlPara.swPWMMaxDuty * cp_stControlPara.uwAcrUdcpOutLim / 1000); // Q15
  349. }
  350. }
  351. /***************************************************************
  352. Function: scm_voSpdCtrMdTbs;
  353. Description: Speed control mode TBS scheduler
  354. Call by: tbs_voIsr();
  355. Input Variables: N/A
  356. Output/Return Variables: N/A
  357. Subroutine Call: ...;
  358. Reference: N/A
  359. ****************************************************************/
  360. void scm_voSpdCtrMdTbs(void)
  361. {
  362. SWORD swIqLowerPu;
  363. /* Speed feedback LPF */
  364. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  365. {
  366. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  367. }
  368. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  369. {
  370. mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  371. }
  372. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  373. {
  374. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  375. }
  376. else
  377. {
  378. //do noting
  379. }
  380. /* Speed feedback Absolute */
  381. scm_uwSpdFbkLpfAbsPu = (UWORD)ABS(scm_stSpdFbkLpf.slY.sw.hi);
  382. /*============================================================
  383. Speed command generator to generate speed ramp
  384. =============================================================*/
  385. if(curSpeed_state.state == ClzLoop || curSpeed_state.state == Open2Clz)
  386. {
  387. cmd_stCmdIn.swSpdCmdRpm = (SWORD)uart_slSpdRefRpm;
  388. cmd_stCmdIn.swSpdNowPu = scm_stSpdFbkLpf.slY.sw.hi;
  389. cmd_voCmdOut(&cmd_stCmdIn, &cmd_stCmdCoef, &cmd_stCmdOut);
  390. scm_swRotateDir = cmd_stCmdOut.swNewCmdDir;
  391. scm_swSpdRefPu = cmd_stCmdOut.swIntRefPu; // cmd_stCmdGenOut.Out.swSpdRefPu;
  392. }
  393. else if (curSpeed_state.state == StartUp)
  394. {
  395. SWORD tempSpeed = 0;
  396. tempSpeed = cp_stControlPara.swDragSpdHz * 60 / cp_stMotorPara.swMotrPolePairs;
  397. if(cp_stFlg.RunModelSelect == ClZLOOP)
  398. {
  399. if(uart_slSpdRefRpm>0)
  400. {
  401. cmd_stCmdIn.swSpdCmdRpm = tempSpeed;
  402. }
  403. else
  404. {
  405. cmd_stCmdIn.swSpdCmdRpm = -tempSpeed;
  406. }
  407. }
  408. else if(cp_stFlg.RunModelSelect == VFContorl || cp_stFlg.RunModelSelect == IFContorl)
  409. {
  410. if(cp_stFlg.RotateDirectionSelect == ForwardRotate)
  411. {
  412. cmd_stCmdIn.swSpdCmdRpm = tempSpeed;
  413. }
  414. else
  415. {
  416. cmd_stCmdIn.swSpdCmdRpm = -tempSpeed;
  417. }
  418. }
  419. else
  420. {
  421. //do noting
  422. }
  423. cmd_stCmdIn.swSpdNowPu = scm_stSpdFbkLpf.slY.sw.hi;
  424. cmd_voCmdOut(&cmd_stCmdIn, &cmd_stCmdCoef, &cmd_stCmdOut);
  425. scm_swRotateDir = cmd_stCmdOut.swNewCmdDir;
  426. scm_swSpdRefPu = cmd_stCmdOut.swIntRefPu; // cmd_stCmdGenOut.Out.swSpdRefPu;
  427. }
  428. else
  429. {
  430. cmd_stCmdIn.swSpdCmdRpm = 0;
  431. cmd_stCmdIn.swSpdNowPu = scm_stSpdFbkLpf.slY.sw.hi;
  432. cmd_voCmdOut(&cmd_stCmdIn, &cmd_stCmdCoef, &cmd_stCmdOut);
  433. scm_swRotateDir = cmd_stCmdOut.swNewCmdDir;
  434. scm_swSpdRefPu = cmd_stCmdOut.swIntRefPu; // cmd_stCmdGenOut.Out.swSpdRefPu;
  435. }
  436. /*=======================================================================
  437. Speed PI Controller
  438. =======================================================================*/
  439. asr_stSpdPIIn.swSpdRefPu = scm_swSpdRefPu; // Q15
  440. asr_stSpdPIIn.swSpdFdbPu = scm_stSpdFbkLpf.slY.sw.hi; // Q15
  441. if(curSpeed_state.state != ClzLoop)
  442. {
  443. swIqLowerPu = flx_stCtrlOut.swIqLimPu;
  444. }
  445. else
  446. {
  447. swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < ABS(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : ABS(pwr_stPwrLimOut2.swIqLimPu));
  448. }
  449. if (scm_swRotateDir > 0)
  450. {
  451. asr_stSpdPIIn.swIqMaxPu = swIqLowerPu;
  452. asr_stSpdPIIn.swIqMinPu = -swIqLowerPu;
  453. }
  454. else
  455. {
  456. asr_stSpdPIIn.swIqMaxPu = swIqLowerPu;
  457. asr_stSpdPIIn.swIqMinPu = -swIqLowerPu;
  458. }
  459. asr_voSpdPI(&asr_stSpdPIIn, &asr_stSpdPICoef, &asr_stSpdPIOut);
  460. /* Torque observe */
  461. // if (scm_swRotateDir > 0)
  462. // {
  463. // torqobs_stCalIn.swIqMaxPu = swIqLowerPu;
  464. // torqobs_stCalIn.swIqMinPu = -swIqLowerPu;
  465. // }
  466. // else
  467. // {
  468. // torqobs_stCalIn.swIqMaxPu = swIqLowerPu;
  469. // torqobs_stCalIn.swIqMinPu = -swIqLowerPu;
  470. // }
  471. // torqobs_stCalIn.swIqfbkPu = scm_swIqFdbLpfPu;
  472. // torqobs_stCalIn.swSpdPu = spi_stResolverOut.swSpdFbkPu;
  473. // torqobs_voCal(&torqobs_stCalIn, &torqobs_stCoef, &torqobs_stCalOut);
  474. // mth_voLPFilter(torqobs_stCalOut.swIqLoadPu, &scm_stIqLoadLpf);
  475. // swCurRefrompu = asr_stSpdPIOut.swIqRefPu - scm_stIqLoadLpf.slY.sw.hi;
  476. // if (swCurRefrompu > swIqLowerPu)
  477. // {
  478. // swCurRefrompu = swIqLowerPu;
  479. // }
  480. // else if (swCurRefrompu < -swIqLowerPu)
  481. // {
  482. // swCurRefrompu = -swIqLowerPu;
  483. // }
  484. // else
  485. // {}
  486. swCurRefrompu = asr_stSpdPIOut.swIqRefPu;
  487. curSpeed_state.Tbs_hook();
  488. }
  489. void scm_voTorqCtrMdTbs(void)
  490. {
  491. SWORD swIqLowerPu;
  492. /* Speed feedback LPF */
  493. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  494. {
  495. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  496. }
  497. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  498. {
  499. mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  500. }
  501. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  502. {
  503. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  504. }
  505. else
  506. {
  507. //do noting
  508. }
  509. /* Speed feedback Absolute */
  510. scm_uwSpdFbkLpfAbsPu = (UWORD)ABS(scm_stSpdFbkLpf.slY.sw.hi);
  511. /* Current limit value */
  512. swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < ABS(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : ABS(pwr_stPwrLimOut2.swIqLimPu));
  513. /* Torque observer */
  514. // if (scm_swRotateDir > 0)
  515. // {
  516. // torqobs_stCalIn.swIqMaxPu = swIqLowerPu;
  517. // torqobs_stCalIn.swIqMinPu = -swIqLowerPu;
  518. // }
  519. // else
  520. // {
  521. // torqobs_stCalIn.swIqMaxPu = swIqLowerPu;
  522. // torqobs_stCalIn.swIqMinPu = -swIqLowerPu;
  523. // }
  524. // torqobs_stCalIn.swIqfbkPu = scm_swIqFdbLpfPu;
  525. // torqobs_stCalIn.swSpdPu = spi_stResolverOut.swSpdFbkPu;
  526. // torqobs_voCal(&torqobs_stCalIn, &torqobs_stCoef, &torqobs_stCalOut);
  527. // mth_voLPFilterCoef(1000000 / 250, FTBS_HZ, &scm_stIqLoadLpf.uwKx);
  528. // mth_voLPFilter((torqobs_stCalOut.swIqLoadPu + scm_swIqFdbLpfPu), &scm_stIqLoadLpf);
  529. /* Speed fluctuation compensation */
  530. mth_voLPFilterCoef(1000000 / 50, FTBS_HZ, &scm_stIqLoadLpf.uwKx);
  531. mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stIqLoadLpf);
  532. scm_swSpdFbkCompPu = spi_stResolverOut.swSpdFbkPu - scm_stIqLoadLpf.slY.sw.hi;
  533. swTmpSpdRate = (SLONG)scm_swSpdFbkCompPu * ass_stParaSet.uwSpeedAssistSpdRpm >> 11;
  534. mth_voLPFilterCoef(1000000 / 250, FTBS_HZ, &swTmpSpdRateLpf.uwKx);
  535. mth_voLPFilter(swTmpSpdRate, &swTmpSpdRateLpf);
  536. // swTmpSpdRateLpf.slY.sw.hi = swTmpSpdRate;
  537. /* Torque Calculate */
  538. // swTmpSpdRate = spi_stResolverOut.swSpdFbkPu - swTmpSpdFbkPuZ1; //Q15
  539. // mth_voLPFilterCoef(1000000 / 30, FTBS_HZ, &swTmpSpdRateLpf.uwKx); //30Hz,TBS
  540. // mth_voLPFilter(swTmpSpdRate, &swTmpSpdRateLpf);
  541. // swTmpSpdFbkPuZ1 = spi_stResolverOut.swSpdFbkPu;
  542. // scm_swSpdFbkCompPu = scm_stSpdFbkLpf.slY.sw.hi + (SLONG)swTmpSpdRateLpf.slY.sw.hi * FTBS_HZ / 30; //30Hz,TBS
  543. // mth_voLPFilterCoef(1000000 / 250, FTBS_HZ, &scm_stIqLoadLpf.uwKx);
  544. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stIqLoadLpf);
  545. // scm_swSpdFbkCompPu = spi_stResolverOut.swSpdFbkPu - scm_stIqLoadLpf.slY.sw.hi;
  546. //
  547. // swTmpSpdRate = (SLONG)scm_swSpdFbkCompPu * ass_stParaSet.uwSpeedAssistSpdRpm >> 11; // 系数 = J/Tlpf/Pesi
  548. //
  549. //// mth_voLPFilterCoef(1000000 / 250, FTBS_HZ, &swTmpSpdRateLpf.uwKx);
  550. //// mth_voLPFilter(swTmpSpdRate, &swTmpSpdRateLpf);
  551. // swTmpSpdRateLpf.slY.sw.hi = swTmpSpdRate;
  552. //
  553. /* Iqref Compensation */
  554. if(((uart_swTorqRefNm < -200)||(uart_swTorqRefNm > 200)) && (Ass_FSM !=Spd2Torq) && (Ass_FSM !=SpeedAssit))
  555. {
  556. /* Torque Calculate */
  557. //swTestIqref = uart_swTorqRefNm - (((SLONG)swTmpSpdRateLpf.slY.sw.hi * cof_uwJmPu * 2 << 11) / cof_uwFluxPu); //Q15+Q0+Q11-Q12=Q14
  558. /* Speed fluctuation compensation*/
  559. swTestIqref = uart_swTorqRefNm - swTmpSpdRateLpf.slY.sw.hi;
  560. /* Torgque observer */
  561. // swTestIqref = uart_swTorqRefNm - scm_stIqLoadLpf.slY.sw.hi;
  562. }
  563. else
  564. {
  565. swTestIqref = uart_swTorqRefNm;
  566. }
  567. /* Current limit */
  568. if (swTestIqref > swIqLowerPu)
  569. {
  570. swTestIqref = swIqLowerPu;
  571. }
  572. else if (swTestIqref < -swIqLowerPu)
  573. {
  574. swTestIqref = -swIqLowerPu;
  575. }
  576. else
  577. {
  578. //do noting
  579. }
  580. swCurRefrompu = swTestIqref;
  581. /* 3rd FSM*/
  582. curSpeed_state.Tbs_hook();
  583. }
  584. /***************************************************************
  585. Function: scm_voSpdCtrMdUpTbc;
  586. Description: Speed control mode TBC scheduler
  587. Call by: tbc_voIsr();
  588. Input Variables: N/A
  589. Output/Return Variables: N/A
  590. Subroutine Call: ...;
  591. Reference: N/A
  592. ****************************************************************/
  593. void scm_voSpdCtrMdUpTbc(void)
  594. {
  595. /*=======================================================================
  596. Max voltage of current PI out
  597. =======================================================================*/
  598. scm_swVsLimPu = (SWORD)((SWORD)adc_stUpOut.uwVdcLpfPu * (SLONG)scm_uwAcrLimCof >> 15); // Q14+Q15-Q15=Q14
  599. scm_swVsDcpLimPu = (SWORD)((SWORD)adc_stUpOut.uwVdcLpfPu * (SLONG)scm_uwUdcpLimCof >> 15); // Q14+Q15-Q15=Q14
  600. /*=======================================================================
  601. Voltage get
  602. =======================================================================*/
  603. /* Get Ualpha & Ubeta from command voltage */
  604. scm_swUalphaPu = pwm_stGenOut.swUalphaPu - scm_swUalphaCompPu; // Q14
  605. scm_swUbetaPu = pwm_stGenOut.swUbetaPu - scm_swUbetaCompPu; // Q14
  606. /*=======================================================================
  607. Startup control FSM
  608. =======================================================================*/
  609. scm_voSpdCtrMdFSM();
  610. curSpeed_state.Tbcup_hook();
  611. }
  612. /***************************************************************
  613. Function: scm_voSpdCtrMdTbc;
  614. Description: Speed control mode TBC scheduler
  615. Call by: tbc_voIsr();
  616. Input Variables: N/A
  617. Output/Return Variables: N/A
  618. Subroutine Call: ...;
  619. Reference: N/A
  620. ****************************************************************/
  621. void scm_voSpdCtrMdDownTbc(void)
  622. {
  623. /*=======================================================================
  624. Clark transformation for phase current
  625. =======================================================================*/
  626. crd_stClarkIn.swAPu = adc_stDownOut.swIaPu; // Q14
  627. crd_stClarkIn.swBPu = adc_stDownOut.swIbPu; // Q14
  628. crd_stClarkIn.swCPu = adc_stDownOut.swIcPu; // Q14
  629. crd_voClark(&crd_stClarkIn, &crd_stCurClarkOut);
  630. /*=======================================================================
  631. Code Of spdFSM
  632. =======================================================================*/
  633. curSpeed_state.Tbcdown_hook();
  634. /*=======================================================================
  635. Current loop control
  636. =======================================================================*/
  637. /* Get Id & Iq for current PI control */
  638. /*=======================================================================
  639. Park transformation for current
  640. =======================================================================*/
  641. crd_stParkIn.swAlphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  642. crd_stParkIn.swBetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  643. crd_stParkIn.uwThetaPu = scm_uwAngParkPu; // Q15
  644. crd_voPark(&crd_stParkIn, &crd_stCurParkOut);
  645. /*=======================================================================
  646. Current feedback LPF
  647. =======================================================================*/
  648. mth_voLPFilter(crd_stCurParkOut.swDPu, &scm_stIdFbkLpf);
  649. mth_voLPFilter(crd_stCurParkOut.swQPu, &scm_stIqFbkLpf);
  650. scm_swIdFdbLpfPu = scm_stIdFbkLpf.slY.sw.hi;
  651. scm_swIqFdbLpfPu = scm_stIqFbkLpf.slY.sw.hi;
  652. /*=======================================================================
  653. Calculate input power of motor
  654. =======================================================================*/
  655. scm_swMotorPwrInPu = (SWORD)(((SLONG)Test_U_out.swDPu * scm_swIdFdbLpfPu + (SLONG)Test_U_out.swQPu * scm_swIqFdbLpfPu) >> 13); // Q14+Q14-Q13=Q15
  656. mth_voLPFilter(scm_swMotorPwrInPu, &scm_stMotoPwrInLpf);
  657. scm_swMotorPwrInLpfWt = (SWORD)(scm_stMotoPwrInLpf.slY.sw.hi * (SLONG)cof_uwPbWt >> 15); // unit: 0.1w
  658. /*=======================================================================
  659. Id current PI control
  660. =======================================================================*/
  661. //DCPswitch = 0; //0 with forwardFeedBack 1 without forwardFeedBack
  662. acr_stCurIdPIIn.swCurRefPu = scm_swIdRefPu; // Q14
  663. acr_stCurIdPIIn.swCurFdbPu = scm_swIdFdbLpfPu;
  664. if (DCPswitch == 1)
  665. {
  666. acr_stCurIdPIIn.swUmaxPu = scm_swVsDcpLimPu; // Q14
  667. acr_stCurIdPIIn.swUminPu = -scm_swVsDcpLimPu; // Q14
  668. }
  669. else if (DCPswitch == 0)
  670. {
  671. acr_stCurIdPIIn.swUmaxPu = scm_swVsDcpLimPu - acr_stUdqDcpOut.swUdPu; // Q14
  672. acr_stCurIdPIIn.swUminPu = -scm_swVsDcpLimPu - acr_stUdqDcpOut.swUdPu; // Q14
  673. // acr_stCurIdPIIn.swUmaxPu = scm_swVsLimPu - acr_stUdqDcpOut.swUdPu; // Q14
  674. // acr_stCurIdPIIn.swUminPu = -scm_swVsLimPu - acr_stUdqDcpOut.swUdPu; // Q14
  675. }
  676. else
  677. {
  678. //do noting
  679. }
  680. acr_voCurPI(&acr_stCurIdPIIn, &acr_stCurIdPICoef, &acr_stCurIdPIOut);
  681. /*=======================================================================
  682. Iq current PI control
  683. =======================================================================*/
  684. acr_stCurIqPIIn.swCurRefPu = scm_swIqRefPu; // Q14
  685. acr_stCurIqPIIn.swCurFdbPu = scm_swIqFdbLpfPu;
  686. if (DCPswitch == 1)
  687. {
  688. acr_stCurIqPIIn.swUmaxPu = scm_swVsDcpLimPu; // Q14
  689. acr_stCurIqPIIn.swUminPu = -scm_swVsDcpLimPu; // Q14
  690. }
  691. else if (DCPswitch == 0)
  692. {
  693. // if(FSM2nd_Run_state.state == Assistance)
  694. // {
  695. // acr_stCurIqPIIn.swUmaxPu = ass_stCalOut.swVoltLimitPu - acr_stUdqDcpOut.swUqPu; // Q14
  696. // acr_stCurIqPIIn.swUminPu = -ass_stCalOut.swVoltLimitPu - acr_stUdqDcpOut.swUqPu; // Q14
  697. // }
  698. // else
  699. {
  700. acr_stCurIqPIIn.swUmaxPu = scm_swVsDcpLimPu - acr_stUdqDcpOut.swUqPu; // Q14
  701. acr_stCurIqPIIn.swUminPu = -scm_swVsDcpLimPu - acr_stUdqDcpOut.swUqPu; // Q14
  702. }
  703. // scm_swUqLimPu = mth_slSqrt(((SLONG)scm_swVsLimPu * scm_swVsLimPu) - (SLONG)(acr_stCurIdPIOut.swURefPu + acr_stUdqDcpOut.swUdPu) * (acr_stCurIdPIOut.swURefPu + acr_stUdqDcpOut.swUdPu));//Q14
  704. // acr_stCurIqPIIn.swUmaxPu = scm_swUqLimPu - acr_stUdqDcpOut.swUqPu; // Q14
  705. // acr_stCurIqPIIn.swUminPu = -scm_swUqLimPu - acr_stUdqDcpOut.swUqPu; // Q14
  706. }
  707. else
  708. {
  709. //do noting
  710. }
  711. acr_voCurPI(&acr_stCurIqPIIn, &acr_stCurIqPICoef, &acr_stCurIqPIOut);
  712. if (DCPswitch == 1)
  713. {
  714. scm_swUqRefPu = acr_stCurIqPIOut.swURefPu; // Q14
  715. scm_swUdRefPu = acr_stCurIdPIOut.swURefPu; // Q14
  716. }
  717. else if (DCPswitch == 0)
  718. {
  719. scm_swUqRefPu = acr_stCurIqPIOut.swURefPu + acr_stUdqDcpOut.swUqPu; // Q14
  720. scm_swUdRefPu = acr_stCurIdPIOut.swURefPu + acr_stUdqDcpOut.swUdPu; // Q14
  721. }
  722. else
  723. {
  724. //do noting
  725. }
  726. /*=======================================================================
  727. IPark transformation for current
  728. =======================================================================*/
  729. crd_stIParkIn.swDPu = scm_swUdRefPu;
  730. crd_stIParkIn.swQPu = scm_swUqRefPu;
  731. crd_stIParkIn.uwThetaPu = scm_uwAngIParkPu;
  732. crd_voIPark(&crd_stIParkIn, &crd_stVltIParkOut);
  733. /*=======================================================================
  734. Deadband compensation
  735. =======================================================================*/
  736. #if (0)
  737. dbc_stDbCompIn.swIaPu = adc_stDownOut.swIaPu; // Q14
  738. dbc_stDbCompIn.swIbPu = adc_stDownOut.swIbPu; // Q14
  739. dbc_stDbCompIn.swIcPu = adc_stDownOut.swIcPu; // Q14
  740. dbc_stDbCompIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
  741. dbc_stDbCompIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // Q15
  742. // dbc_stDbCompCoef.uwNegWinVoltDuty = mn_uwNegWinVoltDuty;
  743. // dbc_stDbCompCoef.uwPosLostVoltDuty = mn_uwPosLostVoltDuty;
  744. dbc_voDBComp(&dbc_stDbCompIn, &dbc_stDbCompCoef, &dbc_stDbCompOut);
  745. #endif
  746. scm_swUalphaRefPu = crd_stVltIParkOut.swAlphaPu + dbc_stDbCompOut.swUalphaCompPu; // Q14
  747. scm_swUbetaRefPu = crd_stVltIParkOut.swBetaPu + dbc_stDbCompOut.swUbetaCompPu; // Q14
  748. scm_swUalphaCompPu = dbc_stDbCompOut.swUalphaCompPu; // Q14
  749. scm_swUbetaCompPu = dbc_stDbCompOut.swUbetaCompPu; // Q14
  750. /*=======================================================================
  751. PWM generate
  752. =======================================================================*/
  753. if(cp_stFlg.RunModelSelect == VFContorl)
  754. {
  755. SWORD swVFVolAmp = 0;
  756. swVFVolAmp = (SWORD)(((SLONG)cp_stControlPara.swDragVolAp << 14) / VBASE);
  757. if(cp_stFlg.RotateDirectionSelect == ForwardRotate)
  758. {
  759. crd_stIParkIn.swDPu = 0;
  760. crd_stIParkIn.swQPu = swVFVolAmp;
  761. }
  762. else if(cp_stFlg.RotateDirectionSelect == BackwardRotate)
  763. {
  764. crd_stIParkIn.swDPu = 0;
  765. crd_stIParkIn.swQPu = -swVFVolAmp;
  766. }
  767. else
  768. {
  769. //do noting
  770. }
  771. crd_stIParkIn.uwThetaPu = scm_uwAngIParkPu;//scm_uwAngIParkPu;
  772. crd_voIPark(&crd_stIParkIn, &crd_stVltIParkOut);
  773. scm_swUalphaRefPu = crd_stVltIParkOut.swAlphaPu ;
  774. scm_swUbetaRefPu = crd_stVltIParkOut.swBetaPu;
  775. }
  776. pwm_stGenIn.swUalphaPu = scm_swUalphaRefPu; // Q14
  777. pwm_stGenIn.swUbetaPu = scm_swUbetaRefPu; // Q14
  778. pwm_stGenIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
  779. pwm_voGen(&pwm_stGenIn, &pwm_stGenCoef, &pwm_stGenOut);
  780. iPwm_SetCompareGroupValues16(0, pwm_stGenOut.uwNewTIM1COMPR);
  781. ULONG samplingTick[2];
  782. samplingTick[0]=pwm_stGenOut.uwSigRTrig;
  783. samplingTick[1]=pwm_stGenOut.uwRdsonTrig;
  784. iPwm_SyncMultiSamplingCountUp(0, &samplingTick[0], 2);
  785. Test_U_in.swAlphaPu = pwm_stGenOut.swUalphaPu - scm_swUalphaCompPu; // Q14
  786. Test_U_in.swBetaPu = pwm_stGenOut.swUbetaPu - scm_swUbetaCompPu; // Q14
  787. Test_U_in.uwThetaPu = scm_uwAngIParkPu; // Q15
  788. crd_voPark(&Test_U_in, &Test_U_out);
  789. }
  790. /*************************************************************************
  791. Local Functions (N/A)
  792. *************************************************************************/
  793. /*************************************************************************
  794. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  795. All rights reserved.
  796. *************************************************************************/
  797. #ifdef _SPDCTRMODE_C_
  798. #undef _SPDCTRMODE_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
  799. #endif
  800. /*************************************************************************
  801. End of this File (EOF)!
  802. Do not put anything after this part!
  803. *************************************************************************/