spdctrFSM.c 22 KB

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