switchhall.c 33 KB

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