The following was posted in response to a question in the Process Control group on LinkedIn. The question was:
Has anyone programmed a state machine to operate equipment in a PLC environment such as Allen Bradley or Modicon? Did a state machine simplify the logic required or make it more complex?
You can definitely code Finite State Machine in ladder logic. I don't program ladder, but if I had to I would use FSMs for any sequential logic where the system response depends on past history (context)
I can also almost guarantee that the program will be larger (have more rungs, I think you'd call it :-) than a program written optimally for the task at hand. However, the FSM approach has a huge advantage which makes the possibly larger code worth while: It follows a consistent pattern and can be mapped to a state diagram.
Let me clarify..
A state diagram is a diagrammatic representation of what the system (or FSM subsystem) is to do. It very clearly defines how the system shall respond to a given input "event" in a given state. The state (where it is in the state diagram) fully expresses the significant aspects of past history. (If this diagram is a mystery to you, check out my tutorial on finite state machines).
In ladder you would most likely represent N states with N latching relays, only one of which may be on at once. The ON relay enables a rung (or bunch of rungs) that express the "what to look for, what to do" of that state.
Getting back to the state diagram: It is a graphical representation of the required behaviour. It exists at a higher level of abstraction than the ladder diagram. It is therefore more powerful for describing the behaviour (and for trouble shooting bugs). It is incidentally a lot easier to explain to a lay person than a ladder diagram (for the very reason it separates the functionality from the mechanics).
Now, if you design a state diagram, and desk check it thoroughly, coding it into ladder should be a relatively mechanical no-think operation. Avoid clever coding shortcuts. Be consistent in how you translate from diagram to code (style). If you are boringly consistent then it will be more likely to work first off. Any logical problems can be debugged with reference to the state diagram, not to the code. Furthermore, there will be an exact correspondence between the a place in the state diagram and the equivalent bit of ladder code. Use identical labels in the diagram and ladder code. (You ladder programmers have this one advantage over C programmers. An FSM coded in C will typically wind up with the code for a given state schmeared all over the landscape (BTW schmear is a perfectly cromulent word! My spell checker is ignorant :-).)
Apart from being a very powerful concept, FSMs thus have the added power of reducing a broad range of problems to a single way of solving them. The better you become at applying FSM solutions, the less you will have to think about the mechanics and the more you will be able to concentrate on the required system behaviour.
ladder state diagram should always be the first port of call if a change is required. By studying the
diagram you can easily locate the point where an incision needs to be made, then go from there to the
ladder code and know you are cutting in exactly the right place. That is not the case for "clever" coding.
The one disadvantage you may find in this (apart maybe from a larger program), is that the state diagram really needs to be stored with the ladder diagram for future maintenance. The connection between the two can be lost unless you are working in a well controlled, disciplined environment. You must also enforce the discipline that the state diagram is always referred to for changes, and of course updated.
In summary, yes you can use FSMs in ladder PLCs. The code may become a little bigger, but the logic will not become more complicated, it will become more structured.
A message from our sponsor: My company makes board level embedded controllers for OEM use. We use a proprietary text based language which, though not an FSM language as such makes it extremely easy to express FSMs. They also have a multitasking operating system that is very easy to understand and use.
We do not at this time (October 2009) support relay ladder logic programming in our products. That does not preclude us doing so in future products.
For more information about FSMs see Finite State Machine Tutorial.
If you want to get started programming FSMs in ladder PLCs, this white paper goes into useable detail (pdf).