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.
Embedded Coder Support Package for Texas Instruments C2000™ Processors
LAUNCHXL-F28379D controller hardware board
Use these steps to prepare the PIL model for profiling:
Open the model
mcb_pmsm_foc_sim.slx by using this
This model simulates the PMSM motor and FOC algorithm for closed-loop speed control.
On the Hardware tab of the Simulink toolstrip, click Hardware Settings.
In the Configuration Parameters window, under
Hardware Implementation, set the
Hardware board field to
Delfino F28379D LaunchPad.
Use these steps to verify the code in 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 - 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.
Open the script file
mcb_PIL_config_TI.m to set the
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.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
Click Build to create a model named
untitiled that includes a PIL subsystem called
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
Model blocks in SIL/PIL mode in 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.
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)) 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.
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: