Code Verification and Profiling Using PIL Testing
In 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 simulates the input and output signals for the controller 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 metric of an algorithm that you obtain from PIL simulation, helps you detect algorithm overrun on the target hardware. The PIL profiling report shows the average and maximum execution times of an algorithm on the target hardware. This example explains PIL profiling on Texas Instruments™ LAUNCHXL-F28379D hardware board.
This example uses the
mcb_pmsm_foc_sim.slx model to show code
verification in PIL simulation. This example shows PIL profiling for the Current Control
subsystem in the 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 and maximum execution times of the control algorithm in the
This example consists of 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 Support Package for Texas Instruments C2000™ Processors
LAUNCHXL-F28379D controller hardware board
Prepare PIL Model
This model simulates the PMSM motor and the FOC algorithm for closed-loop speed control.
On the Hardware tab of the Simulink toolstrip, click Hardware Settings.
In the Configuration Parameters dialog box, under Hardware Implementation, set the Hardware board field to
TI Delfino F28379D LaunchPad.
Verify Code by Using PIL
In the Configuration Parameters dialog box, select these configuration settings under Hardware Implementation > Hardware board settings > Target hardware resources > PIL:
Communication Interface — Select
SCI module — Select
Serial port in MATLAB preferences — The 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.
mcb_PIL_config_TI.mscript file to set the configuration parameters.
Update the model name and stop time in the script.
Run the script to update the configuration parameters of the simulation model and the PIL preferences.
Right-click the Current Control subsystem in the
mcb_pmsm_foc_sim.slxexample 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
Inlinedfor all parameters.
Click Build to create a model named
untitiledthat includes a PIL subsystem called Current Control.
Rename the Current Control subsystem to Current Control (PIL).
Copy the Current Control (PIL) subsystem and replace the Current Control subsystem in the
In the PIL mode, the system deploys the Current Control (PIL) subsystem to the target and executes the subsystem in the target hardware.
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.
On the Simulink toolstrip, select the SIL/PIL Manager app from the Apps tab.
On the SIL/PIL toolstrip, select
SIL/PIL Sim Only.
Model blocks in SIL/PIL modein the System Under Test field.
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.
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) subsystem) between the host machine and subsystem that runs on the target hardware.
This profiling report, which is for the 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.
If the execution time exceeds 60% of the budgeted time, you can 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 less critical functionalities like speed calculation to a slower rate.
For more details on SIL/PIL code verification, see: