switchhall.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /**
  2. * @file switchhall.c
  3. * @author
  4. * @brief
  5. * @version
  6. * @date 2021-11-22
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. /************************************************************************
  12. Beginning of File, do not put anything above here except notes
  13. Compiler Directives:
  14. *************************************************************************/
  15. #ifdef RUN_ARCH_SIM
  16. #include "switchhall.h"
  17. SWITCHHALL_OUT switchhall_stOut = SWITCHHALL_OUT_DEFAULT;
  18. void switchhall_voInit(void)
  19. {}
  20. void switchhall_voTim3Int(void)
  21. {}
  22. void switchhall_voPosCalTbc(void)
  23. {}
  24. #else
  25. #include "gd32f30x.h"
  26. #include "typedefine.h"
  27. #include "syspar.h"
  28. #include "hwsetup.h"
  29. #include "glbcof.h"
  30. #include "switchhall.h"
  31. #include "spdctrFSM.h"
  32. #include "spdctrmode.h"
  33. /******************************
  34. *
  35. * Parameter
  36. *
  37. ******************************/
  38. static SWITCHHALL_CAP switchhall_stCap = SWITCHHALL_CAP_DEFAULT;
  39. SWITCHHALL_OUT switchhall_stOut = SWITCHHALL_OUT_DEFAULT;
  40. /***************************************************************
  41. Function:
  42. Description:
  43. Call by:
  44. Input Variables:
  45. Output/Return Variables:
  46. Subroutine Call:
  47. Reference:
  48. ****************************************************************/
  49. void switchhall_voInit(void)
  50. {
  51. switchhall_stOut.swLowSpdLpfPu = 0;
  52. switchhall_stOut.swLowSpdPu = 0;
  53. switchhall_stOut.swSpdCWorCCW = 0;
  54. switchhall_stOut.slInitThetaPu = 0;
  55. switchhall_stOut.uwLowThetaPu = 0;
  56. switchhall_stOut.uwSectorNum = 0;
  57. switchhall_stOut.uwSectorNumPre = 0;
  58. switchhall_stOut.slLowThetaPu = 0;
  59. switchhall_stOut.swLowThetaOffsetPu =-15000;
  60. switchhall_stCap.slCapValueDelta = HALL3_SPD_CAP_LEVEL1;
  61. switchhall_stCap.ulCapValue = 0;
  62. switchhall_stCap.ulCapValuePre = 0;
  63. switchhall_stCap.uwCaptureOverFlowCnt = 0;
  64. }
  65. /***************************************************************
  66. Function:
  67. Description:
  68. Call by:
  69. Input Variables:
  70. Output/Return Variables:
  71. Subroutine Call:
  72. Reference:
  73. ****************************************************************/
  74. static void switchhall_voSectorJudge(void)
  75. {
  76. /* HALLA(PC8), HALLB(PC7), PHALLC(PC6), wzy */
  77. // if(iGpio_Read(HW_GPIO_HALL_A_PIN) != 0)
  78. // {
  79. // switchhall_stOut.uwSectorNum = 0;
  80. // }
  81. // else
  82. // {
  83. // switchhall_stOut.uwSectorNum = 1;
  84. // }
  85. // if(iGpio_Read(HW_GPIO_HALL_B_PIN) != 0)
  86. // {
  87. // switchhall_stOut.uwSectorNum |= 0<<1;
  88. // }
  89. // else
  90. // {
  91. // switchhall_stOut.uwSectorNum |= 1<<1;
  92. // }
  93. // if(iGpio_Read(HW_GPIO_HALL_C_PIN) != 0)
  94. // {
  95. // switchhall_stOut.uwSectorNum |= 0<<2;
  96. // }
  97. // else
  98. // {
  99. // switchhall_stOut.uwSectorNum |= 1<<2;
  100. // }
  101. // switchhall_stOut.uwSectorNum = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6);
  102. // switchhall_stOut.uwSectorNum |= (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) << 1);
  103. // switchhall_stOut.uwSectorNum |= (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8) << 2);
  104. }
  105. /***************************************************************
  106. Function:
  107. Description:
  108. Call by:
  109. Input Variables:
  110. Output/Return Variables:
  111. Subroutine Call:
  112. Reference:
  113. ****************************************************************/
  114. static SLONG switchhall_pvt_slLowSpdLpfPu;
  115. static SLONG switchhall_pvt_slHighSpdLpfPu;
  116. static SLONG Speed_buffer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  117. static SQWORD Speed_fb_sum = 0;
  118. static SLONG Speed_Value = 0;
  119. static SLONG Speed_AvgValue = 0;
  120. static SWORD Speed_sample_index = 0;
  121. static SWORD HallFltFlg;
  122. void switchhall_voTim3Int(void)
  123. {
  124. /*Sector Judgement*/
  125. switchhall_voSectorJudge();
  126. /* Capture Value */
  127. if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP) == SET)
  128. {
  129. // if(curSpeed_state.state == StartUp || curSpeed_state.state == Open2Clz || curSpeed_state.state == ClzLoop)
  130. // {
  131. // if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH0) == SET)
  132. // {
  133. // switchhall_stCap.uwState = (UWORD)timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH0);
  134. // timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
  135. // }
  136. // else if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH1) == SET)
  137. // {
  138. // switchhall_stCap.uwState = (UWORD)timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH1);
  139. // timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH1);
  140. // }
  141. // else if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH2) == SET)
  142. // {
  143. // switchhall_stCap.uwState = (UWORD)timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH2);
  144. // timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH2);
  145. // }
  146. // else
  147. // {
  148. // switchhall_stCap.uwState=0;
  149. // }
  150. // if (switchhall_stCap.uwState > timer_counter_read(TIMER2))
  151. // {
  152. // switchhall_stCap.ulCapValue = switchhall_stCap.uwState;
  153. // switchhall_stCap.slCapValueDelta = (SLONG)switchhall_stCap.uwCaptureOverFlowCnt * 60000 + (SLONG)switchhall_stCap.ulCapValue - (SLONG)switchhall_stCap.ulCapValuePre;
  154. // switchhall_stCap.uwCaptureOverFlowCnt ++;
  155. // }
  156. // else
  157. // {
  158. // switchhall_stCap.uwCaptureOverFlowCnt ++;
  159. // }
  160. // if (switchhall_stCap.uwCaptureOverFlowCnt > 3000)
  161. // {
  162. // switchhall_stCap.uwCaptureOverFlowCnt=0;
  163. // }
  164. // }
  165. // else
  166. // {
  167. // switchhall_stCap.uwCaptureOverFlowCnt = 0;
  168. // }
  169. switchhall_stCap.uwCaptureOverFlowCnt ++;
  170. timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
  171. }
  172. else if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH0)== SET)
  173. {
  174. switchhall_stCap.ulCapValue = timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH0);
  175. switchhall_stCap.slCapValueDelta = (SLONG)switchhall_stCap.uwCaptureOverFlowCnt * 60000 + (SLONG)switchhall_stCap.ulCapValue - (SLONG)switchhall_stCap.ulCapValuePre;
  176. switchhall_stCap.uwCaptureOverFlowCnt = 0;
  177. timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
  178. }
  179. else if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH1) == SET)
  180. {
  181. switchhall_stCap.ulCapValue = timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH1);
  182. switchhall_stCap.slCapValueDelta = (SLONG)switchhall_stCap.uwCaptureOverFlowCnt * 60000 + (SLONG)switchhall_stCap.ulCapValue - (SLONG)switchhall_stCap.ulCapValuePre;
  183. switchhall_stCap.uwCaptureOverFlowCnt = 0;
  184. timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH1);
  185. }
  186. else if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH2) == SET)
  187. {
  188. /* Low Speed*/
  189. switchhall_stCap.ulCapValue = timer_channel_capture_value_register_read(TIMER2, TIMER_INT_FLAG_CH2);
  190. switchhall_stCap.slCapValueDelta = (SLONG)switchhall_stCap.uwCaptureOverFlowCnt * 60000 + (SLONG)switchhall_stCap.ulCapValue - (SLONG)switchhall_stCap.ulCapValuePre;
  191. switchhall_stCap.uwCaptureOverFlowCnt = 0;
  192. // /* High Speed*/
  193. // switchhall_stCap.ulHighCapValue = switchhall_stCap.ulCapValue;
  194. // switchhall_stCap.slHighCapValueDelta = (SLONG)switchhall_stCap.ulHighCapValue - switchhall_stCap.ulHighCapValuePre;
  195. // if(switchhall_stCap.slHighCapValueDelta < 0)
  196. // {
  197. // switchhall_stCap.slHighCapValueDelta += 60000;
  198. // }
  199. timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH2);
  200. }
  201. else
  202. {
  203. // switchhall_stCap.slCapValueDelta = HALL3_SPD_CAP_LEVEL1;
  204. }
  205. /* Move Average Filter */
  206. Speed_Value = switchhall_stCap.slCapValueDelta;
  207. Speed_fb_sum -= Speed_buffer[Speed_sample_index];
  208. Speed_buffer[Speed_sample_index] = Speed_Value;
  209. Speed_fb_sum += (SQWORD)Speed_Value;
  210. Speed_AvgValue = (SLONG)(Speed_fb_sum >> 4) ;
  211. Speed_sample_index++;
  212. if (Speed_sample_index >= 16)
  213. {
  214. Speed_sample_index = 0;
  215. }
  216. if(curSpeed_state.state ==StartUp || curSpeed_state.state == Open2Clz || curSpeed_state.state == ClzLoop)
  217. {
  218. // if ((switchhall_stOut.swLowSpdLpfPu > 1950)||(switchhall_stOut.swLowSpdLpfPu < -1950))//500rpm
  219. // {
  220. // switchhall_stCap.slCapValueDelta = Speed_AvgValue;
  221. // }
  222. // else
  223. //{
  224. // switchhall_stCap.slCapValueDelta = Speed_Value;
  225. // }
  226. switchhall_stCap.slCapValueDelta = Speed_AvgValue;
  227. }
  228. else
  229. {
  230. switchhall_stCap.slCapValueDelta = HALL3_SPD_CAL_COEF;
  231. }
  232. switchhall_stCap.ulCapValuePre = switchhall_stCap.ulCapValue;
  233. }
  234. /***************************************************************
  235. Function:
  236. Description:
  237. Call by:
  238. Input Variables:
  239. Output/Return Variables:
  240. Subroutine Call:
  241. Reference:
  242. ****************************************************************/
  243. void switchhall_voPosCalTbc(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
  244. {
  245. SLONG Hall_Theta_Temp = 0;
  246. /*Low Speed Theta Update*/
  247. switchhall_stOut.slLowThetaPu += ((SLONG)TBC_TM * switchhall_stOut.swLowSpdLpfPu) >> 10; // Q10+Q15-Q10=Q15
  248. //switchhall_stOut.slLowThetaPu += (((SLONG)TBC_TM * scm_stSpdFbkLpf.slY.sw.hi) >> 10); // Q10+Q15-Q10=Q15
  249. if (switchhall_stOut.slLowThetaPu >= (cof_sl360DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  250. {
  251. switchhall_stOut.slLowThetaPu -= cof_sl360DegreePu;
  252. }
  253. else if (switchhall_stOut.slLowThetaPu < 0 + switchhall_stOut.swLowThetaOffsetPu)
  254. {
  255. switchhall_stOut.slLowThetaPu += cof_sl360DegreePu;
  256. }
  257. else
  258. {
  259. //do noting
  260. }
  261. /*High Speed Theta Update*/
  262. // switchhall_stOut.slHighThetaPu += (((SLONG)TBC_TM * switchhall_stOut.swHighSpdLpfPu) >> 10); // Q10+Q15-Q10=Q15
  263. // if (switchhall_stOut.slHighThetaPu >= cof_sl360DegreePu + switchhall_stOut.swLowThetaOffsetPu)
  264. // {
  265. // switchhall_stOut.slHighThetaPu -= cof_sl360DegreePu;
  266. // }
  267. // else if (switchhall_stOut.slHighThetaPu < switchhall_stOut.swLowThetaOffsetPu)
  268. // {
  269. // switchhall_stOut.slHighThetaPu += cof_sl360DegreePu;
  270. // }
  271. /*Sector Judgement*/
  272. switchhall_voSectorJudge();
  273. /*!< Zero Correction 1*/
  274. // switch(switchhall_stOut.uwSectorNum)
  275. // {
  276. // case 1:
  277. // if(switchhall_stOut.uwSectorNumPre == 3)
  278. // {
  279. // switchhall_stOut.swSpdCWorCCW = 1;
  280. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  281. // }
  282. // else if(switchhall_stOut.uwSectorNumPre == 5)
  283. // {
  284. // switchhall_stOut.swSpdCWorCCW =-1;
  285. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  286. // }
  287. //
  288. // switchhall_stOut.slInitThetaPu = cof_sl240DegreePu;
  289. // if(switchhall_stOut.slLowThetaPu < cof_sl210DegreePu)
  290. // {
  291. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  292. // }
  293. // else if(switchhall_stOut.slLowThetaPu > cof_sl270DegreePu)
  294. // {
  295. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  296. // }
  297. // else
  298. // {
  299. // }
  300. // break;
  301. // case 2:
  302. // if(switchhall_stOut.uwSectorNumPre == 6)
  303. // {
  304. // switchhall_stOut.swSpdCWorCCW = 1;
  305. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  306. // }
  307. // else if(switchhall_stOut.uwSectorNumPre == 3)
  308. // {
  309. // switchhall_stOut.swSpdCWorCCW =-1;
  310. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  311. // }
  312. //
  313. // switchhall_stOut.slInitThetaPu = cof_sl120DegreePu;
  314. // if(switchhall_stOut.slLowThetaPu < cof_sl90DegreePu)
  315. // {
  316. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  317. // }
  318. // else if(switchhall_stOut.slLowThetaPu > cof_sl150DegreePu)
  319. // {
  320. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  321. // }
  322. // else
  323. // {
  324. // }
  325. // break;
  326. // case 3:
  327. // if(switchhall_stOut.uwSectorNumPre == 2)
  328. // {
  329. // switchhall_stOut.swSpdCWorCCW = 1;
  330. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  331. // }
  332. // else if(switchhall_stOut.uwSectorNumPre == 1)
  333. // {
  334. // switchhall_stOut.swSpdCWorCCW =-1;
  335. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  336. // }
  337. //
  338. // switchhall_stOut.slInitThetaPu = cof_sl180DegreePu;
  339. // if(switchhall_stOut.slLowThetaPu < cof_sl150DegreePu)
  340. // {
  341. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  342. // }
  343. // else if(switchhall_stOut.slLowThetaPu > cof_sl210DegreePu)
  344. // {
  345. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  346. // }
  347. // else
  348. // {
  349. // }
  350. // break;
  351. // case 4:
  352. // if(switchhall_stOut.uwSectorNumPre == 5)
  353. // {
  354. // switchhall_stOut.swSpdCWorCCW = 1;
  355. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  356. // }
  357. // else if(switchhall_stOut.uwSectorNumPre == 6)
  358. // {
  359. // switchhall_stOut.swSpdCWorCCW =-1;
  360. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  361. // }
  362. //
  363. // switchhall_stOut.slInitThetaPu = 0;
  364. // if((switchhall_stOut.slLowThetaPu > cof_sl30DegreePu)&&(switchhall_stOut.slLowThetaPu < cof_sl330DegreePu))
  365. // {
  366. // if(switchhall_stOut.swSpdCWorCCW == 1)
  367. // {
  368. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  369. // }
  370. // else if(switchhall_stOut.swSpdCWorCCW == -1)
  371. // {
  372. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  373. // }
  374. // }
  375. // break;
  376. // case 5:
  377. // if(switchhall_stOut.uwSectorNumPre == 1)
  378. // {
  379. // switchhall_stOut.swSpdCWorCCW = 1;
  380. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  381. // }
  382. // else if(switchhall_stOut.uwSectorNumPre == 4)
  383. // {
  384. // switchhall_stOut.swSpdCWorCCW =-1;
  385. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  386. // }
  387. //
  388. // switchhall_stOut.slInitThetaPu = cof_sl300DegreePu;
  389. // if(switchhall_stOut.slLowThetaPu < cof_sl270DegreePu)
  390. // {
  391. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  392. // }
  393. // else if(switchhall_stOut.slLowThetaPu > cof_sl330DegreePu)
  394. // {
  395. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  396. // }
  397. // else
  398. // {
  399. // }
  400. // break;
  401. // case 6:
  402. // if(switchhall_stOut.uwSectorNumPre == 4)
  403. // {
  404. // switchhall_stOut.swSpdCWorCCW = 1;
  405. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  406. // }
  407. // else if(switchhall_stOut.uwSectorNumPre == 2)
  408. // {
  409. // switchhall_stOut.swSpdCWorCCW = -1;
  410. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  411. // }
  412. //
  413. // switchhall_stOut.slInitThetaPu = cof_sl60DegreePu;
  414. // if(switchhall_stOut.slLowThetaPu < cof_sl30DegreePu)
  415. // {
  416. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  417. // }
  418. // else if(switchhall_stOut.slLowThetaPu > cof_sl90DegreePu)
  419. // {
  420. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  421. // }
  422. // else
  423. // {
  424. // }
  425. // break;
  426. // default:
  427. // switchhall_stOut.swSpdCWorCCW =0;
  428. // switchhall_stOut.swLowSpdPu = 0;
  429. // break;
  430. // }
  431. //
  432. //
  433. /*!< Zero Correction 2*/
  434. switch (switchhall_stOut.uwSectorNum)
  435. {
  436. case 1:
  437. if (switchhall_stOut.uwSectorNumPre == 3)
  438. {
  439. switchhall_stOut.swSpdCWorCCW = 1;
  440. switchhall_stOut.slLowThetaPu = cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  441. }
  442. else if (switchhall_stOut.uwSectorNumPre == 5)
  443. {
  444. switchhall_stOut.swSpdCWorCCW = -1;
  445. switchhall_stOut.slLowThetaPu = cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  446. }
  447. else
  448. {
  449. //do noting
  450. }
  451. switchhall_stOut.slInitThetaPu = cof_sl60DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  452. if (switchhall_stOut.slLowThetaPu < (cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  453. {
  454. switchhall_stOut.slLowThetaPu = cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  455. }
  456. else if (switchhall_stOut.slLowThetaPu > (cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  457. {
  458. switchhall_stOut.slLowThetaPu = cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  459. }
  460. else
  461. {
  462. //do noting
  463. }
  464. break;
  465. case 2:
  466. if (switchhall_stOut.uwSectorNumPre == 6)
  467. {
  468. switchhall_stOut.swSpdCWorCCW = 1;
  469. switchhall_stOut.slLowThetaPu = cof_sl270DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  470. }
  471. else if (switchhall_stOut.uwSectorNumPre == 3)
  472. {
  473. switchhall_stOut.swSpdCWorCCW = -1;
  474. switchhall_stOut.slLowThetaPu = cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  475. }
  476. else
  477. {
  478. //do noting
  479. }
  480. switchhall_stOut.slInitThetaPu = cof_sl300DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  481. if (switchhall_stOut.slLowThetaPu < (cof_sl270DegreePu+ switchhall_stOut.swLowThetaOffsetPu))
  482. {
  483. switchhall_stOut.slLowThetaPu = cof_sl270DegreePu + switchhall_stOut.swLowThetaOffsetPu ;
  484. }
  485. else if (switchhall_stOut.slLowThetaPu > (cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  486. {
  487. switchhall_stOut.slLowThetaPu = cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  488. }
  489. else
  490. {
  491. //do noting
  492. }
  493. break;
  494. case 3:
  495. if (switchhall_stOut.uwSectorNumPre == 2)
  496. {
  497. switchhall_stOut.swSpdCWorCCW = 1;
  498. switchhall_stOut.slLowThetaPu = cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  499. }
  500. else if (switchhall_stOut.uwSectorNumPre == 1)
  501. {
  502. switchhall_stOut.swSpdCWorCCW = -1;
  503. switchhall_stOut.slLowThetaPu = cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  504. }
  505. else
  506. {
  507. //do noting
  508. }
  509. switchhall_stOut.slInitThetaPu = cof_sl360DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  510. if ((switchhall_stOut.slLowThetaPu > (cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu)) && (switchhall_stOut.slLowThetaPu < (cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu)))
  511. {
  512. if (switchhall_stOut.swSpdCWorCCW == 1)
  513. {
  514. switchhall_stOut.slLowThetaPu = cof_sl30DegreePu + switchhall_stOut.swLowThetaOffsetPu ;
  515. }
  516. else if (switchhall_stOut.swSpdCWorCCW == -1)
  517. {
  518. switchhall_stOut.slLowThetaPu = cof_sl330DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  519. }
  520. else
  521. {
  522. //do noting
  523. }
  524. }
  525. break;
  526. case 4:
  527. if (switchhall_stOut.uwSectorNumPre == 5)
  528. {
  529. switchhall_stOut.swSpdCWorCCW = 1;
  530. switchhall_stOut.slLowThetaPu = cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  531. }
  532. else if (switchhall_stOut.uwSectorNumPre == 6)
  533. {
  534. switchhall_stOut.swSpdCWorCCW = -1;
  535. switchhall_stOut.slLowThetaPu = cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  536. }
  537. else
  538. {
  539. //do noting
  540. }
  541. switchhall_stOut.slInitThetaPu = cof_sl180DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  542. if (switchhall_stOut.slLowThetaPu < (cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  543. {
  544. switchhall_stOut.slLowThetaPu = cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  545. }
  546. else if (switchhall_stOut.slLowThetaPu > (cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  547. {
  548. switchhall_stOut.slLowThetaPu = cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  549. }
  550. else
  551. {
  552. //do noting
  553. }
  554. break;
  555. case 5:
  556. if (switchhall_stOut.uwSectorNumPre == 1)
  557. {
  558. switchhall_stOut.swSpdCWorCCW = 1;
  559. switchhall_stOut.slLowThetaPu = cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  560. }
  561. else if (switchhall_stOut.uwSectorNumPre == 4)
  562. {
  563. switchhall_stOut.swSpdCWorCCW = -1;
  564. switchhall_stOut.slLowThetaPu = cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  565. }
  566. else
  567. {
  568. //do noting
  569. }
  570. switchhall_stOut.slInitThetaPu = cof_sl120DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  571. if (switchhall_stOut.slLowThetaPu < (cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  572. {
  573. switchhall_stOut.slLowThetaPu = cof_sl90DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  574. }
  575. else if (switchhall_stOut.slLowThetaPu > (cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  576. {
  577. switchhall_stOut.slLowThetaPu = cof_sl150DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  578. }
  579. else
  580. {
  581. //do noting
  582. }
  583. break;
  584. case 6:
  585. if (switchhall_stOut.uwSectorNumPre == 4)
  586. {
  587. switchhall_stOut.swSpdCWorCCW = 1;
  588. switchhall_stOut.slLowThetaPu = cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  589. }
  590. else if (switchhall_stOut.uwSectorNumPre == 2)
  591. {
  592. switchhall_stOut.swSpdCWorCCW = -1;
  593. switchhall_stOut.slLowThetaPu = cof_sl270DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  594. }
  595. else
  596. {
  597. //do noting
  598. }
  599. switchhall_stOut.slInitThetaPu = cof_sl240DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  600. if (switchhall_stOut.slLowThetaPu < (cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  601. {
  602. switchhall_stOut.slLowThetaPu = cof_sl210DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  603. }
  604. else if (switchhall_stOut.slLowThetaPu > (cof_sl270DegreePu + switchhall_stOut.swLowThetaOffsetPu))
  605. {
  606. switchhall_stOut.slLowThetaPu = cof_sl270DegreePu + switchhall_stOut.swLowThetaOffsetPu;
  607. }
  608. else
  609. {
  610. //do noting
  611. }
  612. break;
  613. default:
  614. switchhall_stOut.swSpdCWorCCW = 0;
  615. switchhall_stOut.swLowSpdPu = 0;
  616. break;
  617. }
  618. // /*!< Zero Correction 3*/
  619. // switch (switchhall_stOut.uwSectorNum)
  620. // {
  621. // case 1:
  622. // if (switchhall_stOut.uwSectorNumPre == 3)
  623. // {
  624. // switchhall_stOut.swSpdCWorCCW = 1;
  625. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  626. // }
  627. // else if (switchhall_stOut.uwSectorNumPre == 5)
  628. // {
  629. // switchhall_stOut.swSpdCWorCCW = -1;
  630. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  631. // }
  632. //
  633. // switchhall_stOut.slInitThetaPu = cof_sl60DegreePu;
  634. //
  635. // if (switchhall_stOut.slLowThetaPu < cof_sl30DegreePu)
  636. // {
  637. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  638. // }
  639. // else if (switchhall_stOut.slLowThetaPu > cof_sl90DegreePu )
  640. // {
  641. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  642. // }
  643. // else
  644. // {}
  645. // break;
  646. // case 2:
  647. // if (switchhall_stOut.uwSectorNumPre == 6)
  648. // {
  649. // switchhall_stOut.swSpdCWorCCW = 1;
  650. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  651. // }
  652. // else if (switchhall_stOut.uwSectorNumPre == 3)
  653. // {
  654. // switchhall_stOut.swSpdCWorCCW = -1;
  655. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  656. // }
  657. //
  658. // switchhall_stOut.slInitThetaPu = cof_sl300DegreePu;
  659. // if (switchhall_stOut.slLowThetaPu < cof_sl270DegreePu)
  660. // {
  661. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  662. // }
  663. // else if (switchhall_stOut.slLowThetaPu > cof_sl330DegreePu)
  664. // {
  665. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  666. // }
  667. // else
  668. // {}
  669. // break;
  670. // case 3:
  671. // if (switchhall_stOut.uwSectorNumPre == 2)
  672. // {
  673. // switchhall_stOut.swSpdCWorCCW = 1;
  674. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  675. // }
  676. // else if (switchhall_stOut.uwSectorNumPre == 1)
  677. // {
  678. // switchhall_stOut.swSpdCWorCCW = -1;
  679. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  680. // }
  681. //
  682. // switchhall_stOut.slInitThetaPu = cof_sl360DegreePu;
  683. // if ((switchhall_stOut.slLowThetaPu > cof_sl30DegreePu) && (switchhall_stOut.slLowThetaPu < cof_sl330DegreePu ))
  684. // {
  685. // if (switchhall_stOut.swSpdCWorCCW == 1)
  686. // {
  687. // switchhall_stOut.slLowThetaPu = cof_sl30DegreePu;
  688. // }
  689. // else if (switchhall_stOut.swSpdCWorCCW == -1)
  690. // {
  691. // switchhall_stOut.slLowThetaPu = cof_sl330DegreePu;
  692. // }
  693. // }
  694. //
  695. // break;
  696. // case 4:
  697. // if (switchhall_stOut.uwSectorNumPre == 5)
  698. // {
  699. // switchhall_stOut.swSpdCWorCCW = 1;
  700. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  701. // }
  702. // else if (switchhall_stOut.uwSectorNumPre == 6)
  703. // {
  704. // switchhall_stOut.swSpdCWorCCW = -1;
  705. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  706. // }
  707. //
  708. // switchhall_stOut.slInitThetaPu = cof_sl180DegreePu ;
  709. // if (switchhall_stOut.slLowThetaPu < cof_sl150DegreePu)
  710. // {
  711. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  712. // }
  713. // else if (switchhall_stOut.slLowThetaPu > cof_sl210DegreePu)
  714. // {
  715. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  716. // }
  717. // else
  718. // {}
  719. // break;
  720. // case 5:
  721. // if (switchhall_stOut.uwSectorNumPre == 1)
  722. // {
  723. // switchhall_stOut.swSpdCWorCCW = 1;
  724. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  725. // }
  726. // else if (switchhall_stOut.uwSectorNumPre == 4)
  727. // {
  728. // switchhall_stOut.swSpdCWorCCW = -1;
  729. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  730. // }
  731. //
  732. // switchhall_stOut.slInitThetaPu = cof_sl120DegreePu;
  733. // if (switchhall_stOut.slLowThetaPu < cof_sl90DegreePu)
  734. // {
  735. // switchhall_stOut.slLowThetaPu = cof_sl90DegreePu;
  736. // }
  737. // else if (switchhall_stOut.slLowThetaPu > cof_sl150DegreePu)
  738. // {
  739. // switchhall_stOut.slLowThetaPu = cof_sl150DegreePu;
  740. // }
  741. // else
  742. // {}
  743. // break;
  744. // case 6:
  745. // if (switchhall_stOut.uwSectorNumPre == 4)
  746. // {
  747. // switchhall_stOut.swSpdCWorCCW = 1;
  748. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  749. // }
  750. // else if (switchhall_stOut.uwSectorNumPre == 2)
  751. // {
  752. // switchhall_stOut.swSpdCWorCCW = -1;
  753. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  754. // }
  755. //
  756. // switchhall_stOut.slInitThetaPu = cof_sl240DegreePu;
  757. // if (switchhall_stOut.slLowThetaPu < cof_sl210DegreePu )
  758. // {
  759. // switchhall_stOut.slLowThetaPu = cof_sl210DegreePu;
  760. // }
  761. // else if (switchhall_stOut.slLowThetaPu > cof_sl270DegreePu )
  762. // {
  763. // switchhall_stOut.slLowThetaPu = cof_sl270DegreePu;
  764. // }
  765. // else
  766. // {}
  767. // break;
  768. // default:
  769. // switchhall_stOut.swSpdCWorCCW = 0;
  770. // switchhall_stOut.swLowSpdPu = 0;
  771. // break;
  772. // }
  773. switchhall_stOut.uwSectorNumPre = switchhall_stOut.uwSectorNum;
  774. /* Theta Compensation */
  775. // if (switchhall_stOut.swSpdCWorCCW == -1)
  776. // {
  777. // switchhall_stOut.swLowThetaOffsetPu = switchhall_stOut.swLowThetaOffsetPu + HALL_THETEA_OFFSET_FANXIANG;
  778. // }
  779. //
  780. // /* Speed Calculation */
  781. // if (switchhall_stOut.swSpdCWorCCW == 1)
  782. // {
  783. // switchhall_stOut.swLowSpdPu = (SWORD)(HALL3_SPD_CAL_COEF / switchhall_stCap.slCapValueDelta);
  784. // }
  785. // else if (switchhall_stOut.swSpdCWorCCW == -1)
  786. // {
  787. // switchhall_stOut.swLowSpdPu = -(SWORD)(HALL3_SPD_CAL_COEF / switchhall_stCap.slCapValueDelta);
  788. // }
  789. // else
  790. // {
  791. // switchhall_stOut.swLowSpdPu = 0;
  792. // }
  793. if (switchhall_stCap.slCapValueDelta >= HALL3_SPD_CAP_LEVEL1) //lower than level1
  794. {
  795. // switchhall_stOut.swLowSpdPu = 0;
  796. // switchhall_stOut.uwLowThetaPu = switchhall_stOut.slInitThetaPu;
  797. // Hall_Theta_Temp = (UWORD)(switchhall_stOut.slInitThetaPu+switchhall_stOut.swLowThetaOffsetPu); // Q15
  798. Hall_Theta_Temp = switchhall_stOut.slInitThetaPu;
  799. if (Hall_Theta_Temp >= cof_sl360DegreePu)
  800. {
  801. Hall_Theta_Temp -= cof_sl360DegreePu;
  802. }
  803. else if (Hall_Theta_Temp < 0)
  804. {
  805. Hall_Theta_Temp += cof_sl360DegreePu;
  806. }
  807. else
  808. {
  809. //do noting
  810. }
  811. switchhall_stOut.uwLowThetaPu = (UWORD)Hall_Theta_Temp;
  812. }
  813. else if (switchhall_stCap.slCapValueDelta >= HALL3_SPD_CAP_LEVEL2) // lower than level2
  814. {
  815. /* Use fixed angle(middle of sector) at low speed*/
  816. if (switchhall_stCap.slCapValueDelta >= HALL3_SPD_CAP_STARTUP_LEVEL) // lower than Startup level
  817. {
  818. // switchhall_stOut.uwLowThetaPu = switchhall_stOut.slInitThetaPu;
  819. //Hall_Theta_Temp = (UWORD)(switchhall_stOut.slInitThetaPu+switchhall_stOut.swLowThetaOffsetPu); // Q15
  820. Hall_Theta_Temp = switchhall_stOut.slInitThetaPu;
  821. if (Hall_Theta_Temp >= cof_sl360DegreePu)
  822. {
  823. Hall_Theta_Temp -= cof_sl360DegreePu;
  824. }
  825. else if (Hall_Theta_Temp < 0)
  826. {
  827. Hall_Theta_Temp += cof_sl360DegreePu;
  828. }
  829. else
  830. {
  831. //do noting
  832. }
  833. switchhall_stOut.uwLowThetaPu = (UWORD)Hall_Theta_Temp;
  834. }
  835. else
  836. {
  837. /*Low Speed Theta */
  838. //Hall_Theta_Temp = (UWORD)(switchhall_stOut.slLowThetaPu+switchhall_stOut.swLowThetaOffsetPu); // Q15
  839. Hall_Theta_Temp = switchhall_stOut.slLowThetaPu;
  840. if (Hall_Theta_Temp >= cof_sl360DegreePu)
  841. {
  842. Hall_Theta_Temp -= cof_sl360DegreePu;
  843. }
  844. else if (Hall_Theta_Temp < 0)
  845. {
  846. Hall_Theta_Temp += cof_sl360DegreePu;
  847. }
  848. else
  849. {
  850. //do noting
  851. }
  852. switchhall_stOut.uwLowThetaPu = (UWORD)Hall_Theta_Temp;
  853. /*High Speed Theta */
  854. // if (switchhall_stOut.slHighThetaPu >= cof_sl360DegreePu)
  855. // {
  856. // switchhall_stOut.slHighThetaPu -= cof_sl360DegreePu;
  857. // }
  858. // else if (switchhall_stOut.slHighThetaPu < 0)
  859. // {
  860. // switchhall_stOut.slHighThetaPu += cof_sl360DegreePu;
  861. // }
  862. // switchhall_stOut.uwHighThetaPu = cof_sl360DegreePu;
  863. }
  864. if (switchhall_stOut.swSpdCWorCCW == 1)
  865. {
  866. switchhall_stOut.swLowSpdPu = (SWORD)(HALL3_SPD_CAL_COEF / switchhall_stCap.slCapValueDelta);
  867. switchhall_stOut.swHighSpdPu = (SWORD)(HALL1_SPD_CAL_COEF / switchhall_stCap.slHighCapValueDelta);
  868. }
  869. else if (switchhall_stOut.swSpdCWorCCW == -1)
  870. {
  871. switchhall_stOut.swLowSpdPu = -(SWORD)(HALL3_SPD_CAL_COEF / switchhall_stCap.slCapValueDelta);
  872. switchhall_stOut.swHighSpdPu = -(SWORD)(HALL1_SPD_CAL_COEF / switchhall_stCap.slHighCapValueDelta);
  873. }
  874. else
  875. {
  876. // switchhall_stOut.swLowSpdPu = 0;
  877. }
  878. }
  879. else
  880. {
  881. // High speed: use single hall
  882. }
  883. /* Low Speed LPF */
  884. switchhall_pvt_slLowSpdLpfPu =
  885. (SLONG)0x00CB * (switchhall_stOut.swLowSpdPu - switchhall_stOut.swLowSpdLpfPu) + switchhall_pvt_slLowSpdLpfPu; //50Hz
  886. switchhall_stOut.swLowSpdLpfPu = (SWORD)(switchhall_pvt_slLowSpdLpfPu >> 15);
  887. /* High Speed LPF */
  888. // switchhall_pvt_slHighSpdLpfPu =
  889. // (SLONG)0x00CB * (switchhall_stOut.swHighSpdPu - switchhall_stOut.swHighSpdLpfPu) + switchhall_pvt_slHighSpdLpfPu;
  890. // switchhall_stOut.swHighSpdLpfPu = switchhall_pvt_slHighSpdLpfPu >> 15;
  891. }
  892. #endif
  893. /*************************************************************************
  894. End of this File (EOF)!
  895. Do not put anything after this part!
  896. *************************************************************************/