%% Share Data Store Between Instances of a Reusable Algorithm
% To reuse an algorithm, instead of copying and pasting the blocks, you can
% encapsulate them in a separate model file. Then, you can refer to the
% model repeatedly with Model blocks. Each Model block is a standalone
% instance of the algorithm.
%
% By default, each instance calculates and stores its own copy of the
% signals that the model contains. Therefore, the instances do not
% interact. However, you can use a data store to share a piece of data
% between the instances.
%
% This example shows how to share a fault indication between all instances
% of a reusable control algorithm. Any instance can set the fault, causing
% all instances to take action to clear it.
%% Explore Example Models
% Open the example model |sldemo_shared_data|.

%   Copyright 2017 The MathWorks, Inc.

open_system('sldemo_shared_data')
%%
% The Controller subsystem refers to the controller algorithm model,
% |sldemo_shared_data_alg|, multiple times through three Model blocks.
%
% Open the algorithm model.
open_system('sldemo_shared_data_alg')
%%
% The Data Store Memory block |fault_ind| represents a fault indication.
% The block parameter *Signal Attributes > Share across model instances* is
% selected, so all instances of |sldemo_shared_data_alg| share the
% indication. Because the block exists in |sldemo_shared_data_alg|, the
% indication is local to the model. Other models that refer to
% |sldemo_shared_data_alg| cannot read or write to the indication.
%
% The Algorithm subsystem uses a PID controller to generate a position
% command for an actuator (represented by a block in |sldemo_shared_data|).
%
% The Fault Handling subsystem sets and clears the fault indication. Each
% time an instance of the algorithm experiences a fault condition (the
% controlled input exceeds 2.63), the subsystem increments the value of the
% data store by 1. Each time an instance clears its fault condition, the
% subsystem decrements the value by 1.
%
open_system('sldemo_shared_data_alg/Fault Handling');
%
% The subsystem either passes the position command (if the data store value
% is zero, meaning there are no faults) or locks the command at a zero
% value (if there is a fault in any instance).
%% Simulate Example Models
% Simulate the parent model, |sldemo_shared_data|.
out = sim('sldemo_shared_data');
%%
% Use the Simulation Data Inspector to observe the position commands and
% the controlled signals. In the Simulink Editor toolbar, click the
% highlighted button.
%
% Partway through the simulation, the controlled signal |conc_mid|
% experiences a disturbance.
hold;
plot(out.logsout{5}.Values)
title('Signal over time')
xlabel('Time')
ylabel('Value')
%%
% The disturbance triggers a fault in the corresponding instance of the
% control algorithm. Due to the fault, all three position commands (the
% |vlv| signals) lock at zero.
plot(out.logsout{1}.Values)
plot(out.logsout{2}.Values)
plot(out.logsout{3}.Values)
%% 
% Later, when the fault condition clears, all of the position commands and
% controlled signals return to nominal values.
plot(out.logsout{4}.Values)
plot(out.logsout{6}.Values)
hold off;
