spdctrFSM.c 30 KB

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