Writing S-Functions    

The Direct-Index Lookup Table Example

This section shows how to improve the lookup table by inlining a direct-index S-function with a TLC file. Note that this direct-index lookup table S-function doesn't require a TLC file to work with the Real-Time Workshop. Here the example uses a TLC file for the direct-index lookup table S-function to reduce the code size and increase efficiency of the generated code.

Implementation of the direct-index algorithm with inlined TLC file requires the S-function main module, sfun_directlook.c (see page 8-28), and a corresponding lookup_index.c module (see page 8-37). The lookup_index.c module contains the GetDirectLookupIndex routine that is used to locate the index in the XData for the current x input value when the XData is unevenly spaced. The GetDirectLookupIndex routine is called from both the S-function and the generated code. Here the example uses the wrapper concept for sharing C code between Simulink MEX-files and the generated code.

If the XData is evenly spaced, then both the S-function main module and the generated code contain the lookup algorithm (not a call to the algorithm) to compute the y-value of a given x-value, because the algorithm is short. This demonstrates the use of a fully inlined S-function for generating optimal code.

The inlined TLC file, which performs either a wrapper call or embeds the optimal C code, is sfun_directlook.tlc (see page 8-39).

Error Handling

In this example, the mdlCheckParameters routine on page 8-31 verifies that:

Note that the mdlInitializeSizes routine explicitly calls mdlCheckParameters after it verifies that the number of parameters passed to the S-function is correct. After Simulink calls mdlInitializeSizes, it then calls mdlCheckParameters whenever you change the parameters or there is a need to reevaluate them.

User Data Caching

The mdlStart routine on page 8-34 illustrates how to cache information that does not change during the simulation (or while the generated code is executing). The example caches the value of the XDataEvenlySpaced parameter in UserData, a field of the SimStruct. The line

in mdlInitializeSizes tells Simulink to disallow changes to the XDataEvenlySpaced parameter. During execution, mdlOutputs accesses the value of XDataEvenlySpaced from UserData rather than calling the mxGetPr MATLAB API function. This results in a slight increase in performance.

mdlRTW Usage

The Real-Time Workshop calls the mdlRTW routine while it (the Real-Time Workshop) generates the model.rtw file. You can add information to the model.rtw file about the mode in which your S-Function block is operating to produce optimal code for your Simulink model.

This example adds the following information to the model.rtw file:

Example Model

Before examining the S-function and the inlined TLC file, consider the generated code for the following model.

When creating this model, you need to specify the following for each S-Function block.

This informs the Real-Time Workshop build process that the module lookup_index.c is needed when creating the executable.

The generated code for the lookup table example model is

matlabroot/simulink/src/sfun_directlook.c

matlabroot/simulink/src/lookup_index.c

matlabroot/toolbox/simulink/blocks/tlc_c/sfun_directlook.tlc


  The Direct-Index Lookup Table Algorithm Creating Code-Reuse-Compatible S-Functions