Documentation

Integrate External C Functions That Implement N-Dimensional Table Lookups

This example shows how to use the Legacy Code Tool to integrate legacy C functions that implement N-dimensional table lookups.

The Legacy Code Tool allows you to:

  • Provide the legacy function specification.

  • Generate a C-MEX S-function that is used during simulation to call the legacy code.

  • Compile and build the generated S-function for simulation.

  • Generate a block TLC file and optional rtwmakecfg.m file that specify how generated code calls the legacy code.

Provide the Legacy Function Specification

Functions provided with the Legacy Code Tool take a specific data structure or array of structures as the argument. The data structure is initialized by calling the function legacy_code() using 'initialize' as the first input. After initializing the structure, assign its properties to values corresponding to the legacy code being integrated. For detailed help on the properties, call legacy_code('help'). The prototype of the legacy function being called in this example is:

FLT directLookupTableND(const FLT *tableND, const UINT32 nbDims, const UINT32 *tableDims, const UINT32 *tableIdx)

where FLT is a typedef to float, and UINT32 is a typedef to unsigned int32. The legacy source code is in the files your_types.h, lookupTable.h, and directLookupTableND.c.

defs = [];
evalin('base','load rtwdemo_lct_data.mat')

% rtwdemo_sfun_dlut3D
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_dlut3D';
def.OutputFcnSpec = 'single y1 = DirectLookupTable3D(single p1[][][], uint32 p2[3], uint32 u1[3])';
def.HeaderFiles   = {'lookupTable.h'};
def.SourceFiles   = {'directLookupTableND.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
defs = [defs; def];

% rtwdemo_sfun_dlut4D
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_dlut4D';
def.OutputFcnSpec = 'single y1 = DirectLookupTable4D(single p1[][][][], uint32 p2[4], uint32 u1[4])';
def.HeaderFiles   = {'lookupTable.h'};
def.SourceFiles   = {'directLookupTableND.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
defs = [defs; def];

Generate S-Functions for Simulation

To generate C-MEX S-functions according to the description provided by the input argument 'defs', call the function legacy_code() again with the first input set to 'sfcn_cmex_generate'. The S-functions call the legacy functions in simulation. The source code for the S-functions is in the files rtwdemo_sfun_dlut3D.c and rtwdemo_sfun_dlut4D.c.

legacy_code('sfcn_cmex_generate', defs);

Compile the Generated S-Functions for Simulation

After you generate the C-MEX S-function source files, to compile the S-functions for simulation with Simulink®, call the function legacy_code() again with the first input set to 'compile'.

legacy_code('compile', defs);
### Start Compiling rtwdemo_sfun_dlut3D
    mex('-compatibleArrayDims', '-I/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc16a_342494_6186/tpb227ff9c_7457_42c7_ad22_0e13480900da', '-c', '-outdir', '/tmp/Bdoc16a_342494_6186/tp62fa50dc_2bcd_46d8_a224_d855b41147d3', '/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/directLookupTableND.c')
Building with 'gcc'.
MEX completed successfully.
    mex('rtwdemo_sfun_dlut3D.c', '-compatibleArrayDims', '-I/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc16a_342494_6186/tpb227ff9c_7457_42c7_ad22_0e13480900da', '/tmp/Bdoc16a_342494_6186/tp62fa50dc_2bcd_46d8_a224_d855b41147d3/directLookupTableND.o')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_dlut3D
### Exit

### Start Compiling rtwdemo_sfun_dlut4D
    mex('-compatibleArrayDims', '-I/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc16a_342494_6186/tpb227ff9c_7457_42c7_ad22_0e13480900da', '-c', '-outdir', '/tmp/Bdoc16a_342494_6186/tp5845f6ab_365b_4bc1_b676_60b48134788a', '/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/directLookupTableND.c')
Building with 'gcc'.
MEX completed successfully.
    mex('rtwdemo_sfun_dlut4D.c', '-compatibleArrayDims', '-I/mathworks/devel/bat/Bdoc16a/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc16a_342494_6186/tpb227ff9c_7457_42c7_ad22_0e13480900da', '/tmp/Bdoc16a_342494_6186/tp5845f6ab_365b_4bc1_b676_60b48134788a/directLookupTableND.o')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_dlut4D
### Exit

Generate TLC Block Files for Code Generation

After you compile the S-functions and use them in simulation, you can call the function legacy_code() again with the first input set to 'sfcn_tlc_generate' in order to generate TLC block files to support code generation through Simulink® Coder™. If the TLC block file is not created and you try to generate code for a model that includes the S-function, code generation fails. The TLC block files for the S-functions are rtwdemo_sfun_dlut3D.tlc and rtwdemo_sfun_dlut4D.tlc.

legacy_code('sfcn_tlc_generate', defs);

Generate an rtwmakecfg.m File for Code Generation

After you create the TLC block file, you can call the function legacy_code() again with the first input set to 'rtwmakecfg_generate' to generate an rtwmakecfg.m file to support code generation through Simulink® Coder™. If the required source and header files for the S-functions are not in the same folder as the S-functions, and you want to add these dependencies in the makefile produced during code generation, generate the rtwmakecfg.m file.

legacy_code('rtwmakecfg_generate', defs);

Generate a Masked S-Function Block for Calling the Generated S-Function

After you compile the C-MEX S-function source, you can call the function legacy_code() again with the first input set to 'slblock_generate' to generate a masked S-function block that is configured to call that S-function. The software places the block in a new model and it can be copied to an existing model.

% legacy_code('slblock_generate', defs);

Show the Generated Integration with Legacy Code

The model rtwdemo_lct_lut shows integration with the legacy code. The subsystem TestFixpt serves as a harness for the call to the legacy C function, and the Display blocks compare the output of the function with the output of the built-in Simulink® lookup blocks. The results are identical.

open_system('rtwdemo_lct_lut')
open_system('rtwdemo_lct_lut/TestLut1')
sim('rtwdemo_lct_lut')

Was this topic helpful?