Use processor-in-the-loop (PIL) execution to verify code that you intend to deploy in production.
To set up and start a PIL execution from the command line:
Create, register, and verify your target connectivity configuration.
Create a coder.EmbeddedCodeConfig
object.
Configure the object for PIL.
Use the codegen
function
to generate library code for your MATLAB® function and the PIL
interface.
Use the coder.runTest
function
to run the test file for your original MATLAB function.
To terminate the PIL execution, use the clear
or function
_pilclear
mex
command.
The following example shows how you can use line commands to set up and run a PIL execution on your development computer.
Create a target connectivity API implementation
In your current working folder, make a local copy of the connectivity classes.
src_dir = ... fullfile(matlabroot,'toolbox','coder','simulinkcoder','+coder','+mypil'); if exist(fullfile('.','+mypil'),'dir') rmdir('+mypil','s') end mkdir +mypil copyfile(fullfile(src_dir,'Launcher.m'), '+mypil'); copyfile(fullfile(src_dir,'TargetApplicationFramework.m'), '+mypil'); copyfile(fullfile(src_dir,'ConnectivityConfig.m'), '+mypil');
Make the copied files writable.
fileattrib(fullfile('+mypil', '*'),'+w');
Update the package name to reflect the new location of the files.
coder.mypil.Utils.UpdateClassName(... './+mypil/ConnectivityConfig.m',... 'coder.mypil',... 'mypil');
Check that you now have a folder +mypil
in the current folder, which
includes three files, Launcher.m
,
TargetApplicationFramework.m
, and
ConnectivityConfig.m
.
dir './+mypil'
Review the code that starts the PIL application. The mypil.Launcher
class
configures a tool for starting the PIL executable. Open this
class in the
editor.
edit(which('mypil.Launcher'))
setArgString
method. This method allows
additional command line parameters to be supplied to the
application. These parameters can include a TCP/IP port number.
For an embedded processor implementation, you might have to hard
code these settings.The class mypil.ConnectivityConfig
configures target
connectivity.
edit(which('mypil.ConnectivityConfig'))
The creation of an instance of
rtw.connectivity.RtIOStreamHostCommunicator
that configures the host side of the TCP/IP
communications channel.
A call to the setArgString
method of Launcher
that
configures the target side of the TCP/IP
communications channel.
A call to setTimer
that
configures a timer for execution time measurement.
To define your own target-specific timer for
execution time profiling, you must use the Code
Replacement Library to specify a replacement for the
function
code_profile_read_timer
.
Review the target-side communication drivers.
rtiostreamtcpip_dir=fullfile(matlabroot,'toolbox','coder','rtiostream','src',... 'rtiostreamtcpip'); edit(fullfile(rtiostreamtcpip_dir,'rtiostream_tcpip.c'))
rtIOStreamOpen
,
rtIOStreamSend
,
andrtIOStreamRecv
. These functions are
required for target communication with the host. For each of
these functions, you must provide an implementation that is
specific to your target hardware and communication
channel.The mypil.TargetApplicationFramework
class adds target-side communication
drivers to the connectivity
configuration.
edit(which('mypil.TargetApplicationFramework'))
Register a target connectivity configuration
Use an rtwTargetInfo.m
file to:
Create a target connectivity configuration object.
Invoke registerTargetInfo
, which
registers the target connectivity configuration.
The target connectivity configuration object specifies, for example:
The configuration name and associated API implementation. See rtw.connectivity.ConfigRegistry
.
A toolchain for your target hardware. This example assumes that the target hardware is your host computer, and uses the toolchain supplied for host-based PIL verification. For information about toolchains, see Custom Toolchain Registration.
Insert the following code into your rtwTargetInfo.m
file, and save the file
in the current working folder or in a folder that is on the
MATLAB search
path:
function rtwTargetInfo(tr) % Register PIL connectivity config: mypil.ConnectivityConfig tr.registerTargetInfo(@loc_createConfig); % local function function config = loc_createConfig % Create object for connectivity configuration config = rtw.connectivity.ConfigRegistry; % Assign connectivity configuration name config.ConfigName = 'My PIL Example'; % Associate the connectivity configuration with the connectivity % API implementation config.ConfigClass = 'mypil.ConnectivityConfig'; % Specify toolchains for host-based PIL config.Toolchain = rtw.connectivity.Utils.getHostToolchainNames; % Through the HardwareBoard and TargetHWDeviceType properties, % define compatible code for the target connectivity configuration config.HardwareBoard = {}; % Any hardware board config.TargetHWDeviceType = {'Generic->32-bit x86 compatible' ... 'Generic->Custom' ... 'Intel->x86-64 (Windows64)', ... 'Intel->x86-64 (Mac OS X)', ... 'Intel->x86-64 (Linux 64)'};
Refresh the MATLAB Coder™ library registration information.
RTW.TargetRegistry.getInstance('reset');
Verify target connectivity configuration
Use the supplied piltest
function
to verify your target connectivity configuration.
Create a coder.EmbeddedCodeConfig
object for verifying the target
connectivity
configuration.
configVerify = coder.config('lib');
Specify the manufacturer and test hardware type. For example, PIL execution on a 64-bit Windows® development computer requires:
configVerify.HardwareImplementation.TargetHWDeviceType =... 'Intel->x86-64 (Windows64)'; configVerify.HardwareImplementation.ProdLongLongMode = true;
Run piltest
.
piltest(configVerify, 'ConfigParam', {'ProdLongLongMode'} )
Copy MATLAB code for Kalman estimator
Copy the MATLAB code to your working folder.
src_dir = ... fullfile(docroot,'toolbox','coder','examples','kalman'); copyfile(fullfile(src_dir,'kalman01.m'), '.') copyfile(fullfile(src_dir,'test01_ui.m'), '.') copyfile(fullfile(src_dir,'plot_trajectory.m'), '.') copyfile(fullfile(src_dir,'position.mat'), '.')
Create a coder.EmbeddedCodeConfig
object.
config = coder.config('lib');
Configure the object for PIL.
config.VerificationMode = 'PIL';
Specify production hardware, which must match one of the test hardware settings in
rtwTargetInfo.m
. For PIL execution on
your development computer, specify settings that match the
computer. For example, if your computer is a Windows 64-bit system,
specify:
config.HardwareImplementation.ProdHWDeviceType =... 'Intel->x86-64 (Windows64)'; config.HardwareImplementation.ProdLongLongMode = true;
For a Linux® 64-bit system, set
ProdHWDeviceType
to
'Intel->x86-64 (Linux
64)'
.
For a Mac OS X system, set ProdHWDeviceType
to
'Intel->x86-64 (Mac OS
X)'
.
Generate code and run PIL execution
Generate library code for the kalman01
MATLAB function and the PIL interface, and run the MATLAB test file, test01_ui
. The test file
uses kalman01_pil
, the generated PIL interface for
kalman01
.
codegen -config config -args {zeros(2,1)} kalman01 -test test01_ui
codegen\lib\kalman01
—
Standalone code for kalman01
.
codegen\lib\kalman01\pil
— PIL
interface code for kalman01
.
Verify that the output of this run matches the output from the
original kalman01.m
function.
Note
On a Windows operating system, the Windows Firewall can potentially block a SIL or PIL execution. To allow the execution, use the Windows Security Alert dialog box. For example, in Windows 7, click Allow access.
Terminate the PIL execution process.
clear kalman01_pil;