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