switchhall.c 32 KB

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