spdctrFSM.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  109. {
  110. align_stIn.uwObsElecThetaPu = switchhall_stOut.uwLowThetaPu;
  111. //align_stIn.uwObsElecThetaPu = rtY.Angle_Filtered;
  112. }
  113. else
  114. {}
  115. align_stIn.swCurRefrompu = swCurRefrompu; // swCurRefrompu;
  116. align_voOpen2Clz(&align_stIn, &align_stCoef, &align_stOut);
  117. scm_swIdRefPu = align_stOut.swIdRefPu;
  118. scm_swIqRefPu = align_stOut.swIqRefPu;
  119. scm_blCurSwitchOvrFlg = align_stOut.blCurSwitchOvrFlg;
  120. scm_blAngSwitchOvrFlg = align_stOut.blAngSwitchOvrFlg;
  121. scm_slAngManuPu = align_stOut.slAngManuPu;
  122. scm_uwAngManuPu = align_stOut.uwAngManuPu;
  123. scm_uwAngRefPu = align_stOut.uwAngRefPu;
  124. }
  125. void ClzLoop_TbcupHook(void)
  126. {
  127. /*=======================================================================
  128. Flux weakening
  129. =======================================================================*/
  130. #if(FLUX_MODE == 0)
  131. spdflx_stCtrlIn.swSpdFbkLpfAbsPu = (SWORD)scm_uwSpdFbkLpfAbsPu;
  132. spdflx_voCtrl( &spdflx_stCtrlIn, &spdflx_stCtrlCoef, &spdflx_stCtrlOut );
  133. flx_stCtrlOut.swIdRefPu = spdflx_stCtrlOut.swIdRefPu;
  134. flx_stCtrlOut.swIqLimPu = spdflx_stCtrlOut.swIqLimPu;
  135. #elif(FLUX_MODE == 1)
  136. flx_stCtrlIn.swUalphaPu = crd_stVltIParkOut.swAlphaPu; // Q14
  137. flx_stCtrlIn.swUbetaPu = crd_stVltIParkOut.swBetaPu; // Q14
  138. flx_stCtrlIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
  139. flx_stCtrlIn.swIqRefPu = swCurRefrompu; // Q14
  140. flx_stCtrlIn.swUqRefPu = scm_swUqRefPu; // Q14
  141. flx_voCtrl(&flx_stCtrlIn, &flx_stCtrlCoef, &flx_stCtrlOut);
  142. #else
  143. //Config Error
  144. #endif
  145. /*=======================================================================
  146. Power Limit
  147. =======================================================================*/
  148. mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
  149. mth_voLPFilter(adc_stUpOut.MotorTemp, &scm_stMotorTempLpf);
  150. pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
  151. pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;
  152. pwr_stPwrLimIn.swMotorTemp = scm_stMotorTempLpf.slY.sw.hi;
  153. pwr_stPwrLimIn.uwBatCap = MC_RunInfo.SOC;
  154. pwr_stPwrLimIn.swMotoriqfdb = scm_swIqFdbLpfPu;
  155. pwr_stPwrLimIn.uwThrottleLimit_K = Throttle_PowerLimit_K.K_Result;
  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. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  281. {
  282. obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  283. obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  284. obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  285. obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  286. obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  287. obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  288. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  289. scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  290. // temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  291. //
  292. // if (temptheta >= cof_sl360DegreePu)
  293. // {
  294. // temptheta -= cof_sl360DegreePu;
  295. // }
  296. // else if (temptheta < (-(cof_sl360DegreePu)))
  297. // {
  298. // temptheta += cof_sl360DegreePu;
  299. // }
  300. // scm_uwAngRefPu=temptheta;
  301. // tstThetaCorrect=scm_uwAngRefPu - rtY.Angle_Filtered;
  302. }
  303. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  304. {
  305. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  306. // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  307. }
  308. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  309. {
  310. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  311. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  312. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  313. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  314. scm_uwAngRefPu = rtY.Angle_Filtered;
  315. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  316. }
  317. else
  318. {}
  319. /*=======================================================================
  320. Set Iq limit and Id reference for Constant Voltage Break
  321. =======================================================================*/
  322. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  323. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  324. cvb_stBrakeIn.swIqRefPu = swCurRefrompu; //scm_swIqRefPu;
  325. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  326. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  327. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  328. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  329. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  330. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  331. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  332. {
  333. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  334. }
  335. else
  336. {
  337. scm_swIdRefPu = flx_stCtrlOut.swIdRefPu;
  338. }
  339. /* Speed feedback Absolute */
  340. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  341. /* Get angle for park transformation */
  342. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  343. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  344. {
  345. swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  346. }
  347. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  348. {
  349. // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  350. }
  351. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  352. {
  353. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  354. }
  355. else
  356. {}
  357. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  358. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  359. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  360. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  361. /*=======================================================================
  362. Current decoupling
  363. =======================================================================*/
  364. if(switch_flg.SysRun_Flag == TRUE)
  365. {
  366. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  367. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  368. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  369. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  370. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  371. }
  372. else
  373. {
  374. if(++UdqDcpOutCount>100)
  375. {
  376. UdqDcpOutCount=0;
  377. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  378. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  379. }
  380. }
  381. }
  382. void Clz2Stop_TbcdownHook(void)
  383. {
  384. ULONG ulTmp1;
  385. SWORD swAngCompPu; // Q15
  386. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  387. {
  388. obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  389. obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  390. obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  391. obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  392. obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  393. obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  394. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  395. scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  396. temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  397. if (temptheta >= cof_sl360DegreePu)
  398. {
  399. temptheta -= cof_sl360DegreePu;
  400. }
  401. else if (temptheta < (-(cof_sl360DegreePu)))
  402. {
  403. temptheta += cof_sl360DegreePu;
  404. }
  405. scm_uwAngRefPu=temptheta;
  406. tstThetaCorrect=scm_uwAngRefPu - rtY.Angle_Filtered;
  407. }
  408. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  409. {
  410. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  411. // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  412. }
  413. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  414. {
  415. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  416. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  417. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  418. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  419. scm_uwAngRefPu = rtY.Angle_Filtered;
  420. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  421. }
  422. else
  423. {}
  424. /*=======================================================================
  425. Set Iq limit and Id reference for Constant Voltage Break
  426. =======================================================================*/
  427. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  428. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  429. cvb_stBrakeIn.swIqRefPu = 0;//swCurRefrompu; //scm_swIqRefPu;
  430. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  431. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  432. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  433. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  434. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  435. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  436. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  437. {
  438. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  439. }
  440. else
  441. {
  442. scm_swIdRefPu = 0;//flx_stCtrlOut.swIdRefPu;
  443. }
  444. /* Speed feedback Absolute */
  445. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  446. /* Get angle for park transformation */
  447. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  448. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  449. {
  450. swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  451. }
  452. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  453. {
  454. // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  455. }
  456. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  457. {
  458. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  459. }
  460. else
  461. {}
  462. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  463. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  464. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  465. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  466. /*=======================================================================
  467. Current decoupling
  468. =======================================================================*/
  469. if(switch_flg.SysRun_Flag == TRUE)
  470. {
  471. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  472. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  473. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  474. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  475. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  476. }
  477. else
  478. {
  479. if(++UdqDcpOutCount>100)
  480. {
  481. UdqDcpOutCount=0;
  482. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  483. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  484. }
  485. }
  486. }
  487. void Stop_TbcdownHook(void)
  488. {
  489. scm_swIdRefPu = 0;
  490. scm_swIqRefPu = 0;
  491. scm_swUdRefPu=0;
  492. scm_swUqRefPu=0;
  493. hw_voPWMInit();
  494. cmfsm_stFlg.blMotorStopFlg = TRUE;
  495. }
  496. void InitPosDet_TbsHook(void)
  497. {}
  498. void ParDet_TbsHook(void)
  499. {}
  500. void StartUp_TbsHook(void)
  501. {
  502. /*=======================================================================
  503. Speed PI output limit in "OpenDrg"
  504. =======================================================================*/
  505. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  506. {
  507. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  508. {
  509. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  510. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  511. asr_stSpdPIOut.slIqiPu = 0;
  512. }
  513. else
  514. {
  515. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  516. {
  517. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  518. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  519. }
  520. }
  521. }
  522. }
  523. void Open2Clz_TbsHook(void)
  524. {}
  525. void ClzLoop_TbsHook(void)
  526. {}
  527. void Stop_TbsHook(void)
  528. {}
  529. void Clz2Stop_TbsHook(void)
  530. {}
  531. void scm_voSpdCtrMdFSM(void)
  532. {
  533. switch (curSpeed_state.state)
  534. {
  535. case Charge:
  536. break;
  537. case InitPosDet:
  538. /* Command run disable */
  539. /* Motor run flag set */
  540. cmfsm_stFlg.blMotorStopFlg = FALSE;
  541. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  542. {
  543. Switch_speed_FSM(&Stop_state);
  544. }
  545. else if(switch_flg.SysWarnning_Flag == 1)
  546. {
  547. Switch_speed_FSM(&Clz2Stop_state);
  548. }
  549. else if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  550. {}
  551. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  552. {
  553. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  554. {
  555. align_stCoef.uwSPIreadOnceCt = 0;
  556. }
  557. else
  558. {}
  559. if(cp_stFlg.RunModelSelect == InitPos)
  560. {
  561. }
  562. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  563. {
  564. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  565. {
  566. Switch_speed_FSM(&StartUp_state);
  567. }
  568. else
  569. {
  570. Switch_speed_FSM(&ClzLoop_state);
  571. }
  572. }
  573. else
  574. {
  575. Switch_speed_FSM(&StartUp_state);
  576. }
  577. }
  578. else
  579. {}
  580. break;
  581. case StartUp:
  582. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  583. {
  584. Switch_speed_FSM(&Stop_state);
  585. }
  586. else if(switch_flg.SysWarnning_Flag == 1)
  587. {
  588. Switch_speed_FSM(&Clz2Stop_state);
  589. }
  590. if (scm_StartUpOvrFlg == TRUE)
  591. {
  592. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  593. {
  594. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  595. {
  596. if (FSM2nd_Run_state.state == Boost)
  597. {
  598. cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  599. }
  600. else if (FSM2nd_Run_state.state == Assistance)
  601. {}
  602. else
  603. {}
  604. Switch_speed_FSM(&Open2Clz_state);
  605. }
  606. }
  607. else
  608. {
  609. }
  610. }
  611. /* Command run disable */
  612. break;
  613. case Open2Clz:
  614. /* Command run disable */
  615. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  616. {
  617. Switch_speed_FSM(&Stop_state);
  618. }
  619. else if(switch_flg.SysWarnning_Flag == 1)
  620. {
  621. Switch_speed_FSM(&Clz2Stop_state);
  622. }
  623. else if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)/* Switch over */
  624. {
  625. Switch_speed_FSM(&ClzLoop_state);
  626. }
  627. break;
  628. case ClzLoop:
  629. /* Go to stop */
  630. cmfsm_stFlg.blMotorStopFlg = FALSE;
  631. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  632. {
  633. Switch_speed_FSM(&Stop_state);
  634. }
  635. else if(switch_flg.SysWarnning_Flag == 1)
  636. {
  637. Switch_speed_FSM(&Clz2Stop_state);
  638. }
  639. else if (!switch_flg.SysRun_Flag)
  640. {
  641. /* Go to stop */
  642. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  643. /*||(uart_swTorqRefNm==0)*/)
  644. {
  645. scm_swIdRefPu = 0;
  646. scm_swIqRefPu = 0;
  647. Switch_speed_FSM(&Clz2Stop_state);
  648. }
  649. }
  650. else
  651. {}
  652. break;
  653. case Clz2Stop:
  654. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF)
  655. {
  656. Switch_speed_FSM(&Stop_state);
  657. }
  658. else if((scm_ulStatCt > 24000) || (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  659. {
  660. Switch_speed_FSM(&Stop_state);
  661. }
  662. else
  663. {
  664. //do nothing
  665. }
  666. break;
  667. case Stop:
  668. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE && switch_flg.SysWarnning_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  669. {
  670. scm_voSpdCtrMdInit();
  671. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  672. {
  673. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  674. {
  675. Switch_speed_FSM(&InitPosDet_state);
  676. }
  677. else
  678. {
  679. Switch_speed_FSM(&ClzLoop_state);
  680. }
  681. }
  682. else
  683. {
  684. Switch_speed_FSM(&InitPosDet_state);
  685. }
  686. }
  687. break;
  688. default:
  689. break;
  690. }
  691. }
  692. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  693. {
  694. scm_ulStatCt = 0;
  695. curSpeed_state = *in;
  696. }
  697. void Switch_speed_FSMInit(void)
  698. {
  699. scm_ulStatCt = 0;
  700. //adc_stDownOut.swIaPu = 0;
  701. //adc_stDownOut.swIbPu = 0;
  702. // adc_stDownOut.swIcPu = 0;
  703. curSpeed_state = Stop_state;
  704. }
  705. /************************************************************************
  706. Function: void RUN_FSM_Main(void)
  707. Description:
  708. Call by:
  709. Input Variables:
  710. Output/Return Variables:
  711. Subroutine Call:
  712. Reference:
  713. ************************************************************************/
  714. /************************************************************************
  715. Local Functions: N/A
  716. ************************************************************************/
  717. /************************************************************************
  718. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  719. All rights reserved.
  720. ************************************************************************/
  721. #ifdef _STARTFSM_C_
  722. #undef _STARTFSM_C_
  723. #endif
  724. /************************************************************************
  725. End of this File (EOF)!
  726. Do not put anything after this part!
  727. ************************************************************************/