Use target connectivity configurations and the target connectivity API to customize processor-in-the-loop (PIL) simulation for your target environments.
Through a target connectivity configuration, you specify:
A configuration name for a target connectivity API implementation.
Settings that define the set of compatible Simulink® models. For example, the set of models that have a particular system target file, template makefile, and hardware implementation.
A PIL simulation requires a target connectivity API implementation that integrates third-party tools for:
Cross-compiling generated code, creating the PIL application that runs on the target hardware.
Downloading, starting, and stopping the application on the target.
Communicating between Simulink and the target.
You can have many different target connectivity configurations
for PIL simulation. Register a connectivity configuration with Simulink by
sl_customization.m file and placing
it on the MATLAB® search path.
When you run a PIL simulation, the software determines which of the available connectivity configurations to use. The software looks for a connectivity configuration that is compatible with the model under test. If the software finds multiple or no compatible connectivity configurations, the software generates an error message with information about resolving the problem.
This diagram shows the components of the PIL target connectivity API.
You must provide implementations of the three API components:
Build API — Specify the Simulink Coder™ toolchain or template makefile approach for building generated code.
Launcher API — Control how Simulink starts and stops the PIL executable.
Communications API — Customize connectivity between Simulink and the PIL target. Embedded Coder® provides host-side support for TCP/IP and serial communications, which you can adapt for other protocols.
These steps outline how you create a target connectivity API implementation. The example code
shown in the steps is taken from
ConnectivityConfig.m in Configure Processor-In-The-Loop (PIL) for a Custom Target.
Create a subclass of
ConnectivityConfig < rtw.connectivity.Config
In the subclass:
rtw.connectivity.MakefileBuilder, which configures the
builder = rtw.connectivity.MakefileBuilder(componentArgs, ... targetApplicationFramework, ... exeExtension);
Create a subclass of
rtw.connectivity.Launcher, which downloads and executes
the application using a third-party
launcher = mypil.Launcher(componentArgs, builder);
rtiostream API implementation
of the host-target communications channel.
For the target side, you must provide the driver code for communications, for example, TCP/IP
or serial communications. To integrate this code into the build process,
create a subclass of
For the host side, you can use a supplied library for TCP/IP or serial communications.
rtw.connectivity.RtIOStreamHostCommunicator, which loads
and initializes the library that you
hostCommunicator = rtw.connectivity.RtIOStreamHostCommunicator(componentArgs, ... launcher, ... rtiostreamLib);
If you require execution-time profiling of generated code, create a timer object that provides details of the hardware-specific timer and associated source files. See Specify Hardware Timer.
Each time you modify a connectivity implementation, close and reopen the models to refresh them.
To register a target connectivity API implementation as a target connectivity configuration in Simulink:
Create or update an
In this file:
Create a target connectivity configuration object that specifies, for example, the configuration name for a target connectivity API implementation and compatible models.
Add the folder containing
the search path and refresh your customizations.
For more information, see
To verify your target connectivity configuration early on and
independently of your model development and code generation, use the
With the function, you can run a suite of tests. In the tests, the
function runs various normal, SIL, and PIL simulations. The function
compares results and produces errors if it detects differences between
For step-by-step examples, see:
This example shows you how to create a custom PIL implementation using the target connectivity APIs. You can examine the code that configures the build process to support PIL, a downloading and execution tool, and a communication channel between host and target. To activate a full host-based PIL configuration, follow the steps in the example.
This example shows you how to implement a communication channel for use with the Embedded Coder product and your embedded target. This communication channel enables exchange of data between different processes. PIL simulation requires exchange of data between the Simulink software running on your development computer and deployed code executing on target hardware.
rtiostream interface provides a generic
communication channel that you can implement in the form of target
connectivity drivers for a range of connection types. The example shows
how to configure your own target-side driver for TCP/IP, to operate with
the default host-side TCP/IP driver. The default TCP/IP communications
allow high-bandwidth communication between host and target, which you
can use for transferring data such as video.
If you customize the
implementation for your PIL simulations, you must turn off Nagle's
algorithm for the server side of the connection. If Nagle's
algorithm is not turned off, your PIL simulations can run at a
significantly slower speed. The
file shows how you can
turn off Nagle's
/* Disable Nagle's Algorithm*/ option = 1; sockStatus = setsockopt(lFd,IPPROTO_TCP,TCP_NODELAY,(char*)&option,sizeof(option));
The example also shows how to implement custom target connectivity drivers, for example, using serial, CAN, or USB for both host and target sides of the communication channel.