MATLAB/simulink S-函数编程基础

simulink 为编写S-函数提供了丰富的模板函数,MATLAB/toolbox/simulink/blocks/sfuntmol.m为编程S-函数常用模板,其源代码如下:

function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
%   FLAG   RESULT             DESCRIPTION
%   -----  ------             --------------------------------------------
%   0      [SIZES,X0,STR,TS]  Initialization, return system sizes in SYS,
%                             initial state in X0, state ordering strings
%                             in STR, and sample times in TS.
%   1      DX                 Return continuous state derivatives in SYS.
%   2      DS                 Update discrete states SYS = X(n+1)
%   3      Y                  Return outputs in SYS.
%   4      TNEXT              Return next time hit for variable step sample
%                             time in SYS.
%   5                         Reserved for future (root finding).
%   9      []                 Termination, perform any cleanup SYS=[].
switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;

  %%%%%%%%%%%%%%%
  % Derivatives %
  %%%%%%%%%%%%%%%
  case 1,
    sys=mdlDerivatives(t,x,u);

  %%%%%%%%%%
  % Update %
  %%%%%%%%%%
  case 2,
    sys=mdlUpdate(t,x,u);

  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  case 3,
    sys=mdlOutputs(t,x,u);

  %%%%%%%%%%%%%%%%%%%%%%%
  % GetTimeOfNextVarHit %
  %%%%%%%%%%%%%%%%%%%%%%%
  case 4,
    sys=mdlGetTimeOfNextVarHit(t,x,u);

  %%%%%%%%%%%%%
  % Terminate %
  %%%%%%%%%%%%%
  case 9,
    sys=mdlTerminate(t,x,u);

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 0;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);

%
% initialize the initial conditions
%
x0  = [];

%
% str is always an empty matrix
%
str = [];

%
% initialize the array of sample times
%
ts  = [0 0];
simStateCompliance = 'UnknownSimState';

% end mdlInitializeSizes

%=============================================================================
% mdlDerivatives
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)

sys = [];

% end mdlDerivatives

%
%=============================================================================
% mdlUpdate
%=============================================================================
%
function sys=mdlUpdate(t,x,u)

sys = [];

% end mdlUpdate

%
%=============================================================================
% mdlOutputs
%=============================================================================
%
function sys=mdlOutputs(t,x,u)

sys = [];

% end mdlOutputs

%
%=============================================================================
% mdlGetTimeOfNextVarHit
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)

sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;

% end mdlGetTimeOfNextVarHit

%
%=============================================================================
% mdlTerminate
%=============================================================================
%
function sys=mdlTerminate(t,x,u)

sys = [];

% end mdlTerminate

 以该模板为例,设计一个正余弦叠加波形示波实例:

function[sys, x0, str, ts] = sinAddcos(t,x,u,flag)
%% Create a s function to add the sine wave and cosine wave
% t         --simulated time
% x         --inner simulated block
% u         --simulated output 
% flag      --operation flage
% sys       --S function output
% x0        --S function initial states
% str       --temp empty vector
% ts        --(2 X 2)time sample matrix,[:,1] sampling time and [:,2] time shifting

switch flag,
  case 0,
        [sys, x0, str, ts] = 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
        error(['Unhandled flag = ', num2str(flag)])
end

function [sys, x0, str, ts] = mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 1;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0];
str = [];
ts = [0 0];

function sys = mdlDerivatives(t, x, u)
        sys = [];
function sys = mdlUpdate(t, x, u)
        sys = x;
function sys = mdlOutputs(t, x, u)
        sys = u(1) + u(2);
function sys = mdlGetTimeOfNextVarHit(t, x, u)
        sampleTime = 1;
        sys = t + sampleTime;
function sys = mdlTerminate(t, x,u)
        sys = [];

  测试如下:加入100Hz正弦信号和200Hz余弦信号,通过示波器检验叠加器仿真效果。simulink建模如下:

                                                                                                           MATLAB/simulink S-函数编程基础_第1张图片

 

仿真结果测试:

                                                

 

 实验表明该S-函数实现了简单的波形叠加。更多复杂设计还在学习中,待更新!!!

转载于:https://www.cnblogs.com/Welcome-Xwell/p/9826457.html

你可能感兴趣的:(MATLAB/simulink S-函数编程基础)