Main Content

Code Verification and Validation Using PIL Simulation on Android

This example shows how to use the Simulink® Support Package for Android® Devices for code verification and validation using processor-in-the-loop (PIL) simulation on Android devices. It introduces the Simulink code generation and verification workflow by showing you how to configure a Simulink model to run PIL simulations on Android devices such as phones or tablets.

In a PIL simulation, the generated code runs on Android devices. With PIL simulations, you can test whether your model and generated code are numerically equivalent to one another, observe code coverage, and perform code execution profiling. The PIL verification process is a crucial part of the development cycle to ensure that the behavior of the deployed code matches the design.

For more information on PIL simulation, see SIL and PIL Simulations (Embedded Coder).

Prerequisites

For more information on how to use the Simulink Support Package for Android Devices to run a Simulink model on your Android device, see Getting Started with Android Devices.

Required Hardware

  • Android device, such as a phone or tablet

  • USB cable

Verify Generated Code Using PIL Simulation for Top Model

This section shows how to verify the code that is generated for a top model by running a PIL simulation. With this approach:

  • You can verify the generated code for a top model.

  • You can switch the entire model between normal and PIL simulation modes.

  • You must configure the model to load test vectors or stimulus inputs from the MATLAB® workspace.

1. Open the androidTopModelPil Simulink model.

2. In the Simulink model, on the Apps tab, in the Apps section, under Code Verification, Validation, and Test, click SIL/PIL Manager.

3. On the SIL/PIL tab, set SIL/PIL Mode to Processor-in-the-Loop (PIL).

4. In the Run Automated Verification section, click Run Verification.

5. When the PIL simulation completes, it outputs a logsOut variable, containing the PIL simulation results, to the base workspace. You can access the logged data for signals count_a and count_b using these commands.

count_a = get(logsOut,'count_a');
count_a.Values.Data
count_b = get(logsOut,'count_b');
count_b.Values.Data

Verify Generated Code Using PIL Simulation for Simulink Model Blocks

This section shows how to verify the generated code for a referenced model by running a PIL simulation. With this approach:

  • You can verify code generated for referenced models.

  • You can switch a model block between normal and PIL simulation modes.

  • You must provide a test harness model to supply a test vector or stimulus inputs.

1. Open the androidModelPilBlock Simulink model.

This Simulink model contains two Model blocks, CounterA and CounterB, that both point at the same referenced model. This model runs one of the Model blocks in PIL simulation mode and the other in normal mode.

2. In the Simulink model, on the Apps tab, in the Apps section, under Code Verification, Validation, and Test, click SIL/PIL Manager.

3. On the SIL/PIL tab, set System Under Test to Model blocks in SIL/PIL mode.

4. The Simulink model has been preconfigured to run the CounterA block in PIL simulation mode, by setting its Simulation mode parameter to Processor-in-the-loop (PIL), and the CounterB block in the normal mode, by setting its Simulation mode parameter to Normal.

5. In the Simulink model, on the SIL/PIL tab, click Run Verification to start the PIL simulation.

6. While the model runs, Scope1 displays the PIL simulation output running on the Android device. Scope2 displays the normal mode simulation output.

Verify Generated Code Using PIL Block

This section shows how to use a PIL block for subsystem code verification. With this approach:

  • You can verify the code generated for a subsystem.

  • You must provide a test harness model to supply a test vector or stimulus inputs.

  • You must swap your original subsystem with a generated PIL block; Note that, if you save your model in this state, you can lose your original subsystem.

Open the androidPilBlock Simulink model.

To enable PIL, follow these steps.

1. In the Simulink model, on the Modeling tab, click Model Settings.

2. In the Configuration Parameters dialog box, in the left pane, select Code Generation > Verification.

3. In the Advanced Parameters section, set Create block to PIL.

To create a PIL block for the Controller subsystem, follow these steps.

1. Right-click the Controller subsystem and select C/C++ Code > Deploy this Subsystem to Hardware.

2. In the Build code for Subsystem dialog box, click Build to create a PIL version of the Controller subsystem block in a new model.

To run PIL simulation, follow these steps.

1. Copy the Controller PIL subsystem block from the new model to the PIL Block model.

2. On the Simulation tab, click Run to start the PIL simulation.

Once you start simulating the model, the PIL executable starts running on the Android device. To switch between the original and PIL block subsystems, double-click the Manual Switch block. Double-click the Numerical Difference Scope block to see the difference between the simulated Controller subsystem and the PIL block running on the hardware.

Perform Code Profiling Using PIL

You can perform code profiling while using PIL to verify code. Code profiling enables you to check whether the generated code meets real-time performance requirements. You can identify the tasks that require the most time and then investigate whether you can increase speed with an acceptable reduction of functionality.

You can enable code profiling along with PIL simulation by using the SIL/PIL Manager app, which enables basic code profiling, or by using the Configuration Parameters dialog box, which provides advanced options for code profiling.

To enable code profiling from the SIL/PIL Manager app, follow these steps.

1. In the Simulink model, on the Apps tab and select SIL/PIL Manager.

2. In the SIL/PIL tab, set SIL/PIL Mode to Processor-in-the-Loop(PIL).

3. Click Settings and select Task Profiling.

To enable code profiling from the Configuration Parameters dialog box, follow these steps.

1. In the Simulink model, on the Modeling tab, click Model Settings.

2. In the Configuration Parameters dialog box, in the left pane, select Code Generation > Verification.

3. In the Advanced Parameters section, set Create block to PIL.

4. To profile the execution time for each rate in the model, select Measure task execution time.

5. By default, the Measure function execution times parameter is set to Off. You can set this parameter to Coarse (referenced models and subsystems only) or to Detailed (all function call sites).

6. Set Save options to All data. Verify that the name of the Workspace variable is executionProfile.

7. Click Apply, and then OK.

You can perform code profiling for any of the types of PIL simulation described in the previous sections of this example. Once the PIL simulation is complete, the executionProfile variable appears in the MATLAB workspace.

Obtain the profiling report, and analyze the various turnaround and execution times.

report(executionProfile)

See Also