%% Bind a Function-Call Subsystem to a State
% This model triggers a function-call subsystem with a trigger event |E|
% that binds to a state of a chart. In the *Solver* pane of the Model
% Configuration Parameters dialog box, the model specifies a fixed-step
% solver with a fixed-step size of 1.

% Copyright 2018-2019 The MathWorks, Inc.

mdl = 'sf_bind_fcn_call_subsys_to_state';
load_system(mdl)
sim(mdl);
open_system(mdl)

%%
% The chart contains two states. Event |E| binds to state |A| with the
% action
%
%    bind:E
%
% Event |E| is defined for the chart with a scope of |Output to Simulink|
% and a trigger type of |function-call|.

open_system('sf_bind_fcn_call_subsys_to_state/Chart')

%%
% The function-call subsystem contains a trigger port block, an input port,
% an output port, and a simple block diagram. The block diagram increments
% a counter by 1 at each time step, using a Unit Delay block.

open_system('sf_bind_fcn_call_subsys_to_state/Function-Call Subsystem')

%%
% The Block Parameters dialog box for the trigger port contains these
% settings:
%
% * *Trigger type*: |function-call|.
% * *States when enabling*: |reset|. This setting resets the state values
% for the function-call subsystem to zero when it is enabled.
% * *Sample time type*: |triggered|. This setting sets the function-call
% subsystem to execute only when it is triggered by a calling event while
% it is enabled.
%
% Setting *Sample time type* to |periodic| enables the *Sample time* field
% below it, which defaults to 1. These settings force the function-call
% subsystem to execute for each time step specified in the *Sample time*
% field while it is enabled. To accomplish this, the state that binds the
% calling event for the function-call subsystem must send an event for the
% time step coinciding with the specified sampling rate in the *Sample
% time* field. States can send events with entry or during actions at the
% simulation sample rate.
% 
% * For fixed-step sampling, the *Sample time* value must be an integer
% multiple of the fixed-step size.
% * For variable-step sampling, the Sample time value has no limitations.

%% 
% To see how a state controls a bound function-call subsystem, begin
% simulating the model.
%
% * At time t = 0, the default transition to state |A| occurs. State |A|
% executes its bind and entry actions. The binding action binds event |E|
% to state |A|, enabling the function-call subsystem and resetting its state
% variables to 0. The entry action triggers the function-call subsystem and
% executes its block diagram. The block diagram increments a counter by 1
% using a Unit Delay block. The Unit Delay block outputs a value of 0 and
% holds the new value of 1 until the next call to the subsystem.
% * At time t = 1, the next update event from the model tests state |A| for
% an outgoing transition. The transition to state |B| does not occur
% because the temporal operator |after(10,tick)| allows the transition to
% be taken only after ten update events are received. State |A| remains
% active and its during action triggers the function-call subsystem. The
% Unit Delay block outputs its held value of 1. The subsystem also
% increments its counter to produce the value of 2, which the Unit Delay
% block holds until the next triggered execution.
% * The next eight update events increment the subsystem output by one at
% each time step.
% * At time t = 10, the transition from state |A| to state |B| occurs.
% Because the binding to state |A| is no longer active, the function-call
% subsystem is disabled, and its output drops back to 0.
% * At time t = 11, the transition from state |B| to state |A| occurs.
% Again, the binding action enables the function-call subsystem. Subsequent
% update events increment the subsystem output by one at each time step
% until the next transition to state |B| occurs at time t = 21.

open_system('sf_bind_fcn_call_subsys_to_state/Scope')
