spdctrFSM.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: sysfsm.c
  4. Partner Filename: sysfsm.h
  5. Description: System finite state machine
  6. Complier: IAR Embedded Workbench for ARM 7.80.4
  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 _STARTFSM_C_
  20. #define _STARTFSM_C_
  21. #endif
  22. /************************************************************************
  23. Include File
  24. ************************************************************************/
  25. #include "syspar.h"
  26. #include "user.h"
  27. #include "FSM_1st.h"
  28. #include "FSM_2nd.h"
  29. #include "spdctrFSM.h"
  30. #include "switchhall.h"
  31. #include "spi_master.h"
  32. #include "AssistCurve.h"
  33. #include "bikeinformation.h"
  34. #include "cmdgennew.h"
  35. #include "FuncLayerAPI.h"
  36. #include "AngleObserver_discrete.h"
  37. #include "canAppl.h"
  38. #include "obs.h"
  39. /************************************************************************
  40. Constant Table:
  41. ************************************************************************/
  42. /************************************************************************
  43. Exported Functions:
  44. ************************************************************************/
  45. void InitPosDet_TbcupHook(void)
  46. {
  47. scm_ulStatCt++;
  48. align_stIn.ulStatCt = scm_ulStatCt;
  49. align_voInitPos(&align_stIn, &align_stCoef, &align_stOut);
  50. scm_slIdRefPu = align_stOut.slIdRefPu; // Q29
  51. scm_swIdRefPu = align_stOut.swIdRefPu; // Q14
  52. scm_swIqRefPu = align_stOut.swIqRefPu; // Q14
  53. scm_uwAngRefPu = align_stOut.uwAngRefPu; // Q15
  54. }
  55. void ParDet_TbcupHook(void)
  56. {}
  57. void StartUp_TbcupHook(void)
  58. {
  59. flx_stCtrlOut.swIqLimPu = (SWORD)cof_uwCurMaxPu;
  60. scm_ulStatCt++;
  61. align_stIn.ulStatCt = scm_ulStatCt;
  62. align_stIn.swRotateDir = scm_swRotateDir;
  63. align_voStartUp(&align_stIn, &align_stCoef, &align_stOut);
  64. scm_slDragSpdRefPu = align_stOut.slDragSpdRefPu;
  65. scm_slDragSpdPu = align_stOut.slDragSpdPu;
  66. scm_swIdRefPu = align_stOut.swIdRefPu;
  67. if(align_stOut.swIqRefPu > 0)
  68. {
  69. if((scm_swIqRefPu + 2) <= align_stOut.swIqRefPu)
  70. {
  71. scm_swIqRefPu += 2;
  72. }
  73. else
  74. {
  75. scm_swIqRefPu = align_stOut.swIqRefPu;
  76. }
  77. }
  78. else if(align_stOut.swIqRefPu < 0)
  79. {
  80. if((scm_swIqRefPu - 2) >= align_stOut.swIqRefPu)
  81. {
  82. scm_swIqRefPu -= 2;
  83. }
  84. else
  85. {
  86. scm_swIqRefPu = align_stOut.swIqRefPu;
  87. }
  88. }
  89. else
  90. {
  91. scm_swIqRefPu = 0;
  92. }
  93. scm_slAngManuPu = align_stOut.slAngManuPu;
  94. scm_uwAngRefPu = align_stOut.uwAngRefPu;
  95. scm_StartUpOvrFlg = align_stOut.blStartUpOvrFlg;
  96. }
  97. void Open2Clz_TbcupHook(void)
  98. {
  99. flx_stCtrlOut.swIqLimPu = (SWORD)cof_uwCurMaxPu;
  100. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  101. // {
  102. // align_stIn.uwObsElecThetaPu = obs_stObsOutPu.uwElecThetaPu;
  103. // }
  104. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  105. // {
  106. // //align_stIn.uwObsElecThetaPu = spi_stResolverOut.uwSpiThetaPu;
  107. // }
  108. // else
  109. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  110. {
  111. align_stIn.uwObsElecThetaPu = switchhall_stOut.uwLowThetaPu;
  112. //align_stIn.uwObsElecThetaPu = rtY.Angle_Filtered;
  113. }
  114. else
  115. {}
  116. align_stIn.swCurRefrompu = swCurRefrompu; // swCurRefrompu;
  117. align_voOpen2Clz(&align_stIn, &align_stCoef, &align_stOut);
  118. scm_swIdRefPu = align_stOut.swIdRefPu;
  119. scm_swIqRefPu = align_stOut.swIqRefPu;
  120. scm_blCurSwitchOvrFlg = align_stOut.blCurSwitchOvrFlg;
  121. scm_blAngSwitchOvrFlg = align_stOut.blAngSwitchOvrFlg;
  122. scm_slAngManuPu = align_stOut.slAngManuPu;
  123. scm_uwAngManuPu = align_stOut.uwAngManuPu;
  124. scm_uwAngRefPu = align_stOut.uwAngRefPu;
  125. }
  126. void ClzLoop_TbcupHook(void)
  127. {
  128. /*=======================================================================
  129. Flux weakening
  130. =======================================================================*/
  131. #if(FLUX_MODE == 0)
  132. spdflx_stCtrlIn.swSpdFbkLpfAbsPu = (SWORD)scm_uwSpdFbkLpfAbsPu;
  133. spdflx_voCtrl( &spdflx_stCtrlIn, &spdflx_stCtrlCoef, &spdflx_stCtrlOut );
  134. flx_stCtrlOut.swIdRefPu = spdflx_stCtrlOut.swIdRefPu;
  135. flx_stCtrlOut.swIqLimPu = spdflx_stCtrlOut.swIqLimPu;
  136. #elif(FLUX_MODE == 1)
  137. flx_stCtrlIn.swUalphaPu = crd_stVltIParkOut.swAlphaPu; // Q14
  138. flx_stCtrlIn.swUbetaPu = crd_stVltIParkOut.swBetaPu; // Q14
  139. flx_stCtrlIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
  140. flx_stCtrlIn.swIqRefPu = swCurRefrompu; // Q14
  141. flx_stCtrlIn.swUqRefPu = scm_swUqRefPu; // Q14
  142. flx_voCtrl(&flx_stCtrlIn, &flx_stCtrlCoef, &flx_stCtrlOut);
  143. #else
  144. //Config Error
  145. #endif
  146. /*=======================================================================
  147. Power Limit
  148. =======================================================================*/
  149. mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
  150. mth_voLPFilter(adc_stUpOut.MotorTemp, &scm_stMotorTempLpf);
  151. pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
  152. pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;
  153. pwr_stPwrLimIn.swMotorTemp = scm_stMotorTempLpf.slY.sw.hi;
  154. pwr_stPwrLimIn.uwBatCap = MC_RunInfo.SOC;
  155. pwr_stPwrLimIn.swMotoriqfdb = scm_swIqFdbLpfPu;
  156. pwr_stPwrLimIn.uwThrottleLimit_K = Throttle_PowerLimit_K.K_Result;
  157. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  158. }
  159. void Stop_TbcupHook(void)
  160. {}
  161. void Clz2Stop_TbcupHook(void)
  162. {
  163. scm_ulStatCt++;
  164. /*=======================================================================
  165. Power Limit
  166. =======================================================================*/
  167. mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
  168. mth_voLPFilter(adc_stUpOut.MotorTemp, &scm_stMotorTempLpf);
  169. pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
  170. pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;
  171. pwr_stPwrLimIn.swMotorTemp = scm_stMotorTempLpf.slY.sw.hi;
  172. pwr_stPwrLimIn.uwBatCap = MC_RunInfo.SOC;
  173. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  174. }
  175. void InitPosDet_TbcdownHook(void)
  176. {
  177. /* Get angle for park transformation */
  178. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  179. scm_uwAngIParkPu = scm_uwAngParkPu;
  180. /*=======================================================================
  181. Current decoupling
  182. =======================================================================*/
  183. acr_stUdqDcpOut.swUdPu = 0;
  184. acr_stUdqDcpOut.swUqPu = 0;
  185. }
  186. void ParDet_TbcdownHook(void)
  187. {
  188. /* Get angle for park transformation */
  189. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  190. scm_uwAngIParkPu = scm_uwAngParkPu;
  191. /*=======================================================================
  192. Current decoupling
  193. =======================================================================*/
  194. acr_stUdqDcpOut.swUdPu = 0;
  195. acr_stUdqDcpOut.swUqPu = 0;
  196. }
  197. void StartUp_TbcdownHook(void)
  198. {
  199. /* Speed feedback LPF */
  200. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  201. // {
  202. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  203. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  204. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  205. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  206. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  207. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  208. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  209. // }
  210. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  211. // {
  212. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  213. // }
  214. // else
  215. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  216. {
  217. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  218. }
  219. else
  220. {}
  221. /* Speed feedback Absolute */
  222. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  223. /* Get angle for park transformation */
  224. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  225. scm_uwAngIParkPu = scm_uwAngParkPu;
  226. /*=======================================================================
  227. Current decoupling
  228. =======================================================================*/
  229. acr_stUdqDcpOut.swUdPu = 0;
  230. acr_stUdqDcpOut.swUqPu = 0;
  231. }
  232. void Open2Clz_TbcdownHook(void)
  233. {
  234. /* Speed feedback LPF */
  235. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  236. {
  237. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  238. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  239. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  240. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  241. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  242. //// obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  243. //// mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  244. }
  245. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  246. {
  247. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  248. }
  249. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  250. {
  251. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  252. }
  253. else
  254. {}
  255. /* Speed feedback Absolute */
  256. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  257. /* Get angle for park transformation */
  258. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  259. scm_uwAngIParkPu = scm_uwAngParkPu;
  260. /*=======================================================================
  261. Current decoupling
  262. =======================================================================*/
  263. acr_stUdqDcpOut.swUdPu = 0;
  264. acr_stUdqDcpOut.swUqPu = 0;
  265. }
  266. SWORD thetaoffset=0;
  267. SLONG temptheta=0;
  268. SWORD SwitchFlg=0;
  269. UWORD uwAngRefPu = 0;
  270. UWORD uwAngSwitchK = 0;
  271. _Bool blAngSwitchOvrFlg = FALSE;
  272. UWORD cnt;
  273. SWORD tstThetaDelta1;
  274. SWORD tstThetaDelta2;
  275. SWORD tstThetaCorrect;
  276. UWORD UdqDcpOutCount = 0;
  277. void ClzLoop_TbcdownHook(void)
  278. {
  279. ULONG ulTmp1;
  280. SWORD swAngCompPu; // Q15
  281. /* Speed feedback LPF */
  282. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  283. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  284. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  285. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  286. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  287. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  288. /* Angle Switch */
  289. // if(scm_stSpdFbkLpf.slY.sw.hi > 7801) // 2000rpm
  290. // {
  291. // cp_stFlg.ThetaGetModelSelect = ANG_OBSERVER;
  292. // }
  293. // else
  294. // {
  295. // if(scm_stSpdFbkLpf.slY.sw.hi < 5851) // 1500rpm
  296. // {
  297. // cp_stFlg.ThetaGetModelSelect = ANG_SWITCHHALL;
  298. // }
  299. // }
  300. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  301. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  302. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  303. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  304. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  305. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  306. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  307. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  308. // {
  309. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  310. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  311. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  312. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  313. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  314. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  315. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  316. //// scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  317. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  318. //
  319. // if (temptheta >= cof_sl360DegreePu)
  320. // {
  321. // temptheta -= cof_sl360DegreePu;
  322. // }
  323. // else if (temptheta < (-(cof_sl360DegreePu)))
  324. // {
  325. // temptheta += cof_sl360DegreePu;
  326. // }
  327. // scm_uwAngRefPu=temptheta;
  328. // tstThetaCorrect=scm_uwAngRefPu-rtY.Angle_Filtered;
  329. //
  330. //
  331. // }
  332. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  333. // {
  334. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  335. // // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  336. // }
  337. // else
  338. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  339. {
  340. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  341. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  342. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  343. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  344. scm_uwAngRefPu = rtY.Angle_Filtered;
  345. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  346. }
  347. else
  348. {}
  349. /*=======================================================================
  350. Set Iq limit and Id reference for Constant Voltage Break
  351. =======================================================================*/
  352. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  353. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  354. cvb_stBrakeIn.swIqRefPu = swCurRefrompu; //scm_swIqRefPu;
  355. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  356. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  357. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  358. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  359. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  360. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  361. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  362. {
  363. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  364. }
  365. else
  366. {
  367. scm_swIdRefPu = flx_stCtrlOut.swIdRefPu;
  368. }
  369. /* Speed feedback Absolute */
  370. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  371. /* Get angle for park transformation */
  372. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  373. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  374. // {
  375. // swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  376. // }
  377. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  378. // {
  379. // // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  380. // }
  381. // else
  382. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  383. {
  384. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  385. }
  386. else
  387. {}
  388. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  389. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  390. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  391. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  392. /*=======================================================================
  393. Current decoupling
  394. =======================================================================*/
  395. if(switch_flg.SysRun_Flag == TRUE)
  396. {
  397. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  398. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  399. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  400. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  401. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  402. }
  403. else
  404. {
  405. if(++UdqDcpOutCount>100)
  406. {
  407. UdqDcpOutCount=0;
  408. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  409. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  410. }
  411. }
  412. }
  413. void Clz2Stop_TbcdownHook(void)
  414. {
  415. ULONG ulTmp1;
  416. SWORD swAngCompPu; // Q15
  417. /* Speed feedback LPF */
  418. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  419. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  420. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  421. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  422. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  423. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  424. /* Angle Switch */
  425. // if(scm_stSpdFbkLpf.slY.sw.hi > 7801) // 2000rpm
  426. // {
  427. // cp_stFlg.ThetaGetModelSelect = ANG_OBSERVER;
  428. // }
  429. // else
  430. // {
  431. // if(scm_stSpdFbkLpf.slY.sw.hi < 5851) // 1500rpm
  432. // {
  433. // cp_stFlg.ThetaGetModelSelect = ANG_SWITCHHALL;
  434. // }
  435. // }
  436. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  437. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  438. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  439. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  440. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  441. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  442. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  443. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  444. // {
  445. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  446. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  447. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  448. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  449. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  450. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  451. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  452. //// scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  453. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  454. //
  455. // if (temptheta >= cof_sl360DegreePu)
  456. // {
  457. // temptheta -= cof_sl360DegreePu;
  458. // }
  459. // else if (temptheta < (-(cof_sl360DegreePu)))
  460. // {
  461. // temptheta += cof_sl360DegreePu;
  462. // }
  463. // scm_uwAngRefPu=temptheta;
  464. // tstThetaCorrect=scm_uwAngRefPu-rtY.Angle_Filtered;
  465. //
  466. //
  467. // }
  468. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  469. // {
  470. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  471. // // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  472. // }
  473. // else
  474. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  475. {
  476. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  477. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  478. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  479. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  480. scm_uwAngRefPu = rtY.Angle_Filtered;
  481. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  482. }
  483. else
  484. {}
  485. /*=======================================================================
  486. Set Iq limit and Id reference for Constant Voltage Break
  487. =======================================================================*/
  488. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  489. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  490. cvb_stBrakeIn.swIqRefPu = 0;//swCurRefrompu; //scm_swIqRefPu;
  491. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  492. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  493. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  494. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  495. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  496. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  497. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  498. {
  499. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  500. }
  501. else
  502. {
  503. scm_swIdRefPu = 0;//flx_stCtrlOut.swIdRefPu;
  504. }
  505. /* Speed feedback Absolute */
  506. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  507. /* Get angle for park transformation */
  508. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  509. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  510. // {
  511. // swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  512. // }
  513. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  514. // {
  515. // // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  516. // }
  517. // else
  518. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  519. {
  520. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  521. }
  522. else
  523. {}
  524. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  525. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  526. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  527. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  528. /*=======================================================================
  529. Current decoupling
  530. =======================================================================*/
  531. if(switch_flg.SysRun_Flag == TRUE)
  532. {
  533. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  534. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  535. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  536. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  537. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  538. }
  539. else
  540. {
  541. if(++UdqDcpOutCount>100)
  542. {
  543. UdqDcpOutCount=0;
  544. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  545. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  546. }
  547. }
  548. }
  549. void Stop_TbcdownHook(void)
  550. {
  551. scm_swIdRefPu = 0;
  552. scm_swIqRefPu = 0;
  553. scm_swUdRefPu=0;
  554. scm_swUqRefPu=0;
  555. hw_voPWMInit();
  556. cmfsm_stFlg.blMotorStopFlg = TRUE;
  557. }
  558. void InitPosDet_TbsHook(void)
  559. {}
  560. void ParDet_TbsHook(void)
  561. {}
  562. void StartUp_TbsHook(void)
  563. {
  564. /*=======================================================================
  565. Speed PI output limit in "OpenDrg"
  566. =======================================================================*/
  567. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  568. {
  569. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  570. {
  571. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  572. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  573. asr_stSpdPIOut.slIqiPu = 0;
  574. }
  575. else
  576. {
  577. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  578. {
  579. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  580. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  581. }
  582. }
  583. }
  584. }
  585. void Open2Clz_TbsHook(void)
  586. {}
  587. void ClzLoop_TbsHook(void)
  588. {}
  589. void Stop_TbsHook(void)
  590. {}
  591. void Clz2Stop_TbsHook(void)
  592. {}
  593. void scm_voSpdCtrMdFSM(void)
  594. {
  595. switch (curSpeed_state.state)
  596. {
  597. case Charge:
  598. break;
  599. case InitPosDet:
  600. /* Command run disable */
  601. /* Motor run flag set */
  602. cmfsm_stFlg.blMotorStopFlg = FALSE;
  603. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  604. {
  605. Switch_speed_FSM(&Stop_state);
  606. }
  607. else if(switch_flg.SysWarnning_Flag == 1)
  608. {
  609. Switch_speed_FSM(&Clz2Stop_state);
  610. }
  611. else if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  612. {}
  613. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  614. {
  615. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  616. {
  617. align_stCoef.uwSPIreadOnceCt = 0;
  618. }
  619. else
  620. {}
  621. if(cp_stFlg.RunModelSelect == InitPos)
  622. {
  623. }
  624. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  625. {
  626. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  627. // {
  628. // Switch_speed_FSM(&StartUp_state);
  629. // }
  630. //// else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  631. //// {
  632. //// Switch_speed_FSM(&StartUp_state);
  633. //// }
  634. // else
  635. {
  636. Switch_speed_FSM(&ClzLoop_state);
  637. }
  638. }
  639. else
  640. {
  641. Switch_speed_FSM(&StartUp_state);
  642. }
  643. }
  644. else
  645. {}
  646. break;
  647. case StartUp:
  648. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  649. {
  650. Switch_speed_FSM(&Stop_state);
  651. }
  652. else if(switch_flg.SysWarnning_Flag == 1)
  653. {
  654. Switch_speed_FSM(&Clz2Stop_state);
  655. }
  656. // if (scm_StartUpOvrFlg == TRUE)
  657. // {
  658. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  659. // {
  660. // if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  661. // {
  662. // if (FSM2nd_Run_state.state == Boost)
  663. // {
  664. // cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  665. // }
  666. // else if (FSM2nd_Run_state.state == Assistance)
  667. // {}
  668. // else
  669. // {}
  670. // Switch_speed_FSM(&Open2Clz_state);
  671. // }
  672. // }
  673. // else
  674. // {
  675. //
  676. // }
  677. // }
  678. /* Command run disable */
  679. break;
  680. case Open2Clz:
  681. /* Command run disable */
  682. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  683. {
  684. Switch_speed_FSM(&Stop_state);
  685. }
  686. else if(switch_flg.SysWarnning_Flag == 1)
  687. {
  688. Switch_speed_FSM(&Clz2Stop_state);
  689. }
  690. else if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)/* Switch over */
  691. {
  692. Switch_speed_FSM(&ClzLoop_state);
  693. }
  694. break;
  695. case ClzLoop:
  696. /* Go to stop */
  697. cmfsm_stFlg.blMotorStopFlg = FALSE;
  698. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  699. {
  700. Switch_speed_FSM(&Stop_state);
  701. }
  702. else if(switch_flg.SysWarnning_Flag == 1)
  703. {
  704. Switch_speed_FSM(&Clz2Stop_state);
  705. }
  706. else if (!switch_flg.SysRun_Flag)
  707. {
  708. /* Go to stop */
  709. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  710. /*||(uart_swTorqRefNm==0)*/)
  711. {
  712. scm_swIdRefPu = 0;
  713. scm_swIqRefPu = 0;
  714. Switch_speed_FSM(&Clz2Stop_state);
  715. }
  716. }
  717. else
  718. {}
  719. break;
  720. case Clz2Stop:
  721. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF)
  722. {
  723. Switch_speed_FSM(&Stop_state);
  724. }
  725. else if((scm_ulStatCt > 24000) || (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  726. {
  727. Switch_speed_FSM(&Stop_state);
  728. }
  729. else
  730. {
  731. //do nothing
  732. }
  733. break;
  734. case Stop:
  735. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE && switch_flg.SysWarnning_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  736. {
  737. scm_voSpdCtrMdInit();
  738. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  739. {
  740. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  741. // {
  742. // Switch_speed_FSM(&InitPosDet_state);
  743. // }
  744. // else
  745. {
  746. Switch_speed_FSM(&ClzLoop_state);
  747. }
  748. }
  749. else
  750. {
  751. Switch_speed_FSM(&InitPosDet_state);
  752. }
  753. }
  754. break;
  755. default:
  756. break;
  757. }
  758. }
  759. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  760. {
  761. scm_ulStatCt = 0;
  762. curSpeed_state = *in;
  763. }
  764. void Switch_speed_FSMInit(void)
  765. {
  766. scm_ulStatCt = 0;
  767. //adc_stDownOut.swIaPu = 0;
  768. //adc_stDownOut.swIbPu = 0;
  769. // adc_stDownOut.swIcPu = 0;
  770. curSpeed_state = Stop_state;
  771. }
  772. /************************************************************************
  773. Function: void RUN_FSM_Main(void)
  774. Description:
  775. Call by:
  776. Input Variables:
  777. Output/Return Variables:
  778. Subroutine Call:
  779. Reference:
  780. ************************************************************************/
  781. /************************************************************************
  782. Local Functions: N/A
  783. ************************************************************************/
  784. /************************************************************************
  785. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  786. All rights reserved.
  787. ************************************************************************/
  788. #ifdef _STARTFSM_C_
  789. #undef _STARTFSM_C_
  790. #endif
  791. /************************************************************************
  792. End of this File (EOF)!
  793. Do not put anything after this part!
  794. ************************************************************************/