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