Call existing, external functions from a simulation or from the generated code by using the Legacy Code Tool.
Learn how to:
Evaluate a C function as part of a Simulink® model simulation.
Call a C function from the code that you generate from a model.
For information about the example model and other examples in this series, see Generate C Code from a Control Algorithm for an Embedded System.
Open the example model,
For many Model-Based Design applications, in addition to Simulink® models, a design includes a set of existing C functions that have been tested and validated. You can integrate these functions into a Simulink® model and generate code that uses the functions.
In this example, you create a custom Simulink® block that calls an existing C function. You then include the block in a model and test the overall system through model simulation in Simulink®.
In the example model, you can replace the Lookup blocks (lookup tables) in the PI controllers with calls to an existing C function. The function is defined in the files
To specify a call to an existing C function, use an S-Function block. You can automate the creation of the S-Function block by using the Legacy Code Tool. In the tool, you first specify an interface for your existing C function. The tool then uses that interface to create an S-Function block.
Use the Legacy Code Tool to create an S-Function block for the existing C function in
def = legacy_code('initialize')
You can use the structure
def to define the function interface to the existing C code.
The generated S-Function block calls the C function in
SimpleTable.c. You can now use and reuse this S-Function block in models.
This command creates a TLC file, which is the component of an S-Function that specifies how to generate code for the block.
When you integrate existing C code in a Simulink® model, first validate the generated S-Function block.
To validate the replacement of the Lookup blocks, compare the simulation results produced by the Lookup blocks with the results produced by the new S-Function block.
The Sine Wave block produces output values from [-2 : 2].
The input range of the lookup table is from [-1 : 1].
The lookup table outputs the absolute value of the input.
The lookup table clips the output at the input limits.
The figure shows the validation results. The existing C code and the Simulink® table block produce the same output values.
After validating the existing C function code as a standalone component, you can validate the S-function in the model. To complete the validation, use the test harness model.
The simulation results match the golden values.
The code generator uses the TLC file to process the S-Function block like any other block. The code generator can implement expression folding with the S-Function block, an operation that combines multiple computations into a single output calculation.
The generated code now calls the
SimpleTable C function.
The figures show the generated code before and after the C code integration. Before the integration, the code calls a generated lookup routine. After the integration, the generated code calls the C function
For the next example in this series, see Integrate Generated Code with Custom Code in External Environment.