spdctrmode.c 38 KB

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