function[Tcmp1,Tcmp2,Tcmp3,Sector] = fcn(Ualpha,Ubeta,Udc,Tpwm) %=============Initial======================== N = single(0); Tcmp1 = single(0); Tcmp2 = single(0); Tcmp3 = single(0); %=============A B C statement================ A = Ubeta; B = (sqrt(3) * Ualpha - Ubeta)/2; C = (-sqrt(3) * Ualpha - Ubeta)/2; %============Sector Judgment================= if(A > 0) N = single(1); end if(B > 0) N = N + single(2); end if(C > 0) N = N + single(4); end switch(N) case 1 Sector = 2; case 2 Sector = 6; case 3 Sector = 1; case 4 Sector = 4; case 5 Sector = 3; otherwise Sector = 5; end
/*根据Ualpha和Ubeta判断当前扇区*/ SectorJudgement_t getSectorNumber(AlphaBetaCoord_t uAlphaBeta) { SectorJudgement_t SectorStructure_temp; /*扇区判断数据N清零,防止随调用次数不断累加*/ SectorStructure_temp.mu8_SectorJudgement_N = 0; /*计算扇区判断中间变量ABC*/ SectorStructure_temp.mf_SectorJudgement_A = uAlphaBeta.mf_Beta; SectorStructure_temp.mf_SectorJudgement_B = (1.7320508 * uAlphaBeta.mf_Alpha - uAlphaBeta.mf_Beta); SectorStructure_temp.mf_SectorJudgement_C = (-1.7320508 * uAlphaBeta.mf_Alpha - uAlphaBeta.mf_Beta); /*由中间变量ABC根据公式N=A+2B+4C计算N值*/ if(SectorStructure_temp.mf_SectorJudgement_A > 0) { SectorStructure_temp.mu8_SectorJudgement_N += 1; } if(SectorStructure_temp.mf_SectorJudgement_B > 0) { SectorStructure_temp.mu8_SectorJudgement_N += 2; } if(SectorStructure_temp.mf_SectorJudgement_C > 0) { SectorStructure_temp.mu8_SectorJudgement_N += 4; } /*由N值通过查表法查出扇区号*/ if(SectorStructure_temp.mu8_SectorJudgement_N > 0 && SectorStructure_temp.mu8_SectorJudgement_N <= SECTOR_NUM) SectorStructure_temp.me_Sector = e_SectorIndex[SectorStructure_temp.mu8_SectorJudgement_N]; /*N值不合法*/ else SectorStructure_temp.me_Sector = SECTOR_ERROR; return SectorStructure_temp; }
/*扇区号枚举*/ typedef enum { SECTOR_ERROR = 0, SECTOR_1, SECTOR_2, SECTOR_3, SECTOR_4, SECTOR_5, SECTOR_6 }SectorNum_e; /*扇区判断相关结构体*/ typedef struct { float mf_SectorJudgement_A; float mf_SectorJudgement_B; float mf_SectorJudgement_C; uint8_t mu8_SectorJudgement_N; SectorNum_e me_Sector; }SectorJudgement_t; typedef struct { float mf_Alpha; float mf_Beta; }AlphaBetaCoord_t;