Main Content

Code Verification and Validation with PIL

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由于

  • Arduino Uno

  • Arduino Nano 3.0

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino MKR ZERO

  • Arduino Nano 33物联网

  • 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 theGetting Started with Arduino Hardwareexample.

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 theArduino公益诉讼集团k model.

2. ClickSimulation > Model Configuration Parametersto openConfiguration Parametersdialog.

3. Select theHardware Implementationpane and select your required Arduino hardware from theHardware boardparameter list. Do not change any other settings.

4. ClickOK.

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 theArduino公益诉讼集团k model. This model is configured forArduinohardware. The objective here is to create a PIL block out of theControllersubsystem that you will run on the Arduino hardware.

2. Enable PIL feature in theArduino公益诉讼集团k Model. On theHardwaretab, clickHardware Settings. In theSearchbar, enterCreateSILPILBlock. In theCreate blockparameter, selectPIL. ClickOK.

3. Create a PIL block for theControllersubsystem. Right-click on theControllersubsystem. SelectC/C++ Code>Deploy this Subsystem to Hardwareto createPILversion of theControllersubsystem block in a new model.

4. Copy the PIL block and place it in thePlace PIL block hereholder in theArduino公益诉讼集团k model.

5. In theSimulationtab, clickRunto 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 theManual Switchblock. Double click on theNumerical Differencesblock to see the difference between the simulatedControllersubsystem 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 theModel Block PIL model. This model is configured forArduinohardware. 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 theAppstab, underCode Verification, Validation, and Test中,选择SIL/PIL Manager.

3. In theSIL/PILtab, setSystem Under TesttoModel blocks in SIL/PIL mode.

4. Configure and runCounterAblock in PIL simulation mode. To open theCounterAblock mask, right-click the block and selectBlock Parameters (ModelReference). In theFunction Block Parameters: CounterAdialog box, setSimulation modetoProcessor-in-the-loop (PIL). ClickOK.

5. In theSIL/PILtab, clickRun Verificationto start the PIL simulation.

6. When the model starts running,Scope1displays the PIL simulation output running on the target hardware whileScope2shows 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 theTop Model PIL model. This model is configured forArduinohardware.

2. In theAppstab, underCode Verification, Validation, and Test中,选择SIL/PIL Manager.

3. In theSIL/PILtab, setSIL/PIL modetoProcessor-in-the-loop (PIL).

4. ClickRun Verificationto start the PIL simulation.

5. When the PIL simulation terminates, alogsOutvariable is created in the base workspace. ThelogsOutdata contains PIL simulation results. You can access the logged data for signalscount_aandcount_busing the following commands:

count_a = get(logsOut,'count_a');

count_a.Values.Data

count_b =得到(logsOut count_b);

count_b.Values.Data

Summary

This example introduced code verification workflow using PIL simulation.