function[ sys, x0, str, ts]=PMSMdq(t,x,u,flag,parameters,x0_in)
%PMSM model. %parameters;
%ld,lq:inductance in dp reference of frame
%r:stater resistance
%psi_f:flux in webers by PM on rotor
%p:number of pole pairs
%j:inertia of motor and load
%mu_f:viscous friction %inputs:
%ud,uq:voltages in dp reference of frame
%tl:torque of load %inner variants:
%id,iq currents in dp reference of frame
%ud,uq:voltage int dp reference of frame
%wr:angular velocity of the rotor
%te:electronmagnetic torque
%theta: position of rotor %outputs:
%wr:angular velocity of the rotor
%te:electronmagnetic torque
%id,iq currents in dp reference of frame
%theta :position of rotor
%----------------------------
%u(1 2 3)=
%ud uq tl
%parameters (1 2 3 4 5 6 7)=
% ld lq r psi_f p j mu_f
%sys(1 2 3 4 5 )=
% wr te id iq theta
%x(1 2 3 4 )=
% id iq wr theta
switch flag
case 0
[sys x0 str ts]=mdlInitializeSizes(x0_in);%iniatialization
case 1
%calculate the derivatives
sys=mdlDerivatives(x,u,parameters);
case 3
%output
sys=mdlOutputs(x,u,parameters);
case{2,4,9}
%unused flags
sys=[];
otherwise
%Error handling
error(['Unhandled flag=',num2str(flag)]);
end
%end of PMSMdq
%-----------------------------------
%mdlInitializeSizes
%----------------------------------
function[sys,x0,str,ts]=mdlInitializeSizes(x0_in)
%-------------------------------------
%u(1 2 3)=
% ud uq tl
%parameters(1 2 3 4 5 6 7 )=
% ld lq r psi_f p j mu_f
%x( 1 2 3 4)=
% id iq wr theta
sizes=simsizes;
sizes.NumContStates=4;
sizes.NumDiscStates=0;
sizes.NumOutputs=5;
sizes.NumInputs=3;
sizes.DirFeedthrough=0;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=x0_in;
str=[];
ts=[0 0];
%End of mdlInitializeSizes.
%---------------------------
%mdlDerivatives
%Return the derivatives for the continuous states
%-----------------------------
function[ sys ]=mdlDerivatives(x,u,parameters)
%-----------------------------
%u( 1 2 3)=
% ud uq tl
%parameters(1 2 3 4 5 6 7)=
% ld lq r psi_f p j mu_f
%sys(1 2 3 4 5)=
% wr te id iq theta
%x(1 2 3 4)=
% id iq wr theta
%id'=ud/ld-r*iq/lq+lq*p*wr*iq/ld
sys(1)=u(1)/parameters(1)-parameters(3)*x(1)/parameters(1)+parameters(2)*parameters(5)*x(3)*x(2)/parameters(1);
%iq'=uq/lq-r*iq/lq-ld*p*wr*id/lq-psi_f*p*wr/lq
sys(2)=u(2)/parameters(2)-parameters(3)*x(2)/parameters(2)-parameters(1)*parameters(5)*x(3)*x(1)/parameters(2)-parameters(4)*parameters(5)*x(3)/parameters(2);
%te=1.5*p*[psi_f*iq+(ld-lq)*id*iq]
te=1.5*parameters(5)*(parameters(4)*x(2)+(parameters(1)-parameters(2))*x(1)*x(2));
%wr'=(te-mu_f*wr-tl)/j
sys(3)=(te-parameters(7)*x(3)-u(3))/parameters(6);
%theta'=p*wr
sys(4)=parameters(5)*x(3);
%End of mdlDerivatives
%-------------------------------------------
%mdlOutputs
%Return the block outputs.
%%-----------------------------------------
function sys=mdlOutputs(x,u,parameters,te)
%------------------------------------------
%u(1 2 3)=
% ud uq tl
%parameters(1 2 3 4 5 6 7)=
% ld lq r psi_f p j mu_f
%sys(1 2 3 4 5)=
% wr te id iq theta
%x(1 2 3 4 )=
% id iq wr theta %output wr
sys(1)=x(3);
%output te
%te=1.5*p*[psi_f*iq+(ld-lq)*id*iq]
te=1.5*parameters(5)*(parameters(4)*x(2)+(parameters(1)-parameters(2))*x(1)*x(2));
sys(2)=te;
%output idq
sys(3)=x(1);%id
sys(4)=x(2);%iq
%out theta
sys(5)=x(4);
%End of mdlOutputs