Main Content

SIL and PIL Verification for Deployment on Raspberry Pi

This example shows how to verify the generated code by using the software-in-the-loop (SIL) and processor-in-the-loop (PIL) executions. In this example, the PIL execution uses a Raspberry Pi® hardware board.

Run MATLAB Code

This example uses these supporting files:

  • kalman01.m contains the MATLAB® function kalman01 for the Kalman estimator.

  • position.mat contains the input data about the trajectory of an object.

  • test01_ui.m is a MATLAB script that loads the input data from position.mat into the workspace, calls kalman01 to estimate the object location by using the Kalman filtering algorithm, and finally calls the plot_trajectory function that plots both the actual trajectory of the object and the Kalman estimate.

  • plot_trajectory.m contains the plot_trajectory function described above.

To test the MATLAB code, run the script test01_ui.

test01_ui
Current plot held
Current plot released

In the plot that is displayed, you see that the Kalman estimate closely follows the actual trajectory of the object. The test data includes two sudden shifts or discontinuities in position. These discontinuities check that the Kalman filter can quickly readjust its estimate and get back to tracking the object accurately.

For more information, see C Code Generation for a MATLAB Kalman Filtering Algorithm.

Software-in-the-Loop (SIL) Execution

In SIL execution, through a MATLAB SIL interface, the code generator compiles and runs library code on your development computer.

Configure SIL Execution

Create a coder.EmbeddedCodeConfig object to generate a static C library. Set the VerificationMode property to 'SIL'. Optionally, enable the Microsoft® Visual Studio® debugger for SIL execution.

config = coder.config('lib');
config.VerificationMode = 'SIL';
% config.SILPILDebugging = true;

Check that the production hardware setting is set to the default value 'Generic->MATLAB Host Computer'.

disp(config.HardwareImplementation.ProdHWDeviceType); 
Generic->MATLAB Host Computer

Generate Static Library and Run SIL Execution

Use the codegen command with the configuration object config to generate a static C library and the SIL interface for the kalman01 function. To perform SIL execution, you can simply call the generated SIL interface kalman01_sil at the MATLAB command line.

In the codegen command, the -test option enables you to run the test script test01_ui with calls to the kalman01 function replaced by calls to the generated SIL interface kalman01_sil.

codegen -config config -args {zeros(2,1)} kalman01 -report -test test01_ui 
Code generation successful: View report

Running test file: 'test01_ui' with MEX function 'kalman01_sil.mexw64'.
Current plot held
### Starting SIL execution for 'kalman01'
    To terminate execution: clear kalman01_sil
Current plot released

The code generator creates these output folders:

  • codegen\lib\kalman01 — Static library for kalman01.

  • codegen\lib\kalman01\sil — SIL interface code for kalman01.

Observe that the output of this run matches the output from the kalman01 MATLAB function.

Debug Code During SIL Execution

If you enable the Microsoft Visual Studio debugger, then running the test file opens the Microsoft Visual Studio IDE with debugger breakpoints at the start of the kalman01_initialize and kalman01 functions.

You can use the debugger features to observe code behavior. For example, you can step through code and examine variables.

To end the debugging session:

  • Remove all breakpoints.

  • Click the Continue button (F5).

The SIL execution runs to completion.

Terminate SIL Execution

Terminate the SIL execution process.

clear kalman01_sil;
### Application stopped
### Stopping SIL execution for 'kalman01'

You can also use the command clear mex, which clears MEX functions from memory.

Usage Notes for Windows® platform

Usage notes for SIL execution on a Windows platform:

  • The Windows Firewall can potentially block a SIL execution. To allow the execution, use the Windows Security Alert dialog box. For example, in Windows 7, click Allow access.

  • Suppose that the MATLAB current working directory (the folder that pwd displays) is a UNC path (for example, \\server\a\b\c) on a Windows platform. If you launch a SIL executable from this path, the location where the SIL executable is launched can be unpredictable and different from the MATLAB current working directory (for example, C:\Windows). To fix this issue, use a mapped network drive for the UNC path as your MATLAB current working directory.

Processor-in-the-Loop (PIL) Execution

In PIL execution, through a MATLAB PIL interface, the code generator cross-compiles and runs production object code on a target processor or an equivalent instruction set simulator. Before you run a PIL execution, you must set up a PIL connectivity configuration for your target. In this example, the PIL execution uses a Raspberry Pi® hardware board.

Configure PIL Execution

Create a coder.EmbeddedCodeConfig object to generate a static C library. Set the VerificationMode property to 'PIL'. Set the Hardware property to a hardware board configuration object for Raspberry Pi.

config = coder.config('lib');
config.VerificationMode = 'PIL';
config.Hardware = coder.hardware('Raspberry Pi');

If you are to use your Raspberry Pi hardware board for the first time, you must also specify the Username, DeviceAddress, and Password properties of the config.Hardware parameter.

Generate Static Library and Run PIL Execution

Use the codegen command with the configuration object config to generate a static C library and the PIL interface for the kalman01 function. To perform PIL execution, you can simply call the generated PIL interface kalman01_pil at the MATLAB command line.

In the codegen command, the -test option enables you to run the test script test01_ui with calls to the kalman01 function replaced by calls to the generated PIL interface kalman01_pil.

codegen -config config -args {zeros(2,1)} kalman01 -report -test test01_ui 
### Connectivity configuration for function 'kalman01': 'Raspberry Pi'
Location of the generated elf : /home/pi/MATLAB_ws/R2024b/C/Users/aghosh/Documents/ExampleManager/aghosh.Bdoc.j2547188/ecoder-ex65983262/codegen/lib/kalman01/pil
Code generation successful: View report

Running test file: 'test01_ui' with MEX function 'kalman01_pil.mexw64'.
Current plot held
### Starting application: 'codegen\lib\kalman01\pil\kalman01.elf'
    To terminate execution: clear kalman01_pil
### Launching application kalman01.elf...
Current plot released

The code generator creates these output folders:

  • codegen\lib\kalman01 — Static library for kalman01.

  • codegen\lib\kalman01\pil — PIL interface code for kalman01.

Observe that the output of this run matches the output from the kalman01 MATLAB function.

The Windows Firewall can potentially block a PIL execution. To allow the execution, use the Windows Security Alert dialog box. For example, in Windows 7, click Allow access.

Terminate PIL Execution

Terminate the PIL execution process.

clear kalman01_pil;
### Host application produced the following standard output (stdout) and standard error (stderr) messages:

You can also use the command clear mex, which clears MEX functions from memory.

See Also

| |

Related Topics