www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/TestGeneratedCodeExample.m

    %% Test Generated Code
% Validate generated code by using system-level 
% S-functions or by running the code in an external environment.
% 
% Learn about:
%
% * Different methods for testing the generated code
% * How to test generated code in Simulink(R)
% * How to test generated code outside of Simulink
%
% For information about the example model and other examples in this
% series, see <docid:ecoder_examples.example-rtwdemo_pcgd_stage_1_p1_script>.
%% Validation Methods
% Simulink(R) supports multiple system testing methods for validating the
% behavior of generated code.  
%
% *Windows(R) run-time executable*: Generate a Microsoft(R) Windows(R) executable and run the executable from the command prompt.
%
% * Benefit: Easy to create and enables use of a C debugger to evaluate code
% * Drawback: Partial emulation of target hardware
%
% *Software-in-the-loop (SIL) testing*: Use an S-function to include the generated code in the Simulink model.
%
% * Benefit: Easy to create, enables reuse of the Simulink test environment, and enables use of a C debugger to evaluate code
% * Drawback: Partial emulation of target hardware
%                                
% *Processor in the loop (PIL) testing*: Run a non-real-time cosimulation. Simulink executes a portion of the model (typically a plant model) while 
% the target processor runs a different portion of the model (typically a controller). After you download code to the target processor, the processor-in-the-loop 
% technology handles the communication of signals between Simulink and the target during the cosimulation.
%
% * Benefit: Enables reuse of the Simulink test environment, enables use of a
% C debugger with the simulation, and involves the target processor
% * Drawback: Requires additional steps to set up the test environment and the processor does not run in real time
%                                
% *On-target rapid prototyping*: Run the generated code on the target processor as part of the full system.
%
% * Benefit: You can determine actual hardware constraints and test the component within the full system. The processor runs in real time
% * Drawback: Requires hardware and requires additional steps to set up the test environment
%  
% *External Mode*: Run the generated code on the target processor as part of the full system.
%
% * Benefit: You can determine actual hardware constraints and test the component within the full system
% * Drawback: Requires hardware and requires additional steps to set up the test environment
%% Reuse Test Data Outside of Simulink(R)
% In other examples in this series, you test the system through simulation
% in Simulink. The test input data come from a Signal Builder block in the test harness model.
%
% To test the system outside of Simulink(R), you can reuse the test input data.
%
% # Save the Simulink test data into a file.
% # Format the data in a way that the system code can access.
% # Read the data file as part of the system code procedures.
%
% To use Simulink to perform verification, you can save the test output data
% from the external environment in a format that MATLAB(R) can read. 
%
% In
% this example, the file |hardwareInputs.c| contains the data from
% the Signal Builder block in the test harness model.
%
% <<../Test_Impor_Output_Data.jpg>>
%
%% Test via Software-in-the-Loop (Model Block SIL)
% *Create and Configure Model Block for SIL*
%
% Simulink can generate code from a Model block, wrap the code into an
% S-Function, and add the S-Function back into the model for
% software-in-the-loop testing.
%
% Open the example model, <matlab:RTWDemos.pcgd_open_pcg_model(6,0);
% |rtwdemo_PCG_Eval_P6|>.
% 
% <matlab:rtwdemo_PCGEvalHarnessHTGTSIL Open the test harness model.>
% 
% The test harness uses a Model block to access the example model. In this
% example, you
% run a software-in-the-loop test on the example model.
%
% # Right-click the Model block and select *Block Parameters
% (ModelReference)*.
% # Set *Model name* to the name of the model that you want to test.
% # Set *Simulation mode* to |Software-in-the-loop (SIL)|.
%
% <<../Model_Block_SIL.png>>
%
% The Model block now shows a |(SIL)| tag.
%
% <<../Model_Block_SIL2.jpg>>
%
% *Configure Models for SIL*
% 
% Configure the example model, |rtwdemo_PCG_Eval_P6|, and the test harness
% model, |rtwdemo_PCGEvalHarnessHTGTSIL|, for SIL simulation by adjusting
% the hardware implementation parameters.
% 
% <matlab:RTWDemos.pcgd_open_pcg_model(6,0); Open the example model.> 
%
% In both models, select *Configuration Parameters > Hardware
% Implementation > Device details > Support long long*.
%
% Save a copy of the example model, |rtwdemo_PCG_Eval_P6|, in your current
% folder.
% 
% *Run Model Block SIL*
% 
% In this example, the test harness model contains a Model block that is configured for SIL simulation.
%
% <matlab:rtwdemo_PCGEvalHarnessHTGTSIL Open the test harness.>
%
% <matlab:sim('rtwdemo_PCGEvalHarnessHTGTSIL') Run the test harness.>
%
% The generated code and the simulation yield the same results.
% 
% <<../FirstPassTest.jpg>>
%
%% Configure System for Testing via Test Vector Import and Export
% This example extends the integration example in <docid:ecoder_examples.example-rtwdemo_pcgd_stage_5_p1_script>. In this case, |example_main.c| uses simulated hardware I/O. 
%
% The augmented |example_main.c| file now uses this execution order:
% 
% *1.* Initialize data (one time) 
%
% |while < endTime| 
% 
% *2.* Read the simulated hardware inputs
% 
% *3.* |PI_cnrl_1|
% 
% *4.* |PI_ctrl_2|
% 
% *5.* |Pos_Command_Arbitration|
% 
% *6.* Write the simulated hardware outputs 
% 
% |end while|
% 
% <matlab:edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos','EmbeddedCoderOverview','stage_6_files','example_main.c')) View |example_main.c|.>
%
% Two functions, |plant| and |hardwareInputs|, supply the input test data.
%
% |Plant.c|: This function, which was generated from the plant portion of the test harness, simulates the throttle body response to throttle commands.
%
% |void Plant(void)|
%          
% |HardwareInputs.c|: This function provides the |pos_req| signal and adds noise from the |Input_Signal_Scaling| subsystems into the plant feedback signal.
%
% |void hardwareInputs(void)|
%
% The handwritten function in |WriteDataForEval.c| logs output data from the test.  
% After the test is finished, the function executes and writes the data 
% to the file |PCG_Eval_ExternSimData.m|. You can run this script file 
% in MATLAB to access the data. You can then compare the data to the simulated data from Simulink(R).
%
% To enable these additional files, identify them by using *Configuration
% Parameters > Code Generation > Custom Code > Additional build
% information*.
%
% <<../customCode.png>>
%% Test via Test Vector Import and Export (Eclipse(TM) Environment)
% Before building an executable in the Eclipse(TM) environment, regenerate the
% code without the S-function interface.  
%
% <matlab:RTWDemos.pcgd_buildDemo(6,0) Build C code for integration.>
%
% To learn how to install and use Eclipse(TM) and GCC, see 
% <docid:ecoder_examples.example-rtwdemo_pcgd_Install_External_Tools_script>.
%
% To install the files for this example automatically, click the hyperlink.
%
% <matlab:RTWDemos.pcgd_autoSetup(6) Automatically set up the build folder.>
% 
% Alternatively, to manually install the files:
% 
% # Create a build folder (|Eclipse_Build_P6|).
% # Unzip the file |rtwdemo_PCG_Eval_P6.zip| into the build folder.
% # Delete the files |rtwdemo_PCG_Eval_P6.c|, |ert_main.c|, and |rt_logging.c|, which are replaced by |example_main.c.|
%
% Run the control code in Eclipse(TM). The function in |writeDataForEval.c|
% creates the file |eclipseData.m|, which contains the output test data.
% In MATLAB(R), load this Eclipse(TM) data by running the
% script file. Then, compare this data with the data from the Simulink(R) test
% harness model by using |plot|.
%
% For the next example in this series, see <docid:ecoder_examples.example-rtwdemo_pcgd_stage_7_p1_script>.
%
%   Copyright 2007-2015 The MathWorks, Inc.