spdctrmode.c 37 KB

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