Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
S-Functions is computer language description of Simulink block If written in MATLAB programming environment it is called M-file s-function If written in C/C++ programming environment it is called C Mex s-function
Allows you to add your own code to simulink model or environment Code can be written for continuous, discrete or hybrid system Can be use with Real Time Workshop
Real Time workshop generates code for Simulink/s-function to run in real time ..faster
Simulation Stages
Simulation
Initialization
port widths, data types, sample time, memories allocation, execution order, evaluates block parameters loop Execute block determined during initialization, computes/updates blocks states, derivatives and outputs during a sample time
F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional
M-file S-function
M-files s-function is a matlab function [sys,x0,str,ts] = f(t,x,u,flag,p1,p2,) Inputs f s-function name t is the current time x is the state vector flag indicates the task to be performed p1, p2 are block parameter to be changed externally
Outputs sys x0 str ts Output of s-function [ initial value vector of states] not used [ ] [sample time offset time]
M-file s-function template Examples: <matlabroot>/toolbox/simulink/blocks/msfuntmpl.m Sfundemos: >>sfundemos >> is matlab workspace prompt <matlabroot>/toolbox/simulink/simdemos User Manaul:
http://www.mathworks.com/access/helpdesk/help/pdf_doc/simulink/sfunction.pdf
C MEX s-function
Mex s-function are written in C or C++, Fortran, Adao C Mex files can modify data structure of Simulink SimStruct which is used for housekeeping Can handle multiple data types and matrix signals Used with Real Time Workshop , wrapper s-function
How to build C Mex s-function Simulink specific naming convention and housekeeping (SimStruct) standards should be followed by C Mex files Template and examples in <matlabroot> / simulink /src sfuntmpl_basic.c >> mex setup should be run a priori to setup the compiler C Mex files are compiled in MATLAB with mex command Matlab Executable (mex) creates xxx.dll, dynamically loadable file excutable for simulink S- function Builder
F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional
S-Function Builder Easiest way to embed your C code in Simulink model S-function builder is a Simulink block Act as calling program (wrapper) to C source code from Simulink providing necessary interfacing options S-function builder generates its own: builder-name.c Have standard code representation
similar to sfuntmpl_basic.c builder-name_wrapper.c Customized interfacing options entered in builder builder-name.tlc Permit code generated to run in accelerated and real time Workshop builder-name.dll Simulink executable dll file
>> sim(mymodel)
M-code S-function Caution: interpreted, not compiled
mdlInitializeSampleTimes()
mdlOutputs()
Done? y
mdlTerminate()
y = 0;
Different from static variables in C are persistent but shared, If a leaf contains static data it can only be instantiated once
input u
states xd, w
output y=f(t,xd,w,u)
yt = f(t, xt, ut), xt+ t = g(t, xt, ut) y = outputs, t = time, x = states, u = inputs
2) General structure or advanced needs use full S-function Leaf with output and update functions intermixed Use work vectors , update them in mdlOutputs()
mdlOutputs() mdlUpdate()
n Done? y
mdlTerminate()
mdlInitializeSampleTimes()
mdlOutputs()
Done?
y
yt = f(t, wt, ut) wt+t = g(t, wt, ut) y = outputs t = time w = work vec u = inputs
mdlTerminate()
Demonstrations
Examples of S-function builder
S-function Builder block features Slicer: a memoryless block IIR Filter: a block with memory Output-update structure S-function Builder A Discrete state Output in mdlOutputs()and update in mdlUpdate() General structure Full S-function 1-element Work vector Output and update in mdlOutputs() Using the sample code as a resource Tour of the S-function reference material
Setup breakpoint in sfunction.c using Right Mouse button Click Right mouse button on sfunc_name.dll open setting dialouge box, select
OK!
registers
use F11 or steps increment icons from the MSVC toolbar to step to next line in sfunction.c
Ver 2.2.0
Always set MATLAB Command Window directory to the directory where source files are stored >> mex setup % At matlab startup % >> rtwintgt install % At startup % >> mex g sfun_name.c % s-function debugging%
F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional
s-function use with Real Time Workshop For Real Time Window Target Real time Workshop (RTW) generates portable , customize standalone C/C++ code of simulink model which operates in Real time Target Language Compiler (TLC) transform Simulink Block into C/C++ Simulink external mode enables communication between simulink and external process Real Time Window Target (RTWT) is a component of RTW RTWT helps to connect simulink model with external actuators and sensor for control and monitoring of Physical process
F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional