spdctrFSM.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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.uwThrottleLimit_K = Throttle_PowerLimit_K.K_Result;
  156. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  157. }
  158. void Stop_TbcupHook(void)
  159. {}
  160. void InitPosDet_TbcdownHook(void)
  161. {
  162. /* Get angle for park transformation */
  163. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  164. scm_uwAngIParkPu = scm_uwAngParkPu;
  165. /*=======================================================================
  166. Current decoupling
  167. =======================================================================*/
  168. acr_stUdqDcpOut.swUdPu = 0;
  169. acr_stUdqDcpOut.swUqPu = 0;
  170. }
  171. void ParDet_TbcdownHook(void)
  172. {
  173. /* Get angle for park transformation */
  174. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  175. scm_uwAngIParkPu = scm_uwAngParkPu;
  176. /*=======================================================================
  177. Current decoupling
  178. =======================================================================*/
  179. acr_stUdqDcpOut.swUdPu = 0;
  180. acr_stUdqDcpOut.swUqPu = 0;
  181. }
  182. void StartUp_TbcdownHook(void)
  183. {
  184. /* Speed feedback LPF */
  185. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  186. // {
  187. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  188. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  189. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  190. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  191. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  192. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  193. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  194. // }
  195. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  196. // {
  197. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  198. // }
  199. // else
  200. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  201. {
  202. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  203. }
  204. else
  205. {}
  206. /* Speed feedback Absolute */
  207. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  208. /* Get angle for park transformation */
  209. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  210. scm_uwAngIParkPu = scm_uwAngParkPu;
  211. /*=======================================================================
  212. Current decoupling
  213. =======================================================================*/
  214. acr_stUdqDcpOut.swUdPu = 0;
  215. acr_stUdqDcpOut.swUqPu = 0;
  216. }
  217. void Open2Clz_TbcdownHook(void)
  218. {
  219. /* Speed feedback LPF */
  220. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  221. {
  222. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  223. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  224. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  225. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  226. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  227. //// obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  228. //// mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  229. }
  230. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  231. {
  232. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  233. }
  234. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  235. {
  236. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  237. }
  238. else
  239. {}
  240. /* Speed feedback Absolute */
  241. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  242. /* Get angle for park transformation */
  243. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  244. scm_uwAngIParkPu = scm_uwAngParkPu;
  245. /*=======================================================================
  246. Current decoupling
  247. =======================================================================*/
  248. acr_stUdqDcpOut.swUdPu = 0;
  249. acr_stUdqDcpOut.swUqPu = 0;
  250. }
  251. SWORD thetaoffset=0;
  252. SLONG temptheta=0;
  253. SWORD SwitchFlg=0;
  254. UWORD uwAngRefPu = 0;
  255. UWORD uwAngSwitchK = 0;
  256. _Bool blAngSwitchOvrFlg = FALSE;
  257. UWORD cnt;
  258. SWORD tstThetaDelta1;
  259. SWORD tstThetaDelta2;
  260. SWORD tstThetaCorrect;
  261. void ClzLoop_TbcdownHook(void)
  262. {
  263. ULONG ulTmp1;
  264. SWORD swAngCompPu; // Q15
  265. /* Speed feedback LPF */
  266. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  267. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  268. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  269. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  270. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  271. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  272. /* Angle Switch */
  273. // if(scm_stSpdFbkLpf.slY.sw.hi > 7801) // 2000rpm
  274. // {
  275. // cp_stFlg.ThetaGetModelSelect = ANG_OBSERVER;
  276. // }
  277. // else
  278. // {
  279. // if(scm_stSpdFbkLpf.slY.sw.hi < 5851) // 1500rpm
  280. // {
  281. // cp_stFlg.ThetaGetModelSelect = ANG_SWITCHHALL;
  282. // }
  283. // }
  284. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  285. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  286. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  287. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  288. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  289. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  290. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  291. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  292. // {
  293. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  294. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  295. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  296. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  297. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  298. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  299. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  300. //// scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  301. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  302. //
  303. // if (temptheta >= cof_sl360DegreePu)
  304. // {
  305. // temptheta -= cof_sl360DegreePu;
  306. // }
  307. // else if (temptheta < (-(cof_sl360DegreePu)))
  308. // {
  309. // temptheta += cof_sl360DegreePu;
  310. // }
  311. // scm_uwAngRefPu=temptheta;
  312. // tstThetaCorrect=scm_uwAngRefPu-rtY.Angle_Filtered;
  313. //
  314. //
  315. // }
  316. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  317. // {
  318. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  319. // // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  320. // }
  321. // else
  322. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  323. {
  324. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  325. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  326. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  327. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  328. scm_uwAngRefPu = rtY.Angle_Filtered;
  329. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  330. }
  331. else
  332. {}
  333. /*=======================================================================
  334. Set Iq limit and Id reference for Constant Voltage Break
  335. =======================================================================*/
  336. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  337. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  338. cvb_stBrakeIn.swIqRefPu = swCurRefrompu; //scm_swIqRefPu;
  339. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  340. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  341. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  342. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  343. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  344. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  345. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  346. {
  347. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  348. }
  349. else
  350. {
  351. scm_swIdRefPu = flx_stCtrlOut.swIdRefPu;
  352. }
  353. /* Speed feedback Absolute */
  354. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  355. /* Get angle for park transformation */
  356. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  357. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  358. // {
  359. // swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  360. // }
  361. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  362. // {
  363. // // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  364. // }
  365. // else
  366. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  367. {
  368. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  369. }
  370. else
  371. {}
  372. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  373. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  374. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  375. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  376. /*=======================================================================
  377. Current decoupling
  378. =======================================================================*/
  379. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  380. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  381. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  382. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  383. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  384. }
  385. void Stop_TbcdownHook(void)
  386. {
  387. scm_swIdRefPu = 0;
  388. scm_swIqRefPu = 0;
  389. scm_swUdRefPu=0;
  390. scm_swUqRefPu=0;
  391. hw_voPWMInit();
  392. cmfsm_stFlg.blMotorStopFlg = TRUE;
  393. }
  394. void InitPosDet_TbsHook(void)
  395. {}
  396. void ParDet_TbsHook(void)
  397. {}
  398. void StartUp_TbsHook(void)
  399. {
  400. /*=======================================================================
  401. Speed PI output limit in "OpenDrg"
  402. =======================================================================*/
  403. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  404. {
  405. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  406. {
  407. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  408. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  409. asr_stSpdPIOut.slIqiPu = 0;
  410. }
  411. else
  412. {
  413. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  414. {
  415. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  416. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  417. }
  418. }
  419. }
  420. }
  421. void Open2Clz_TbsHook(void)
  422. {}
  423. void ClzLoop_TbsHook(void)
  424. {}
  425. void Stop_TbsHook(void)
  426. {}
  427. void scm_voSpdCtrMdFSM(void)
  428. {
  429. switch (curSpeed_state.state)
  430. {
  431. case Charge:
  432. break;
  433. case InitPosDet:
  434. /* Command run disable */
  435. /* Motor run flag set */
  436. cmfsm_stFlg.blMotorStopFlg = FALSE;
  437. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  438. {
  439. Switch_speed_FSM(&Stop_state);
  440. }
  441. if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  442. {}
  443. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  444. {
  445. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  446. {
  447. align_stCoef.uwSPIreadOnceCt = 0;
  448. }
  449. else
  450. {}
  451. if(cp_stFlg.RunModelSelect == InitPos)
  452. {
  453. }
  454. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  455. {
  456. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  457. // {
  458. // Switch_speed_FSM(&StartUp_state);
  459. // }
  460. //// else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  461. //// {
  462. //// Switch_speed_FSM(&StartUp_state);
  463. //// }
  464. // else
  465. {
  466. Switch_speed_FSM(&ClzLoop_state);
  467. }
  468. }
  469. else
  470. {
  471. Switch_speed_FSM(&StartUp_state);
  472. }
  473. }
  474. else
  475. {}
  476. break;
  477. case StartUp:
  478. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  479. {
  480. Switch_speed_FSM(&Stop_state);
  481. }
  482. // if (scm_StartUpOvrFlg == TRUE)
  483. // {
  484. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  485. // {
  486. // if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  487. // {
  488. // if (FSM2nd_Run_state.state == Boost)
  489. // {
  490. // cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  491. // }
  492. // else if (FSM2nd_Run_state.state == Assistance)
  493. // {}
  494. // else
  495. // {}
  496. // Switch_speed_FSM(&Open2Clz_state);
  497. // }
  498. // }
  499. // else
  500. // {
  501. //
  502. // }
  503. // }
  504. /* Command run disable */
  505. break;
  506. case Open2Clz:
  507. /* Command run disable */
  508. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  509. {
  510. Switch_speed_FSM(&Stop_state);
  511. }
  512. /* Switch over */
  513. if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)
  514. {
  515. Switch_speed_FSM(&ClzLoop_state);
  516. }
  517. break;
  518. case ClzLoop:
  519. /* Go to stop */
  520. cmfsm_stFlg.blMotorStopFlg = FALSE;
  521. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  522. {
  523. Switch_speed_FSM(&Stop_state);
  524. }
  525. if (!switch_flg.SysRun_Flag)
  526. {
  527. /* Go to stop */
  528. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  529. /*||(uart_swTorqRefNm==0)*/)
  530. {
  531. scm_swIdRefPu = 0;
  532. scm_swIqRefPu = 0;
  533. Switch_speed_FSM(&Stop_state);
  534. }
  535. }
  536. else
  537. {}
  538. break;
  539. case Stop:
  540. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  541. {
  542. scm_voSpdCtrMdInit();
  543. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  544. {
  545. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  546. // {
  547. // Switch_speed_FSM(&InitPosDet_state);
  548. // }
  549. // else
  550. {
  551. Switch_speed_FSM(&ClzLoop_state);
  552. }
  553. }
  554. else
  555. {
  556. Switch_speed_FSM(&InitPosDet_state);
  557. }
  558. }
  559. break;
  560. default:
  561. break;
  562. }
  563. }
  564. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  565. {
  566. scm_ulStatCt = 0;
  567. curSpeed_state = *in;
  568. }
  569. void Switch_speed_FSMInit(void)
  570. {
  571. scm_ulStatCt = 0;
  572. //adc_stDownOut.swIaPu = 0;
  573. //adc_stDownOut.swIbPu = 0;
  574. // adc_stDownOut.swIcPu = 0;
  575. curSpeed_state = Stop_state;
  576. }
  577. /************************************************************************
  578. Function: void RUN_FSM_Main(void)
  579. Description:
  580. Call by:
  581. Input Variables:
  582. Output/Return Variables:
  583. Subroutine Call:
  584. Reference:
  585. ************************************************************************/
  586. /************************************************************************
  587. Local Functions: N/A
  588. ************************************************************************/
  589. /************************************************************************
  590. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  591. All rights reserved.
  592. ************************************************************************/
  593. #ifdef _STARTFSM_C_
  594. #undef _STARTFSM_C_
  595. #endif
  596. /************************************************************************
  597. End of this File (EOF)!
  598. Do not put anything after this part!
  599. ************************************************************************/