27-07-2012, 12:23 PM
overview of s-function
sfunctions.pdf (Size: 1.07 MB / Downloads: 280)
Introduction
S-functions (system-functions) provide a powerful mechanism for extending
the capabilities of Simulink®. The introductory sections of this chapter describe
what an S-function is and when and why you might use one. This chapter then
presents a comprehensive description of how to write your own S-functions.
S-functions allow you to add your own algorithms to Simulink models. You can
write your algorithms inMATLAB® or C. By following a set of simple rules, you
can implement your algorithms in an S-function. After you have written your
S-function and placed its name in an S-Function block (available in the
Nonlinear Block sublibrary), you can customize the user interface by using
masking.
You can also customize the code generated by the Real Time Workshop® for
S-functions by writing a Target Language CompilerTM (TLC) file. See the
Target Language Compiler Reference Guide and the Real-Time Workshop
User’s Guide for more information.
What Is an S-Function?
An S-function is a computer language description of a dynamic system.
S-functions can be written using MATLAB or C. C language S-functions are
compiled as MEX-files using the mex utility described in the Application
Program InterfaceGuide. As with otherMEX-files, they are dynamically linked
into MATLAB when needed.
S-functions use a special calling syntax that enables you to interact with
Simulink’s equation solvers. This interaction is very similar to the interaction
that takes place between the solvers and built-in Simulink blocks.
The form of an S-function is very general and can accommodate continuous,
discrete, and hybrid systems. As a result, nearly all Simulink models can be
described as S-functions.
S-functions are incorporated into Simulink models by using the S-Function
block in the Nonlinear Block sublibrary. Use the S-Function block’s dialog box
to specify the name of the underlying S-function, as illustrated in the figure
below:
When to Use an S-Function
The most common use of S-functions is to create custom Simulink blocks. You
can use S-functions for a variety of applications, including:
•Adding new general purpose blocks to Simulink
•Incorporating existing C code into a simulation
•Describing a system as a mathematical set of equations
•Using graphical animations (see the inverted pendulum demo, penddemo)
An advantage of using S-functions is that you can build a general purpose block
that you can use many times in a model, varying parameters with each
instance of the block.
Simulation Stages and S-Function Routines
Simulink makes repeated calls during specific stages of simulation to each
block in the model, directing it to perform tasks such as computing its outputs,
updating its discrete states, or computing its derivatives. Additional calls are
made at the beginning and end of a simulation to perform initialization and
termination tasks.
The figure below illustrates how Simulink performs a simulation. First,
Simulink initializes the model; this includes initializing each block, including
S-functions. Then Simulink enters the simulation loop, where each pass
through the loop is referred to as a simulation step. During each simulation
step, Simulink executes your S-function block. This continues until the
simulation is complete:
Direct Feedthrough
Direct feedthrough means that the output or the variable sample time is
controlled directly by the value of an input port. A good rule of thumb is that
an S-function input port has direct feedthrough if:
•The output function (mdlOutputs or flag==3) is a function of the input u.
That is, there is direct feedthrough if the input u is used in equations defined
in mdlOutputs. Outputs may also include graphical outputs, as in the case of
an XY Graph scope.
•It is a variable sample time S-function (calls mdlGetTimeOfNextVarHit or
flag==4) and the computation of the next sample hit requires the input u.