📄 mybldc2.m
字号:
function [sys,x0,str,ts] = bldc(t,x,u,flag,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs)
% BLDC An example M-file S-function for defining a system of
% The expected input vector is:
% (1): I_U: The U phase instantaneous current
%(2): I_V: The V phase instantaneous current
%(3): I_W:The W phase instantaneous current
%(4): wn: The current angular velocity
%(5): Theta: The current angle (INTEGRAL OF wn)
%OUTPUT VECTOR GENERATED BY THE SYSTEM:
%(1): EMF_u
%(2): EMF_v
%(3): EMF_w
%(4): Torque Phase U
%(5): Torque Phase V
%(6): Torque Phase W
%(7): Friction generated
%(8): angular position of rotor (Normalised by 2*pi)
% Dispatch the flag.
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs); % Initialization
case 1,
sys = mdlDerivatives(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs); % Calculate derivatives
case 2, %model update
sys = mdlUpdate(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs); % update the model
case 3,
sys = mdlOutputs(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs); % Calculate outputs
case 4, % Unused flags
sys = [];
case 'reset'
name = 'mybldc_mdl2/mybldc/my state-space';
LocalResetController(name);
case 9,
sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% End of csfunc.
%==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
% S-function.
%==============================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs)
%
% Call simsizes for a sizes structure, fill it in and convert it
% to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 8;
sizes.NumInputs = 5;
sizes.DirFeedthrough = 8; % Matrix D is nonempty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%
% Initialize the initial conditions.
%
x0 = [];
%
% str is an empty matrix.
%
str = [];
%
% Initialize the array of sample times; in this example the sample
% time is continuous, so set ts to 0 and its offset to 0.
%
ts = [0 0];
% End of mdlInitializeSizes.
%
%==============================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
% Nothing needs to be done here.
%==============================================================
function sys = mdlDerivatives(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs)
sys=[];
%==============================================================
% mdlUpdate
% Update the model parameters.
% Nothing needs to be done here
%==============================================================
function sys = mdlUpdate(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs); % update the model
sys=[];
%==============================================================
% mdlOutput
% calculate the model outputs and update other blocks ( Adaptive coefficients)
%==============================================================
function sys = mdlOutputs(t,x,u,N,R,L,M,BM,Rl,Rr,DF,J,P,F0,Fs)
theta = rem (u(5),2*pi); % make theta betweeen 2*pi and -2*pi
sys(8)=theta; %output normalised angular position
if (theta>=0 & theta<pi/6)
LA= 6*theta/pi;
LB=-1;
LC=1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if (theta>=pi/6 & theta<pi/3)
LA=1;
LB=-1;
LC=-6*(theta-(2*pi/6))/pi;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if (theta>=pi/3 & theta<pi/2)
LA=1;
LB=-1;
LC=-6*(theta-(2*pi/6))/pi;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if (theta>=pi/2 & theta<2*pi/3)
LA=1;
LB=( theta-(4*pi/6) )*6/pi;
LC=-1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if(theta>=2*pi/3 & theta<5*pi/6)
LA=1;
LB=( theta-(2*pi/3) )*6/pi;
LC=-1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if (theta>=5*pi/6 & theta<pi)
LA=(pi-theta)*6/pi;
LB=1;
LC=-1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if(theta>=pi & theta<7*pi/6)
LA=(pi-theta)*6/pi;
LB=1;
LC=-1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if(theta>=7*pi/6 & theta<4*pi/3)
LA= -1;
LB=1;
LC=( theta-(4*pi/3))*6/pi;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if (theta>=4*pi/3 & theta<3*pi/2)
LA= -1;
LB=1;
LC=( theta -(4*pi/3))*6/pi;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
sys(4)=BM*Rl*Rr*N*LA*u(1); % Phase Torque U
sys(5)=BM*Rl*Rr*N*LB*u(2); %Phase Torque V
sys(6)=BM*Rl*Rr*N*LC*u(3); %Phase Torque W
end;
if(theta>=3*pi/2 & theta<5*pi/3)
LA= -1;
LB=((5*pi/3)-theta)*6/pi;
LC=1;
sys(1)= LA*N*BM*Rl*Rr*u(4); % Back emf for phase U
sys(2)=LB*N*BM*Rl*Rr*u(4); %Back emf for phase V
sys(3)=LC*N*BM*Rl*Rr*u(4); %Back emf for phase W
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -