function [sys,x0,str,ts,simStateCompliance] = RCMV_VSV_combined(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 19;
sizes.NumInputs = 10;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
simStateCompliance = 'UnknownSimState';
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 用户代码段
% 输入 alpha beta数值 和直流母线电压Udc
%输出
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sys=mdlOutputs(t,x,u)
Ualphabeta=(2/3)*[1,-1/2,-1/2;0,sqrt(3)/2,-sqrt(3)/2]*[u(1);u(2);u(3)];
Ualpha=Ualphabeta(1);
Ubeta =Ualphabeta(2);
Udc=u(4); %直流侧母线电压
RampInput=u(5); %三角波(载波)输入 用于生产PWM
det_u=u(6); %输入电压差
ia=u(7);
ib=u(8);
ic=u(9);
persistent PWM_ActionEnable; %定义一个静态变量 用于判断PWM是否需要更新
if isempty(PWM_ActionEnable) %判断该静态变量是否为空
PWM_ActionEnable=0; %初次赋值
end
persistent PWM_CMP; %定义一个静态变量 PWM的占空比
if isempty(PWM_CMP) %判断该静态变量是否为空
PWM_CMP=zeros(6,1); %初次赋值
end
persistent PWM_CMP111; %定义一个静态变量 PWM的占空比
if isempty(PWM_CMP111) %判断该静态变量是否为空
PWM_CMP111=zeros(6,1); %初次赋值
end
persistent InverseFlag; %定义一个静态变量 用于调整PWM的关系
if isempty(InverseFlag) %判断该静态变量是否为空
InverseFlag=zeros(6,1); %初次赋值
end
persistent Area; %定义一个静态变量 用于调整PWM的关系
if isempty(Area) %判断该静态变量是否为空
Area=zeros(6,1); %初次赋值
end
persistent BalanceParameter; %定义一个静态变量 用于调整PWM的关系
if isempty(BalanceParameter) %判断该静态变量是否为空
BalanceParameter=0; %初次赋值
end
if(RampInput==0)
PWM_ActionEnable=1;
else
PWM_ActionEnable=0;
end
persistent k11; %定义一个静态变量
if isempty(k11) %判断该静态变量是否为空
k11=1/2; %初次赋值
end
persistent k21; %定义一个静态变量
if isempty(k21) %判断该静态变量是否为空
k21=1/2; %初次赋值
end
persistent T1; %定义一个静态变量
if isempty(T1) %判断该静态变量是否为空
T1=0; %初次赋值
end
persistent T2; %定义一个静态变量
if isempty(T2) %判断该静态变量是否为空
T2=0; %初次赋值
end
persistent T3; %定义一个静态变量
if isempty(T3) %判断该静态变量是否为空
T3=0; %初次赋值
end
persistent PWM_6CH; %定义一个静态变量 AdjustEnable
if isempty(PWM_6CH) %判断该静态变量是否为空
PWM_6CH=zeros(6,1);
end
persistent det_u_cal; %定义一个静态变量 AdjustEnable
if isempty(det_u_cal) %判断该静态变量是否为空
det_u_cal=0;
end
persistent da1_B1;
if isempty(da1_B1) %判断该静态变量是否为空
da1_B1=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(PWM_ActionEnable==1)
U_amp=sqrt(Ualpha^2+Ubeta^2); %获得幅值
U_angle=atan(Ubeta/Ualpha); %获得相角
%% 计算大扇区 输出BigSector
if (Ualpha<=0) %第二象限 %第三象限
U_angle=pi+U_angle;
elseif (Ualpha>0)&&(Ubeta<=0) %第四象限
U_angle=2*pi+U_angle;
end
BigSector=ceil(U_angle/pi*3); %范围缩小到0-6 并向下取整
SectorAngle=U_angle-(BigSector-1)*pi/3;
Sector_x=U_amp*cos(SectorAngle);
Sector_y=U_amp*sin(SectorAngle);
Ug=Sector_x-(1/sqrt(3))*Sector_y;
Uh=(2/sqrt(3))*Sector_y;
g=Ug*3/Udc;
h=Uh*3/Udc;
if(u(10)>0.02)
if(det_u>=0.5)
k11=0/2;k21=0/2;
else
k11=1/2;k21=1/2;
end
else
k11=1/2;k21=1/2;
end
line1=-g+h;
line2=((1/3)-1)/(1/3)*g-((1/3)-1)/(1/3)-h;
line3=(1/3)/((1/3)-1)*g+1-h;
line4=((1/3)-1)/(1+(1/3))*g+2*(1-(1/3))/(1+(1/3))-h;
line5=((1/3)+1)/((1/3)-1)*g+2-h;
line6=-g-h+1;
if(line6<=0)
if((line4<0)&&(line5<0))
Area=5;
else
if(line1<=0)
if(line2<=0)
Area=3;
else
Area=2;
end
else
if(line3<=0)
Area=4;
else
Area=2;
end
end
end
else
Area=1;
end
%% A1
da1_A1= - g*(k11 - 1) - h*(k21 - 1);
da2_A1= 1;
db1_A1= -h*(k21 - 1);
db2_A1= 1 - g*k11;
dc1_A1= 0;
dc2_A1= 1 - h*k21 - g*k11;
da1_B1= -g*(k11 - 1);
da2_B1= 1 - h*k21;
db1_B1= - g*(k11 - 1) - h*(k21 - 1);
db2_B1= 1;
dc1_B1= 0;
dc2_B1= 1 - h*k21 - g*k11;
PWM_CMP_Table_A1=[
da1_A1 da2_A1 db1_A1 db2_A1 dc1_A1 dc2_A1
da1_B1 da2_B1 db1_B1 db2_B1 dc1_B1 dc2_B1
dc1_A1 dc2_A1 da1_A1 da2_A1 db1_A1 db2_A1
dc1_B1 dc2_B1 da1_B1 da2_B1 db1_B1 db2_B1
db1_A1 db2_A1 dc1_A1 dc2_A1 da1_A1 da2_A1
db1_B1 db2_B1 dc1_B1 dc2_B1 da1_B1 da2_B1];
%% A2
da1_A2 =2*g + 2*h + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 2;
da2_A2 = 1;
db1_A2 =g + h + (k21 - 1)*(2*g + h - 2) - 1;
db2_A2 =2*g + 2*h - k21*(2*g + h - 2) + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 2;
dc1_A2 = 0;
dc2_A2 =g + h + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 1;
da1_B2 =g + h + (k11 - 1)*(g + 2*h - 2) - 1;
da2_B2 =2*g + 2*h - k11*(g + 2*h - 2) + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 2;
db1_B2 =2*g + 2*h + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 2;
db2_B2 = 1;
dc1_B2 =0;
dc2_B2 =g + h + (k11 - 1)*(g + 2*h - 2) + (k21 - 1)*(2*g + h - 2) - 1;
PWM_CMP_Table_A2=[
da1_A2 da2_A2 db1_A2 db2_A2 dc1_A2 dc2_A2
da1_B2 da2_B2 db1_B2 db2_B2 dc1_B2 dc2_B2
dc1_A2 dc2_A2 da1_A2 da2_A2 db1_A2 db2_A2
dc1_B2 dc2_B2 da1_B2 da2_B2 db1_B2 db2_B2
db1_A2 db2_A2 dc1_A2 dc2_A2 da1_A2 da2_A2
db1_B2 db2_B2 dc1_B2 dc2_B2 da1_B2 da2_B2];
%% A3
da1_A3 =g + (3*h)/2 + (k11 - 1)*(g + 2*h - 2) - 1;
da2_A3 =1;
db1_A3 =h/2;
db2_A3 =h + (k11 - 1)*(g + 2*h - 2);
dc1_A3 = 0;
dc2_A3 =h/2 + (k11 - 1)*(g + 2*h - 2);
da1_B3 =g + h + (k11 - 1)*(g + 2*h - 2) - 1;
da2_B3 =g + (3*h)/2 - k11*(g + 2*h - 2) + (k11 - 1)*(g + 2*h - 2) - 1;
db1_B3 =g + (3*h)/2 + (k11 - 1)*(g + 2*h - 2) - 1;
db2_B3 =1;
dc1_B3 =0;
dc2_B3 =h/2 + (k11 - 1)*(g + 2*h - 2);
PWM_CMP_Table_A3=[
da1_A3 da2_A3 db1_A3 db2_A3 dc1_A3 dc2_A3
da1_B3 da2_B3 db1_B3 db2_B3 dc1_B3 dc2_B3
dc1_A3 dc2_A3 da1_A3 da2_A3 db1_A3 db2_A3
dc1_B3 dc2_B3 da1_B3 da2_B3 db1_B3 db2_B3
db1_A3 db2_A3 dc1_A3 dc2_A3 da1_A3 da2_A3
db1_B3 db2_B3 dc1_B3 dc2_B3 da1_B3 da2_B3];
%% A4
da1_A4 =(3*g)/2 + h + (k11 - 1)*(2*g + h - 2) - 1;
da2_A4 =1;
db1_A4 =g + h + (k11 - 1)*(2*g + h - 2) - 1;
db2_A4 =(3*g)/2 + h - k11*(2*g + h - 2) + (k11 - 1)*(2*g + h - 2) - 1;
dc1_A4 =0;
dc2_A4 =g/2 + (k11 - 1)*(2*g + h - 2);
da1_B4 =g/2;
da2_B4 =g + (k11 - 1)*(2*g + h - 2);
db1_B4 =(3*g)/2 + h + (k11 - 1)*(2*g + h - 2) - 1;
db2_B4 =1;
dc1_B4 =0;
dc2_B4 =g/2 + (k11 - 1)*(2*g + h - 2);
PWM_CMP_Table_A4=[
da1_A4 da2_A4 db1_A4 db2_A4 dc1_A4 dc2_A4
da1_B4 da2_B4 db1_B4 db2_B4 dc1_B4 dc2_B4
dc1_A4 dc2_A4 da1_A4 da2_A4 db1_A4 db2_A4
dc1_B4 dc2_B4