spdctrmode.c 38 KB

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