spdctrFSM.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. UWORD UdqDcpOutCount = 0;
  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. if(switch_flg.SysRun_Flag == TRUE)
  380. {
  381. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  382. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  383. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  384. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  385. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  386. }
  387. else
  388. {
  389. if(++UdqDcpOutCount>100)
  390. {
  391. UdqDcpOutCount=0;
  392. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  393. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  394. }
  395. }
  396. }
  397. void Stop_TbcdownHook(void)
  398. {
  399. scm_swIdRefPu = 0;
  400. scm_swIqRefPu = 0;
  401. scm_swUdRefPu=0;
  402. scm_swUqRefPu=0;
  403. hw_voPWMInit();
  404. cmfsm_stFlg.blMotorStopFlg = TRUE;
  405. }
  406. void InitPosDet_TbsHook(void)
  407. {}
  408. void ParDet_TbsHook(void)
  409. {}
  410. void StartUp_TbsHook(void)
  411. {
  412. /*=======================================================================
  413. Speed PI output limit in "OpenDrg"
  414. =======================================================================*/
  415. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  416. {
  417. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  418. {
  419. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  420. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  421. asr_stSpdPIOut.slIqiPu = 0;
  422. }
  423. else
  424. {
  425. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  426. {
  427. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  428. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  429. }
  430. }
  431. }
  432. }
  433. void Open2Clz_TbsHook(void)
  434. {}
  435. void ClzLoop_TbsHook(void)
  436. {}
  437. void Stop_TbsHook(void)
  438. {}
  439. void scm_voSpdCtrMdFSM(void)
  440. {
  441. switch (curSpeed_state.state)
  442. {
  443. case Charge:
  444. break;
  445. case InitPosDet:
  446. /* Command run disable */
  447. /* Motor run flag set */
  448. cmfsm_stFlg.blMotorStopFlg = FALSE;
  449. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  450. {
  451. Switch_speed_FSM(&Stop_state);
  452. }
  453. if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  454. {}
  455. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  456. {
  457. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  458. {
  459. align_stCoef.uwSPIreadOnceCt = 0;
  460. }
  461. else
  462. {}
  463. if(cp_stFlg.RunModelSelect == InitPos)
  464. {
  465. }
  466. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  467. {
  468. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  469. // {
  470. // Switch_speed_FSM(&StartUp_state);
  471. // }
  472. //// else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  473. //// {
  474. //// Switch_speed_FSM(&StartUp_state);
  475. //// }
  476. // else
  477. {
  478. Switch_speed_FSM(&ClzLoop_state);
  479. }
  480. }
  481. else
  482. {
  483. Switch_speed_FSM(&StartUp_state);
  484. }
  485. }
  486. else
  487. {}
  488. break;
  489. case StartUp:
  490. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  491. {
  492. Switch_speed_FSM(&Stop_state);
  493. }
  494. // if (scm_StartUpOvrFlg == TRUE)
  495. // {
  496. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  497. // {
  498. // if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  499. // {
  500. // if (FSM2nd_Run_state.state == Boost)
  501. // {
  502. // cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  503. // }
  504. // else if (FSM2nd_Run_state.state == Assistance)
  505. // {}
  506. // else
  507. // {}
  508. // Switch_speed_FSM(&Open2Clz_state);
  509. // }
  510. // }
  511. // else
  512. // {
  513. //
  514. // }
  515. // }
  516. /* Command run disable */
  517. break;
  518. case Open2Clz:
  519. /* Command run disable */
  520. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  521. {
  522. Switch_speed_FSM(&Stop_state);
  523. }
  524. /* Switch over */
  525. if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)
  526. {
  527. Switch_speed_FSM(&ClzLoop_state);
  528. }
  529. break;
  530. case ClzLoop:
  531. /* Go to stop */
  532. cmfsm_stFlg.blMotorStopFlg = FALSE;
  533. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  534. {
  535. Switch_speed_FSM(&Stop_state);
  536. }
  537. if (!switch_flg.SysRun_Flag)
  538. {
  539. /* Go to stop */
  540. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  541. /*||(uart_swTorqRefNm==0)*/)
  542. {
  543. scm_swIdRefPu = 0;
  544. scm_swIqRefPu = 0;
  545. Switch_speed_FSM(&Stop_state);
  546. }
  547. }
  548. else
  549. {}
  550. break;
  551. case Stop:
  552. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  553. {
  554. scm_voSpdCtrMdInit();
  555. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  556. {
  557. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  558. // {
  559. // Switch_speed_FSM(&InitPosDet_state);
  560. // }
  561. // else
  562. {
  563. Switch_speed_FSM(&ClzLoop_state);
  564. }
  565. }
  566. else
  567. {
  568. Switch_speed_FSM(&InitPosDet_state);
  569. }
  570. }
  571. break;
  572. default:
  573. break;
  574. }
  575. }
  576. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  577. {
  578. scm_ulStatCt = 0;
  579. curSpeed_state = *in;
  580. }
  581. void Switch_speed_FSMInit(void)
  582. {
  583. scm_ulStatCt = 0;
  584. //adc_stDownOut.swIaPu = 0;
  585. //adc_stDownOut.swIbPu = 0;
  586. // adc_stDownOut.swIcPu = 0;
  587. curSpeed_state = Stop_state;
  588. }
  589. /************************************************************************
  590. Function: void RUN_FSM_Main(void)
  591. Description:
  592. Call by:
  593. Input Variables:
  594. Output/Return Variables:
  595. Subroutine Call:
  596. Reference:
  597. ************************************************************************/
  598. /************************************************************************
  599. Local Functions: N/A
  600. ************************************************************************/
  601. /************************************************************************
  602. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  603. All rights reserved.
  604. ************************************************************************/
  605. #ifdef _STARTFSM_C_
  606. #undef _STARTFSM_C_
  607. #endif
  608. /************************************************************************
  609. End of this File (EOF)!
  610. Do not put anything after this part!
  611. ************************************************************************/