Академический Документы
Профессиональный Документы
Культура Документы
Simulation Stages
Initialization
Simulation
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]
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
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:
Have standard code representation
builder-name.c
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
Call
How?
MATLAB
MATLAB Engine
ActiveX/COM & DDE
MATLAB Compiler & runtime
Simulink
Engine or COM
[x,y,t] =
sim(mymodel,u0)
MATLAB
MEX
ActiveX/COM & DDE
MATLAB
Simulink
>> sim(mymodel)
Simulink
MATLAB
M-code S-function
Caution: interpreted, not compiled
Simulink
S-function Builder
C-code S-function
Structure of code to be
imported: `main() and slicer.c
test_slicer.exe
//test_slicer.c
main()
{
for(){
slicer();
}
}
//slicer.c
slicer()
{
return;
}
Simulink
//wrap_slicer.c
mdlOutputs(S)
{
slicer();
}
//slicer.c
//definition
// of
//slicer()
mdlInitializeSampleTimes()
mdlOutputs()
Done?
y
mdlTerminate()
yt = f(t, ut)
y = outputs, t = time
u = inputs
y = 0;
Different from
static variables in C are persistent but shared, not
re-entrant
If a leaf contains static data it can only be
instantiated once
input u
states
xd, w
output y=f(t,xd,w,u)
1) Output-update
function.c structure
mdlInitializeSizes()
mdlInitializeSampleTimes()
mdlOutputs()
mdlUpdate()
n
Done?
y
mdlTerminate()
mdlInitializeSampleTimes()
mdlOutputs()
Done?
y
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
in MSVC
Setup breakpoint in sfunction.c using Right Mouse button
Click Right mouse button on sfunc_name.dll open setting dialouge
box, select debug tab, under executable field enter
C:\MATLAB6p5\bin\win32\matlab.exe,
OK!
MATLAB
Real Time
Workshop
Windows
XP
or
2000
98
or
2000
Ver 7.1
Recommend
ed
Version 7
Ver 2.5.0
Version 6.0
Version 6
Ver 2.2.0
<%matlabroot>/extern/examples/mex/
<%matlabroot>/toolbox/simulink/blocks/
i) fnpointer - use pointers to pass simulink matrix data to
and from
external source in C. MATLAB7, RTWT compilation
ii) myadd - add matrix internally using array indexes,
MATLAB7
iii) disc_stsp7 Discrete state updates internally, MATLAB7
iv) fnmat use pointers and array index for internal and
external codes, MATLAB7
v) ludlub Linear system equation solver, using C source
code from Numerical Recipes in C ludcmp.c & lubksb.c,
MATLAB 6.5
vi) mylms Least mean square , MATLAB7, RTWT