Code Verification and Profiling Using Processor-In-the-Loop Testing

In the processor-in-the-loop (PIL) simulation, the control algorithm executes in the target hardware, but the plant model runs on the host machine. The plant model (running on the host machine) simulates the input and output signals for the controller (running on the target hardware) and communicates with the controller by using the serial communication interface. This functionality allows you to use PIL simulation to determine the execution time on the target hardware, which you can then compare with the execution time for simulating the model on the host machine.

The execution time or the performance metrics of an algorithm that you obtain from PIL simulation, helps you to detect algorithm overrun on the target hardware. The PIL profiling report indicates the average and maximum execution times of an algorithm on the target hardware. This example explains PIL profiling on the Texas Instruments™ LAUNCHXL-F28379D hardware board.

We use an example model mcb_pmsm_foc_sim.slx to demonstrate code verification in PIL simulation. This example shows PIL profiling for the Current Control subsystem in the example model. This subsystem includes the Field-Oriented Control (FOC), current scaling (per-unit conversion), speed measurement, and rotor position scaling (computation of angle from the encoder position counts) algorithms. The PIL profiling report shows the average execution and maximum execution times of the control algorithm in the target hardware.

This section addresses these tasks:

  • Verify code execution by using PIL testing by comparing the algorithm in the simulation and target hardware operating modes.

  • Perform PIL profiling by measuring the algorithm execution time in the target hardware and generate the PIL profiling report.

Required MathWorks Products

  • Embedded Coder®

  • Embedded Coder Support Package for Texas Instruments C2000™ Processors

Supported Hardware

  • LAUNCHXL-F28379D controller hardware board

Prepare PIL Model

Use these steps to prepare the PIL model for profiling:

  1. Open the model mcb_pmsm_foc_sim.slx by using this command:

    open_system('mcb_pmsm_foc_sim.slx');

    This model simulates the PMSM motor and FOC algorithm for closed-loop speed control.

  2. On the Hardware tab of the Simulink toolstrip, click Hardware Settings.

  3. In the Configuration Parameters window, under Hardware Implementation, set the Hardware board field to TI Delfino F28379D LaunchPad.

Verify Code by Using PIL

Use these steps to verify the code in PIL:

  1. In the Configuration Parameters dialog box, select these configuration settings under Hardware Implementation > Hardware board settings > Target hardware resources > PIL:

    1. Communication Interface - Select serial.

    2. SCI module - Select SCI_A.

    3. Serial port in MATLAB preferences - Model automatically detects the communication port to which you have connected the hardware. This parameter remains unchanged for the rest of the currently active MATLAB® session. Click the Refresh button to detect the communication port again.

  2. Open the script file mcb_PIL_config_TI.m to set the configuration parameters:

    edit('mcb_PIL_config_TI.m');
  3. Update the model name and stop time in the script.

  4. Run the script to update the configuration parameters of the simulation model and the PIL preferences.

  5. Right-click the Current Control subsystem in the mcb_pmsm_foc_sim.slx example model. Under the C/C++ Code menu, select Deploy this Subsystem to Hardware.

    The system displays the Build code for Subsystem dialog box. Set the Storage Class to Inlined for all parameters.

  6. Click Build to create a model named untitiled that includes a PIL subsystem called Current Control.

  7. Rename the Current Control subsystem to Current Control (PIL).

  8. Copy the Current Control (PIL) subsystem and replace the Current Control subsystem in the mcb_pmsm_foc_sim.slx example model.

    In the PIL mode, the system deploys the Current Control (PIL) subsystem to the target and executes the subsystem in the target hardware.

  9. To compare the algorithm execution on the host machine simulation and in the PIL simulation, connect the Current Control subsystem parallely to the Current Control (PIL) subsystem. In addition, enable signal logging in the subsystem outputs.

  10. On the Simulink toolstrip, select the SIL/PIL Manager app from the Apps tab.

  11. On the SIL/PIL toolstrip, select SIL/PIL Sim Only.

  12. Select Model blocks in SIL/PIL mode in the System Under Test field.

  13. Click Run SIL/PIL on the SIL/PIL toolstrip to build the Current Control (PIL) subsystem and deploy it to the target.

    After the system deploys the subsystem, the Current Control (PIL) subsystem executes on the target hardware processor, while the plant model runs on the host machine.

Analyze PIL Profiling Results

When PIL simulation ends, the system generates a profiling report.

Note

PIL simulation takes more time than the host-machine-based simulation. This is because of the serial communication (related to inputs and outputs of the Current Control (PIL)) between the host machine and subsystem that runs on the target hardware.

The preceding profiling report, which is for fixed-point datatype, shows the maximum and average execution times of the Current Control (PIL) subsystem running on the target hardware.

You can use the Data Inspector button on the Simulation tab to compare the signals logged during host-machine-based simulation and PIL simulation (executed on the target). This helps you verify the accuracy of host-machine-based simulation and PIL simulation.

This plot compares the speed feedback signals from the Current Control (PIL) and Current Control subsystems.

Note

If the execution time exceeds 60% of the budgeted time, we recommend you optimize the algorithm using one of these techniques:

  • Execute from RAM.

  • Offload some functionalities to CLA or other CPUs.

  • Scale the algorithm to run at every alternate cycle.

  • Move the less critical functionalities like speed calculation to a slower rate.

For more details on SIL/PIL code verification, see: