Target Language Compiler | ![]() ![]() |
Generating Code for Models with States
Objective: Look at Real-Time Workshop code generation for the models that have states. This does not involve many new Real-Time Workshop concepts, but it is instructive to see the changes in the generated C code as opposed to the sfun_x2.mdl
on page 8-3, for example.
This is not an assignment per se, but rather a demonstration of the process involved, from designing a model to generating and using the code. This choice of model involves some subtleties, which you will see in the discussion ahead:
springsfun.mdl
model located in the tlctutorial
/states
subdirectory. This is the model of a linear system with transfer functioninitialize.m
that is in the same subdirectory. This generates initial parameters for the system. These parameters are in the form of a random, stable, eighth order linear system. Once this is done, the model is ready to execute.sisosf
, is the state space equivalent of where u(t) is the control signal. The output y(t) is given by y(t) = a0x0.
This same information is incorporated in the state space block, via matrices
a
, b
, c
, and d
. The order of the states, however, may be different from that
given above, with the differences being those of nomenclature, and not of
fact.
sisosf.c
, which is also found in this directory. If the precompiled version of sisosf.c
(sisosf.dll)
is not in your directory, you will need to compile it with the commandmex sisosf.c.
Also, study the sisosf.c
program. This program was created by editing a
copy of the sfuntmpl.c
program template, which is what you are advised to
do. Note the various macros (ssNumSetSFcnParams
,
ssSetSFcnParamNotTunable
, ssGetContStates
, etc.) that are used to access
the SimStruct
structure to get/set the various model parameters. These
macros are defined in simstruc.h
and described in the Simulink book
Writing S-Functions. Some things to be noted are:
SiSoSF
are the ai values, which form the last line of the A
matrix. The length of this vector determines the number of states (MdlInitializeSizes
).MdlInitializeConditions
, which is optional).MdlDerivatives
function is the heart of the program and should be studied carefully. The xdot
vector stores the derivatives, and xdot[7]
(in this case) and more generally xdot[NCStates-1]
is calculated incrementally. Additionally, the fact that x
and xdot
have disparate indices in the line.tlc
file to enable the inlining S-function. This has already been done for you, and the file is called sisosf.tlc
. A listing of the file follows.%% FileName: sisosf.tlc %% %% Purpose: To implement a SISO system in controllable canonical form. %implements "sisosf" "C" %% Function: Outputs ========================================================== %% %function Outputs(block,system) Output %assign y = LibBlockOutputSignal(0,"","",0) %assign x = LibContinuousState("","",0) %assign dc = LibBlockParameter(P1,"","",0) %% Dc gain is the negative of the first element of the %% The first state (zeroth derivative) is the output /* Block %<Name>: %<Type> Output */ %<y> = -%<dc> * %<x>; %endfunction %% Outputs %% Function: Derivatives ====================================================== %% %function Derivatives(block,system) Output %assign ncStates = ContStates[0] %assign offset = ContStates[1] %assign input = LibBlockInputSignal(0,"","",0) /* Derivatives function starts here */ { real_T *dx = ssGetdX(%<tSimStruct>); dx[%<ncStates - 1 + offset>] = %<input> + \ %<LibBlockParameter(P1,"","",0)> * \ %<LibContinuousState("","",0)>; %assign rollVars = ["Xc", "<param>/P1"] %roll xIdx = [0:%<ncStates-2>],xlcv = RollThreshold, block, "Roller", rollVars %assign idx = (xlcv != "") ? "%<xIdx+offset> + %<xlcv>" : "%<xIdx+offset>" %assign lcv = (xlcv != "") ? "1 + %<xlcv>" : "" %assign x = LibContinuousState("",lcv,xIdx+1) %assign a = LibBlockParameter(P1,"",lcv,xIdx+1) /* Block %<Name>: %<Type> derivatives */ dx[%<idx>] = %<x>; dx[%<ncStates - 1 + offset>] += %<a> * %<x>; %endroll } %endfunction %% Derivatives %% Function: InitializeConditions ============================================= %% %function InitializeConditions(block,system) Output %assign ncStates = ContStates[0] %% Initialize State zero (position) to 1.0, rest are 0.0 %assign x = LibContinuousState("","",0) /* %<Type>: %<Name> Position Initialization */ %<x> = 1.0; %foreach sigIdx = ncStates - 1 %assign x = LibContinuousState("","",sigIdx+1) /* %<Type>: %<Name> Other States Initialization */ %<x> = 0.0; %endforeach %endfunction %% InitializeConditions %% [EOF] sisosf.tlc
![]() | Generating Auxiliary Files for Batch FTP | Derivatives Function | ![]() |