Main Content

Code Verification and Validation with PIL on Arduino Hardware

This example shows you how to use Simulink® Support Package for Arduino® Hardware for code verification and validation using PIL.

Supported Hardware:

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Due

  • Arduino Uno

  • Arduino Nano 3.0

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino MKR ZERO

  • Arduino Nano 33 IoT

  • Arduino Nano 33 BLE Sense

The provided models are pre-configured for Arduino Mega 2560 and can be run on any of the board listed in the "Supported Hardware" section, by changing the "Hardware board" parameter in the configuration parameters dialog box of the model as described in Task 1 of this example.

Introduction

In this example you will learn how to configure a Simulink model to run Processor-In-the-Loop (PIL) simulation. In PIL simulation, the generated code runs on target hardware. The results of the PIL simulation are transferred to Simulink to verify the numerical equivalence of the simulation and the code generation results. The PIL verification process is a crucial part of the design cycle to ensure that the behavior of the deployment code matches the design.

This example introduces the Simulink code generation and verification workflow by showing you how to:

  • Configure a Simulink model to run PIL simulations on the Arduino hardware.

Prerequisites

Before you start with this example, we recommend you complete the Get Started with Arduino Hardware example.

Dependency

Embedded Coder

Task 1 - Configure the Model for Supported Arduino Hardware

In this task, you will configure the model for the supported Arduino board.

1. Open the arduino_pil_block Simulink model.

2. To open Configuration Parameters dialog box, in the Simulink Editor, go to the Model Settings tab and click Model Settings.

3. Select the Hardware Implementation pane and select your required Arduino hardware from the Hardware board parameter list. Do not change any other settings.

4. Click OK.

Task 2 - Verify the generated code for a subsystem using a PIL block

This example 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 test vector or stimulus inputs

  • You must swap your original subsystem with a generated PIL block; you should be careful to avoid saving your model in this state as you would lose your original subsystem

1. Open the arduino_pil_block Simulink model. This model is configured for Arduino hardware. The objective here is to create a PIL block out of the Controller subsystem that you will run on the Arduino hardware.

2. Enable PIL feature in the Arduino PIL Block Model. On the Hardware tab, click Hardware Settings. In the Search bar, enter CreateSILPILBlock. In the Create block parameter, select PIL. Click OK.

3. Create a PIL block for the Controller subsystem. Right-click on the Controller subsystem. Select C/C++ Code > Deploy this Subsystem to Hardware to create PIL version of the Controller subsystem block in a new model.

4. Copy the PIL block and place it in the Place PIL block here holder in the Arduino PIL Block model.

5. In the Simulation tab, click Run to start the PIL simulation.

6. Once you start simulating the model, the PIL executable starts running on the Arduino hardware. You can switch between the original and PIL block subsystems by double clicking on the Manual Switch block. Double click on the Numerical Differences block to see the difference between the simulated Controller subsystem and the PIL block running on the hardware.

Task 3 - Verify referenced model code using PIL

This example 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 must provide a test harness model to provide a test vector or stimulus inputs

  • You can easily switch a Model block between normal and PIL simulation mode

1. Open the arduino_model_pil_block Simulink model.

This model is configured for Arduino hardware. The model contains two Model blocks that both point to the same referenced model. You will configure one Model block to run in PIL mode while the other one will be set to normal mode.

2. In the Apps tab, under Code Verification, Validation, and Test, select SIL/PIL Manager.

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

4. Configure and run CounterA block in PIL simulation mode. To open the CounterA block mask, right-click the block and select Block Parameters (ModelReference). In the Function Block Parameters: CounterA dialog box, set Simulation mode to Processor-in-the-loop (PIL). Click OK.

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

6. When the model starts running, Scope1 displays the PIL simulation output running on the target hardware while Scope2 shows the normal mode simulation output.

Task 4 - Verify top model code using PIL

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

  • You can verify code generated for a top model

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

  • You can easily switch the entire model between normal and PIL simulation mode

1. Open the arduino_top_model_pil Simulink model.

2. In the Apps tab, under Code Verification, Validation, and Test, select SIL/PIL Manager.

3. In the SIL/PIL tab, set SIL/PIL mode to Processor-in-the-loop (PIL).

4. Click Run Verification to start the PIL simulation.

5. When the PIL simulation terminates, a logsOut variable is created in the base workspace. The logsOut data contains PIL simulation results. You can access the logged data for signals count_a and count_b using the following commands:

count_a = get(logsOut,'count_a');

count_a.Values.Data

count_b = get(logsOut,'count_b');

count_b.Values.Data