stm32f10x_svpwm_3shunt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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(p_MC_ErrorCode->ERROR_Bit.Fault_Circuit == 0)
  86. {
  87. if((HAL_GetTick() - PeriodTimeCnt) > 2000)
  88. {
  89. for(bIndex=0; bIndex < 10; bIndex++)
  90. {
  91. ul_current_offset_sum = ADC1_Result[ADC1_RANK_CURRENT] + ul_current_offset_sum;
  92. }
  93. uw_current_offset = ul_current_offset_sum / 10;
  94. if((uw_current_offset < 1000) || (uw_current_offset > 3000))
  95. {
  96. p_MC_ErrorCode->ERROR_Bit.Fault_Circuit = 1;
  97. //记录故障日志
  98. MC_ErrorLogSaveInfo.NotesInfo1 = 7;
  99. MC_ErrorLogSaveInfo.NotesInfo2 = uw_current_offset;
  100. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  101. IsErrorLogSaveInfoUpdateFlag = TRUE;
  102. //存储故障次数
  103. MC_RunLog2.Circuit_FaultCnt++;
  104. RunLogSaveIndex = 2;
  105. }
  106. PeriodTimeCnt = HAL_GetTick();
  107. }
  108. }
  109. }
  110. LeaveTime = HAL_GetTick();
  111. }
  112. //3相占空比设置
  113. void SVPWM_3ShuntCalcDutyCycles (Volt_Components Stat_Volt_Input)
  114. {
  115. int32_t wX, wY, wZ, wUAlpha, wUBeta;
  116. uint16_t hDeltaDuty;
  117. wUAlpha = Stat_Volt_Input.qV_Component1 * T_SQRT3 ;
  118. wUBeta = -(Stat_Volt_Input.qV_Component2 * T);
  119. wX = wUBeta;
  120. wY = (wUBeta + wUAlpha) >> 1;
  121. wZ = (wUBeta - wUAlpha) >> 1;
  122. // Sector calculation from wX, wY, wZ
  123. if (wY<0)
  124. {
  125. if (wZ<0)
  126. {
  127. bSector = SECTOR_5;
  128. }
  129. else // wZ >= 0
  130. if (wX<=0)
  131. {
  132. bSector = SECTOR_4;
  133. }
  134. else // wX > 0
  135. {
  136. bSector = SECTOR_3;
  137. }
  138. }
  139. else // wY > 0
  140. {
  141. if (wZ>=0)
  142. {
  143. bSector = SECTOR_2;
  144. }
  145. else // wZ < 0
  146. if (wX<=0)
  147. {
  148. bSector = SECTOR_6;
  149. }
  150. else // wX > 0
  151. {
  152. bSector = SECTOR_1;
  153. }
  154. }
  155. PWM4Direction=0;
  156. switch(bSector)
  157. {
  158. case SECTOR_1:
  159. hTimePhase.hTimePhA = (T >> 3) + ((((T + wX) - wZ) >> 1) >> 17);
  160. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  161. hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
  162. // hTimePhD = PWM_PERIOD - 1;
  163. // ADC Syncronization setting value
  164. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
  165. {
  166. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  167. }
  168. else
  169. {
  170. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhB);
  171. // Definition of crossing point
  172. if (hDeltaDuty > TW_DT_TR_TS)
  173. {
  174. hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
  175. }
  176. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) >TW_DT_TN_TS_HALF)
  177. {
  178. hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
  179. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  180. {
  181. // Trigger of ADC at Falling Edge PWM4
  182. // OCR update
  183. //Set Polarity of CC4 Low
  184. PWM4Direction=1;
  185. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  186. }
  187. }
  188. // else
  189. // {
  190. // while(1);
  191. // }
  192. }
  193. break;
  194. case SECTOR_2:
  195. hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
  196. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  197. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  198. // hTimePhD = PWM_PERIOD - 1;
  199. // ADC Syncronization setting value
  200. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
  201. {
  202. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  203. }
  204. else
  205. {
  206. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhA);
  207. // Definition of crossing point
  208. if (hDeltaDuty > TW_DT_TR_TS)
  209. {
  210. hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
  211. }
  212. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
  213. {
  214. hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
  215. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  216. {
  217. PWM4Direction=1;
  218. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  219. }
  220. }
  221. // else
  222. // {
  223. // while(1);
  224. // }
  225. }
  226. break;
  227. case SECTOR_3:
  228. hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
  229. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  230. hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
  231. // hTimePhD = PWM_PERIOD - 1;
  232. // ADC Syncronization setting value
  233. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
  234. {
  235. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  236. }
  237. else
  238. {
  239. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhC);
  240. // Definition of crossing point
  241. if (hDeltaDuty > TW_DT_TR_TS)
  242. {
  243. hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
  244. }
  245. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
  246. {
  247. hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
  248. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  249. {
  250. // Trigger of ADC at Falling Edge PWM4
  251. // OCR update
  252. //Set Polarity of CC4 Low
  253. PWM4Direction=1;
  254. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  255. }
  256. }
  257. // else
  258. // {
  259. // while(1);
  260. // }
  261. }
  262. break;
  263. case SECTOR_4:
  264. hTimePhase.hTimePhA = (T>> 3) + ((((T + wX) - wZ) >> 1) >> 17);
  265. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  266. hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
  267. // hTimePhD = PWM_PERIOD - 1;
  268. // ADC Syncronization setting value
  269. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
  270. {
  271. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  272. }
  273. else
  274. {
  275. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhB);
  276. // Definition of crossing point
  277. if (hDeltaDuty > TW_DT_TR_TS)
  278. {
  279. hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
  280. }
  281. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
  282. {
  283. hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
  284. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  285. {
  286. // Trigger of ADC at Falling Edge PWM4
  287. // OCR update
  288. //Set Polarity of CC4 Low
  289. PWM4Direction=1;
  290. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  291. }
  292. }
  293. // else
  294. // {
  295. // while(1);
  296. // }
  297. }
  298. break;
  299. case SECTOR_5:
  300. hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
  301. hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
  302. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  303. // hTimePhD = PWM_PERIOD - 1;
  304. // ADC Syncronization setting value
  305. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
  306. {
  307. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  308. }
  309. else
  310. {
  311. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhA);
  312. // Definition of crossing point
  313. if (hDeltaDuty > TW_DT_TR_TS)
  314. {
  315. hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
  316. }
  317. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
  318. {
  319. hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
  320. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  321. {
  322. // Trigger of ADC at Falling Edge PWM4
  323. // OCR update
  324. //Set Polarity of CC4 Low
  325. PWM4Direction=1;
  326. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  327. }
  328. }
  329. // else
  330. // {
  331. // while(1);
  332. // }
  333. }
  334. break;
  335. case SECTOR_6:
  336. hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
  337. hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
  338. hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
  339. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  340. // ADC Syncronization setting value
  341. if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
  342. {
  343. hTimePhase.hTimePhD = PWM_PERIOD - 1;
  344. }
  345. else
  346. {
  347. hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhC);
  348. // Definition of crossing point
  349. if (hDeltaDuty > TW_DT_TR_TS)
  350. {
  351. hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
  352. }
  353. else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_DT_TN_TS_HALF)
  354. {
  355. hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
  356. if (hTimePhase.hTimePhD >= PWM_PERIOD)
  357. {
  358. // Trigger of ADC at Falling Edge PWM4
  359. // OCR update
  360. //Set Polarity of CC4 Low
  361. PWM4Direction=1;
  362. hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
  363. }
  364. }
  365. // else
  366. // {
  367. // while(1);
  368. // }
  369. }
  370. break;
  371. default:
  372. break;
  373. }
  374. if (PWM4Direction == 0)
  375. {
  376. //Set Polarity of CC4 High
  377. Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
  378. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  379. }
  380. else
  381. {
  382. //Set Polarity of CC4 Low
  383. Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  384. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
  385. }
  386. // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
  387. hTimePhase.hTimePhD=PWM_PERIOD-20;
  388. //Load compare registers values
  389. Set_Pwm_Chanle1_Compare(hTimePhase.hTimePhA);
  390. Set_Pwm_Chanle2_Compare(hTimePhase.hTimePhB);
  391. Set_Pwm_Chanle3_Compare(hTimePhase.hTimePhC);
  392. Set_Pwm_Chanle4_Compare(hTimePhase.hTimePhD);
  393. }
  394. //读取三相电流
  395. Curr_Components SVPWM_3ShuntGetPhaseCurrentValues(void)
  396. {
  397. Curr_Components Local_Stator_Currents = {0, 0};
  398. int16_t wAux;
  399. switch (bSector)
  400. {
  401. case 4:
  402. case 5: //Current on Phase C not accessible
  403. wAux =-(ADC_3ShuntCurrent.uw_phase_a);
  404. //Saturation of Ia
  405. if (wAux < -32768)
  406. {
  407. Local_Stator_Currents.qI_Component1= -32768;
  408. }
  409. else if (wAux > 32767)
  410. {
  411. Local_Stator_Currents.qI_Component1= 32767;
  412. }
  413. else
  414. {
  415. Local_Stator_Currents.qI_Component1= wAux;
  416. }
  417. wAux =-(ADC_3ShuntCurrent.uw_phase_b);
  418. // Saturation of Ib
  419. if (wAux < -32768)
  420. {
  421. Local_Stator_Currents.qI_Component2= -32768;
  422. }
  423. else if (wAux > 32767)
  424. {
  425. Local_Stator_Currents.qI_Component2= 32767;
  426. }
  427. else
  428. {
  429. Local_Stator_Currents.qI_Component2= wAux;
  430. }
  431. break;
  432. case 6:
  433. case 1:
  434. wAux =-(ADC_3ShuntCurrent.uw_phase_b);
  435. //Saturation of Ib
  436. if (wAux < -32768)
  437. {
  438. Local_Stator_Currents.qI_Component2= -32768;
  439. }
  440. else if (wAux > 32767)
  441. {
  442. Local_Stator_Currents.qI_Component2= 32767;
  443. }
  444. else
  445. {
  446. Local_Stator_Currents.qI_Component2= wAux;
  447. }
  448. //Ia = -Ib - Ic;
  449. wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component2;
  450. if (wAux> 32767)
  451. {
  452. Local_Stator_Currents.qI_Component1 = 32767;
  453. }
  454. else if (wAux <-32768)
  455. {
  456. Local_Stator_Currents.qI_Component1 = -32768;
  457. }
  458. else
  459. {
  460. Local_Stator_Currents.qI_Component1 = wAux;
  461. }
  462. break;
  463. case 2:
  464. case 3:
  465. // Current on Phase B not accessible
  466. wAux =-(ADC_3ShuntCurrent.uw_phase_a);
  467. // Saturation of Ia
  468. if (wAux> 32767)
  469. {
  470. Local_Stator_Currents.qI_Component1=32767;
  471. }
  472. else if (wAux <-32768)
  473. {
  474. Local_Stator_Currents.qI_Component1 = -32768;
  475. }
  476. else
  477. {
  478. Local_Stator_Currents.qI_Component1 = wAux;
  479. }
  480. wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component1; //Ib = -Ia - Ic;
  481. //Saturation of Ib
  482. if (wAux < -32768)
  483. {
  484. Local_Stator_Currents.qI_Component2= -32768;
  485. }
  486. else if (wAux > 32767)
  487. {
  488. Local_Stator_Currents.qI_Component2= 32767;
  489. }
  490. else
  491. {
  492. Local_Stator_Currents.qI_Component2= wAux;
  493. }
  494. break;
  495. default:
  496. break;
  497. }
  498. Local_Stator_Currents.qI_Component1 = Local_Stator_Currents.qI_Component1 >> 4;
  499. Local_Stator_Currents.qI_Component2 = Local_Stator_Currents.qI_Component2 >> 4;
  500. return(Local_Stator_Currents);
  501. }
  502. //读母线电流
  503. int16_t GetCurrentValues(void)
  504. {
  505. int16_t Result;
  506. Result = ADC1_Result[ADC1_RANK_CURRENT] - (int16_t)uw_current_offset;
  507. return Result;
  508. }