MATLAB & Simulink for Dynamic System Analysis & Control

by

 

19 Dec 2007 (Updated )

An instruction package for Control Toolbox, Simulink and SimMechanics in Mech. Eng. Control Course

spr_series_sfun(t,x,u,flag)
function [sys,x0,str,ts] = spr_series_sfun(t,x,u,flag)

switch flag,
  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  % Initialize the states, sample times, and state ordering strings.
   case 0,
    [sys,x0,str,ts]=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);
case 4,
    sys=mdlGetTimeOfNextVarHit(t,x,u);
        %
  % Terminate %
        %
  case 9,
    sys=mdlTerminate(t,x,u);  
  % Unexpected flags %   
  otherwise
    error(['Unhandled flag = ',num2str(flag) ]);
end
% end sfuntmpl
% end sfuntmpl

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Unexpected flags (error handling)%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Return an error message for unhandled flag values.
 

% end timestwo

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()

sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     =0; 
sizes.NumInputs      = 2;              %                       
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed

sys = simsizes(sizes);
str = [];
x0  = [];
ts  = [.02 0];    % inherited sample time

if(isempty(findobj('UserData',gcb)))
    h_fig=figure('position',[200 200 500 400] ,'menubar','none',...
        'numbertitle','off','resize','off','name',...
        'Animation : spr_series','renderer','OpenGL','color','c' ) ;         
    set(h_fig,'userdata',gcb);
    
   x1=0                     % 
   x2=0;
   axis([-0.5 1 -0.5 0.5]);axis off
   hold on;
   hd0=plot([x1 x2],[0 0],'s','markersize',30,'markerfacecolor','b');
   L=0.5   ;               % initial length of both srings
   H=L/20*[0 0 0 1 -1 1 -1 0 0 0];
   h_spr1=plot(linspace(-L,x1,10),H);
   h_spr2=plot(linspace(x1,0.5+x2,10),H);
                                                  
  
   set_param(gcb,'userdata',[hd0,h_spr1,h_spr2]);
end
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
sys=[ ];
% end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u); 
sys=[ ];                                                    
userdat=get_param(gcb,'UserData');
hd0=userdat(1);
h_spr1=userdat(2);
h_spr2=userdat(3);
    
x1=u(1);x2=u(2);
L=0.5 ;                 % initial length of both srings
H=L/20*[0 0 0 1 -1 1 -1 0 0 0];
set(hd0,'xdata',[x1,0.5+x2]);                     
set(h_spr1,'xdata',linspace(-L,x1,10));   
set(h_spr2,'xdata',linspace(x1,0.5+x2,10));       
   
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
sys=[];
% end mdlOutputs
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block.  Note that the result is
% absolute time.  Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)

sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)
sys=[ ];
% end mdlTerminate

Contact us