function [sys,x0,str,ts] = SPLL(t,x,u,flag,gain)
if(flag==0)%%初始化
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 5;%%%
sizes.NumInputs = 3;%%%
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0=[];
str=[];
ts=[1/10000,0];%%采样时间1e-6 1us
%从这里开始声明变量
end
if(flag==1 || flag==2 || flag==4 || flag==9)%%其余功能
sys = [];
end
if(flag==3)
global update_complete;
global spll_start;
update_complete=u(1);
if(update_complete+spll_start==1)
% spll算法
global Vab;
global Vbc;
global Vca;
global Va;
global Vb;
global Vc;
global theta;
global Vd;
global Vq;
global Fzeroc;
global e_pu;
Vab=u(2);
Vbc=u(3);
Vca=u(4);
Ua=(Vab-Vca)/(3*e_pu);
Ub=(Vbc-Vab)/(3*e_pu);
Uc=(Vca-Vbc)/(3*e_pu);
if(Fzeroc==0)
{
if ((Ua>0) && (Ub<=0) && (Uc<0))
{
theta=0;}
else if ((Ua>=0) && (Ub>0) && (Uc<0))
{ %//pi/3=1.04719755
theta = 1.04719755;}
else if ((Ua<0) && (Ub>0) && (Uc<=0))
{ %//2*pi/3=2.09439510
theta = 2.09439510;}
else if ((Ua<0) && (Ub>=0) && (Uc>0))
{ %//pi=3.14159265
theta = 3.14159265;}
else if ((Ua<=0) && (Ub<0) && (Uc>0))
{ %//4*pi/3=4.18879020
theta = 4.18879020;}
else{ %//5*pi/3=5.23598776
theta = 5.23598776;}
Fzeroc=1;
}
count1=1;
end
sintheta_PLL=sin(theta);
costheta_PLL=cos(theta);
temp11 = 0.50000000*sintheta_PLL + 0.86602540*costheta_PLL;
temp12 = 0.50000000*sintheta_PLL - 0.86602540*costheta_PLL;
Uqp_PLL=-0.66666667*(Ua*sintheta_PLL - Ub*temp11 - Uc*temp12);
PNS_p_vq_PLL=0.9575*Uqp_PLL-1.9112*Uqp_PLL_1+0.9575*Uqp_PLL_2+1.9112*PNS_p_vq_PLL_1-0.9150*PNS_p_vq_PLL_2;
Uqp_PLL_2=Uqp_PLL_1;
Uqp_PLL_1=Uqp_PLL;
PNS_p_vq_PLL_2=PNS_p_vq_PLL_1;
PNS_p_vq_PLL_1=PNS_p_vq_PLL;
delta_ek = 0 - PNS_p_vq_PLL;
delta_p = PLL_dq_kp * delta_ek;
delta_i += PLL_dq_ki * delta_ek;
if(delta_i >= 31.41592654)
{
delta_i = 31.41592654;
}
else if(delta_i <= -31.41592654)
{
delta_i = -31.41592654;
}
dk = delta_p + delta_i;
omega=Omega_initial - dk; % //Omega_initial=314.15926535 //deta_omega是偏差//2*PI*f
theta=theta+omega * 0.00010000; %//2.5k--0.00040000 10k--0.00010000
if(theta>6.28318530)
{
theta=theta-6.28318530;
}
else if(theta<0)
{
theta=theta+6.28318530;
}
Theta_output = theta+Theta_offset_dq;
if(Theta_output>6.28318530)
{
Theta_output=Theta_output-6.28318530;
}
else if(Theta_output<0)
{
Theta_output=Theta_output+6.28318530;
}
global spll_start;
spll_start=update_complete;
end
if(1==1) %%输出PWM
global theta;
global spll_start;
global Va;
global Vb;
global Vc;
sys(1)=spll_start;
sys(2)=(theta);
sys(3)=Va;
sys(4)=Vb;
sys(5)=Vc;
end
end
end