Main Content

Code Verification and Validation with PIL and External Mode

This example shows you how to use Embedded Coder® Support Package for BeagleBone Black™ Hardware for code verification and validation using PIL and External Mode features.

Introduction

In this example, you will learn how to configure a Simulink model to run Processor-In-the-Loop (PIL) and External Mode simulations. In a 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.

The External mode feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware. The effects of the parameter tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink.

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 BeagleBone Black hardware.

  • Configure a Simulink model to run External Mode simulation on BeagleBone Black hardware.

With this approach:

  • You can verify code generated for subsystems

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

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

Prerequisites

We recommend completing Getting Started with Embedded Coder Support Package for BeagleBone Black Hardware example.

Task 1: Verify generated code using PIL block

This section shows how the automatically generated PIL block can be used for verification. The objective here is to create a PIL block out of the Controller subsystem running on the BeagleBone Black hardware.

1. Open the Beaglebone PIL Block model.

2. Enable PIL feature in the Beaglebone 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. In the Build code for Subsystem: Controller dialog box, click Build to create a 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 Beaglebone 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 Beaglebone 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 2: Verify generated code using PIL Simulation for Model Blocks

This section shows how you can verify the automatically 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 test vector or stimulus inputs

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

1. Open Model Block PIL model. The model contains two Model blocks that both point at the same referenced model. You will configure one of the Model blocks to run in PIL simulation mode and the other in 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. Scope2 displays the Normal mode simulation output.

Task 3: Verify generated code using PIL simulation for top models

This section shows how you can verify the automatically generated code for a top 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 Top Model PIL 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. In the SIL/PIL tab, click Run Verification to start the PIL simulation.

5. When the PIL simulation is completed, 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

Task 4: External Mode

This section shows you how to run a Simulink model in External mode. When you are prototyping and developing an algorithm, it is useful to monitor and tune the algorithm while it runs on hardware. The External mode feature in Simulink enables this capability.

1. Open the External Mode model.

2. In the Hardware tab, click Monitor & Tune to start the external mode simulation. You can stop the simulation at any time by clicking Monitor & Tune again.

3. After the model builds, deploys, ans starts running on the target hardware, you can double-click the Manual Switch block to change the input source. Open the Gain block to change the signal gain. Finally, double-click on the Scope block to view the External mode simulation results.

For additional external mode simulation settings, you can open the External Mode Control Panel. In the Hardware tab, click Control Panel to open the External Model Control Panel.