Main Content

Compare Model Simulation and Generated Code Results

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

  • Optimize your code.

  • Progress toward achieving IEC 61508, IEC 62304, ISO 26262, EN 50128, or DO-178 certification.

Inspect and Configure Test Harness Model

Model rtwdemo_roll_harness references the model-under-test, rtwdemo_roll, 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.

  1. Open model rtwdemo_roll_harness. If you closed your copy of model rtwdemo_roll, reopen it.

  2. In the rtwdemo_roll_harness model, right-click the Model block and select Subsystem & Model Reference > Refresh Selected Model Block.

  3. Save a copy of rtwdemo_roll_harness in the current working folder.

  4. Open the Configuration Parameters dialog boxes for rtwdemo_roll_harness and rtwdemo_roll.

  5. To run SIL and PIL simulations, on the Code Generation pane, verify that parameter Generate code only is cleared. Do this for both models.

  6. For both models, on the Hardware Implementation pane, expand Device details. Verify that Support long long is selected.

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

  1. In the rtwdemo_roll_harness model, open the Model Data Editor. On the Modeling tab, click Model Data Editor.

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

  3. Set the Change view list to Instrumentation.

  4. In the data table, select all rows.

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

  6. Right-click the Model block, Roll Axis Autopilot. From the context menu, select Block Parameters.

  7. In the Block Parameters dialog box, for Simulation mode, verify that the Normal option is selected. Click OK.

  8. Simulate rtwdemo_roll_harness.

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

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

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

  1. In the rtwdemo_roll_harness model window, right-click the Roll Axis Autopilot 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.

  3. Exclude external code files from the build process. In the Configuration Parameters dialog box for model rtwdemo_roll, set Code Generation > Custom Code > Additional build information > Source files to the default value, which is empty. Save the model.

  4. Simulate the rtwdemo_roll_harness model.

    Minimize the code generation report window for exploration later in this tutorial.

  5. In the Simulation Data Inspector, double-click the run name field and rename the new run as roll_harness: SIL mode.

  6. Select Ail_Cmd to plot the signal.

  7. Reconfigure the build process for model rtwdemo_roll to include the external source files roll_input_data.c and roll_heading_mode.c. In the Model Configuration Parameters dialog box, set Code Generation > Custom Code > Additional build information > Source files to roll_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:

  1. Click the Compare tab.

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

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

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

Next, explore ways that you can deploy generated code.