%% Model An Intersection Of One-Way Streets
%
% This example models an intersection of one-way roads controlled by a
% Stateflow(R) traffic light system. The Stateflow chart tracks the state
% of each traffic light by using active state output. The behavior of the
% traffic lights is controlled by parameters on the Stateflow mask.
%
%   Copyright 2012-2018 The MathWorks, Inc.

%% Intersection Model
% The phase of the animated traffic lights is determined by the output data
% from the Stateflow chart. The value of the output data corresponds to the
% active child of the substates |Light1Controller| and |Light2Controller|,
% respectively.

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

model = 'sf_traffic_light';
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);

%% Traffic Controller
% The Stateflow chart Traffic Controller manages two traffic controllers in
% parallel. Each controller determines the phase of the downstream traffic
% light based on traffic congestion at the intersection, an input from
% Simulink(R), and parameters on the chart's mask. For more information, see
% <docid:stateflow_ug#bts299r>.

open_system([model '/Traffic Controller'], 'force');

%% Active State Output
% The child activity of both |Light1Controller| and |Light2Controller| is
% output to Simulink through a data of enumerated type. Stateflow manages
% this data automatically. For more information, see
% <docid:stateflow_ug#bt5zn6g-1>.
%
% * Open one of the Light controllers (for instance, |Light1Controller|).
% * Right click and select Properties.
% * Notice that the *Create output for monitoring* option is selected and
% set to |Child activity|.
% * The field *Data name* corresponds to the name of the output data on the
% linked instance.
% * This output is mapped to a chart level output called |Light1|.

lib_name = 'sf_traffic_light_lib';
open_system([lib_name '/Traffic Controller'], 'force');

%% Simulation
% Simulate the model to see the traffic light blocks animate.
