%% Tank Control Simulation and Code Generation Using Ladder Logic
% This example shows how to do ladder logic simulation and code generation 
% from ladder tank controller model.
%
%   Copyright 2018 The MathWorks, Inc.

%% Step 1: Create plcdemo_ladder_tankcontrol example model  
% To start, create a new folder with write permission, copy the files
% plcdemo_ladder_tankcontrol_template.slx and TankControl.L5X into the
% new folder.


%% Step 2: Create plcdemo_ladder_tankcontrol example model  
% Change the current folder to the new folder, rename 
% plcdemo_ladder_tankcontrol_template.slx to
% plcdemo_ladder_tankcontrol.slx.

%% Step 3: Import ladder file and generate ladder controller model  
% Run plcimportladder command in MATLAB:

%%
% plcimportladder TankControl TopAOI TankControl

%% Step 4: Copy tank controller into plcdemo_ladder_tankcontrol model
% Open the generated model TankControl_runner.slx, select and 
% copy the TankControl_runner block. 
% Open plcdemo_ladder_tankcontrol, replace Controller/TankControl_runner 
% with the copied block 

%% Step 5: Run simulation
% Click Run button to start simulation. Open Tank HMI and use 
% the rotary switch to set controller command input:
% * Set switch to 'Fill' position to fill tank;
% * Set switch to 'Hold' position to hold current tank state;
% * Set switch to 'Empty' position to empty tank;
% * Set switch to 'Stir' position to activate tank stir state.
% Note: Tank will enter 'STIR' state only when fluid level is full;
% otherwise, 'Stir' command has no effect. If tank is in 'STIR' state,
% the STIR indicator lamp will be on, otherwise off.
% The numerical value of tank command is the following:
% 'Fill' - 0
% 'Hold' - 1
% 'Empty' - 2
% 'Stir' -3 


% Tank Animation GUI will show tank status as simulation runs.

% To observe states of ladder rungs and data values, select and open the 
% ladder block 
% 'Controller/TankControl_runner/__Logic/TankControl/__Enable/__Logic'
% in separate window. 

% Alternatively, open plcdemo_ladder_tankcontrol_complete model directly 
% and skip model creation in MATLAB:

plcdemo_ladder_tankcontrol_complete;

%% Step 6: Ladder code and testbench generation
% Open ladder tank control testbench model:

plcdemo_ladder_tankcontrol_tb;

%Select the Controller/TankControl_runner block and right-click PLC 
%Coder->Generate Code for Subsystem to generate ladder code.

%To generate testbench, select the Generate testbench for subsystem option
%in the PLC Configuration Parameters Dialog and generate code.

% LocalWords:  slx plcimportladder HMI
