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:

  1. Open the springsfun.mdl model located in the tlctutorial/states subdirectory. This is the model of a linear system with transfer function

    that represents a simple SISO system. The picture of the model is given below.

  1. Before executing the model, run the m-file initialize.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.
  2. Examine the properties of the SiSoSF S-function block and the state space block. Both of these blocks are intended to do exactly the same thing, so that they can be checked against each other. The S-function, sisosf, is the state space equivalent of above, in the controllable canonical form. Therefore, the system is described by being the state in question

    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.

  1. The S-function is driven by the program 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 command

    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:

  1. The next step is to write a .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.

 Generating Auxiliary Files for Batch FTP Derivatives Function