PLC State Machine Coding
PLC State Machine Coding
(OP)
Mucking about in this forum, I've been reading the threads pertaining to state machines trapped and forced to live out their existence on Ladder Logic platforms.
Why? Because I'm working on a big(for me) PLC job that I've come to realize should be done as a SM.
Here is a statement I think makes a lot of sense.
How is this implemented? Is it just a single output rung with a slew of parallel 'state number' contacts?
Where do You put this output statement? In an "outputs area" or perhaps after the last state that would apply?
Why? Because I'm working on a big(for me) PLC job that I've come to realize should be done as a SM.
Here is a statement I think makes a lot of sense.
Quote:
I believe that every step in a process should have a state. That way you can have a simple single point for outputs: A solenoid is on in states 3 to 5 and state 7. The solenoid operation is clearly defined in a single rung and there are no surprises when you go back to modify the solenoid operation.
How is this implemented? Is it just a single output rung with a slew of parallel 'state number' contacts?
Where do You put this output statement? In an "outputs area" or perhaps after the last state that would apply?
Keith Cress
kcress - http://www.flaminsystems.com





RE: PLC State Machine Coding
I do a lot of state coding for a client. The proper way is to group your outputs in a area as you suggest. The rung (if ladder) is as you suggest with multiple or'd Normally Open contacts of the state machine continuous states. Keeping mind the type of equipment the output operates. If its a single sided spring return solenoid then the state will have to hold the output on by a self latch that is broken by a Normally Closed of another state further along in the sequence. A proper state machine will use a comparator for every state driving a OTE for each state as well as a pulse bit. If possible jump state numbers to allow for spare insertion for later modifications. Hope this helps.
RE: PLC State Machine Coding
Hey! Welcome to Eng-Tips. And thanks for signing up to provide an answer to me. Most appreciated.
OK, group the outputs.
Yes this small PLC is strictly a LL machine. This job looks like about 200 machine states. I'm hoping it will all fit.
This project is virtually all pneumatic. There are twenty air lines leaving the enclosure, two VFDs, and a clutch drive. The inputs are a couple of prox detector signals and a slew of rod/piston location switches.
The valves are two types. ON-OFF-ON and ON-ON. (Parker digital manifolded types) I'm told all the ON-ON ones are latching. I'm not sure that actually means they will latch on only a rung or two of ON time.
"OTE" ?
Ha! I was just thinking about that. Realizing I will likely need to add states I can see myself having to run down thru a 100 states manually incrementing all the state numbers by one. - About a dozen times. (make that 2 dozen!)
Thanks.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Also, there are forums and website that cover state machine logic in more detail.
I did do state machine coding but the oem standard was maybe 15 states not over 100.
RE: PLC State Machine Coding
RE: PLC State Machine Coding
TygerDawg
Blue Technik LLC
Virtuoso Robotics Engineering
www.bluetechnik.com
RE: PLC State Machine Coding
tygerdawg; Unless you're referring to the "drum" function are you thinking their DL05/06 series? It's about twice the price and BIGGER.
I'm annoyed by ADirect's lack of documentation. I expected to see a coding manual with at least some simple examples. Instead they appear to funnel you into, 'take this over priced course'. Of the ones I've sampled they seem to completely miss providing the desired knowledge being a bunch of noise instead.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
I don't have an argument against your 'size of PLC' complaint.
My experience in buying AD's re-branded Koyo products ("DirectLogic" series), both hardware & software: they are fabulously well documented. After a quick glance at the website, it appears that all of the hardware manuals are freely downloadable.
I believe the explanation of the AD method of state programming is in the documentation for the DirectSoft programming software. You must purchase that, it seems they don't allow free downloads of that manual. I thought they did at one time, but could be mistaken. I vaguely recall that the last time I purchased it, it was cheap ($350 or so??). It is simply a few special function rungs that allows one to group a series of rungs, make series of groups, and advance through the groups in desired sequence. When a particular group is enabled, all other groups are disabled. This could be done manually.
TygerDawg
Blue Technik LLC
Virtuoso Robotics Engineering
www.bluetechnik.com
RE: PLC State Machine Coding
As for the manuals, Yes, they're generally pretty good as is the CLICK one. Nothing missing except a chapter explaining the actual commands. Most commands like NO or NC inputs are of course simple but since this PLC is considered their get-your-feet-wet unit you'd think they would bother here. Oh well.
BTW the Drum instruction actually will do Time Steps or State Steps. Still looks a wee bit too constrained to be useful to me. Only one input allowed per state. You can have up to 16 outputs per state.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Why dont you combine all the inputs necesssary into one input for it to go on to next step? Its probably not constrained to a physical input but just one bool, which can be any combined state of logic.
RE: PLC State Machine Coding
I did not end up using the drum function but I did play with it for a while and got an idea of how it worked.
RE: PLC State Machine Coding
I've decided not to use the drum function either. I've gone with the latch/unlatch state machine and it's working fine. Now I wish you could cut-and-paste multiple lines of code since there is a huge amount of repetition but, that's probably just being greedy. :)
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
RE: PLC State Machine Coding
Automation Direct Code Examples List
RE: PLC State Machine Coding
To top that off, nothing in the normal LINE editor works when I've woken it from its crypt with the CNTRL key. I can't subsequently 'pick' any lines and have them 'substantiate' like they should. ODD.
And another thing. :)
I can't believe that when you select "..." before entering a contact address or coil address and it pedantically launches the Address Picker, you can't Nickname the chosen contact/coil even though the space is staring at you. You have to select the contact double-clicking it. Once the Address Picker closes you have to go over and open it again, wait for to eventually re-open with the exact same view and then you have to muck about to find the same contact again T H E N you can nickname it. Is there a better way I'm missing?
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
My question is this: If I do as described above but then have several different state machines, is it reasonable to leave the output statements after each state machine or should I move all outputs to the end of the entire program?
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Another way is to have one rung for each state and turn on or off the I/O for that state. Some people don't like this because the outputs can be turned on or off in multiple states but at least it is easy to see in both cases.
If there is a lot of I/O this second method can save a lot of ladder rungs because there are usually fewer states than I/O.
Some people like state bits and others like state numbers. With state bits one doesn't get hung up on the oreder they are allocated.
One thing that is important is to avoid race conditions where two or more states can be executed in one scan. To avoid this I keep two sets of state bits. One is the next_state bits and the other is the current state bits. At the top of the rung I copy the next state bits into the current state bits by words or dwords. In the state machine I only change the next state bits. This way no changes in the next state will activate a new state in the current scan. One needs to do the same thing if working with state numbers.
Each state may have more than one rung. The first rung is a one shot where code that needs to be executed once on entry to the state are execute. The second rung is ladder that gets executed all the time. The second rung should probably be divided in to more rungs. For instance there can be a transition rung that determines when to change the next state and perhaps start a time out timer in case an I/O doesn't come one. There can also be a clean up rung for on exit.
Don't try to put too much into a state. I once helped a PLC programmer that was programming a lifting device. He had one rung for up and one for down. He struggled with this for a week or so. I asked him to explain what he was doing and we broke the problem down into many states with 11 rungs or so. The PLC programmer did not like having 11 rungs but he got it to go in 4 hours after that. It was a simple program that should have been done in four hours. The two rung solution suffered from race conditions.
Look at Moore and Mealy state diagrams.
The main thing is to avoid the race conditions and be consistent on how you do your state machine.
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
Programming Equipment States Efficiently
State based control is a method of defining the required states of some equipment and then driving the equipment to one of these states, typically where a Phase step sets the Equipment States. ControlDraw has long supported this method as it provides a very efficient method of representing functional ....
See the link for details
Francis
www.controldraw.co.uk
www.s88control.blogspot.com
RE: PLC State Machine Coding
I'm using the following form:
CODE
This is working really well. My biggest fear is having to make a change to state_2 when
I have twenty states.. :)
It's really nice to have the ouput directly after the state. But of course this causes the
"multiple same outputs" hassle because inevitably multiple states control the same outputs.
So, I moved them all to the end of their respective state machines. That was fine until,
being done with all four state machines, I discovered that I had to do an initialization
state machine to setup the physical machine. That state machine uses outputs from all
the other statemachines combined! So now I probably have to move all the outputs down
to the very end of all the state machines.
The only problem with that is that it's hard to easily see what a particular state you're looking at effects without scrolling down two hundred lines.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
for the copy and pasting of multiple lines I was just clicking on the very first line I wanted to copy then hold shift and arrow down the old fashioned way unfortunately. The quick keys have different actions in the Click Software so you can't ctrl click or shift click and skip lines that I have found yet so I was taking big chunks then deleting what I didn't want in the middle.
as for the nicknaming of the coils etc when you double click and then click ... at the bottom go to "Edit Mode" instead of "Pickup Mode" and you can name any/all of the ones that you want. unfortunately I think when you see the blank spot you still must name it by the "x001" etc given corresponding port on the PLC unit instead of using nicknames but the nicknames show up in the ladder and you can see if you have the right one. (Unless I am misunderstanding what you are trying to name or pick there?)
The "Same multiple outputs" is only a warning and will still compile and run as long as you are not depending on one rung to turn on/off an output and another to use that same output under completely different methods. if that makes sense? I was using the Click to program a 7 thermostat/7 furnace system.
For my program I had multiple rungs with the same output or multiple outputs that were the same. for example I created a coil of "System Delay" and this was set for 1 minute after any furnace turned on, set for 3 minutes after any furnace shut off and also set if there were 3 heaters running. Each rung to turn on one of the 7 furnaces had to meet the condition of C14 being "closed" (---|\|---) before it would be able to turn on. Each furnace therefor set that delay bit for me until a timer was met and turned it off.
It was probably made way more complicated than it needed to be but it was the way that I saw to make it work for my application and it works. Commenting the code will get rid of the confusion.
Also you might look into using the C bits (Control Relays) as place holders. It made my application easier to accomplish. If you need certain conditions met to set an output put the smaller conditions to set like C1, C2 etc then for that output you need to set make the rung conditions
C1 C2 Set Y001
--| |----------| |---------------
maybe? That might make it easier than having to change the states for later on but it will probably add more rungs to your ladder for the smaller conditions.
RE: PLC State Machine Coding
I wouldn't be too concerned about have multiple instances of the same output when you are using a state machine AND YOU DON'T HAVE RACE CONDITIONS!!! Using the same output more than once is poor if you aren't disciplined and not using a state machine. The problem with using an output more than once is that state one could turn an output on, and in your example, state two could turn the same output off in the same scan. You are depending on SOME_CONDITION and SOME_OTHER _CONDITION to be mutually exclusive. If using a state machine where only one state gets executed per scan you really shouldn't have to worry about using the same output in multiple states. You don't need to worry about another state executing that will change the outputs in that scan.
Even if you put all the outputs at the end of the ladder all coils that should be set in state one would never be set because your state machine went from state 1 to state 2 in one scan. Obviously putting all the outputs at the end makes no difference real difference. It is avoiding the race conditions that make a difference.
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
YES!!! Thank you, thank you!
Huge speed improvement. Wish I'd know about these 300 rungs ago. I had to copy every third rung then go back and do every other rung, etc., etc., ad nauseum. LOL
And now I won't have to go around in circles with naming new elements by using the "Edit Mode".
Both actions are so lame.. At least these work around it somewhat.
Ha! My next job is a four stage thermostat with an HMI screen. I liked your multi-furnace example.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Peter thanks for the details, I've read them about 10 times.
CODE
I thought this was the case only during this one single rung and not true by the end of this same exact rung? Am I misinformed?
============================================
This is a machine swimming with pneumatic cylinders, SOME_OTHER_CONDITION cannot happen until a cylinder has actually moved a distance taking >50 x scan_times,(scan_time=~5ms). In these cases "_CONDITIONs" are rod position sensors. So, for the machine to have been in SOME_CONDITION, SOME_OTHER_CONDITION had to have been un-activated. (I now realize this isn't obvious from my code example above.)
Do you think I still need to include anti-race code for this type of machine? I can definitely see the need in a more complex machine where, say, there are parallel functions going on and SOME_OTHER_CONDITION could be pre-setup while STATE_1 is still active.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
I really don't do PLC programming, I make motion controllers. I have had to learn PLC programming because I/we would get too many tech support calls from people the claimed out motion controllers didn't work or go to where they think the actuators should move to. The main reason is race conditions. A prime example is that a move command is issued on a rung and then the in position bit is checked to see if the move is done on the next rung in the same state. If the in position bit state machine goes to the next state and the next move command is executed. The problem is that the motion controller doesn't get the command until the end of the scan so the in position bit is not cleared till the next scan but the programmer checked for the in position in the next rung and the bit is still set. The state machine thinks the actuator is in the new position when in fact it hasn't moved yet and the motion controller hasn't even got the move command. The cure is to break the move and wait till in position into two different states so only one thing can be done at a time. Just write your code to avoid race conditions so you NEVER have to worry about them now or in the future.
If you want extra credit add some code that can determine what state to restart your machine in. My back ground is in saw mill automation. Early on a senior programmer told me that anybody can write code to make a machine run when every thing is working properly. The boards or pieces per minute were pretty much determined by the machinery. However, when things didn't work right the PLC can make a BIG difference in the recovery time and how much production is lost. I am sure there are people on this forum that can tell you about how when their system has a fault they must run off all the widgets in various stages of production. A good PLC program may be able to restart so the production can start where it left off and no production is wasted.
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
Initialization: I waited until I had the various state machines fleshed out then turned to initialization. YiKes! That's way harder to figure out and get right. A lot of cases you don't want to think about - but have to.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
example
state 1 intial
move 1 step
state 2 start
mov 2 step
Use a equal statement with other logic to qualify the next step, this way you can see that the machine cycles thru each step or skips certain steps, based on different inputs.
etc for each step
Best way to check out a cycling machine when you have it together is force all the outputs to a safe state.
example
Then when you go to run mode make sure all the logic for state 1 intialize goes true, then qualifies the next step, and then qualifys that next step, etc .. till your machine goes back to step whatever where u want to cycle repeatly. All the time when you are at each step let the outputs go to their on state during the step, then after its done force it back to the safe state. This way you can see if their is a race condition on other steps or just bad logic somewhere that would let an output you do not want fired to stay off.
Used this type of sequencer on many machines and seemed to work with good success.
RE: PLC State Machine Coding
I understand what you're describing. I actually started that way. This PLC doesn't appear to have MOV commands. This meant everything had to be done thru the MATH statement which is rather laborious to manipulate. I was also unclear about the speed of doing math functions. Hence, I went to the SET/RESET method.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
state_word = 1 ==> step 1
state_word = 2 ==> step 2
state_word = 4 ==> step 3
etc...
use a bit shift left or right,( maybe they have this function? ) to change the step
I know ab and siemens had an execution time spreadsheet for each function. Does this processor have such a table with all its function versus execution time? I would think they would if its sold commercially.
RE: PLC State Machine Coding
Interpreted math functions will be slow.
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
That would probably be workable but painful on this PLC.
PNachtwey; This one: :)
http://www.automationdirect.com/static/specs/c0pgmsw.pdf
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
RE: PLC State Machine Coding
But if you CAN pull it off, oh so cost effective and physically convenient.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
Very cost effective compared to the AB's and still ways to figure out what you need it to do with the limited commands.
Sorry I was AWOL there for a bit on helping you out, I had a bunch of stuff land on my desk that is due today/tomorrow and not a lot of time to get it all sorted out but hope you figured out what you needed!
RE: PLC State Machine Coding
If you like Automation Direct get AD PLC of suitable size and use their stage programming. If you don't you will waste far more money in time than you save in the difference in cost between what you have and what you should have.
I have a DL250 that I use for compatibility testing. It is a very good PLC with a high 'bang for the buck'.
Your time has value, so does ours.
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
In my defense I'm fairly noob on PLCs so working my way up has some benefit. Next time around I know what to look for.
Keith Cress
kcress - http://www.flaminsystems.com
RE: PLC State Machine Coding
http://forum.automationdirect.com/
You should read through posts and ask questions BEFORE choosing PLCs.
There is a much better and more general PLC from at
http://www.plctalk.net/qanda/forumdisplay.php?f=2
Did you look up stage programming just so that you know in the future?
http://www.automationdirect.com/static/manuals/d4user/ch7.pdf
http://www.plcdev.com/stage_programming_tutorial_by_example
If you don't have much time invested in the project you still may be better off to get a small but better AD PLC.
What is your time worth?
Peter Nachtwey
Delta Computer Systems
http://www.deltamotion.com
RE: PLC State Machine Coding
RE: PLC State Machine Coding
Keith Cress
kcress - http://www.flaminsystems.com