11.1.1 State Diagram Example
Consider the traffic lights in Figure 11.1 Traffic Lights. The normal sequences for traffic lights are a green light in one direction for a long period of time, typically 10 or more seconds. This is followed by a brief yellow light, typically 4 seconds. This is then followed by a similar light pattern in the other direction. It is understood that a green or yellow light in one direction implies a red light in the other direction. Pedestrian buttons are provided so that when pedestrians are present a cross walk light can be turned on and the duration of the green light increased.
The first step for developing a controller is to define the inputs and outputs of the system as shown in Figure 11.1 Inputs and Outputs for Traffic Light Controller. First we will describe the system variables. These will vary as the system moves from state to state. Please note that some of these together can define a state (alone they are not the states). The inputs are used when defining the transitions. The outputs can be used to define the system state.
Previously state diagrams were used to define the system, it is possible to use a state table as shown in Figure 11.1 System State Table for Traffic Lights. Here the light sequences are listed in order. Each state is given a name to ease interpretation, but the corresponding output pattern is also given. The system state is defined as the bit pattern of the 6 lights. Note that there are only 4 patterns, but 6 binary bits could give as many as 64.
Transitions can be added to the state table to clarify the operation, as shown in Figure 11.1 State Table with Transitions. Here the transition from Green E/W to Yellow E/W is S1. What this means is that a cross walk button must be pushed to end the green light. This is not normal, normally the lights would use a delay. The transition from Yellow E/W to Green N/S is caused by a 4 second delay (this is normal.) The next transition is also abnormal, requiring that the cross walk button be pushed to end the Green N/S state. The last state has a 4 second delay before returning to the first state in the table. In this state table the sequence will always be the same, but the times will vary for the green lights.
A state diagram for the system is shown in Figure 11.1 A Traffic Light State Diagram. This diagram is equivalent to the state table in Figure 11.1 State Table with Transitions, but it can be valuable for doing visual inspection.
11.1.2 Conversion to Ladder Logic
State diagrams can be converted directly to ladder logic using block logic. This technique will produce larger programs, but it is a simple method to understand, and easy to debug. The previous traffic light example is to be implemented in ladder logic. The inputs and outputs are defined in Figure 11.1 Inputs and Outputs for Traffic Light Controller, assuming it will be implemented on an Allen Bradley Micrologix. first scan is the address of the first scan in the PLC. The locations state_1 to state_4 are internal memory locations that will be used to track which states are on. The behave like outputs, but are not available for connection outside the PLC. The input and output values are determined by the PLC layout.
The initial ladder logic block shown in Figure 11.1 Ladder Logic to Initialize Traffic Light Controller will initialize the states of the PLC, so that only state 1 is on. The first scan indicator first scan will execute the MCR block when the PLC is first turned on, and the latches will turn on the value for state_1 and turn off the others.
The next section of ladder logic only deals with outputs. For example the output O/1 is the N/S red light, which will be on for states 1 and 2, or B3/1 and B3/2 respectively. Putting normal outputs outside the MCR blocks is important. If they were inside the blocks they could only be on when the MCR block was active, otherwise they would be forced off. Note: Many beginners will make the careless mistake of repeating outputs in this section of the program.
The first state is implemented in Figure 11.1 Ladder Logic to Initialize Traffic Light Controller. If state_1 is active this will be active. The transition is S1 which will end state_1 and start state_2.
The second state is more complex because it involves a time delay, as shown in Figure 11.1 Ladder Logic for Second State. When the state is active the TON timer will be timing. When the timer is done state 2 will be unlatched, and state 3 will be latched on. The timer is nonretentive, so if state_2 if off the MCR block will force all of the outputs off, including the timer, causing it to reset.
The previous example only had one path through the state tables, so there was never a choice between states. The state diagram in Figure 11.1 A State Diagram with Priority Problems could potentially have problems if two transitions occur simultaneously. For example if state STB is active and A and C occur simultaneously, the system could go to either STA or STC (or both in a poorly written program.) To resolve this problem we should choose one of the two transitions as having a higher priority, meaning that it should be chosen over the other transition. This decision will normally be clear, but if not an arbitrary decision is still needed.
The state diagram in Figure 11.1 A State Diagram with Priority Problems is implemented with ladder logic in Figure 11.1 State Diagram for Prioritization Problem and Figure 11.1 State Diagram for Prioritization Problem. The implementation is the same as described before, but for state STB additional ladder logic is added to disable transition A if transition C is active, therefore giving priority to C.
The Block Logic technique described does not require any special knowledge and the programs can be written directly from the state diagram. The final programs can be easily modified, and finding problems is easier. But, these programs are much larger and less efficient.
State diagrams can be converted to Boolean equations and then to Ladder Logic. The first technique that will be described is state equations. These equations contain three main parts, as shown below in Figure 11.1 State Equations. To describe them simply - a state will be on if it is already on, or if it has been turned on by a transition from another state, but it will be turned off if there was a transition to another state. An equation is required for each state in the state diagram.
The state equation method can be applied to the traffic light example in Figure 11.1 A Traffic Light State Diagram. The first step in the process is to define variable names (or PLC memory locations) to keep track of which states are on or off. Next, the state diagram is examined, one state at a time. The first equation if for ST1, or state 1 - green NS. The start of the equation can be read as ST1 will be on if it is on, or if ST4 is on, and it has been on for 4s, or if it is the first scan of the PLC. The end of the equation can be read as ST1 will be turned off if it is on, but S1 has been pushed and S2 is off. As discussed before, the first half of the equation will turn the state on, but the second half will turn it off. The first scan is also used to turn on ST1 when the PLC starts. It is put outside the terms to force ST1 on, even if the exit conditions are true.
The equations in Figure 11.1 State Equations for the Traffic Light Example cannot be implemented in ladder logic because of the NOT over the last terms. The equations are simplified in Figure 11.1 Simplified Boolean Equations so that all NOT operators are only over a single variable.
These equations are then converted to the ladder logic shown in Figure 11.1 Ladder Logic for the State Equations and Figure 11.1 Ladder Logic for the State Equations. At the top of the program the two timers are defined. (Note: it is tempting to combine the timers, but it is better to keep them separate.) Next, the Boolean state equations are implemented in ladder logic. After this we use the states to turn specific lights on.
This method will provide the most compact code of all techniques, but there are potential problems. Consider the example in Figure 11.1 Ladder Logic for the State Equations. If push button S1 has been pushed the line for ST1 should turn off, and the line for ST2 should turn on. But, the line for ST2 depends upon the value for ST1 that has just been turned off. This will cause a problem if the value of ST1 goes off immediately after the line of ladder logic has been scanned. In effect the PLC will get lost and none of the states will be on. This problem arises because the equations are normally calculated in parallel, and then all values are updated simultaneously. To overcome this problem the ladder logic could be modified to the form shown in Figure 11.1 Delayed State Updating. Here some temporary variables are used to hold the new state values. After all the equations are solved the states are updated to their new values.
When multiple transitions out of a state exist we must take care to add priorities. Each of the alternate transitions out of a state should be give a priority, from highest to lowest. The state equations can then be written to suppress transitions of lower priority when one or more occur simultaneously. The state diagram in Figure 11.1 State Equations with Prioritization has two transitions A and C that could occur simultaneously. The equations have been written to give A a higher priority. When A occurs, it will block C in the equation for STC. These equations have been converted to ladder logic in Figure 11.1 Ladder Logic with Prioritization.
A state diagram may be converted to equations by writing an equation for each state and each transition. A sample set of equations is seen in Figure 11.1 State-Transition Equations for the traffic light example of Figure 11.1 A Traffic Light State Diagram. Each state and transition needs to be assigned a unique variable name. (Note: It is a good idea to note these on the diagram) These are then used to write the equations for the diagram. The transition equations are written by looking at the each state, and then determining which transitions will end that state. For example, if ST1 is true, and crosswalk button S1 is pushed, and S2 is not, then transition T1 will be true. The state equations are similar to the state equations in the previous State Equation method, except they now only refer to the transitions. Recall, the basic form of these equations is that the state will be on if it is already on, or it has been turned on by a transition. The state will be turned off if an exiting transition occurs. In this example the first scan was given it’s own transition, but it could have also been put into the equation for T4.
These equations can be converted directly to the ladder logic in Figure 11.1 Ladder Logic for the State-Transition Equations, Figure 11.1 Ladder Logic for the State-Transition Equations and Figure 11.1 Ladder Logic for the State-Transition Equations. It is very important that the transition equations all occur before the state equations. By updating the transition equations first and then updating the state equations the problem of state variable values changing is negated - recall this problem was discussed in the State Equations section.
The problem of prioritization also occurs with the State-Transition equations. Equations were written for the State Diagram in Figure 11.1 Prioritization for State Transition Equations. The problem will occur if transitions A and C occur simultaneously. In the example transition T2 is given a higher priority, and if it is true, then the transition T3 will be suppressed when calculating STC. In this example the transitions have been considered in the state update equations, but they can also be used in the transition equations.