main content

sil and pil verification for deployment on raspberry pi -凯发k8网页登录

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 .

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 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.sildebugging = 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;
### 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 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 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 your raspherry 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/r2021b/c/users/aghosh/documents/examples/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
### connectivity configuration for function 'kalman01': 'raspberry pi'
### 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

    网站地图