Simulate and Verify Generated C++ Code
In this step of the tutorial, you verify that the results provided by the generated code
are numerically equivalent to the results of the algorithm modeled in Simulink®. You create a test harness model to simulate
CppClassWorkflowKeyIgnition
in normal mode and in
software-in-the-loop mode, and then compare the simulations by using the Simulation Data
Inspector.
In general, 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.
Progress toward achieving IEC 61508, IEC 62304, ISO 26262, EN 50128, or DO-178 certification.
Create a Test Harness
In this section, you create test harness CppClassHarness
from a
blank Simulink model to reference CppClassWorkflowKeyIgnition
, the
model under test. If you have a Simulink
Test™ license, you can create a test harness from the component model using a
simplified workflow. For more information, see Create or Import Test Harnesses and Select Properties (Simulink Test).
The harness model generates the test input for the referenced model.
Open the model
CppClassWorkflowKeyIgnition
.openExample("CppClassWorkflowKeyIgnition.slx");
Open Model Explorer. On the Modeling tab, under Design, click Model Explorer.
In Model Explorer, export the configuration for model
CppClassWorkflowKeyIgnition
.Under Model Hierarchy, expand Simulink Root, right-click CppClassWorkflowKeyIgnition, and select Configuration > Export Active Configuration.
In the Export dialog box, save the configuration as
cpp_sil_configuration
.
In the Model Explorer toolbar, click to create a new model.
Save the new untitled model as
CppClassHarness
.Import the saved configuration to harness model
CppClassHarness
.Under Model Hierarchy, right-click CppClassHarness, and select Configuration > Import.
In the Import dialog box, select
cpp_sil_configuration
, and click Open.If a confirmation dialog box opens, click OK to save the configuration. Then, in Model Explorer, right-click CppClassHarness and save the model.
Close Model Explorer.
At this point, the harness model is configured to use the same configuration parameters as the model under test (
CppClassWorkflowKeyIgnition
). You can verify that the configuration parameters are imported by opening the Configuration Parameters dialog box for each model and comparing the settings.In the Simulink canvas of the harness model
CppClassHarness
, add a Pulse Generator block, two Outport blocks, and a Model block between the output ports and the pulse generator.Double-click the Model block. In the Block Parameters dialog box:
Under Model name, click Browse and select
CppClassWorkflowKeyIgnition
.Click Open, and then click OK.
Connect the Pulse Generator block to the
keyState
input port of Model blockCppClassWorkflowKeyIgnition
, and connect the two Outport blocks to theengineState
andcycleTime
output ports of Model blockCppClassWorkflowKeyIgnition
.Double-click the Pulse Generator block to open the Block Parameters dialog box. In the Block Parameters dialog box:
Set Pulse type to
Sample based
.Set Amplitude to
2
.Set Period (number of samples) to
200
.Set Pulse width (number of samples) to
2
.Set Phase delay (number of samples) to
200
.Set Sample time to
0.01
.Leave the other options with their default settings.
Click OK.
Add labels to top-level input and output signal lines.
Double-click the signal line that connects the pulse generator to the Model block and enter
input
.Double-click the signal line that connects the Model block to the
engineState
output port and enteroutputState
.
Open the Configuration Parameters dialog box for
CppClassHarness
.In the Data Import/Export pane, select Signal logging. Click OK.
Save the model.
Simulate the Model in Normal Mode
Run the harness model in normal mode and capture the results in the Simulation Data Inspector.
In the harness model, open the Model Data Editor by selecting the Modeling tab and clicking Model Data Editor.
In the Model Data Editor, select the Signals tab.
Change the setting in the drop-down list from
Design
toInstrumentation
.In the data table, under the Name column, find the
input
andoutputState
signals and select the corresponding check boxes in the Log Data column.Selected check boxes in the Log Data column specify which signals log simulation data to the Simulation Data Inspector.
Right-click the Model block
CppClassWorkflowKeyIgnition
. From the context menu, select Block Parameters.In the Block Parameters dialog box, verify that Simulation mode is set to
Normal
. Click OK.Click Run to simulate
CppClassHarness
.When the simulation completes, view the simulation results in the Simulation Data Inspector. If the Simulation Data Inspector is not already open, on the Simulation tab under Review Results, click Data Inspector.
For the most recent (current) run, double-click the run name field and rename the run as
CppClassHarness: Normal mode
.Select
input
andoutputState
to plot the signals.
Simulate the Model in SIL Mode
Next, you run the harness model in SIL mode. The SIL simulation generates, compiles, and executes code on your development computer. The Simulation Data Inspector logs results.
In the
CppClassHarness
model window, right-click theCppClassWorkflowKeyIgnition
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. The Model block indicates that simulations run in SIL mode on the top model.Simulate the
CppClassHarness
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.
Once the simulation is complete, in the Simulation Data Inspector, double-click the run name field and rename the new run as
CppClassHarness: SIL mode
.Select
input
andoutputState
to plot the signals.
Compare Simulation Results
Use the Simulation Data Inspector to compare the simulation results.
In the Simulation Data Inspector, click the Compare tab.
In the Baseline field, select
CppClassHarness: Normal mode
.In the Compare To field, select
CppClassHarness: SIL mode
.Click Compare.
The Simulation Data Inspector shows that the normal mode and SIL mode results match. In a similar way, you can compare results from Normal mode and PIL mode. 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 generated model code.
Next, explore ways that you can deploy generated code.