Simulate and Verify Generated C Code
In this step of the tutorial, you verify that when executed, the generated code is
numerically equivalent to the algorithm modeled in Simulink®. You use a test harness model to simulate
ComponentDeploymentFcn
in normal mode and in SIL mode, and 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 whether simulation and code execution results are numerically equivalent.
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
ComponentDeploymentFcnHarness
to verify whether simulation and code
execution results for component model ComponentDeploymentFcn
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
The model ComponentDeploymentFcnHarness
references the
model-under-test, ComponentDeploymentFcn
, by using a Model
block. The harness model generates test inputs for the referenced model. You can easily
switch the Model block between the normal, SIL, and PIL simulation modes.
Save copies of the models
ComponentDeploymentFcn
andComponentDeploymentFcnHarness
in your current working folder.Open the model
ComponentDeploymentFcnHarness
.openExample('ComponentDeploymentFcnHarness');
Open the model
ComponentDeploymentFcn
.openExample('ComponentDeploymentFcn');
In the
ComponentDeploymentFcnHarness
model, right-click the Model block and select Subsystem & Model Reference > Refresh Selected Model Block.Configure both models for use with the Simulation Data Inspector by adjusting these model configuration parameter settings in the Data Import/Export pane of the Configuration Parameters dialog box:
Select States.
Set Format to
Dataset
.Select Record logged workspace data in Simulation Data Inspector.
Click OK. Then, save the models.
Simulate the Model in Normal Mode
Run the test harness model in normal mode and capture the results in the Simulation Data Inspector.
In model
ComponentDeploymentFcnHarness
, right-click the Model block forComponenDeploymentFcn
. From the context menu, select Block Parameters.In the Block Parameters dialog box, verify that the parameter Simulation mode is set to
Normal
. Click OK.Simulate
ComponentDeploymentFcnHarness
.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, in the Properties pane, double-click the Run field and then rename the run as
ComponentDeploymentFcn: Normal mode
.Change the Simulation Data Inspector visualization to sparklines. Click the Visualizations icon and click Sparklines. Then, in the Filter Signals pane, expand the States node and then select UnitDelay (10).
In the Multidimensional Signal dialog box, select Convert to channels.
Expand the UnitDelay (10) node.
Select one or more of the UnitDelay signal lines. The Simulation Data Inspector displays the sparklines for the selected channels.
This figure shows sparklines for UnitDelay (1) and UnitDelay (2).
Simulate the Model in SIL Mode
The SIL simulation compiles and executes the generated code on your development computer. The Simulation Data Inspector logs results.
In the
ComponentDeploymentFcnHarness
model window, right-click theComponenDeploymentFcn
Model block and select Block Parameters.In the Block Parameters dialog box, make these parameter changes:
Set Simulation mode to
Software-in-the-loop (SIL)
.Set Code interface to
Top model
.
Click OK.
For the component model
ComponenDeploymentFcn
, in the Configuration Parameters dialog box, make these model configuration parameter changes:In the Code Generation pane, set Toolchain to
Automatically locate an installed toolchain
.In the Code Generation > Optimization pane, clear Remove internal data zero initialization.
Save the configuration changes.
Simulate the
ComponentDeploymentFcnHarness
model. The Diagnostic Viewer panel opens and displays build and code generation status. If a code generation report window opens, minimize it for exploration later in this tutorial.When the simulation is complete, in the Simulation Data Inspector Properties pane, double-click the Run field and then rename the new run as
ComponentDeploymentFcn: SIL mode
.Change the Simulation Data Inspector visualization to sparklines. Click the Visualizations icon and click Sparklines. Then, in the Filter Signals pane, expand the States node and then select delay (10).
In the Multidimensional Signal dialog box, select Convert to channels.
Expand the delay (10) node.
Select one or more of the UnitDelay signal lines. The Simulation Data Inspector displays the sparklines for the selected channels.
Compare Simulation Results
In the Simulation Data Inspector:
Click the Compare tab.
From the Baseline list, select
ComponentDeploymentFcn: Normal mode
.From the Compare To list, select
ComponentDeploymentFcn: SIL mode
.Click Compare.
This 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 code 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.