%% Use Absolute-Time Temporal Logic in an Enabled Subsystem
% You can use absolute-time temporal logic in a chart that resides in a
% conditionally executed subsystem. When the subsystem is disabled, the
% chart becomes inactive and the temporal logic operator pauses while the
% chart is asleep. The operator does not continue to count simulation time
% until the subsystem is reenabled and the chart is awake.

% Copyright 2018-2020 The MathWorks, Inc.

%%
% This model has an enabled subsystem with the *States when enabling*
% parameter set to |held|.

% Set up charts with white background
set_param(0,'ExportBackgroundColorMode','white');

model = 'sf_enabled_subsystem_temp_logic';
load_system(model);

% Delete all annotations at the top level of the model
ah = find_system(model, 'FindAll', 'on', 'type', 'annotation');
for i = 1:length(ah)
   ao = get_param(ah(i), 'Object');
   ao.delete;
end

open_system(model)

%% 
% The subsystem contains a chart that uses the |after| operator to trigger
% a transition.

open_system([model '/Enabled Subsystem/Chart'])

%%
% The <docid:simulink_ref#mw_9e7f554e-7316-4c04-b686-1af7a8fe4a15 Signal
% Editor> block provides an input signal with these characteristics:
%
% * The signal enables the subsystem at t = 0.
% * The signal disables the subsystem at t = 2.
% * The signal reenables the subsystem at t = 6.

sim(model)
open_system([model '/Input pulse'])

%%
% This graph shows the total time elapsed in the chart. When the input
% signal enables the subsystem at time t = 0, state |A| becomes active.
% While the system is enabled, the elapsed time increases. When the
% subsystem is disabled at t = 2, the chart goes to sleep and the elapsed
% time stops increasing. For 2 < t < 6, the elapsed time stays frozen at 2
% seconds because the system is disabled. When the chart wakes up at t = 6,
% elapsed time starts to increase again.
%
% <<../enabled_subsystem_with_after_elapsed_time.png>>

%% 
% The transition from state |A| to state |B| depends on the elapsed time
% while state |A| is active, not on the simulation time. Therefore, the
% transition occurs at t = 9, when the elapsed time in state |A| equals 5
% seconds. When the transition occurs, the output value |y| changes from 0
% to 1.

open_system([model '/Output y'])

%%
% This model behavior applies only to subsystems where you set the Enable
% block parameter *States when enabling* to |held|. If you set the
% parameter to |reset|, the chart reinitializes completely when the
% subsystem is reenabled. Default transitions execute and any temporal
% logic counters reset to 0.