spdctrFSM.c 27 KB

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