123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- #include "stm32f10x_svpwm_3shunt.h"
- #include "MC_const.h"
- #include "adc.h"
- #include "pwm_driver.h"
- #include "log_save.h"
- //全局变量定义
- uint16_t bSector;
- uint8_t PWM4Direction=0;
- typedef struct
- {
- uint16_t hTimePhA;
- uint16_t hTimePhB;
- uint16_t hTimePhC;
- uint16_t hTimePhD;
- }hTimePhase_Struct_t;
- hTimePhase_Struct_t hTimePhase;
- /**************************全局函数定义*************************/
- //3相电流校零
- void SVPWM_3ShuntCurrentReadingCalibration(MC_ErrorCode_Struct_t* p_MC_ErrorCode)
- {
- uint16_t bIndex;
- uint32_t ul_phase_a_offset_sum = 0;
- uint32_t ul_phase_b_offset_sum = 0;
- uint32_t ul_phase_c_offset_sum = 0;
-
- Disable_Pwm_Output();
-
- for(bIndex=0; bIndex < 10; bIndex++)
- {
- //wait the ADC1 JEOC pending flag */
- do
- {
- ;
- }while(SET != ADC2_ConvCpmplete_Flag);
- ///Clear the ADC1 JEOC pending flag */
- ADC2_ConvCpmplete_Flag = RESET;
- ul_phase_a_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_A] + ul_phase_a_offset_sum;
- ul_phase_b_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_B] + ul_phase_b_offset_sum;
- ul_phase_c_offset_sum = ADC2_Result[ADC2_RANK_CURRENT_C] + ul_phase_c_offset_sum;
- HAL_TIM_PWM_Start(&PWM_TIMER, TIM_CHANNEL_4);
- }
- ADC_3ShuntCurrent_OffSet.uw_phase_a_offset = ul_phase_a_offset_sum / 10;
- ADC_3ShuntCurrent_OffSet.uw_phase_b_offset = ul_phase_b_offset_sum / 10;
- ADC_3ShuntCurrent_OffSet.uw_phase_c_offset = ul_phase_c_offset_sum / 10;
-
- //判断零点值是否在正常范围内
- if(((ADC_3ShuntCurrent_OffSet.uw_phase_a_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_a_offset > 40000)) ||
- ((ADC_3ShuntCurrent_OffSet.uw_phase_b_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_b_offset > 40000)) ||
- ((ADC_3ShuntCurrent_OffSet.uw_phase_c_offset < 25000) || (ADC_3ShuntCurrent_OffSet.uw_phase_c_offset > 40000))
- )
- {
- p_MC_ErrorCode->ERROR_Bit.Fault_Circuit = 1;
- //记录故障日志
- MC_ErrorLogSaveInfo.NotesInfo1 = 6;
- ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
- IsErrorLogSaveInfoUpdateFlag = TRUE;
- //存储故障次数
- MC_RunLog2.Circuit_FaultCnt++;
- RunLogSaveIndex = 2;
- }
-
- Disable_Pwm_Output();
- }
- //母线电流校零
- void CurrentReadingCalibration(MC_ErrorCode_Struct_t* p_MC_ErrorCode)
- {
- static TrueOrFalse_Flag_Struct_t IsFirstEnterFlag = TRUE;
- static uint32_t PeriodTimeCnt = 0;
- static uint32_t LeaveTime = 0;
- uint32_t ul_current_offset_sum = 0;
- uint16_t bIndex;
- if(IsFirstEnterFlag == TRUE)
- {
- for(bIndex=0; bIndex < 10; bIndex++)
- {
- ul_current_offset_sum = ADC1_Result[ADC1_RANK_CURRENT] + ul_current_offset_sum;
- }
- uw_current_offset = ul_current_offset_sum / 10;
- PeriodTimeCnt = HAL_GetTick();
- LeaveTime = HAL_GetTick();
- IsFirstEnterFlag = FALSE;
- }
- else
- {
- if((HAL_GetTick() - LeaveTime) > 500)
- {
- PeriodTimeCnt = HAL_GetTick();
- }
-
- if((HAL_GetTick() - PeriodTimeCnt) > 2000)
- {
- for(bIndex=0; bIndex < 10; bIndex++)
- {
- ul_current_offset_sum = ADC1_Result[ADC1_RANK_CURRENT] + ul_current_offset_sum;
- }
- uw_current_offset = ul_current_offset_sum / 10;
- if((uw_current_offset < 1000) || (uw_current_offset > 3000))
- {
- p_MC_ErrorCode->ERROR_Bit.Fault_Circuit = 1;
- //记录故障日志
- MC_ErrorLogSaveInfo.NotesInfo1 = 7;
- MC_ErrorLogSaveInfo.NotesInfo2 = uw_current_offset;
- ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
- IsErrorLogSaveInfoUpdateFlag = TRUE;
- //存储故障次数
- MC_RunLog2.Circuit_FaultCnt++;
- RunLogSaveIndex = 2;
- }
- PeriodTimeCnt = HAL_GetTick();
- }
- }
-
- LeaveTime = HAL_GetTick();
- }
- //3相占空比设置
- void SVPWM_3ShuntCalcDutyCycles (Volt_Components Stat_Volt_Input)
- {
- int32_t wX, wY, wZ, wUAlpha, wUBeta;
- uint16_t hDeltaDuty;
- wUAlpha = Stat_Volt_Input.qV_Component1 * T_SQRT3 ;
- wUBeta = -(Stat_Volt_Input.qV_Component2 * T);
- wX = wUBeta;
- wY = (wUBeta + wUAlpha) >> 1;
- wZ = (wUBeta - wUAlpha) >> 1;
- // Sector calculation from wX, wY, wZ
- if (wY<0)
- {
- if (wZ<0)
- {
- bSector = SECTOR_5;
- }
- else // wZ >= 0
- if (wX<=0)
- {
- bSector = SECTOR_4;
- }
- else // wX > 0
- {
- bSector = SECTOR_3;
- }
- }
- else // wY > 0
- {
- if (wZ>=0)
- {
- bSector = SECTOR_2;
- }
- else // wZ < 0
- if (wX<=0)
- {
- bSector = SECTOR_6;
- }
- else // wX > 0
- {
- bSector = SECTOR_1;
- }
- }
-
-
- PWM4Direction=0;
-
- switch(bSector)
- {
- case SECTOR_1:
- hTimePhase.hTimePhA = (T >> 3) + ((((T + wX) - wZ) >> 1) >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
- // hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
-
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhB);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) >TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- // Trigger of ADC at Falling Edge PWM4
- // OCR update
-
- //Set Polarity of CC4 Low
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
-
- break;
- case SECTOR_2:
- hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
-
- // hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhA);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
- break;
- case SECTOR_3:
- hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
-
- // hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
-
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhB - hTimePhase.hTimePhC);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhB - TW_BEFORE; // Ts before Phase B
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhB) > TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhB + TW_AFTER; // DT + Tn after Phase B
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- // Trigger of ADC at Falling Edge PWM4
- // OCR update
-
- //Set Polarity of CC4 Low
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
-
- break;
-
- case SECTOR_4:
- hTimePhase.hTimePhA = (T>> 3) + ((((T + wX) - wZ) >> 1) >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhB - (wX >> 17);
-
- // hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
-
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhB);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- // Trigger of ADC at Falling Edge PWM4
- // OCR update
-
- //Set Polarity of CC4 Low
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
-
- break;
-
- case SECTOR_5:
- hTimePhase.hTimePhA = (T >> 3) + ((((T + wY) - wZ) >> 1) >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhA + (wZ >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
-
- // hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhC - hTimePhase.hTimePhA);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhC - TW_BEFORE; // Ts before Phase C
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhC) > TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhC + TW_AFTER; // DT + Tn after Phase C
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- // Trigger of ADC at Falling Edge PWM4
- // OCR update
-
- //Set Polarity of CC4 Low
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
-
- break;
-
- case SECTOR_6:
- hTimePhase.hTimePhA = (T >> 3) + ((((T - wX) + wY) >> 1) >> 17);
- hTimePhase.hTimePhC = hTimePhase.hTimePhA - (wY >> 17);
- hTimePhase.hTimePhB = hTimePhase.hTimePhC + (wX >> 17);
-
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- // ADC Syncronization setting value
-
- if ((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_AFTER)
- {
- hTimePhase.hTimePhD = PWM_PERIOD - 1;
- }
- else
- {
- hDeltaDuty = (uint16_t)(hTimePhase.hTimePhA - hTimePhase.hTimePhC);
-
- // Definition of crossing point
- if (hDeltaDuty > TW_DT_TR_TS)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhA - TW_BEFORE; // Ts before Phase A
- }
- else if((uint16_t)(PWM_PERIOD-hTimePhase.hTimePhA) > TW_DT_TN_TS_HALF)
- {
- hTimePhase.hTimePhD = hTimePhase.hTimePhA + TW_AFTER; // DT + Tn after Phase A
-
- if (hTimePhase.hTimePhD >= PWM_PERIOD)
- {
- // Trigger of ADC at Falling Edge PWM4
- // OCR update
-
- //Set Polarity of CC4 Low
- PWM4Direction=1;
-
- hTimePhase.hTimePhD = (2 * PWM_PERIOD) - hTimePhase.hTimePhD-1;
- }
- }
- // else
- // {
- // while(1);
- // }
- }
-
- break;
- default:
- break;
- }
-
- if (PWM4Direction == 0)
- {
- //Set Polarity of CC4 High
- Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
- // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
- }
- else
- {
- //Set Polarity of CC4 Low
- Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
- // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_HIGH);
- }
- // Set_Pwm_Chanle4_Polarity(TIM_OCPOLARITY_LOW);
- hTimePhase.hTimePhD=PWM_PERIOD-20;
- //Load compare registers values
- Set_Pwm_Chanle1_Compare(hTimePhase.hTimePhA);
- Set_Pwm_Chanle2_Compare(hTimePhase.hTimePhB);
- Set_Pwm_Chanle3_Compare(hTimePhase.hTimePhC);
- Set_Pwm_Chanle4_Compare(hTimePhase.hTimePhD);
- }
- //读取三相电流
- Curr_Components SVPWM_3ShuntGetPhaseCurrentValues(void)
- {
- Curr_Components Local_Stator_Currents = {0, 0};
- int16_t wAux;
- switch (bSector)
- {
- case 4:
- case 5: //Current on Phase C not accessible
- wAux =-(ADC_3ShuntCurrent.uw_phase_a);
- //Saturation of Ia
- if (wAux < -32768)
- {
- Local_Stator_Currents.qI_Component1= -32768;
- }
- else if (wAux > 32767)
- {
- Local_Stator_Currents.qI_Component1= 32767;
- }
- else
- {
- Local_Stator_Currents.qI_Component1= wAux;
- }
- wAux =-(ADC_3ShuntCurrent.uw_phase_b);
- // Saturation of Ib
- if (wAux < -32768)
- {
- Local_Stator_Currents.qI_Component2= -32768;
- }
- else if (wAux > 32767)
- {
- Local_Stator_Currents.qI_Component2= 32767;
- }
- else
- {
- Local_Stator_Currents.qI_Component2= wAux;
- }
- break;
-
- case 6:
- case 1:
- wAux =-(ADC_3ShuntCurrent.uw_phase_b);
- //Saturation of Ib
- if (wAux < -32768)
- {
- Local_Stator_Currents.qI_Component2= -32768;
- }
- else if (wAux > 32767)
- {
- Local_Stator_Currents.qI_Component2= 32767;
- }
- else
- {
- Local_Stator_Currents.qI_Component2= wAux;
- }
-
- //Ia = -Ib - Ic;
- wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component2;
- if (wAux> 32767)
- {
- Local_Stator_Currents.qI_Component1 = 32767;
- }
- else if (wAux <-32768)
- {
- Local_Stator_Currents.qI_Component1 = -32768;
- }
- else
- {
- Local_Stator_Currents.qI_Component1 = wAux;
- }
- break;
-
- case 2:
- case 3:
- // Current on Phase B not accessible
- wAux =-(ADC_3ShuntCurrent.uw_phase_a);
- // Saturation of Ia
- if (wAux> 32767)
- {
- Local_Stator_Currents.qI_Component1=32767;
- }
- else if (wAux <-32768)
- {
- Local_Stator_Currents.qI_Component1 = -32768;
- }
- else
- {
- Local_Stator_Currents.qI_Component1 = wAux;
- }
- wAux = ADC_3ShuntCurrent.uw_phase_c - Local_Stator_Currents.qI_Component1; //Ib = -Ia - Ic;
- //Saturation of Ib
- if (wAux < -32768)
- {
- Local_Stator_Currents.qI_Component2= -32768;
- }
- else if (wAux > 32767)
- {
- Local_Stator_Currents.qI_Component2= 32767;
- }
- else
- {
- Local_Stator_Currents.qI_Component2= wAux;
- }
-
- break;
- default:
- break;
- }
-
- Local_Stator_Currents.qI_Component1 = Local_Stator_Currents.qI_Component1 >> 4;
- Local_Stator_Currents.qI_Component2 = Local_Stator_Currents.qI_Component2 >> 4;
- return(Local_Stator_Currents);
- }
- //读母线电流
- int16_t GetCurrentValues(void)
- {
- int16_t Result;
-
- Result = ADC1_Result[ADC1_RANK_CURRENT] - (int16_t)uw_current_offset;
-
- return Result;
- }
|