Configure Processor-In-The-Loop (PIL) for a Custom Target
Create a target connectivity configuration by using target connectivity APIs. With a target connectivity configuration, you can run processor-in-the-loop (PIL) simulations on custom embedded hardware.
You learn how to:
Adapt the build process to support PIL simulations.
Configure a tool for downloading and starting execution of a PIL executable on your target hardware.
Configure a communication channel between host and target to support PIL simulation on the target hardware.
Start with a model configured for software-in-the-loop (SIL) simulation. This example guides you through the process of creating a target connectivity configuration so that you can simulate the model in PIL mode. The example runs entirely on your host computer. You can follow the same steps to create a connectivity configuration for your own embedded target hardware.
Later in this example, you will add a folder to the search path. Create the folder path.
sl_customization_path = fullfile(matlabroot,... 'toolbox',... 'rtw',... 'rtwdemos',... 'pil_demo');
If this folder is already on the search path, remove it.
if strfind(path,sl_customization_path) rmpath(sl_customization_path) end
Create a temporary folder (in your system's temporary folder) for the build and inspection process.
currentDir = pwd; rtwdemodir();
Test Generated Code with SIL Simulation
Simulate a model configured for SIL. Verify the generated code compiled for your host computer by comparing the SIL simulation behavior with the normal simulation behavior.
Make sure that the example model is newly opened.
close_system('rtwdemo_sil_modelblock',0); close_system('rtwdemo_sil_counter',0) open_system('rtwdemo_sil_modelblock')
The CounterA Model block displays the text (SIL), which indicates that its referenced model is configured for SIL simulation.
Run a simulation of the system.
### Starting build procedure for model: rtwdemo_sil_counter ### Successful completion of build procedure for model: rtwdemo_sil_counter ### Preparing to start SIL simulation ... Building with 'Microsoft Visual C++ 2017 (C)'. MEX completed successfully. ### Updating code generation report with SIL files ... ### Starting SIL simulation for component: rtwdemo_sil_counter ### Stopping SIL simulation for component: rtwdemo_sil_counter
Target Connectivity Configuration
Start work on a target connectivity configuration for PIL.
Make a local copy of the target connectivity configuration 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.
Update the package name to reflect the new location of the files.
coder.mypil.Utils.UpdateClassName(... './+mypil/ConnectivityConfig.m',... 'coder.mypil',... 'mypil');
Verify that you now have a folder +mypil in the current folder, which has the files Launcher.m, TargetApplicationFramework.m, and ConnectivityConfig.m.
. Launcher.m .. TargetApplicationFramework.m ConnectivityConfig.m
Review Code to Launch the PIL Executable
The class that configures a tool for launching the PIL executable is mypil.Launcher. Open this class in the editor.
Review the content of this file. The method setArgString supplies additional command-line parameters to the executable. These parameters can include a TCP/IP port number. For an embedded processor implementation, you can choose to hard-code these settings.
Configure the Overall Target Connectivity Configuration
View the class mypil.ConnectivityConfig.
Review the content of this file. You should be able to identify:
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. Use a command-line API or the crtool user interface.
Review the Target-Side Communications Drivers
View the file rtiostream_tcpip.c.
rtiostreamtcpip_dir=fullfile(matlabroot,'toolbox','coder','rtiostream','src',... 'rtiostreamtcpip'); edit(fullfile(rtiostreamtcpip_dir,'rtiostream_tcpip.c'))
Scroll down to the end of this file. See that this file contains a TCP/IP implementation of the functions rtIOStreamOpen, rtIOStreamSend, and rtIOStreamRecv. These functions are required for the target hardware to communicate with your host computer. You must provide an implementation for each of these functions that is specific to your target hardware and communication channel.
Add Target-Side Communications Drivers to the Connectivity Configuration
The class that configures additional files to include in the build is mypil.TargetApplicationFramework. Open this class in the editor.
Use sl_customization to Register the Target Connectivity Configuration
To use the new target connectivity configuration, you must provide an sl_customization file. The sl_customization file registers your new target connectivity configuration and specifies the required conditions for its use. The conditions specified in this file can include the name of your system target file and your hardware implementation settings.
You can view the sl_customization file. For this example, you do not have to make changes to the file.
Add the sl_customization folder to the search path and refresh the customizations.
Test Generated Code with PIL Simulation
Run the PIL simulation.
close_system('rtwdemo_sil_modelblock',0) open_system('rtwdemo_sil_modelblock') set_param('rtwdemo_sil_modelblock/CounterA','SimulationMode','processor-in-the-loop (pil)'); set_param('rtwdemo_sil_modelblock','StopTime','10'); sim('rtwdemo_sil_modelblock');
### Starting build procedure for model: rtwdemo_sil_counter ### Generated code for 'rtwdemo_sil_counter' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for model: rtwdemo_sil_counter ### Connectivity configuration for referenced model "rtwdemo_sil_counter": My PIL Example ### EXECUTING METHOD SETARGSTRING SETARGSTRING called from line 71 of ConnectivityConfig.m ### Preparing to start PIL simulation ... Building with 'Microsoft Visual C++ 2017 (C)'. MEX completed successfully. ### Updating code generation report with PIL files ... ### Starting application: 'rtwdemo_sil_counter_ert_rtw\pil\rtwdemo_sil_counter.exe' Starting PIL simulation Started new process, pid = 14132 Stopping PIL simulation Terminated process, pid = 14132
Review the preceding messages. Confirm that the simulation ran without errors. You have now implemented a target connectivity configuration for PIL. You can use the same APIs to implement a connectivity configuration for your own combination of embedded processor, download tool, and communications channel.
Remove the search path for this example.
Close the models.
close_system('rtwdemo_sil_modelblock',0) close_system('rtwdemo_sil_counter',0) rtwdemoclean; cd(currentDir)