spdctrFSM.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. #include "glbcof.h"
  40. /************************************************************************
  41. Constant Table:
  42. ************************************************************************/
  43. /************************************************************************
  44. Exported Functions:
  45. ************************************************************************/
  46. void InitPosDet_TbcupHook(void)
  47. {
  48. scm_ulStatCt++;
  49. align_stIn.ulStatCt = scm_ulStatCt;
  50. align_voInitPos(&align_stIn, &align_stCoef, &align_stOut);
  51. scm_slIdRefPu = align_stOut.slIdRefPu; // Q29
  52. scm_swIdRefPu = align_stOut.swIdRefPu; // Q14
  53. scm_swIqRefPu = align_stOut.swIqRefPu; // Q14
  54. scm_uwAngRefPu = align_stOut.uwAngRefPu; // Q15
  55. }
  56. void ParDet_TbcupHook(void)
  57. {}
  58. void StartUp_TbcupHook(void)
  59. {
  60. flx_stCtrlOut.swIqLimPu = (SWORD)cof_uwCurMaxPu;
  61. scm_ulStatCt++;
  62. align_stIn.ulStatCt = scm_ulStatCt;
  63. align_stIn.swRotateDir = scm_swRotateDir;
  64. align_voStartUp(&align_stIn, &align_stCoef, &align_stOut);
  65. scm_slDragSpdRefPu = align_stOut.slDragSpdRefPu;
  66. scm_slDragSpdPu = align_stOut.slDragSpdPu;
  67. scm_swIdRefPu = align_stOut.swIdRefPu;
  68. if(align_stOut.swIqRefPu > 0)
  69. {
  70. if((scm_swIqRefPu + 2) <= align_stOut.swIqRefPu)
  71. {
  72. scm_swIqRefPu += 2;
  73. }
  74. else
  75. {
  76. scm_swIqRefPu = align_stOut.swIqRefPu;
  77. }
  78. }
  79. else if(align_stOut.swIqRefPu < 0)
  80. {
  81. if((scm_swIqRefPu - 2) >= align_stOut.swIqRefPu)
  82. {
  83. scm_swIqRefPu -= 2;
  84. }
  85. else
  86. {
  87. scm_swIqRefPu = align_stOut.swIqRefPu;
  88. }
  89. }
  90. else
  91. {
  92. scm_swIqRefPu = 0;
  93. }
  94. scm_slAngManuPu = align_stOut.slAngManuPu;
  95. scm_uwAngRefPu = align_stOut.uwAngRefPu;
  96. scm_StartUpOvrFlg = align_stOut.blStartUpOvrFlg;
  97. }
  98. void Open2Clz_TbcupHook(void)
  99. {
  100. flx_stCtrlOut.swIqLimPu = (SWORD)cof_uwCurMaxPu;
  101. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  102. {
  103. align_stIn.uwObsElecThetaPu = obs_stObsOutPu.uwElecThetaPu;
  104. }
  105. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  106. {
  107. //align_stIn.uwObsElecThetaPu = spi_stResolverOut.uwSpiThetaPu;
  108. }
  109. else 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_stPwrLimIn.uwThrottleLimit_K = Throttle_PowerLimit_K.K_Result;
  157. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  158. }
  159. void Stop_TbcupHook(void)
  160. {}
  161. void Clz2Stop_TbcupHook(void)
  162. {
  163. scm_ulStatCt++;
  164. /*=======================================================================
  165. Power Limit
  166. =======================================================================*/
  167. mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
  168. mth_voLPFilter(adc_stUpOut.MotorTemp, &scm_stMotorTempLpf);
  169. pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
  170. pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;
  171. pwr_stPwrLimIn.swMotorTemp = scm_stMotorTempLpf.slY.sw.hi;
  172. pwr_stPwrLimIn.uwBatCap = MC_RunInfo.SOC;
  173. pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
  174. }
  175. void InitPosDet_TbcdownHook(void)
  176. {
  177. /* Get angle for park transformation */
  178. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  179. scm_uwAngIParkPu = scm_uwAngParkPu;
  180. /*=======================================================================
  181. Current decoupling
  182. =======================================================================*/
  183. acr_stUdqDcpOut.swUdPu = 0;
  184. acr_stUdqDcpOut.swUqPu = 0;
  185. }
  186. void ParDet_TbcdownHook(void)
  187. {
  188. /* Get angle for park transformation */
  189. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  190. scm_uwAngIParkPu = scm_uwAngParkPu;
  191. /*=======================================================================
  192. Current decoupling
  193. =======================================================================*/
  194. acr_stUdqDcpOut.swUdPu = 0;
  195. acr_stUdqDcpOut.swUqPu = 0;
  196. }
  197. void StartUp_TbcdownHook(void)
  198. {
  199. /* Speed feedback LPF */
  200. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  201. {
  202. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  203. obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  204. obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  205. obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  206. obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  207. obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  208. obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  209. }
  210. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  211. {
  212. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  213. }
  214. else 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. if (temptheta >= cof_sl360DegreePu)
  292. {
  293. temptheta -= cof_sl360DegreePu;
  294. }
  295. else if (temptheta < (-(cof_sl360DegreePu)))
  296. {
  297. temptheta += cof_sl360DegreePu;
  298. }
  299. scm_uwAngRefPu=temptheta;
  300. tstThetaCorrect = scm_uwAngRefPu - rtY.Angle_Filtered;
  301. }
  302. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  303. {
  304. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  305. // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  306. }
  307. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  308. {
  309. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  310. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  311. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  312. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  313. scm_uwAngRefPu = rtY.Angle_Filtered;
  314. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  315. }
  316. else
  317. {}
  318. /* Speed feedback Absolute */
  319. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  320. /*=======================================================================
  321. Set Iq limit and Id reference for Constant Voltage Break
  322. =======================================================================*/
  323. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  324. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  325. cvb_stBrakeIn.swIqRefPu = swCurRefrompu; //scm_swIqRefPu;
  326. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  327. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  328. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  329. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  330. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  331. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  332. // scm_swIqRefPu = -500;
  333. // static UWORD AngleCnt;
  334. // AngleCnt += 200;
  335. // if(AngleCnt > 32767)
  336. // {
  337. // AngleCnt = 0;
  338. // }
  339. // scm_uwAngRefPu = AngleCnt;
  340. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  341. {
  342. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  343. }
  344. else
  345. {
  346. scm_swIdRefPu = flx_stCtrlOut.swIdRefPu;
  347. }
  348. /* Get angle for park transformation */
  349. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  350. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  351. {
  352. swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  353. }
  354. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  355. {
  356. // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  357. }
  358. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  359. {
  360. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  361. }
  362. else
  363. {}
  364. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  365. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  366. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  367. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  368. /*=======================================================================
  369. Current decoupling
  370. =======================================================================*/
  371. // if(switch_flg.SysRun_Flag == TRUE)
  372. // {
  373. // acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  374. // acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  375. // acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  376. // acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  377. // acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  378. // }
  379. // else
  380. // {
  381. // if(++UdqDcpOutCount>100)
  382. // {
  383. // UdqDcpOutCount=0;
  384. // acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  385. // acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  386. // }
  387. // }
  388. acr_stUdqDcpOut.swUdPu = 0;
  389. acr_stUdqDcpOut.swUqPu = 0;
  390. }
  391. void Clz2Stop_TbcdownHook(void)
  392. {
  393. ULONG ulTmp1;
  394. SWORD swAngCompPu; // Q15
  395. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  396. {
  397. obs_stObsCalcIn.swUalphaPu = scm_swUalphaPu; // Q14
  398. obs_stObsCalcIn.swUbetaPu = scm_swUbetaPu; // Q14
  399. obs_stObsCalcIn.swIalphaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  400. obs_stObsCalcIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  401. obs_stObsCalcIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  402. obs_voObsCalc(&obs_stObsCalcIn, &obs_stObsCoefPu, &obs_stObsOutPu);
  403. mth_voLPFilter(obs_stObsOutPu.swElecFreqPu, &scm_stSpdFbkLpf);
  404. // scm_uwAngRefPu = obs_stObsOutPu.uwElecThetaPu;
  405. temptheta = (SWORD)obs_stObsOutPu.uwElecThetaPu + thetaoffset;
  406. if (temptheta >= cof_sl360DegreePu)
  407. {
  408. temptheta -= cof_sl360DegreePu;
  409. }
  410. else if (temptheta < (-(cof_sl360DegreePu)))
  411. {
  412. temptheta += cof_sl360DegreePu;
  413. }
  414. scm_uwAngRefPu=temptheta;
  415. tstThetaCorrect = scm_uwAngRefPu - rtY.Angle_Filtered;
  416. }
  417. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  418. {
  419. // mth_voLPFilter(spi_stResolverOut.swSpdFbkPu, &scm_stSpdFbkLpf);
  420. // scm_uwAngRefPu = spi_stResolverOut.uwSpiThetaPu;
  421. }
  422. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  423. {
  424. // mth_voLPFilter(switchhall_stOut.swLowSpdPu, &scm_stSpdFbkLpf);
  425. //mth_voLPFilter(switchhall_stOut.swLowSpdLpfPu, &scm_stSpdFbkLpf);
  426. scm_stSpdFbkLpf.slY.sw.hi = switchhall_stOut.swLowSpdLpfPu;
  427. // scm_uwAngRefPu = switchhall_stOut.slLowThetaPu;
  428. scm_uwAngRefPu = rtY.Angle_Filtered;
  429. //scm_uwAngRefPu = LoadObsTheta_Y.uwThetaObsPu;
  430. }
  431. else
  432. {}
  433. /*=======================================================================
  434. Set Iq limit and Id reference for Constant Voltage Break
  435. =======================================================================*/
  436. cvb_stBrakeIn.uwVdcLpfPu = (SWORD)adc_stUpOut.uwVdcLpfPu;
  437. cvb_stBrakeIn.swIdRefPu = scm_swIdRefPu;
  438. cvb_stBrakeIn.swIqRefPu = 0;//swCurRefrompu; //scm_swIqRefPu;
  439. cvb_stBrakeIn.swSpdPu = scm_stSpdFbkLpf.slY.sw.hi;
  440. cvb_stBrakeIn.uwAngelPu = scm_uwAngRefPu;
  441. cvb_stBrakeIn.uwSpdLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  442. cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
  443. scm_swIqRefPu = cvb_stBrakeOut.swIqRefPu;
  444. scm_uwAngRefPu = cvb_stBrakeOut.uwAngelPu;
  445. if(cvb_stBrakeIn.uwVdcLpfPu >= cvb_stBrakeCoef.uwVdcStartCvbPu)
  446. {
  447. scm_swIdRefPu = cvb_stBrakeOut.swIdRefPu;
  448. }
  449. else
  450. {
  451. scm_swIdRefPu = 0;//flx_stCtrlOut.swIdRefPu;
  452. }
  453. /* Speed feedback Absolute */
  454. scm_uwSpdFbkLpfAbsPu = abs(scm_stSpdFbkLpf.slY.sw.hi); // Q15
  455. /* Get angle for park transformation */
  456. scm_uwAngParkPu = scm_uwAngRefPu; // Q15
  457. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  458. {
  459. swAngCompPu = ((SLONG)obs_stObsOutPu.swElecFreqPu * TBC_TM) >> 10; // Q15
  460. }
  461. else if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  462. {
  463. // swAngCompPu = ((SLONG)spi_stResolverOut.swSpdFbkPu * TBC_TM) >> 10; // Q15
  464. }
  465. else if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  466. {
  467. swAngCompPu = ((SLONG)switchhall_stOut.swLowSpdLpfPu * TBC_TM) >> 10; // Q15
  468. }
  469. else
  470. {}
  471. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 3) >> 1) + cof_sl720DegreePu; // ˫���²���2.5��PWM���ڣ� �����²���1.5��PWM����
  472. scm_uwAngIParkPu = ulTmp1 & 0x7FFF;
  473. ulTmp1 = scm_uwAngParkPu + ((swAngCompPu * 2) >> 1) + cof_sl720DegreePu;
  474. scm_uwAngIParkPu1 = ulTmp1 & 0x7FFF;
  475. /*=======================================================================
  476. Current decoupling
  477. =======================================================================*/
  478. if(switch_flg.SysRun_Flag == TRUE)
  479. {
  480. acr_stUdqDcpIn.swWsPu = scm_stSpdFbkLpf.slY.sw.hi; // switchhall_stOut.swLowSpdLpfPu;//scm_stSpdFbkLpf.slY.sw.hi; //Q15
  481. acr_stUdqDcpIn.swIdRefPu = 0;//scm_swIdFdbLpfPu; //scm_swIdFdbLpfPu;//scm_swIdRefPu; // Q14
  482. acr_stUdqDcpIn.swIqRefPu = 0;//scm_swIqFdbLpfPu; //scm_swIqFdbLpfPu;//scm_swIqRefPu; // Q14 scm_swIqFdbLpfPu
  483. acr_stUdqDcpIn.swUdqLimPu = scm_swVsDcpLimPu; // Q14
  484. acr_voUdqDcp(&acr_stUdqDcpIn, &acr_stUdqDcpCoef, &acr_stUdqDcpOut);
  485. }
  486. else
  487. {
  488. if(++UdqDcpOutCount>100)
  489. {
  490. UdqDcpOutCount=0;
  491. acr_stUdqDcpOut.swUdPu = ((SLONG)acr_stUdqDcpOut.swUdPu*1010)>>10;
  492. acr_stUdqDcpOut.swUqPu = ((SLONG)acr_stUdqDcpOut.swUqPu*1010)>>10;
  493. }
  494. }
  495. }
  496. void Stop_TbcdownHook(void)
  497. {
  498. scm_swIdRefPu = 0;
  499. scm_swIqRefPu = 0;
  500. scm_swUdRefPu=0;
  501. scm_swUqRefPu=0;
  502. hw_voPWMInit();
  503. cmfsm_stFlg.blMotorStopFlg = TRUE;
  504. }
  505. void InitPosDet_TbsHook(void)
  506. {}
  507. void ParDet_TbsHook(void)
  508. {}
  509. void StartUp_TbsHook(void)
  510. {
  511. /*=======================================================================
  512. Speed PI output limit in "OpenDrg"
  513. =======================================================================*/
  514. if ((curSpeed_state.state == StartUp) && (scm_uwStartMd == START_ALIGN))
  515. {
  516. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu < 0)
  517. {
  518. asr_stSpdPIOut.slIqRefPu = 0; // Q30
  519. asr_stSpdPIOut.slIqSumPu = 0; // Q30
  520. asr_stSpdPIOut.slIqiPu = 0;
  521. }
  522. else
  523. {
  524. if (scm_swRotateDir * asr_stSpdPIOut.slIqRefPu > ((SLONG)mn_uwDragCurPu << 16)) // Q30
  525. {
  526. asr_stSpdPIOut.slIqRefPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q30
  527. asr_stSpdPIOut.slIqiPu = scm_swRotateDir * ((SLONG)mn_uwDragCurPu << 16); // Q14+Q16=Q30
  528. }
  529. }
  530. }
  531. }
  532. void Open2Clz_TbsHook(void)
  533. {}
  534. void ClzLoop_TbsHook(void)
  535. {}
  536. void Stop_TbsHook(void)
  537. {}
  538. void Clz2Stop_TbsHook(void)
  539. {}
  540. void scm_voSpdCtrMdFSM(void)
  541. {
  542. switch (curSpeed_state.state)
  543. {
  544. case Charge:
  545. break;
  546. case InitPosDet:
  547. /* Command run disable */
  548. /* Motor run flag set */
  549. cmfsm_stFlg.blMotorStopFlg = FALSE;
  550. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  551. {
  552. Switch_speed_FSM(&Stop_state);
  553. }
  554. else if(switch_flg.SysWarnning_Flag == 1)
  555. {
  556. Switch_speed_FSM(&Clz2Stop_state);
  557. }
  558. else if (scm_ulStatCt < mn_ulAlignRampTbcCt)
  559. {}
  560. else if (scm_ulStatCt >= (mn_ulAlignRampTbcCt + mn_ulAlignHoldTbcCt + 10))
  561. {
  562. if(cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER)
  563. {
  564. align_stCoef.uwSPIreadOnceCt = 0;
  565. }
  566. else
  567. {}
  568. if(cp_stFlg.RunModelSelect == InitPos)
  569. {
  570. }
  571. else if(cp_stFlg.RunModelSelect == ClZLOOP)
  572. {
  573. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  574. {
  575. Switch_speed_FSM(&StartUp_state);
  576. }
  577. else
  578. {
  579. Switch_speed_FSM(&ClzLoop_state);
  580. }
  581. }
  582. else
  583. {
  584. Switch_speed_FSM(&StartUp_state);
  585. }
  586. }
  587. else
  588. {}
  589. break;
  590. case StartUp:
  591. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  592. {
  593. Switch_speed_FSM(&Stop_state);
  594. }
  595. else if(switch_flg.SysWarnning_Flag == 1)
  596. {
  597. Switch_speed_FSM(&Clz2Stop_state);
  598. }
  599. if (scm_StartUpOvrFlg == TRUE)
  600. {
  601. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER) //|| cp_stFlg.ThetaGetModelSelect ==ANG_SWITCHHALL
  602. {
  603. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  604. {
  605. if (FSM2nd_Run_state.state == Boost)
  606. {
  607. cmd_stCmdOut.slIntRefPu = (((SLONG)scm_stSpdFbkLpf.slY.sw.hi * 5) << 12); // Q29 5/4
  608. }
  609. else if (FSM2nd_Run_state.state == Assistance)
  610. {}
  611. else
  612. {}
  613. Switch_speed_FSM(&Open2Clz_state);
  614. }
  615. }
  616. else
  617. {
  618. }
  619. }
  620. /* Command run disable */
  621. break;
  622. case Open2Clz:
  623. /* Command run disable */
  624. if (!switch_flg.SysRun_Flag || switch_flg.SysFault_Flag || power_stPowStateOut.powerstate == POWER_OFF)
  625. {
  626. Switch_speed_FSM(&Stop_state);
  627. }
  628. else if(switch_flg.SysWarnning_Flag == 1)
  629. {
  630. Switch_speed_FSM(&Clz2Stop_state);
  631. }
  632. else if (scm_blCurSwitchOvrFlg && scm_blAngSwitchOvrFlg)/* Switch over */
  633. {
  634. Switch_speed_FSM(&ClzLoop_state);
  635. }
  636. break;
  637. case ClzLoop:
  638. /* Go to stop */
  639. cmfsm_stFlg.blMotorStopFlg = FALSE;
  640. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF_END)
  641. {
  642. Switch_speed_FSM(&Stop_state);
  643. }
  644. else if(switch_flg.SysWarnning_Flag == 1)
  645. {
  646. Switch_speed_FSM(&Clz2Stop_state);
  647. }
  648. else if (!switch_flg.SysRun_Flag)
  649. {
  650. /* Go to stop */
  651. if (((abs(scm_swSpdRefPu) < mn_uwStopSpdRefPu) && (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  652. /*||(uart_swTorqRefNm==0)*/)
  653. {
  654. scm_swIdRefPu = 0;
  655. scm_swIqRefPu = 0;
  656. Switch_speed_FSM(&Clz2Stop_state);
  657. }
  658. }
  659. else
  660. {}
  661. break;
  662. case Clz2Stop:
  663. if (switch_flg.SysFault_Flag == 1 || power_stPowStateOut.powerstate == POWER_OFF)
  664. {
  665. Switch_speed_FSM(&Stop_state);
  666. }
  667. else if((scm_ulStatCt > 24000) || (scm_uwSpdFbkLpfAbsPu < mn_uwStopSpdRefPu))
  668. {
  669. Switch_speed_FSM(&Stop_state);
  670. }
  671. else
  672. {
  673. //do nothing
  674. }
  675. break;
  676. case Stop:
  677. if (switch_flg.SysRun_Flag == TRUE && switch_flg.SysFault_Flag == FALSE && switch_flg.SysWarnning_Flag == FALSE) //&& power_stPowStateOut.powerstate == POWER_ON_END
  678. {
  679. scm_voSpdCtrMdInit();
  680. if(cp_stFlg.RunModelSelect == ClZLOOP || cp_stFlg.RunModelSelect == CadAssist || cp_stFlg.RunModelSelect == TorqAssist)
  681. {
  682. if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  683. {
  684. Switch_speed_FSM(&InitPosDet_state);
  685. }
  686. else
  687. {
  688. Switch_speed_FSM(&ClzLoop_state);
  689. }
  690. }
  691. else
  692. {
  693. Switch_speed_FSM(&InitPosDet_state);
  694. }
  695. }
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. void Switch_speed_FSM(SPD_STATE_HOOK *in)
  702. {
  703. scm_ulStatCt = 0;
  704. curSpeed_state = *in;
  705. }
  706. void Switch_speed_FSMInit(void)
  707. {
  708. scm_ulStatCt = 0;
  709. //adc_stDownOut.swIaPu = 0;
  710. //adc_stDownOut.swIbPu = 0;
  711. // adc_stDownOut.swIcPu = 0;
  712. curSpeed_state = Stop_state;
  713. }
  714. /************************************************************************
  715. Function: void RUN_FSM_Main(void)
  716. Description:
  717. Call by:
  718. Input Variables:
  719. Output/Return Variables:
  720. Subroutine Call:
  721. Reference:
  722. ************************************************************************/
  723. /************************************************************************
  724. Local Functions: N/A
  725. ************************************************************************/
  726. /************************************************************************
  727. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  728. All rights reserved.
  729. ************************************************************************/
  730. #ifdef _STARTFSM_C_
  731. #undef _STARTFSM_C_
  732. #endif
  733. /************************************************************************
  734. End of this File (EOF)!
  735. Do not put anything after this part!
  736. ************************************************************************/