www.gusucode.com > rtwdemos 工具箱matlab源码程序 > rtwdemos/rtwdemo_pcgd_stage_5_p1_script.m

    %% Integrating the Generated Code into the External Environment
% This example shows the external build process, including required files 
% and interfaces for calling generated code.
%
% *Time*: 45 minutes
%
% *Goals*
% 
% Understand...
%
% * How to collect files required for building outside of Simulink(R)
% * How to interface with external variables and functions
%
% <matlab:RTWDemos.pcgd_open_pcg_model(5,0); *Task:* Open the model.> 
%% Building and Collecting the Required Data and Files
%
% The code that Embedded Coder(R) generates is dependent on 
% support files provided by MathWorks(R).  If you need to relocate 
% generated code to another development environment, such as a dedicated build system, 
% you must also relocate the required support files. You can automatically 
% collect all generated and necessary support files and package them in a
% zip file by using the Simulink(R) Coder(TM) |packNGo| utility. This
% utility uses tools for customizing the build process after code
% generation, including a |buildinfo_data| structure, and a |packNGo| function 
% to find and package all files needed to build an executable image, including 
% external files you define in the *Code Generation > Custom Code* pane
% of the Configuration Parameters dialog. The files are packaged in a standard 
% zip file. The |buildinfo| MAT-file is saved automatically
% in the directory |_model__ert_rtw|.
%
% The example model is configured to run |packNGo| automatically after code
% generation. 
%
% <matlab:RTWDemos.pcgd_buildDemo(5,0) *Task:* Generate code for the full model.> 
%
% To generate the zip file manually, do the following in the MATLAB(R) Command Window:
%
% # Load the file |buildInfo.mat| (located in the subdirectory |rtwdemo_PCG_Eval_P5_ert_rtw|).
% # Enter the command |packNGo(buildInfo)|.
% 
% The number of files included in the zip file depends on the version of
% Embedded Coder(R) and the configuration of the model you use. 
% Not all of the files in the zip file are required by the compiler.
% The compiled executable size (RAM/ROM) is
% dependent on the link process.  The linker should be configured to include  
% only required object files.
%
%% Background: Integrating the Generated Code into an Existing System
%
% This module covers tasks required to integrate the
% generated code into an existing code base.  For this evaluation, the  
% Eclipse(TM) IDE and Cygwin(TM)/gcc compiler are used.  The required integration 
% tasks are common to all integration environments.  
%
%% Background: Overview of the Integration Environment
% A full embedded controls system is comprised of multiple components, both
% hardware and software. Control algorithms are just one type of
% component.  The other standard types of components include:
%
% * An operating system (OS)
% * A scheduling layer
% * Physical hardware I/O 
% * Low-level hardware device drivers
%
% In general, Embedded Coder(R) does not generate code for any
% of these components.  Instead, it generates interfaces that connect
% with the components. MathWorks(R) provides hardware interface block
% libraries for many common embedded controllers.  For examples, see the
% Embedded Targets block library located in the Embedded Coder(R) product.
%
% For this evaluation, files are provided to show how you can build a 
% full system.  The main file is |example_main.c|. It is a simple main function 
% that performs the basic actions required to exercise the code.  It is _not_ 
% intended as an example of an actual application main. 
%
% <matlab:edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos','EmbeddedCoderOverview','stage_5_files','example_main.c')) *Task:* View |example_main.c|.>
%
% <<example_main_s5.jpg>>
%
% Functions of |example_main.c| include the following:
%
% * Defines function interfaces (function prototypes)
% * Includes required files for data definition
% * Defines |extern| data
% * Initializes data
% * Calls simulated hardware
% * Calls algorithmic functions
% 
% The order of execution of functions in |example_main.c| matches the order
% in which the subsystems are called in the test harness and in
% |rtwdemo_PCG_Eval_P5.h|.  If you change the order of execution in |example_main.c|, 
% results produced by the executable image will differ from simulation
% results.
%
%% Matching the System Interfaces
% Integration requires matching both the _Data_ and _Function_ interfaces
% of the generated code and the existing system code.  In this example, the
% |example_main.c| file defines the data through #includes and calls the
% functions from the generated code.
%
%% Matching Data Interfaces: Input Data Specification
% The system has three input signals: |pos_rqst|, |fbk_1|, and
% |fbk_2|.  The two feedback signals are imported externs (|ImportedExtern|) and the position 
% signal is an imported extern pointer (|ImportedExternPointer|).  Due to how the signals are defined,
% Simulink(R) Coder(TM) does not define (create) variables for them.  Instead, the
% signal variables are defined in a file that is external to the MATLAB environment.  
% 
% For the example, the file |defineImportedData.c| was created.  This file is a
% simple C stub used to define the signal variables.  The generated code has
% access to the data from the |extern| definitions in the file 
% |rtwdemo_PCG_Eval_P5_Private.h|.  In a real system, the data would come from other 
% software components or from hardware devices.
%
% <matlab:edit(fullfile(matlabroot,'','toolbox','rtw','rtwdemos','EmbeddedCoderOverview','stage_5_files','defineImportedData.c')) *Task:* View |defineImportedData.c|.>
%
% <<defineImportedData.jpg>>
%
% <matlab:RTWDemos.pcgd_showSection(5,'private'); *Task:* View |rtwdemo_PCG_Eval_P5_Private.h|.>
%
% <<Private_Extern_Define.jpg>>
%
%% Matching Data Interfaces: Output Data Specification
% The system does not require you to do anything with the
% output data. However, you can access the data by referring to the
% file |rtwdemo_PCG_Eval_P5.h|.  
%
% The module *Testing the Generated Code* shows how the output data can be saved to a standard log file.
%
% <matlab:RTWDemos.pcgd_showSection(5,'data_def'); *Task:* View |rtwdemo_PCG_Eval_P5.h|.>
%
%% Matching Data Interfaces: Accessing Additional Data
% Embedded Coder(R) creates several data structures during the code 
% generation process.  For this example accessing these structures was
% not required.  Examples of common data elements that
% users wish to access include:
% 
% * Block state values (integrator, transfer functions)
% * Local parameters
% * Time
%
% The following table lists the common Simulink(R) Coder(TM) data structures.
% Depending on the configuration of the model, some or all of 
% these structures will appear in the generated code.  In this example, the
% data is declared in the file |rtwdemo_PCG_Eval_P5.h|.
%
%  Data Type      Data Name     Data Purpose 
%  
%  Constants      |model_cP|    Constant parameters
%  Constants      |model_cB|    Constant block I/O
%  Output         |model_U|     Root and atomic subsystem input
%  Output         |model_Y|     Root and atomic subsystem output
%  Internal data  |model_B|     Value of block output
%  Internal data  |model_D|     State information vectors
%  Internal data  |model_M|     Time and other system level data
%  Internal data  |model_Zero|  Zero-crossings
%  Parameters     |model_P|     Parameters
%
%% Matching Function Call Interfaces
% Functions generated by Simulink(R) Coder(TM) have a |void Func(void)|
% interface, by default.  If the model or atomic subsystem is configured as
% reentrant code, Simulink(R) Coder(TM) creates a more complex function prototype.
% As shown below, the |example_main| function is configured to call the
% functions with the correct input arguments.
%
% <<functionInterface.jpg>>
%
% Calls to the function |PI_Cntrl_Reusable| use a mixture of user-defined
% variables and Simulink(R) Coder(TM) structures.  The structures are defined
% in |rtwdemo_PCG_Eval_P5.h|.  The preceding code fragment also shows how the 
% structures can be mapped onto user-defined variables.  
%
%% Building a Project in the Eclipse(TM) Environment
% This example uses the Eclipse(TM) IDE and the Cygwin(TM) GCC debugger
% to build the embedded system.  The installation files for both programs are
% provided as part of this example. Software components and versions numbers are:
%
% * Eclipse(TM) SDK - 3.2
% * Eclipse(TM) CDT - 3.3
% * Cygwin(TM)/GCC - 3.4.4-1
% * Cygwin(TM)/GDB - 20060706-2
%
% Instructions on how to install and use Eclipse(TM) and GCC appear in 
% *Installing and Using Cygwin(TM) and Eclipse(TM)*.
%
% To install the files for this module automatically, do the following:
%
% <matlab:RTWDemos.pcgd_autoSetup(5) *Task:* Automatically set up the build directory.>
%
% To manually install the files, do the following:
%
% # Create a build directory (|Eclipse_Build_P5|).
% # Unzip the file |rtwdemo_PCG_Eval_P5.zip| into your build directory.
% # Delete the files, |rtwdemo_PCG_Eval_P5.c|, |ert_main.c| and |rt_logging.c| which are replaced by |example_main.c.|
% 
%   *Note:* If code has not been generated for the model or the zip file
%   does not exist, complete the steps in the module *Building and
%   Collecting the Required Data and Files* before continuing to the next module.
%
% You can use the Eclipse(TM) debugger to step through and evaluate the
% execution behavior of the generated C code. *Testing the Generated Code* includes an
% example on how to exercise the model with input data.
%
%% Further Study Topics
%
% * <matlab:helpview([docroot,'/toolbox/rtw/helptargets.map'],'pack_and_go_util'); Using the Pack-and-Go utility>
%   Copyright 2007-2012 The MathWorks, Inc.