This example shows how to call external functions from a simulation or generated code by using the Legacy Code Tool.
Time: 45 minutes
How to evaluate a C function as part of a Simulink® model simulation
How to call a C function from code generated by Simulink® Coder™
|On this page…|
Simulink® models are one part of Model-Based Design. For many applications, a design also includes a set of existing C functions that have been tested and validated. The ability to easily integrate these functions into a Simulink® model and generated code is critical to using Simulink® in the controls development process.
This module shows how to create a custom Simulink® block that calls an existing C function. Once the block is part of the model, you can take advantage of the simulation environment to further test the system.
As an example, the Lookup blocks (lookup tables) in the PI controllers are replaced with calls to an existing C function. The function is defined in the files SimpleTable.c and SimpleTable.h.
To specify a call to an existing C function, you use an S-Function block. You can automate the process of creating the S-Function block by using the Simulink® Legacy Code Tool. Using this tool, you specify an interface for your existing C function. The tool then uses that interface to automate creation of an S-Function block.
Complete steps 1 through 6 below to create an S-Function block for an existing C function SimpleTable.c. A link to more information on using the Legacy Code Tool is provided at the end of this module.
The data structure def defines the function interface to the existing C code.
S-function creation is a one-time task. Once the block exists, you can reuse it in any model.
Steps 1 through 5 created an S-function block that calls the specified function at each time step during simulation. Step 6 creates a TLC file, which is the component of an S-Function that specifies how Simulink® Coder™ generates code for the block.
When you integrate existing C code with a Simulink® model, always validate the results.
In this example, you replace Lookup blocks with an existing C function. To validate the replacement, you compare simulation results produced with the Lookup block with results produced with 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 output from the lookup table is the absolute value of the input.
The lookup table output clips the output at the input limits.
The following figure shows the validation results. Note that the existing C code and the Simulink® table block provide the same output values.
After you validate the functionality of the existing C function code as a standalone component, validate the S-function in the model. Use the test harness model to complete the validation.
The simulation results match the expected golden values:
Simulink® Coder™ uses the TLC file to process the S-Function block like any other block in the system. Calls to the C code of the S-Function block:
Can use data objects
Are subject to expression folding, an operation that combines multiple computations into a single output calculation
The generated code now calls the SimpleTable C function.
The following figures show the generated code before and after the C code integration. Before the integration, the generated code called rt_Lookup. After the integration, the generated code calls the C function SimpleTable.