Main Content

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.

  1. Open the model CppClassWorkflowKeyIgnition.

    openExample("CppClassWorkflowKeyIgnition.slx");
  2. Open Model Explorer. On the Modeling tab, under Design, click Model Explorer.

  3. In Model Explorer, export the configuration for model CppClassWorkflowKeyIgnition.

    1. Under Model Hierarchy, expand Simulink Root, right-click CppClassWorkflowKeyIgnition, and select Configuration > Export Active Configuration.

    2. In the Export dialog box, save the configuration as cpp_sil_configuration.

  4. In the Model Explorer toolbar, click New Model button to create a new model.

  5. Save the new untitled model as CppClassHarness.

  6. Import the saved configuration to harness model CppClassHarness.

    1. Under Model Hierarchy, right-click CppClassHarness, and select Configuration > Import.

    2. In the Import dialog box, select cpp_sil_configuration, and click Open.

    3. If a confirmation dialog box opens, click OK to save the configuration. Then, in Model Explorer, right-click CppClassHarness and save the model.

    4. 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.

  7. 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.

  8. Double-click the Model block. In the Block Parameters dialog box:

    1. Under Model name, click Browse and select CppClassWorkflowKeyIgnition.

    2. Click Open, and then click OK.

  9. Connect the Pulse Generator block to the keyState input port of Model block CppClassWorkflowKeyIgnition, and connect the two Outport blocks to the engineState and cycleTime output ports of Model block CppClassWorkflowKeyIgnition.

    Harness model CppClassHarness shows the connected function generator, Model block CppClassWorkflowKeyIgnition, outputs engineState and cycleTime.

  10. 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.

  11. 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 enter outputState.

    The harness model for CppClassWorkflowKeyIgnition contains the input stimulus pulse generator and labeled input signal and output signal.

  12. Open the Configuration Parameters dialog box for CppClassHarness.

  13. In the Data Import/Export pane, select Signal logging. Click OK.

  14. 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.

  1. In the harness model, open the Model Data Editor by selecting the Modeling tab and clicking Model Data Editor.

  2. In the Model Data Editor, select the Signals tab.

  3. Change the setting in the drop-down list from Design to Instrumentation.

  4. In the data table, under the Name column, find the input and outputState signals and select the corresponding check boxes in the Log Data column.

    The Model Data Editor panel displays at the bottom of the Simulink window and shows the Log Data check boxes selected for the input and outputState signals.

    Selected check boxes in the Log Data column specify which signals log simulation data to the Simulation Data Inspector.

  5. Right-click the Model block CppClassWorkflowKeyIgnition. From the context menu, select Block Parameters.

  6. In the Block Parameters dialog box, verify that Simulation mode is set to Normal. Click OK.

  7. Click Run to simulate CppClassHarness.

  8. 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.

  9. For the most recent (current) run, double-click the run name field and rename the run as CppClassHarness: Normal mode.

  10. Select input and outputState to plot the signals.

    Simulation results for input and outputState 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.

  1. In the CppClassHarness model window, right-click the CppClassWorkflowKeyIgnition model block and select Block Parameters.

  2. In the Block Parameters dialog box, set Simulation mode to Software-in-the-loop (SIL) and Code Interface to Top model. Click OK. The Model block indicates that simulations run in SIL mode on the top model.

    The CppClassWorkflowKeyIgnition Model block indicates "SIL: Top".

  3. 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.

  4. 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.

  5. Select input and outputState to plot the signals.

Compare Simulation Results

Use the Simulation Data Inspector to compare the simulation results.

  1. In the Simulation Data Inspector, click the Compare tab.

  2. In the Baseline field, select CppClassHarness: Normal mode.

  3. In the Compare To field, select CppClassHarness: SIL mode.

  4. 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.

The Simulation Data Inspector shows the outputState waveform and indicates that the simulation results match.

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.