%% Generate Tunable Initial Conditions
% You can represent initial conditions for signals and states by creating
% tunable global variables in the generated code. These variables allow you
% to restart an application by using initial conditions that are stored in memory.
% 
% If you set *Configuration Parameters > Optimization > Default parameter behavior* to |Tunable|, initial
% conditions appear as tunable fields of the global parameters structure.
%
% Whether you set *Default parameter behavior* to |Tunable| or |Inlined|,
% you can use a tunable parameter to specify the |InitialValue| property of
% a signal object or the *Initial condition* parameter of a block. For
% basic information about tunable parameters, see <docid:rtw_ug.bsmzxo9 Create Tunable Calibration Parameter in the Generated Code>.
%
% This example shows how to use tunable parameters to specify initial
% conditions for signals and states.  
%% Explore Example Model
% Open the example model
% <matlab:open_system('rtwdemo_sigobj_iv');set_param('rtwdemo_sigobj_iv','HideAutomaticNames','off');
% |rtwdemo_sigobj_iv|> and configure it to show the generated names of blocks. The signal |S2| uses a |Simulink.Signal| object in
% the base workspace.
load_system('rtwdemo_sigobj_iv')
set_param('rtwdemo_sigobj_iv','HideAutomaticNames','off')
open_system('rtwdemo_sigobj_iv')
%%
% Double-click the |Simulink.Signal| object |S2| to
% view its properties. The *Initial value* property is set to |aa2|. The
% object uses the variable |aa2| to specify an initial condition for the
% signal |S2|. The *Storage class* property is set to |ExportedGlobal|. To
% use a |Simulink.Signal| object to initialize a signal, the signal object
% must use a storage class other than |Auto| or, if the corresponding data category in the Code Mapping Editor
% uses a storage class setting other than |Default|, |Model default|.
%
% On the *Optimization* pane, in the Configuration
% Parameters dialog box, click *Configure*. The variable |aa2| is a tunable
% parameter that uses the storage class |ExportedGlobal|.
%
% In the model, open the Enabled Subsystem. In the Outport block dialog box,
% the parameter *Output when disabled* is set to |reset|. When the
% subsystem becomes disabled, the output signal |S2| resets to the initial
% value |aa2|.
%
% Open the Unit Delay block dialog box. On the *State Attributes* tab, 
% the *State name* box is set to |X1|.
%
% Open the Enable block dialog box. The parameter *States when enabling* is
% set to |reset|. When the subsystem transitions from a disabled state to
% an enabled state, it resets internal block states, such as |X1|, to their initial values.
%
% In the base workspace, double-click the |Simulink.Signal| 
% object |X1| to view its properties. The *Initial value* property is set to |aa1|.
%
% Double-click the |Simulink.Parameter| object |aa1| to view its properties.
% The *Storage class* property is set to |ExportedGlobal|. You can generate
% tunable initial conditions for block states by using tunable parameters 
% such as |aa1| and |Simulink.Signal| objects such as |X1|.
%% Generate and Inspect Code
% Generate code with the example model.
rtwbuild('rtwdemo_sigobj_iv');
%%
% In the code generation report, view the file |rtwdemo_sigobj_iv.c|. The
% code uses global variables to represent the block state |X1| and the signal |S2|.
file = fullfile(pwd,'rtwdemo_sigobj_iv_grt_rtw','rtwdemo_sigobj_iv.c');
rtwdemodbtype(file,'/* Exported block states */','X1',1,1)
rtwdemodbtype(file,'/* Exported block signals */','S2',1,1)
%%
% The code uses global variable to represent the tunable parameter |aa1|.
rtwdemodbtype(file,'/* Exported block parameters */','aa1',1,1)

%%
% The model initialization function uses the tunable parameter |aa1| to
% initialize the state |X1|. The function also uses the tunable parameter
% |aa2| to initialize the signal |S2|.
rtwdemodbtype(file,'/* SystemInitialize for Enabled SubSystem: ''<Root>/Enabled Subsystem (state X1 inside)'' */','/* End of SystemInitialize for SubSystem: ''<Root>/Enabled Subsystem (state X1 inside)'' */',1,1)
%%
% In the model step function, when the Enabled Subsystem transitions from a disabled state to an
% enabled state, the Unit Delay block state |X1| resets to its initial
% value.
rtwdemodbtype(file,'if (rtb_PulseGenerator > 0) {','/* UnitDelay: ''<S2>/Unit Delay'' */',1,0)
%%
% If the Enabled Subsystem becomes disabled during code execution, the
% algorithm 
% uses the tunable initial condition |aa2| to set the value of the
% signal |S2|.
rtwdemodbtype(file,'} else {','} else {',1,1)
rtwdemodbtype(file,' if (rtwdemo_sigobj_iv_DW.EnabledSubsystemstateX1inside_M) {','/* End of Outputs for SubSystem: ''<Root>/Enabled Subsystem (state X1 inside)'' */',1,1)

%% 
% Copyright 2012 The MathWorks, Inc.