S-Function Block of time delay on Matlab-language.

4 views (last 30 days)
Hello! I try create S-Function block of time delay element (analog of element 'On Delay' in Simulink). I write m-script on level-1 function, but it is not work. I please you help me: 1.How to define variable. which will change on each simulation step? It is variable 'tt' in my code. 2. I want to set type boolean for my input. 3. Also can I input type for double and boolean?. Thanks for answers!
Code:
function [sys,x0,str,ts,simStateCompliance] = sfunPicDelay(t,x,u,flag)
%SFUNDSC2 Example unit delay MATLAB File S-function
% The MATLAB file S-function is an example of how to implement a unit
% delay.
%
% See sfuntmpl.m for a general S-function template.
%
% See also SFUNTMPL.
% Copyright 1990-2009 The MathWorks, Inc.
tset=0.02;
tt=0;
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(tset,tt);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = mdlUpdate(t,x,u,tset,tt);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3,
sys = mdlOutputs(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
sys = [];
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag))
end
%end sfundsc2
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(tset,tt)
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = -1;
sizes.NumOutputs = -1;
sizes.NumInputs = -1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0];
str = [];
ts = [0.001 0]; % Sample period of 0.1 seconds (10Hz)
% speicfy that the simState for this s-function is same as the default
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function [sys,tt] = mdlUpdate(t,x,u,tset,tt)
if u==1.0
tt = tt + 0.001;
else
tt = 0;
end
if tt>=tset
sys = u;
else
sys = 0;
end
%end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(t,x,u)
sys = x;
%end mdlOutputs

Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!