www.gusucode.com > rtwdemos 工具箱matlab源码程序 > rtwdemos/rtwdemo_fuelsys_script.m
%% Fuel Rate Control System % This example shows how to generate code for a fuel rate % control system designed using Simulink(R) and Stateflow(R). % Copyright 1994-2012 The MathWorks, Inc. %% Familiarize Yourself with the Relevant Portions of the Model % Figures 1-3 show relevant portions of the sldemo_fuelsys model, which is % a closed-loop system containing a "plant" and "controller". The plant is % used to validate the controller in simulation early in the design cycle. % In this example, we'll generate code for the relevant controller % subsystem, "fuel_rate_control". Figure 1 shows the top-level simulation % model. % open sldemo_fuelsys via rtwdemo_fuelsys and compile the diagram to see % see the signal data types. rtwdemo_fuelsys sldemo_fuelsys([],[],[],'compile'); sldemo_fuelsys([],[],[],'term'); %% % *Figure 1: Top-level model of the "plant" and "controller"* % % The fuel rate control system is comprised of Simulink(R) and Stateflow(R) blocks, % and is the portion of the model for which we'll generated code. open_system('sldemo_fuelsys/fuel_rate_control'); %% % *Figure 2: The fuel rate controller subsystem* % % The control logic is a Stateflow(R) chart that specifies the different % modes of operation. open_system('sldemo_fuelsys/fuel_rate_control/control_logic'); %% % *Figure 3: Fuel rate controller logic* % %% % Now let's remove the window clutter. close_system('sldemo_fuelsys/fuel_rate_control/airflow_calc'); close_system('sldemo_fuelsys/fuel_rate_control/fuel_calc'); close_system('sldemo_fuelsys/fuel_rate_control/control_logic'); hDemo.rt=sfroot;hDemo.m=hDemo.rt.find('-isa','Simulink.BlockDiagram'); hDemo.c=hDemo.m.find('-isa','Stateflow.Chart','-and','Name','control_logic'); hDemo.c.visible=false; close_system('sldemo_fuelsys/fuel_rate_control'); %% Configure and Build the Model Using Simulink(R) Coder(TM) % Simulink(R) Coder(TM) generates generic ANSI(R) C code for % Simulink(R) and Stateflow(R) models via the Generic Real-Time (GRT) % target. Configuring a model for code generation can be done % programmatically. rtwconfiguredemo('sldemo_fuelsys','GRT'); %% % For this example, let's build the fuel rate control system only. Once % the code generation process is complete, an HTML report detailing the generated % code is displayed automatically. The main body of the code is located % in fuel_rate_control.c. rtwbuild('sldemo_fuelsys/fuel_rate_control'); %% Configure and Build the Model Using Embedded Coder(R) % Embedded Coder(R) is used to generate production % ANSI(R) C/C++ code via the Embedded Real-Time (ERT) target. Configuring a % model for code generation can be done programmatically. rtwconfiguredemo('sldemo_fuelsys','ERT'); %% % Repeat the build process and inspect the generated code. Figure 4 shows % a portion of the generated control logic. You can navigate to the % relevant code segments interactively in the Simulink(R) Coder(TM) Report % using the *Previous* and *Next* buttons by selecting *Code Generation % > Navigate to Code ...* from the charts context menu (i.e., % right-click on the Stateflow(R) block), or programmatically using the % rtwtrace utility. rtwbuild('sldemo_fuelsys/fuel_rate_control'); rtwtrace('sldemo_fuelsys/fuel_rate_control/control_logic') %% % % <<rtwdemo_fuelsys_control_logic.jpg>> %% % *Figure 4: Portion of the generated code for the fuel rate controller logic* %% % Close the example. clear hDemo close_system('sldemo_fuelsys',0);