spdctrFSM.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 Clz2Stop_TbcupHook(void)
  160. {
  161. scm_ulStatCt++;
  162. /*=======================================================================
  163. Power Limit
  164. =======================================================================*/
  165. mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
  166. mth_voLPFilter(adc_stUpOut.MotorTemp, &scm_stMotorTempLpf);
  167. pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
  168. pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;
  169. pwr_stPwrLimIn.swMotorTemp = scm_stMotorTempLpf.slY.sw.hi;
  170. pwr_stPwrLimIn.uwBatCap = MC_RunInfo.SOC;
  171. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  172. }
  173. void InitPosDet_TbcdownHook(void)
  174. {
  175. /* Get angle for park transformation */
  176. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  177. scm_uwAngIParkPu = scm_uwAngParkPu;
  178. /*=======================================================================
  179. Current decoupling
  180. =======================================================================*/
  181. acr_stUdqDcpOut.swUdPu = 0;
  182. acr_stUdqDcpOut.swUqPu = 0;
  183. }
  184. void ParDet_TbcdownHook(void)
  185. {
  186. /* Get angle for park transformation */
  187. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  188. scm_uwAngIParkPu = scm_uwAngParkPu;
  189. /*=======================================================================
  190. Current decoupling
  191. =======================================================================*/
  192. acr_stUdqDcpOut.swUdPu = 0;
  193. acr_stUdqDcpOut.swUqPu = 0;
  194. }
  195. void StartUp_TbcdownHook(void)
  196. {
  197. /* Speed feedback LPF */
  198. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  199. // {
  200. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  201. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  202. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  203. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  204. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  205. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  206. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  207. // }
  208. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  209. // {
  210. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  211. // }
  212. // else
  213. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  214. {
  215. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  216. }
  217. else
  218. {}
  219. /* Speed feedback Absolute */
  220. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  221. /* Get angle for park transformation */
  222. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  223. scm_uwAngIParkPu = scm_uwAngParkPu;
  224. /*=======================================================================
  225. Current decoupling
  226. =======================================================================*/
  227. acr_stUdqDcpOut.swUdPu = 0;
  228. acr_stUdqDcpOut.swUqPu = 0;
  229. }
  230. void Open2Clz_TbcdownHook(void)
  231. {
  232. /* Speed feedback LPF */
  233. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  234. {
  235. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  236. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  237. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  238. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  239. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  240. //// obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  241. //// mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  242. }
  243. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  244. {
  245. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  246. }
  247. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  248. {
  249. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  250. }
  251. else
  252. {}
  253. /* Speed feedback Absolute */
  254. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  255. /* Get angle for park transformation */
  256. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  257. scm_uwAngIParkPu = scm_uwAngParkPu;
  258. /*=======================================================================
  259. Current decoupling
  260. =======================================================================*/
  261. acr_stUdqDcpOut.swUdPu = 0;
  262. acr_stUdqDcpOut.swUqPu = 0;
  263. }
  264. SWORD thetaoffset=0;
  265. SLONG temptheta=0;
  266. SWORD SwitchFlg=0;
  267. UWORD uwAngRefPu = 0;
  268. UWORD uwAngSwitchK = 0;
  269. _Bool blAngSwitchOvrFlg = FALSE;
  270. UWORD cnt;
  271. SWORD tstThetaDelta1;
  272. SWORD tstThetaDelta2;
  273. SWORD tstThetaCorrect;
  274. UWORD UdqDcpOutCount = 0;
  275. void ClzLoop_TbcdownHook(void)
  276. {
  277. ULONG ulTmp1;
  278. SWORD swAngCompPu; // Q15
  279. /* Speed feedback LPF */
  280. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  281. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  282. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  283. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  284. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  285. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  286. /* Angle Switch */
  287. // if(scm_stSpdFbkLpf.slY.sw.hi > 7801) // 2000rpm
  288. // {
  289. // cp_stFlg.ThetaGetModelSelect = ANG_OBSERVER;
  290. // }
  291. // else
  292. // {
  293. // if(scm_stSpdFbkLpf.slY.sw.hi < 5851) // 1500rpm
  294. // {
  295. // cp_stFlg.ThetaGetModelSelect = ANG_SWITCHHALL;
  296. // }
  297. // }
  298. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  299. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  300. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  301. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  302. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  303. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  304. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  305. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  306. // {
  307. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  308. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  309. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  310. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  311. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  312. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  313. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  314. //// scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  315. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  316. //
  317. // if (temptheta >= cof_sl360DegreePu)
  318. // {
  319. // temptheta -= cof_sl360DegreePu;
  320. // }
  321. // else if (temptheta < (-(cof_sl360DegreePu)))
  322. // {
  323. // temptheta += cof_sl360DegreePu;
  324. // }
  325. // scm_uwAngRefPu=temptheta;
  326. // tstThetaCorrect=scm_uwAngRefPu-rtY.Angle_Filtered;
  327. //
  328. //
  329. // }
  330. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  331. // {
  332. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  333. // // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  334. // }
  335. // else
  336. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  337. {
  338. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  339. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  340. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  341. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  342. scm_uwAngRefPu = rtY.Angle_Filtered;
  343. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  344. }
  345. else
  346. {}
  347. /*=======================================================================
  348. Set Iq limit and Id reference for Constant Voltage Break
  349. =======================================================================*/
  350. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  351. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  352. cvb_stBrakeIn.swIqRefPu = swCurRefrompu; //scm_swIqRefPu;
  353. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  354. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  355. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  356. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  357. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  358. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  359. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  360. {
  361. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  362. }
  363. else
  364. {
  365. scm_swIdRefPu = flx_stCtrlOut.swIdRefPu;
  366. }
  367. /* Speed feedback Absolute */
  368. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  369. /* Get angle for park transformation */
  370. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  371. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  372. // {
  373. // swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  374. // }
  375. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  376. // {
  377. // // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  378. // }
  379. // else
  380. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  381. {
  382. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  383. }
  384. else
  385. {}
  386. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  387. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  388. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  389. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  390. /*=======================================================================
  391. Current decoupling
  392. =======================================================================*/
  393. if(switch_flg.SysRun_Flag == TRUE)
  394. {
  395. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  396. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  397. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  398. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  399. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  400. }
  401. else
  402. {
  403. if(++UdqDcpOutCount>100)
  404. {
  405. UdqDcpOutCount=0;
  406. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  407. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  408. }
  409. }
  410. }
  411. void Clz2Stop_TbcdownHook(void)
  412. {
  413. ULONG ulTmp1;
  414. SWORD swAngCompPu; // Q15
  415. /* Speed feedback LPF */
  416. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  417. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  418. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  419. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  420. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  421. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  422. /* Angle Switch */
  423. // if(scm_stSpdFbkLpf.slY.sw.hi > 7801) // 2000rpm
  424. // {
  425. // cp_stFlg.ThetaGetModelSelect = ANG_OBSERVER;
  426. // }
  427. // else
  428. // {
  429. // if(scm_stSpdFbkLpf.slY.sw.hi < 5851) // 1500rpm
  430. // {
  431. // cp_stFlg.ThetaGetModelSelect = ANG_SWITCHHALL;
  432. // }
  433. // }
  434. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  435. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  436. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  437. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  438. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  439. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  440. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  441. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  442. // {
  443. // obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  444. // obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  445. // obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  446. // obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  447. // obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  448. // obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  449. // mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  450. //// scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  451. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  452. //
  453. // if (temptheta >= cof_sl360DegreePu)
  454. // {
  455. // temptheta -= cof_sl360DegreePu;
  456. // }
  457. // else if (temptheta < (-(cof_sl360DegreePu)))
  458. // {
  459. // temptheta += cof_sl360DegreePu;
  460. // }
  461. // scm_uwAngRefPu=temptheta;
  462. // tstThetaCorrect=scm_uwAngRefPu-rtY.Angle_Filtered;
  463. //
  464. //
  465. // }
  466. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  467. // {
  468. // // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  469. // // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  470. // }
  471. // else
  472. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  473. {
  474. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  475. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  476. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  477. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  478. scm_uwAngRefPu = rtY.Angle_Filtered;
  479. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  480. }
  481. else
  482. {}
  483. /*=======================================================================
  484. Set Iq limit and Id reference for Constant Voltage Break
  485. =======================================================================*/
  486. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  487. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  488. cvb_stBrakeIn.swIqRefPu = 0;//swCurRefrompu; //scm_swIqRefPu;
  489. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  490. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  491. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  492. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  493. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  494. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  495. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  496. {
  497. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  498. }
  499. else
  500. {
  501. scm_swIdRefPu = 0;//flx_stCtrlOut.swIdRefPu;
  502. }
  503. /* Speed feedback Absolute */
  504. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  505. /* Get angle for park transformation */
  506. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  507. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  508. // {
  509. // swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  510. // }
  511. // else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  512. // {
  513. // // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  514. // }
  515. // else
  516. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  517. {
  518. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  519. }
  520. else
  521. {}
  522. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  523. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  524. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  525. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  526. /*=======================================================================
  527. Current decoupling
  528. =======================================================================*/
  529. if(switch_flg.SysRun_Flag == TRUE)
  530. {
  531. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  532. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  533. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  534. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  535. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  536. }
  537. else
  538. {
  539. if(++UdqDcpOutCount>100)
  540. {
  541. UdqDcpOutCount=0;
  542. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  543. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  544. }
  545. }
  546. }
  547. void Stop_TbcdownHook(void)
  548. {
  549. scm_swIdRefPu = 0;
  550. scm_swIqRefPu = 0;
  551. scm_swUdRefPu=0;
  552. scm_swUqRefPu=0;
  553. hw_voPWMInit();
  554. cmfsm_stFlg.blMotorStopFlg = TRUE;
  555. }
  556. void InitPosDet_TbsHook(void)
  557. {}
  558. void ParDet_TbsHook(void)
  559. {}
  560. void StartUp_TbsHook(void)
  561. {
  562. /*=======================================================================
  563. Speed PI output limit in "OpenDrg"
  564. =======================================================================*/
  565. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  566. {
  567. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  568. {
  569. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  570. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  571. asr_stSpdPIOut.slIqiPu = 0;
  572. }
  573. else
  574. {
  575. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  576. {
  577. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  578. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  579. }
  580. }
  581. }
  582. }
  583. void Open2Clz_TbsHook(void)
  584. {}
  585. void ClzLoop_TbsHook(void)
  586. {}
  587. void Stop_TbsHook(void)
  588. {}
  589. void Clz2Stop_TbsHook(void)
  590. {}
  591. void scm_voSpdCtrMdFSM(void)
  592. {
  593. switch (curSpeed_state.state)
  594. {
  595. case Charge:
  596. break;
  597. case InitPosDet:
  598. /* Command run disable */
  599. /* Motor run flag set */
  600. cmfsm_stFlg.blMotorStopFlg = FALSE;
  601. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  602. {
  603. Switch_speed_FSM(&Stop_state);
  604. }
  605. else if(switch_flg.SysWarnning_Flag == 1)
  606. {
  607. Switch_speed_FSM(&Clz2Stop_state);
  608. }
  609. else if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  610. {}
  611. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  612. {
  613. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  614. {
  615. align_stCoef.uwSPIreadOnceCt = 0;
  616. }
  617. else
  618. {}
  619. if(cp_stFlg.RunModelSelect == InitPos)
  620. {
  621. }
  622. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  623. {
  624. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  625. // {
  626. // Switch_speed_FSM(&StartUp_state);
  627. // }
  628. //// else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  629. //// {
  630. //// Switch_speed_FSM(&StartUp_state);
  631. //// }
  632. // else
  633. {
  634. Switch_speed_FSM(&ClzLoop_state);
  635. }
  636. }
  637. else
  638. {
  639. Switch_speed_FSM(&StartUp_state);
  640. }
  641. }
  642. else
  643. {}
  644. break;
  645. case StartUp:
  646. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  647. {
  648. Switch_speed_FSM(&Stop_state);
  649. }
  650. else if(switch_flg.SysWarnning_Flag == 1)
  651. {
  652. Switch_speed_FSM(&Clz2Stop_state);
  653. }
  654. // if (scm_StartUpOvrFlg == TRUE)
  655. // {
  656. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  657. // {
  658. // if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  659. // {
  660. // if (FSM2nd_Run_state.state == Boost)
  661. // {
  662. // cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  663. // }
  664. // else if (FSM2nd_Run_state.state == Assistance)
  665. // {}
  666. // else
  667. // {}
  668. // Switch_speed_FSM(&Open2Clz_state);
  669. // }
  670. // }
  671. // else
  672. // {
  673. //
  674. // }
  675. // }
  676. /* Command run disable */
  677. break;
  678. case Open2Clz:
  679. /* Command run disable */
  680. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  681. {
  682. Switch_speed_FSM(&Stop_state);
  683. }
  684. else if(switch_flg.SysWarnning_Flag == 1)
  685. {
  686. Switch_speed_FSM(&Clz2Stop_state);
  687. }
  688. else if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)/* Switch over */
  689. {
  690. Switch_speed_FSM(&ClzLoop_state);
  691. }
  692. break;
  693. case ClzLoop:
  694. /* Go to stop */
  695. cmfsm_stFlg.blMotorStopFlg = FALSE;
  696. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  697. {
  698. Switch_speed_FSM(&Stop_state);
  699. }
  700. else if(switch_flg.SysWarnning_Flag == 1)
  701. {
  702. Switch_speed_FSM(&Clz2Stop_state);
  703. }
  704. else if (!switch_flg.SysRun_Flag)
  705. {
  706. /* Go to stop */
  707. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  708. /*||(uart_swTorqRefNm==0)*/)
  709. {
  710. scm_swIdRefPu = 0;
  711. scm_swIqRefPu = 0;
  712. Switch_speed_FSM(&Clz2Stop_state);
  713. }
  714. }
  715. else
  716. {}
  717. break;
  718. case Clz2Stop:
  719. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF)
  720. {
  721. Switch_speed_FSM(&Stop_state);
  722. }
  723. else if((scm_ulStatCt > 24000) || (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  724. {
  725. Switch_speed_FSM(&Stop_state);
  726. }
  727. else
  728. {
  729. //do nothing
  730. }
  731. break;
  732. case Stop:
  733. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE && switch_flg.SysWarnning_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  734. {
  735. scm_voSpdCtrMdInit();
  736. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  737. {
  738. // if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  739. // {
  740. // Switch_speed_FSM(&InitPosDet_state);
  741. // }
  742. // else
  743. {
  744. Switch_speed_FSM(&ClzLoop_state);
  745. }
  746. }
  747. else
  748. {
  749. Switch_speed_FSM(&InitPosDet_state);
  750. }
  751. }
  752. break;
  753. default:
  754. break;
  755. }
  756. }
  757. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  758. {
  759. scm_ulStatCt = 0;
  760. curSpeed_state = *in;
  761. }
  762. void Switch_speed_FSMInit(void)
  763. {
  764. scm_ulStatCt = 0;
  765. //adc_stDownOut.swIaPu = 0;
  766. //adc_stDownOut.swIbPu = 0;
  767. // adc_stDownOut.swIcPu = 0;
  768. curSpeed_state = Stop_state;
  769. }
  770. /************************************************************************
  771. Function: void RUN_FSM_Main(void)
  772. Description:
  773. Call by:
  774. Input Variables:
  775. Output/Return Variables:
  776. Subroutine Call:
  777. Reference:
  778. ************************************************************************/
  779. /************************************************************************
  780. Local Functions: N/A
  781. ************************************************************************/
  782. /************************************************************************
  783. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  784. All rights reserved.
  785. ************************************************************************/
  786. #ifdef _STARTFSM_C_
  787. #undef _STARTFSM_C_
  788. #endif
  789. /************************************************************************
  790. End of this File (EOF)!
  791. Do not put anything after this part!
  792. ************************************************************************/