stm32f10x_svpwm_3shunt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. #include "stm32f10x_svpwm_3shunt.h"
  2. #include "MC_const.h"
  3. #include "adc.h"
  4. #include "pwm_driver.h"
  5. #include "log_save.h"
  6. //全局变量定义
  7. uint16_t bSector;
  8. uint8_t PWM4Direction=0;
  9. typedef struct
  10. {
  11. uint16_t hTimePhA;
  12. uint16_t hTimePhB;
  13. uint16_t hTimePhC;
  14. uint16_t hTimePhD;
  15. }hTimePhase_Struct_t;
  16. hTimePhase_Struct_t hTimePhase;
  17. /**************************全局函数定义*************************/
  18. //3相电流校零
  19. void SVPWM_3ShuntCurrentReadingCalibration(MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  20. {
  21. uint16_t bIndex;
  22. uint32_t ul_phase_a_offset_sum = 0;
  23. uint32_t ul_phase_b_offset_sum = 0;
  24. uint32_t ul_phase_c_offset_sum = 0;
  25. Disable_Pwm_Output();
  26. for(bIndex=0; bIndex < 10; bIndex++)
  27. {
  28. //wait the ADC1 JEOC pending flag */
  29. do
  30. {
  31. ;
  32. }while(SET != ADC2_ConvCpmplete_Flag);
  33. ///Clear the ADC1 JEOC pending flag */
  34. ADC2_ConvCpmplete_Flag = RESET;
  35. ul_phase_a_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_A] + ul_phase_a_offset_sum;
  36. ul_phase_b_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_B] + ul_phase_b_offset_sum;
  37. ul_phase_c_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_C] + ul_phase_c_offset_sum;
  38. HAL_TIM_PWM_Start(&PWM_TIMER, TIM_CHANNEL_4);
  39. }
  40. ADC_3ShuntCurrent_OffSet.uw_phase_a_offset = ul_phase_a_offset_sum / 10;
  41. ADC_3ShuntCurrent_OffSet.uw_phase_b_offset = ul_phase_b_offset_sum / 10;
  42. ADC_3ShuntCurrent_OffSet.uw_phase_c_offset = ul_phase_c_offset_sum / 10;
  43. //判断零点值是否在正常范围内
  44. if(((ADC_3ShuntCurrent_OffSet.uw_phase_a_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_a_offset > 40000)) ||
  45. ((ADC_3ShuntCurrent_OffSet.uw_phase_b_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_b_offset > 40000)) ||
  46. ((ADC_3ShuntCurrent_OffSet.uw_phase_c_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_c_offset > 40000))
  47. )
  48. {
  49. p_MC_ErrorCode->ERROR_Bit.Fault_Circuit = 1;
  50. //记录故障日志
  51. MC_ErrorLogSaveInfo.NotesInfo1 = 6;
  52. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  53. IsErrorLogSaveInfoUpdateFlag = TRUE;
  54. //存储故障次数
  55. MC_RunLog2.Circuit_FaultCnt++;
  56. RunLogSaveIndex = 2;
  57. }
  58. Disable_Pwm_Output();
  59. }
  60. //母线电流校零
  61. void CurrentReadingCalibration(MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  62. {
  63. static TrueOrFalse_Flag_Struct_t IsFirstEnterFlag = TRUE;
  64. static uint32_t PeriodTimeCnt = 0;
  65. static uint32_t LeaveTime = 0;
  66. uint32_t ul_current_offset_sum = 0;
  67. uint16_t bIndex;
  68. if(IsFirstEnterFlag == TRUE)
  69. {
  70. for(bIndex=0; bIndex < 10; bIndex++)
  71. {
  72. ul_current_offset_sum = ADC1_Result[ADC1_RANK_CURRENT] + ul_current_offset_sum;
  73. }
  74. uw_current_offset = ul_current_offset_sum / 10;
  75. PeriodTimeCnt = HAL_GetTick();
  76. LeaveTime = HAL_GetTick();
  77. IsFirstEnterFlag = FALSE;
  78. }
  79. else
  80. {
  81. if((HAL_GetTick() - LeaveTime) > 500)
  82. {
  83. PeriodTimeCnt = HAL_GetTick();
  84. }
  85. if((HAL_GetTick() - PeriodTimeCnt) > 2000)
  86. {
  87. for(bIndex=0; bIndex < 10; bIndex++)
  88. {
  89. ul_current_offset_sum = ADC1_Result[ADC1_RANK_CURRENT] + ul_current_offset_sum;
  90. }
  91. uw_current_offset = ul_current_offset_sum / 10;
  92. if((uw_current_offset < 1000) || (uw_current_offset > 3000))
  93. {
  94. p_MC_ErrorCode->ERROR_Bit.Fault_Circuit = 1;
  95. //记录故障日志
  96. MC_ErrorLogSaveInfo.NotesInfo1 = 7;
  97. MC_ErrorLogSaveInfo.NotesInfo2 = uw_current_offset;
  98. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  99. IsErrorLogSaveInfoUpdateFlag = TRUE;
  100. //存储故障次数
  101. MC_RunLog2.Circuit_FaultCnt++;
  102. RunLogSaveIndex = 2;
  103. }
  104. PeriodTimeCnt = HAL_GetTick();
  105. }
  106. }
  107. LeaveTime = HAL_GetTick();
  108. }
  109. //3相占空比设置
  110. void SVPWM_3ShuntCalcDutyCycles (Volt_Components Stat_Volt_Input)
  111. {
  112. int32_t wX, wY, wZ, wUAlpha, wUBeta;
  113. uint16_t hDeltaDuty;
  114. wUAlpha = Stat_Volt_Input.qV_Component1 * T_SQRT3 ;
  115. wUBeta = -(Stat_Volt_Input.qV_Component2 * T);
  116. wX = wUBeta;
  117. wY = (wUBeta + wUAlpha) >> 1;
  118. wZ = (wUBeta - wUAlpha) >> 1;
  119. // Sector calculation from wX, wY, wZ
  120. if (wY<0)
  121. {
  122. if (wZ<0)
  123. {
  124. bSector = SECTOR_5;
  125. }
  126. else // wZ >= 0
  127. if (wX<=0)
  128. {
  129. bSector = SECTOR_4;
  130. }
  131. else // wX > 0
  132. {
  133. bSector = SECTOR_3;
  134. }
  135. }
  136. else // wY > 0
  137. {
  138. if (wZ>=0)
  139. {
  140. bSector = SECTOR_2;
  141. }
  142. else // wZ < 0
  143. if (wX<=0)
  144. {
  145. bSector = SECTOR_6;
  146. }
  147. else // wX > 0
  148. {
  149. bSector = SECTOR_1;
  150. }
  151. }
  152. PWM4Direction=0;
  153. switch(bSector)
  154. {
  155. case SECTOR_1:
  156. hTimePhase.hTimePhA = (T >> 3) + ((((T + wX) - wZ) >> 1) >> 17);
  157. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  158. hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
  159. // hTimePhD = PWM_PERIOD - 1;
  160. // ADC Syncronization setting value
  161. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
  162. {
  163. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  164. }
  165. else
  166. {
  167. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhB);
  168. // Definition of crossing point
  169. if (hDeltaDuty > TW_DT_TR_TS)
  170. {
  171. hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
  172. }
  173. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) >TW_DT_TN_TS_HALF)
  174. {
  175. hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
  176. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  177. {
  178. // Trigger of ADC at Falling Edge PWM4
  179. // OCR update
  180. //Set Polarity of CC4 Low
  181. PWM4Direction=1;
  182. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  183. }
  184. }
  185. // else
  186. // {
  187. // while(1);
  188. // }
  189. }
  190. break;
  191. case SECTOR_2:
  192. hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
  193. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  194. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  195. // hTimePhD = PWM_PERIOD - 1;
  196. // ADC Syncronization setting value
  197. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
  198. {
  199. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  200. }
  201. else
  202. {
  203. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhA);
  204. // Definition of crossing point
  205. if (hDeltaDuty > TW_DT_TR_TS)
  206. {
  207. hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
  208. }
  209. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
  210. {
  211. hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
  212. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  213. {
  214. PWM4Direction=1;
  215. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  216. }
  217. }
  218. // else
  219. // {
  220. // while(1);
  221. // }
  222. }
  223. break;
  224. case SECTOR_3:
  225. hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
  226. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  227. hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
  228. // hTimePhD = PWM_PERIOD - 1;
  229. // ADC Syncronization setting value
  230. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
  231. {
  232. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  233. }
  234. else
  235. {
  236. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhC);
  237. // Definition of crossing point
  238. if (hDeltaDuty > TW_DT_TR_TS)
  239. {
  240. hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
  241. }
  242. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
  243. {
  244. hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
  245. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  246. {
  247. // Trigger of ADC at Falling Edge PWM4
  248. // OCR update
  249. //Set Polarity of CC4 Low
  250. PWM4Direction=1;
  251. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  252. }
  253. }
  254. // else
  255. // {
  256. // while(1);
  257. // }
  258. }
  259. break;
  260. case SECTOR_4:
  261. hTimePhase.hTimePhA = (T>> 3) + ((((T + wX) - wZ) >> 1) >> 17);
  262. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  263. hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
  264. // hTimePhD = PWM_PERIOD - 1;
  265. // ADC Syncronization setting value
  266. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
  267. {
  268. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  269. }
  270. else
  271. {
  272. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhB);
  273. // Definition of crossing point
  274. if (hDeltaDuty > TW_DT_TR_TS)
  275. {
  276. hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
  277. }
  278. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
  279. {
  280. hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
  281. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  282. {
  283. // Trigger of ADC at Falling Edge PWM4
  284. // OCR update
  285. //Set Polarity of CC4 Low
  286. PWM4Direction=1;
  287. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  288. }
  289. }
  290. // else
  291. // {
  292. // while(1);
  293. // }
  294. }
  295. break;
  296. case SECTOR_5:
  297. hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
  298. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  299. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  300. // hTimePhD = PWM_PERIOD - 1;
  301. // ADC Syncronization setting value
  302. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
  303. {
  304. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  305. }
  306. else
  307. {
  308. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhA);
  309. // Definition of crossing point
  310. if (hDeltaDuty > TW_DT_TR_TS)
  311. {
  312. hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
  313. }
  314. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
  315. {
  316. hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
  317. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  318. {
  319. // Trigger of ADC at Falling Edge PWM4
  320. // OCR update
  321. //Set Polarity of CC4 Low
  322. PWM4Direction=1;
  323. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  324. }
  325. }
  326. // else
  327. // {
  328. // while(1);
  329. // }
  330. }
  331. break;
  332. case SECTOR_6:
  333. hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
  334. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  335. hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
  336. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  337. // ADC Syncronization setting value
  338. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
  339. {
  340. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  341. }
  342. else
  343. {
  344. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhC);
  345. // Definition of crossing point
  346. if (hDeltaDuty > TW_DT_TR_TS)
  347. {
  348. hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
  349. }
  350. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_DT_TN_TS_HALF)
  351. {
  352. hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
  353. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  354. {
  355. // Trigger of ADC at Falling Edge PWM4
  356. // OCR update
  357. //Set Polarity of CC4 Low
  358. PWM4Direction=1;
  359. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  360. }
  361. }
  362. // else
  363. // {
  364. // while(1);
  365. // }
  366. }
  367. break;
  368. default:
  369. break;
  370. }
  371. if (PWM4Direction == 0)
  372. {
  373. //Set Polarity of CC4 High
  374. Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
  375. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  376. }
  377. else
  378. {
  379. //Set Polarity of CC4 Low
  380. Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  381. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
  382. }
  383. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  384. hTimePhase.hTimePhD=PWM_PERIOD-20;
  385. //Load compare registers values
  386. Set_Pwm_Chanle1_Compare(hTimePhase.hTimePhA);
  387. Set_Pwm_Chanle2_Compare(hTimePhase.hTimePhB);
  388. Set_Pwm_Chanle3_Compare(hTimePhase.hTimePhC);
  389. Set_Pwm_Chanle4_Compare(hTimePhase.hTimePhD);
  390. }
  391. //读取三相电流
  392. Curr_Components SVPWM_3ShuntGetPhaseCurrentValues(void)
  393. {
  394. Curr_Components Local_Stator_Currents = {0, 0};
  395. int16_t wAux;
  396. switch (bSector)
  397. {
  398. case 4:
  399. case 5: //Current on Phase C not accessible
  400. wAux =-(ADC_3ShuntCurrent.uw_phase_a);
  401. //Saturation of Ia
  402. if (wAux < -32768)
  403. {
  404. Local_Stator_Currents.qI_Component1= -32768;
  405. }
  406. else if (wAux > 32767)
  407. {
  408. Local_Stator_Currents.qI_Component1= 32767;
  409. }
  410. else
  411. {
  412. Local_Stator_Currents.qI_Component1= wAux;
  413. }
  414. wAux =-(ADC_3ShuntCurrent.uw_phase_b);
  415. // Saturation of Ib
  416. if (wAux < -32768)
  417. {
  418. Local_Stator_Currents.qI_Component2= -32768;
  419. }
  420. else if (wAux > 32767)
  421. {
  422. Local_Stator_Currents.qI_Component2= 32767;
  423. }
  424. else
  425. {
  426. Local_Stator_Currents.qI_Component2= wAux;
  427. }
  428. break;
  429. case 6:
  430. case 1:
  431. wAux =-(ADC_3ShuntCurrent.uw_phase_b);
  432. //Saturation of Ib
  433. if (wAux < -32768)
  434. {
  435. Local_Stator_Currents.qI_Component2= -32768;
  436. }
  437. else if (wAux > 32767)
  438. {
  439. Local_Stator_Currents.qI_Component2= 32767;
  440. }
  441. else
  442. {
  443. Local_Stator_Currents.qI_Component2= wAux;
  444. }
  445. //Ia = -Ib - Ic;
  446. wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component2;
  447. if (wAux> 32767)
  448. {
  449. Local_Stator_Currents.qI_Component1 = 32767;
  450. }
  451. else if (wAux <-32768)
  452. {
  453. Local_Stator_Currents.qI_Component1 = -32768;
  454. }
  455. else
  456. {
  457. Local_Stator_Currents.qI_Component1 = wAux;
  458. }
  459. break;
  460. case 2:
  461. case 3:
  462. // Current on Phase B not accessible
  463. wAux =-(ADC_3ShuntCurrent.uw_phase_a);
  464. // Saturation of Ia
  465. if (wAux> 32767)
  466. {
  467. Local_Stator_Currents.qI_Component1=32767;
  468. }
  469. else if (wAux <-32768)
  470. {
  471. Local_Stator_Currents.qI_Component1 = -32768;
  472. }
  473. else
  474. {
  475. Local_Stator_Currents.qI_Component1 = wAux;
  476. }
  477. wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component1; //Ib = -Ia - Ic;
  478. //Saturation of Ib
  479. if (wAux < -32768)
  480. {
  481. Local_Stator_Currents.qI_Component2= -32768;
  482. }
  483. else if (wAux > 32767)
  484. {
  485. Local_Stator_Currents.qI_Component2= 32767;
  486. }
  487. else
  488. {
  489. Local_Stator_Currents.qI_Component2= wAux;
  490. }
  491. break;
  492. default:
  493. break;
  494. }
  495. Local_Stator_Currents.qI_Component1 = Local_Stator_Currents.qI_Component1 >> 4;
  496. Local_Stator_Currents.qI_Component2 = Local_Stator_Currents.qI_Component2 >> 4;
  497. return(Local_Stator_Currents);
  498. }
  499. //读母线电流
  500. int16_t GetCurrentValues(void)
  501. {
  502. int16_t Result;
  503. Result = ADC1_Result[ADC1_RANK_CURRENT] - (int16_t)uw_current_offset;
  504. return Result;
  505. }