%% Using a Data Dictionary to Manage the Data for a Fuel Control System
% This example shows how to use data dictionaries to manage the data for a
% fuel rate control system designed using Simulink(R) and Stateflow(R).

% Copyright 1990-2015 The MathWorks, Inc.

%% Familiarize Yourself with the Model
% The sldemo_fuelsys_dd model is a closed-loop system containing a "plant" and
% "controller".  The plant is used to validate the design of the controller.
% In this example, the plant and controller are represented by separate models
% that are referenced from the test harness model.  Let's take a look at these
% models.

%% Open and Compile the Test Harness Model
open_system('sldemo_fuelsys_dd');

sldemo_fuelsys_dd([],[],[],'compile');
sldemo_fuelsys_dd([],[],[],'term');

%% View the Engine Gas Dynamics System (Plant)
% Double-click on the Engine Gas Dynamics block to open the plant model.

sldemo_fuelsys_dd_plant;

%% View the Fuel Rate Control System (Controller)
% Double-click on the Fuel Rate Controller block to open the controller model.

sldemo_fuelsys_dd_controller;

%% Investigate the Data Used by the Controller
% The global design data for the controller model is defined in a data dictionary.
% Using data dictionaries has many advantages over defining data in the base
% workspace.
%
% The controller model is explicitly linked to a data dictionary.
% This link is set up on the Data tab of the Model Properties dialog.
% 
% The icon in the lower-left corner of the model window shows that this
% model is linked to a data dictionary.  You can open the data dictionary
% in the Model Explorer by clicking on this icon.

open('sldemo_fuelsys_dd_controller.sldd');

%%
% This data dictionary contains parameter and signal objects that are used
% to configure the controller algorithm for simulation and code generation.
% It also contains a reference to another data dictionary that defines
% the data type objects used by this model.

open('sldemo_fuelsys_dd_types.sldd');

%%
% Initially, this data dictionary is configured for a float-point controller,
% as is seen by the data type display on the signal lines in the controller model.
% You can easily switch between floating-point and fixed-point controllers by
% changing the types dictionary (sldemo_fuelsys_dd_types.sldd) to reference the
% appropriate dictionary:
%
% * Floating-point types: sldemo_fuelsys_dd_float.sldd
% * Fixed-point types: sldemo_fuelsys_dd_fixpt.sldd

%% Investigate the Units Used by the Components
% Notice that units are visible on the model and subsystem icons and signal lines.
% Units are specified on the ports and on the bus, signal and parameter objects
% in the data dictionary.

%% Simulate the Test Harness Model
% The test harness model is also linked to a data dictionary (sldemo_fuelsys_dd.sldd).
% This data dictionary contains references to the data dictionaries for the
% plant and controller models but it does not contain any additional data.
%
% Simulate the test harness model to validate the behavior of the controller
% in either the floating-point or fixed-point configuration.

sim('sldemo_fuelsys_dd', 'StopTime', '8');

%% Close the Example
% Close the models and data dictionaries from this example.
close_system('sldemo_fuelsys_dd',0);
close_system('sldemo_fuelsys_dd_controller',0);
close_system('sldemo_fuelsys_dd_plant',0);
show(Simulink.dd.open('sldemo_fuelsys_dd_controller.sldd'), false);
show(Simulink.dd.open('sldemo_fuelsys_dd_types.sldd'), false);