www.gusucode.com > rtwdemos 工具箱matlab源码程序 > rtwdemos/rtwdemo_custom_pil_script.m
%% Creating a Custom Processor-in-the-Loop (PIL) Configuration % This example shows how to create a target connectivity configuration using the % target connectivity APIs. With a target connectivity configuration you can run % PIL simulations on custom embedded hardware. % % You will learn how to: % % * Adapt the build process to support PIL % * Configure a tool to use for downloading and starting execution of a PIL % executable on your target hardware % * Configure a communication channel between host and target that is used to % support PIL simulation on the target % % You will start with a model configured for Software-in-the-Loop (SIL) % simulation. This example will guide you through the process of creating a % target connectivity configuration that allows you to simulate this model in % PIL mode. The example runs entirely on your host machine. However, you can % follow the same steps to create a connectivity configuration for your own % embedded target hardware. % % Note that this example requires the Embedded Coder(R) product. % Copyright 2008-2013 The MathWorks, Inc. %% Preliminaries % Later in this example you will add a folder to the path sl_customization_path = fullfile(matlabroot,... 'toolbox',... 'rtw',... 'rtwdemos',... 'pil_demo'); % If this folder is already on the path, remove it if strfind(path,sl_customization_path) rmpath(sl_customization_path) end % Reset customizations sl_refresh_customizations % Create a temporary folder (in your system's temporary folder) for the % build and inspection process. currentDir = pwd; rtwdemodir(); %% Verify Generated Code Using SIL Simulation % Simulate a model configured for SIL. This uses SIL to verify the generated % code compiled for your host platform by comparing the simulation behavior % with behavior of the corresponding generated code. % Make sure the example model is freshly opened close_system('rtwdemo_sil_modelblock',0); close_system('rtwdemo_sil_counter',0) open_system('rtwdemo_sil_modelblock') % Note that the Model block CounterA has the text (SIL) displayed on it. This % shows that the model referenced by this Model block is configured for SIL % simulation. % Run a simulation of this system set_param('rtwdemo_sil_modelblock','StopTime','10'); sim('rtwdemo_sil_modelblock'); %% Start Work on a Target Connectivity Configuration % In the previous step you ran a simulation in SIL mode. You are now ready to % 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 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 including % three files Launcher.m, TargetApplicationFramework.m and ConnectivityConfig.m dir './+mypil' %% 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: edit(which('mypil.Launcher')) % Review the content of this file. Note the method setArgString that allows % additional command line parameters to be supplied to the executable. These % parameters may include a TCP/IP port number. For an embedded processor % implementation, you may choose to have these settings hard-coded. %% Configure the Overall Target Connectivity Configuration % View the class mypil.ConnectivityConfig: edit(which('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 % Note that 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. You can achieve this using a command % line API or the crtool graphical user interface. %% Review the Target-Side Communications Drivers % View the file rtiostream_tcpip.c rtiostreamtcpip_dir=fullfile(matlabroot,'rtw','c','src','rtiostream',... '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 platform to % communicate with the host machine. 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: edit(which('mypil.TargetApplicationFramework')) %% 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 conditions that must be satisfied % in order to use it. The conditions specified in this file may include the name % of your System Target File and your Hardware Implementation settings. % You can view the sl_customization file (for the example, you do not have % to make changes to this file) edit(fullfile(sl_customization_path,'sl_customization.m')) % Add the sl_customization folder to the path and refresh the % customizations addpath(sl_customization_path); sl_refresh_customizations; %% Verify Generated Code Using 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'); %% % Review the messages above. Confirm that the simulation ran without errors % being reported. 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. %% Clean Up % Remove the path that was added temporarily rmpath(sl_customization_path) % Reset customizations sl_refresh_customizations % Close the models close_system('rtwdemo_sil_modelblock',0) close_system('rtwdemo_sil_counter',0) rtwdemoclean; cd(currentDir)