Simulate and Verify C Code
In this step of the tutorial, you verify that when executed, the code is numerically
equivalent to the algorithm modeled in Simulink®. You use a test harness model to simulate RollAxisAutopilot
in normal mode and in SIL mode, then compare the simulations by using the Simulation Data
Inspector.
To test generated code, you can run software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulations. A SIL simulation compiles and runs the generated code on your development computer. A PIL simulation cross-compiles source code on your development computer. The PIL simulation then downloads and runs the object code on a target processor or an equivalent instruction set simulator. You can use SIL and PIL simulations to:
Verify the numeric behavior of your code.
Collect code coverage and execution-time metrics.
Meet IEC 61508, IEC 62304, ISO 26262, EN 50128, or DO-178 certification objectives.
This tutorial shows you how to use test harness
RollAxisAutopilotHarness
to verify whether simulation and code execution
results for component model RollAxisAutopilot
are equivalent. However, if
you have a Simulink
Test™ license, you can use the Test Manager to create a test harness from the
component model. See Create or Import Test Harnesses and Select Properties (Simulink Test).
Inspect and Configure Test Harness Model
Model RollAxisAutopilotHarness
references the model-under-test,
RollAxisAutopilot
, through a Model block. The harness
model generates test inputs for the referenced model. You can easily switch the
Model block between the normal, SIL, or PIL simulation modes.
Save copies of models
RollAxisAutopilot
andRollAxisAutopilotHarness
in your current working folder.Open the model
RollAxisAutopilotHarness
.openExample('RollAxisAutopilotHarness');
Open the model
RollAxisAutopilot
.openExample('RollAxisAutopilot')
In the
RollAxisAutopilotHarness
model, right-click the Model block and select Subsystem & Model Reference > Refresh Selected Model Block.For both models, adjust these model configuration parameter settings:
On the Code Generation pane, clear Generate code only.
on the Hardware Implementation pane, expand Device details. Select Support long long.
Click OK. Then, save the models.
Simulate the Model in Normal Mode
Run the harness model in normal mode and capture the results in the Simulation Data Inspector.
In the
RollAxisAutopilotHarness
model, open the Model Data Editor. On the Modeling tab, click Model Data Editor.In the Model Data Editor, select the Signals tab.
Set the Change view list to
Instrumentation
.In the data table, select all rows.
To configure signals to log simulation data to the Simulation Data Inspector, select a cleared check box in the Log Data column. When you are finished, make sure that all of the check boxes in the column are selected.
Right-click the Model block,
Roll Axis Autopilot
. From the context menu, select Block Parameters.In the Block Parameters dialog box, for Simulation mode, verify that the
Normal
option is selected. Click OK.Simulate
RollAxisAutopilotHarness
.When the simulation is done, view the simulation results in the Simulation Data Inspector. If the Simulation Data Inspector is not already open, on the Simulation tab, click Data Inspector.
For the most recent (current) run, double-click the run name field and rename the run:
roll_harness: Normal mode
.Select
Ail_Cmd
to plot the signal.
Simulate the Model in SIL Mode
The SIL simulation generates, compiles, and executes code on your development computer. The Simulation Data Inspector logs results.
In the
RollAxisAutopilotHarness
model window, right-click theRoll Axis Autopilot
model block and select Block Parameters.In the Block Parameters dialog box, set Simulation mode to
Software-in-the-loop (SIL)
and Code Interface toTop model
. Click OK.Exclude external code files from the build process. In the Configuration Parameters dialog box for model
RollAxisAutopilot
, set Code Generation > Custom Code > Code information > Source files to the default value, which is empty. Save the model.Simulate the
RollAxisAutopilotHarness
model.Minimize the code generation report window for exploration later in this tutorial.
In the Simulation Data Inspector, double-click the run name field and rename the new run as
roll_harness: SIL mode
.Select
Ail_Cmd
to plot the signal.Reconfigure the build process for model
RollAxisAutopilot
to include the external source filesroll_input_data.c
androll_heading_mode.c
. In the Model Configuration Parameters dialog box, set Code Generation > Custom Code > Code information > Source files toroll_input_data.c roll_heading_mode.c
. Click Apply, close the dialog box, and save the model.
Compare Simulation Results
In the Simulation Data Inspector:
Click the Compare tab.
In the Baseline field, select
roll_harness: Normal mode
.In the Compare To field, select
roll_harness: SIL mode
.Click Compare.
The Simulation Data Inspector shows that the normal mode and SIL mode results match. Comparing the results of normal mode simulation with SIL and PIL simulations can help you verify that the generated application performs as expected.
Alternatively, you can use the SIL/PIL Manager app for a simplified workflow of verifying code generated from a model.
Next, explore ways that you can deploy generated code.