Automated Manufacturing Systems

state



12. STATE BASED DESIGN

 

12.1 INTRODUCTION

A system state is a mode of operation. Consider a bank machine that will go through very carefully selected states. The general sequence of states might be idle, scan card, get secret number, select transaction type, ask for amount of cash, count cash, deliver cash/return card, then idle.

A State based system can be described with system states, and the transitions between those states. A state diagram is shown in Figure 138 A State Diagram. The diagram has two states, State 1 and State 2. If the system is in state 1 and A happens the system will then go into state 2, otherwise it will remain in State 1. Likewise if the system is in state 2, and B happens the system will return to state 1. As shown in the figure this state diagram could be used for an automatic light controller. When the power is turned on the system will go into the lights off state. If motion is detected or an on push button is pushed the system will go to the lights on state. If the system is in the lights on state and 1 hour has passed, or an off push button is pushed then the system will go to the lights off state. The else statements are omitted on the second diagram, but they are implied.

 

Figure 138 A State Diagram

The most essential part of creating state diagrams is identifying states. Some key questions to ask are,

1. Consider the system,

What does the system do normally?

Does the system behavior change?

Can something change how the system behaves?

Is there a sequence to actions?

2. List modes of operation where the system is doing one identifiable activity that will start and stop. Keep in mind that some activities may just be to wait.

Consider the design of a coffee vending machine. The first step requires the identification of vending machine states as shown in Figure 139 Definition of Vending Machine States. The main state is the idle state. There is an inserting coins state where the total can be displayed. When enough coins have been inserted the user may select their drink of choice. After this the make coffee state will be active while coffee is being brewed. If an error is detected the service needed state will be activated.

 

Figure 139 Definition of Vending Machine States

The states are then drawn in a state diagram as shown in Figure 140 State Diagram for a Coffee Machine. Transitions are added as needed between the states. Here we can see that when powered up the machine will start in an idle state. The transitions here are based on the inputs and sensors in the vending machine. The state diagram is quite subjective, and complex diagrams will differ from design to design. These diagrams also expose the controller behavior. Consider that if the machine needs maintenance, and it is unplugged and plugged back in, the service needed statement would not be reentered until the next customer paid for but did not receive their coffee. In a commercial design we would want to fix this oversight.

 

Figure 140 State Diagram for a Coffee Machine

12.1.1 State Diagram Example

Consider the traffic lights in Figure 141 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.

 

Figure 141 Traffic Lights

The first step for developing a controller is to define the inputs and outputs of the system as shown in Figure 142 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.

 

Figure 142 Inputs and Outputs for Traffic Light Controller

Previously state diagrams were used to define the system, it is possible to use a state table as shown in Figure 143 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.

 

Figure 143 System State Table for Traffic Lights

Transitions can be added to the state table to clarify the operation, as shown in Figure 144 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.

 

Figure 144 State Table with Transitions

A state diagram for the system is shown in Figure 145 A Traffic Light State Diagram. This diagram is equivalent to the state table in Figure 144 State Table with Transitions, but it can be valuable for doing visual inspection.

 

Figure 145 A Traffic Light State Diagram

12.1.2 Conversion to Ladder Logic

12.1.2.1 - Block Logic Conversion

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 146 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.

 

Figure 146 Inputs and Outputs for Traffic Light Controller

The initial ladder logic block shown in Figure 147 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.

 

Figure 147 Ladder Logic to Initialize Traffic Light Controller

 

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.

 

Figure 148 General Output Control Logic

The first state is implemented in Figure 147 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.

 

Figure 149 Ladder Logic for First State

The second state is more complex because it involves a time delay, as shown in Figure 150 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.

 

Figure 150 Ladder Logic for Second State

The third and fourth states are shown in Figure 151 Ladder Logic for State Three and Figure 152 Ladder Logic for State Four. Their layout is very similar to that of the first two states.

 

Figure 151 Ladder Logic for State Three

 

Figure 152 Ladder Logic for State Four

The previous example only had one path through the state tables, so there was never a choice between states. The state diagram in Figure 153 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.

 

Figure 153 A State Diagram with Priority Problems

The state diagram in Figure 153 A State Diagram with Priority Problems is implemented with ladder logic in Figure 154 State Diagram for Prioritization Problem and Figure 155 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.

 

Figure 154 State Diagram for Prioritization Problem

 

Figure 155 State Diagram for Prioritization Problem

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.

12.1.2.2 - State Equations

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 156 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.

 

Figure 156 State Equations

The state equation method can be applied to the traffic light example in Figure 145 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.

 

Figure 157 State Equations for the Traffic Light Example

The equations in Figure 157 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 158 Simplified Boolean Equations so that all NOT operators are only over a single variable.

 

Figure 158 Simplified Boolean Equations

These equations are then converted to the ladder logic shown in Figure 159 Ladder Logic for the State Equations and Figure 160 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.

 

Figure 159 Ladder Logic for the State Equations

 

Figure 160 Ladder Logic for the State Equations

This method will provide the most compact code of all techniques, but there are potential problems. Consider the example in Figure 160 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 161 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.

 

Figure 161 Delayed State Updating

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 162 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 163 Ladder Logic with Prioritization.

 

Figure 162 State Equations with Prioritization

 

Figure 163 Ladder Logic with Prioritization
12.1.2.3 - State-Transition Equations

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 164 State-Transition Equations for the traffic light example of Figure 145 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.

 

Figure 164 State-Transition Equations

These equations can be converted directly to the ladder logic in Figure 165 Ladder Logic for the State-Transition Equations, Figure 166 Ladder Logic for the State-Transition Equations and Figure 167 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.

 

Figure 165 Ladder Logic for the State-Transition Equations

 

Figure 166 Ladder Logic for the State-Transition Equations

 

Figure 167 Ladder Logic for the State-Transition Equations

The problem of prioritization also occurs with the State-Transition equations. Equations were written for the State Diagram in Figure 168 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.

 

Figure 168 Prioritization for State Transition Equations

12.2 SUMMARY

· State diagrams are suited to processes with a single flow of execution.

· State diagrams are suited to problems that has clearly defines modes of execution.

· Controller diagrams can be converted to ladder logic using MCR blocks

· State diagrams can also be converted to ladder logic using equations

· The sequence of operations is important when converting state diagrams to ladder logic.

12.3 PRACTICE PROBLEMS

(Note: Problem solutions are available at http://sites.google.com/site/automatedmanufacturingsystems/)

1. Draw a state diagram for a microwave oven.

2. Convert the following state diagram to equations.

3. Implement the following state diagram with equations.

4. Given the following state diagram, use equations to implement ladder logic.

5. Convert the following state diagram to logic using equations.

6. You have been asked to program a PLC that is controlling a handicapped access door opener. The client has provided the electrical wiring diagram below to show how the PLC inputs and outputs have been wired. Button A is located inside and button B is located outside. When either button is pushed the motor will be turned on to open the door. The motor is to be kept on for a total of 15 seconds to allow the person to enter. After the motor is turned off the door will fall closed. In the event that somebody gets caught in the door the thermal relay will go off, and the motor should be turned off. After 20,000 cycles the door should stop working and the light should go on to indicate that maintenance is required.

 

a) Develop a state diagram for the control of the door.

b) Convert the state diagram to ladder logic. (list the input and the output addresses first)

c) Convert the state diagram to delayed update equations.

7. Design a garage door controller using a) block logic, and b) state-transition equations. The behavior of the garage door controller is as follows,

- there is a single button in the garage, and a single button remote control.

- when the button is pushed the door will move up or down.

- if the button is pushed once while moving, the door will stop, a second push will start motion again in the opposite direction.

- there are top/bottom limit switches to stop the motion of the door.

- there is a light beam across the bottom of the door. If the beam is cut while the door is closing the door will stop and reverse.

- there is a garage light that will be on for 5 minutes after the door opens or closes.

8. Convert the following ladder logic to delayed update equations and then draw the state diagram for the system. Is something missing from the system?

9. A program is to perform the following actions for a self-service security check. The device will allow bags to be inserted to the test chamber through an entrance door. If the bag passes the check it can be removed through an exit door, otherwise an alarm is sounded. Create a state diagram using the steps below.

1. The machine starts in an `idle' state. The `open_entry' output is activated to open the input door. The `open_exit' output is deactivated to close the output door.

2. When a bag is inserted the `bag_detected' input goes high. The `open_entry' output should be deactivated to close the door.

3. When the `entry_door_closed' and `exit_door_closed' inputs are active then a `test' output will be set high to start a scan of the bags.

4. When the scan of the bags is complete a `scan_done' input is set. The `test' output should be turned off.

5. The scan results in two real values `nitrates' and `mass'. The calculation below is performed. If the `risk' is below 0.3, or above 23.5, then the machine enters an alarm state (step 8), otherwise it continues to step 6.

6. The `open_exit' output is activated to open the exit door. The machine waits until the `bag_detected' input goes low.

7. The `open_exit' output is deactivated to close the door. The machine waits until the `exit_door_closed' input is high before returning to the `idle state.

8. In the alarm state an operator input `key' must be active to open the exit door. After this input is released the door will close and return to the `idle' state.

12.4 ASSIGNMENT PROBLEMS

1. Describe the difference between the block logic, delayed update, and transition equation methods for converting state diagrams to ladder logic.

2. Write the ladder logic for the state diagram below using the block logic method.

3. Convert the following state diagram to ladder logic using the block logic method. Give the stop button higher priority.

4. Convert the following state diagram to ladder logic using the delayed update method.

5. Use equations to develop ladder logic for the state diagram below using the delayed update method. Be sure to deal with the priority problems.

6. Implement the State-Transition equations.in the figure below with ladder logic.

7. Write ladder logic to implement the state diagram below using state transition equations.

8. Convert the following state diagram to ladder logic using a) an equation based method, b) a method that is not based on equations.

9. The state diagram below is for a simple elevator controller. a) Develop a ladder logic program that implements it with state transition equations. b) Develop the ladder logic using the block logic technique. c) Develop the ladder logic using the delayed update method.

10. Write ladder logic for the state diagram below a) using an equation based method. b) without using an equation based method.

11. For the state diagram for the traffic light example, add a 15 second green light timer and speed up signal for an emergency vehicle. A strobe light mounted on fire trucks will cause the lights to change so that the truck doesn't need to stop. Modify the state diagram to include this option. Implement the new state diagram with ladder logic.

12. Design a program with a state diagram for a hydraulic press that will advance when two palm buttons are pushed. Top and bottom limit switches are used to reverse the advance and stop after a retract. At any time the hands removed from the palm button will stop an advance and retract the press. Include start and stop buttons to put the press in and out of an active mode.

13. In dangerous processes it is common to use two palm buttons that require a operator to use both hands to start a process (this keeps hands out of presses, etc.). To develop this there are two inputs (P1 and P2) that must both be turned on within 0.25s of each other before a machine cycle may begin.

Develop ladder logic with a state diagram to control a process that has a start (START) and stop (STOP) button for the power. After the power is on the palm buttons (P1 and P2) may be used as described above to start a cycle. The cycle will consist of turning on an output (MOVE) for 2 seconds. After the press has been cycled 1000 times the press power should turn off and an output (LIGHT) should go on.

14. Use a state diagram to design a parking gate controller.

15. This morning you received a call from Mr. Ian M. Daasprate at the Old Fashioned Widget Company. In the past when they built a new machine they would used punched paper cards for control, but their supplier of punched paper readers went out of business in 1972 and they have decided to try using PLCs this time. He explains that the machine will dip wooden parts in varnish for 2 seconds, and then apply heat for 5 minutes to dry the coat, after this they are manually removed from the machine, and a new part is put in. They are also considering a premium line of parts that would call for a dip time of 30 seconds, and a drying time of 10 minutes. He then refers you to the project manager, Ann Nooyed.

You call Ann and she explains how the machine should operate. There should be start and stop buttons. The start button will be pressed when the new part has been loaded, and is ready to be coated. A light should be mounted to indicate when the machine is in operation. The part is mounted on a wheel that is rotated by a motor. To dip the part, the motor is turned on until a switch is closed. To remove the part from the dipping bath the motor is turned on until a second switch is closed. If the motor to rotate the wheel is on for more that 10 seconds before hitting a switch, the machine should be turned off, and a fault light turned on. The fault condition will be cleared by manually setting the machine back to its initial state, and hitting the start button twice. If the part has been dipped and dried properly, then a done light should be lit. To select a premium product you will use an input switch that needs to be pushed before the start button is pushed. She closes by saying she will be going on vacation and you need to have it done before she returns.

You hang up the phone and, after a bit of thought, decide to use the following outputs and inputs,

 

a) Draw a state diagram for the process.

b) List the variables needed to indicate when each state is on, and list any timers and counters used.

c) Write a Boolean expression for each transition in the state diagram.

d) Do a simple wiring diagram for the PLC.

e) Write the ladder logic for the state that involves moving the part into the dipping bath.

16. Design ladder logic with a state diagram for the following process description.

a) A toggle start switch (TS1) and a limit switch on a safety gate (LS1) must both be on before a solenoid (SOL1) can be energized to extend a stamping cylinder to the top of a part. Should a part detect sensor (PS1) also be considered? Explain your answer.

b) While the stamping solenoid is energized, it must remain energized until a limit switch (LS2) is activated. This second limit switch indicates the end of a stroke. At this point the solenoid should be de-energized, thus retracting the cylinder.

c) When the cylinder is fully retracted a limit switch (LS3) is activated. The cycle may not begin again until this limit switch is active. This is one way to ensure that a new part is present, is there another?

d) A cycle counter should also be included to allow counts of parts produced. When this value exceeds some variable amount (from 1 to 5000) the machine should shut down, and a job done light lit up.

e) A safety check should be included. If the cylinder solenoid has been on for more than 5 seconds, it suggests that the cylinder is jammed, or the machine has a fault. If this is the case the machine should be shut down, and a maintenance light turned on.

f) Implement the ladder diagram on a PLC in the laboratory.

g) Fully document the ladder logic and prepare a short report - This should be of use to another engineer that will be maintaining the system.

17. a) Write ladder logic to implement the state diagram below using the state transition equation method.

b) Write ladder logic to implement the state diagram below using the delayed update equation method.

 

Site Links


Engineer On A Disk Main Page

- PLC Book Book Page