%% Using Timers in Ladder Logic
%
% This example shows how to model and simulate a motor controller in 
% Simulink(R) by using Ladder Logic. The example uses the ladder |Timer| 
% instruction to implement the logic for delayed switching of a motor. The 
% timer |T1| is used to control the starting delay and the timer |T2| is 
% used to control the stopping delay.

%   Copyright 2018 The MathWorks, Inc.

%%
% 
% <<ladder/timers.png>>
% 

%% Inputs and Outputs
% The Motor Controller has two inputs, |Start| and |Stop|. Changing the 
% |Start| input value to |1| will start the motor after 5 seconds. Changing
% the |Stop| input value to |1| will stop the motor after 2 seconds. |Stop|
% input will override the |Start| input.
%
% The output signal named |Motor| will be |1| when the motor is ON and |0|
% when the motor id OFF.

%% Motor Controller
% The |Motor Controller| block is a PLC Controller block. It contains a
% Ladder Program block which houses the ladder logic. Open the 
% |Motor Controller| block and then open the |Ladder Diagram Program| block
% to view the ladder logic for the controller.
%%
% 
% <<ladder/timers_logic.png>>
%
% This ladder logic has a |TON| timer named as |T1| which is responsible
% for the delay during starting the motor, and a |TOF| timer named as |T2| 
% which is responsible for the delay during the stopping of the motor. 
% 
% When the |Start| input is toggled to |1|, the |MotorStart| output in the 
% first rung gets activated which starts the timer |T1| counting operation.
% |T1.DN| bit is set when the |T1| finishes counting. This causes the third
% rung with timer |T2| to become activated. Since |T2| is a |TOF| timer,
% the |T2.DN| bit is set but, timer starts counting operation only when
% this rung becomes false. Hence, both the inputs to the lowermost rung
% are true and the |Motor| output gets activated.
%
% When the |Stop| input is toggled to |1|, the |MotorStart| coil gets
% deactivated and hence the |T1.DN| bit is reset and the timer |T2| starts
% counting. Once the |T2| finishes counting operation, the |T2.DN| bit gets 
% reset and the |Motor| output gets deactivated.

%% Timer Configuration
% The timer configurations are specified in the |InitFcn| callback inside
% the |Model Properties|. To modify the start and stop delays, open the 
% |InitFcn| callback from the |Model Properties| dialog from Modeling > 
% Model Settings > Model Properties.
%
% |T1_InitialValue.PRE| specifies the Preset value of timer |T1| and the 
% |T2_InitialValue.PRE| specifies the Preset value of timer |T2|. Both 
% these values are specified as milliseconds.

%% Human Machine Interface (HMI)
% Double click the |HMI| Subsystem if it is not already open to bring up
% the Human Machine Interface (HMI) for this example. This HMI has the 
% following sections:
%
% * *Inputs:*
% The |Start| and |Stop| toggle switches are used to change the value of
% the respective inputs. When the toggle switch is in |On| position the
% value of the corresponding input will be 1.
% * *Motor:*
% This indicates the status of the motor. Green colored |Motor ON|
% indicator means that the motor is running whereas grey colored indicator
% means that the motor is stopped.
% * *Graphs:*
% This section of the HMI Subsystem displays the status of |Start|, |Stop| 
% and |Motor| against time as the simulation progresses.