Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

PLC State Machine Coding 2

Status
Not open for further replies.

itsmoked

Electrical
Feb 18, 2005
19,114
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.
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 -
 
Replies continue below

Recommended for you

Sorry itsmoked, I was away from here for a few days. But now that I went back and read over your post:

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.

 
It smoked, I don't see a rung for the on-entry condition. You really should have that. Also, when SOME_CONDITION gets set in the first rung states one and two will be active, not good unless you are trying to run parallel states and if so you should document it. I can see you have the TRANSFER bits similar to my NEXT_STATE bits but you haven't avoided the race condition. You can still go directly from state 1 to state 2 in the same scan and if SOME_OTHER_CONDITION is set state 3 will be executed too!!!

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
 
AgeXVII said:
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"

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 -
 
Peter thanks for the details, I've read them about 10 times.

Peter said:
when SOME_CONDITION gets set in the first rung states one and two will be active


Code:
..ON_STATE_1  TRANFER_TO_STATE_2   ON_STATE_2
 ----| |-------------| |-------------([b]SET[/b])-----
                                |
                                |  ON_STATE_1
                                 ----([b]RST[/b])-----

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 -
 
Do you think I still need to include anti-race code for this type of machine?
With out actually being there it is hard to know for sure if you can get away with how you are doing it but I do know that just because you can get away with it now doesn't mean it is right or will be right in the future.

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
 
Thanks again Peter. I understand now.

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 -
 
The typical way to make a state machine is not a latch or unlatch but with a mov statement .

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.
 
Thanks CD.

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 -
 
So can you use a word and use a bit in each part of the work for each set and reset?

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.
 
The Click PLC is pretty limited... Good beginners one but difficult for lengthy or complex programs. Took me a while just to think how to execute mine with the limited functions
 
Oh I know, I have had 2 put on my desk and they said hey we need this to be updated with working code to do... or hey start from nothing and here's what we want sort of... the rest of the details will come later. It was fun and worked out (so far, crosses fingers). I would not like a call at 2am to go fix either but the coding was sound on my end. One was to run a gas station/pump station for different fuel types/which can be on and running at the same time and the other was those furnace units I kept trying to example for your application.

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!
 
itsmoked, you have got to be kidding. Automation direct has an excellent way of making state machines using their stage programming if you spend a few extra dollars.

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
 
Good point.

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 -
 
Automation Direct has a pretty good forum.
You should read through posts and ask questions BEFORE choosing PLCs.
There is a much better and more general PLC from at

Did you look up stage programming just so that you know in the future?

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
 
The click PLC will support state machine programming using the compare and copy instructions. Use a memory location to store the current state and compare this to a constant to determine the current state. Jump to another state by using copy to copy a new state value into the memory location holding the current state. See attached sample code
 
 http://files.engineering.com/getfile.aspx?folder=ee46bb36-e4ef-45fe-8a63-9d9c1c8d020d&file=State_Machine_Example.ckp
Status
Not open for further replies.

Part and Inventory Search

Sponsor