Jump to content

sequence programming question


John_R

Recommended Posts

Hey All,

Looking for some insight towards sequence programming, I know some people say that it is the prefered method, although in my years of dealing with PLCs (mostly other peoples work) I rarely see it.

I have used sequences from time to time when I want to insure that a bit of code executes in an exact way. I'll use an MI as a sequence "Index", and when the Index equals X then I execute that net.

My question is whether the prefered method of advancing the Index is by incrementing or storing a number to the Index.

The times that I have done it I have put an Increment at the end of a given net so that when it completes the Index increments up a number, and then the next net starts with an Equal To that number, and so on until the end of the sequence where I end the net with a Reset to the Index MI to bring it back to the start.

I have a machine where the OEM has a sequencing program (Allen Bradley, but the same process), and I've noticed that they use a Store (Move) at the end each net (rung) to change the Index number, and at the end of the sequence they Store (Move) the Index number back to the start.

The only advantage to the Store method I see is that you can change the Index number by a larger amount (10, 20, 30, etc), and if you later want to insert another step you just give it a "in between" number (15, 25) and you dont have to go back through all the nets and change the Equal To number at the start of each net.

Anybody have opinions on this, which method is considered "best practice"?

Regards
JohnR
 

Link to comment
Share on other sites

  • MVP 2023

Joe Tauser is the expert on this, but waaay back when I was programming computers with old fashioned languages that used line numbers, I would always number by tens for the precise reason you state - I would always later find the need to insert additional steps and re-numbering can be a pain. With PLCs, if the algorithm is relatively simple and properly planned out, there isn't as much need to insert steps, so I usually number by ones and use the increment function. If it were a very complex situation I would definitely number by tens and use the store direct or addition function to increase the step number by 10.

 

What I usually do is create a coil for each step number, such as: when MIx = 10, then coil MBx is ON, then precede each step with that coil as an activator (or gate). This allows multiple ladder rungs for each step in the state machine and a clear understanding of what is happening in the program. At the end of the series of steps, a condition must be satisfied to index the state machine.

Link to comment
Share on other sites

Yeah, Joe has been my mentor on many things over the past dozen years, he is my Unitronics (as well as other products) vendor.

He has seen snippets of my code work and always has advice to offer.

I just thought I'd throw this out to the community to see what kind of opinions I get.

Actually, I'm sitting at home for a while, just had a long over due hip replacement, so I'm off work for a couple months, I get tired of watching TV and I'm not much on playing solitare, so I find myself thinking about work related stuff, kind of sad isn't it?

But I'm able to remote into my PC at work and check things from here. A couple days ago one of the guys from work called me about problems with the afore mentioned A-B system, and looking at the sequencing method used there got me thing about different methods, which got me here seeking knowlege.....

JohnR

Link to comment
Share on other sites

  • MVP 2014

I was discussing this with a colleague just last week.  I number my steps in 10's and use store functions.  As above, it allows additional steps to be inserted.  It also allows steps to be executed out of order.  As much as I like to think I know the project from the start, I have never used an incrementing index for this, since it is such a rigid framework.  

 

With Visilogic I use a subroutine per step and use a compare block to call the subroutine when that step is valid.  With U90 Ladder I use jumps to execute the current step and jump over the others.

 

Hope this helps.

Link to comment
Share on other sites

  • MVP 2023

If I may pontificate - 

 

John's first post mentions that he rarely sees sequence programming in other programs.  That is so entirely true and a tragedy caused by a lack of proper training.  I can think of three customers who where taught to program by the PLC vendor's salesman, who never did a project himself and only had a basic knowledge of the PLC functions and how they go together.

 

If you look at the staff and operations of a typical machine builder, they spend a LOT of money on machining equipment and the mechanical designer, but often the electrician/panel builder is also the PLC programmer.

 

I have a standard saying about the machine builder's attitude - "Electricity is the evil thing needed to make my wonderful machine work".   I've arrived at this attitude by many different observations of actual machine makers.

 

The most important and almost always ignored Best Practice in programming is to plan and document your program before you start it.  A flow chart is my favorite tool, because you can document everything on it as you work through recreating it with PLC code.  I've attached one I did for a customer.

 

Further commentary would be most interesting!

 

Joe T.

Standard Machine Flowchart V1.3 11.16.14.pdf

  • Upvote 1
Link to comment
Share on other sites

Hey Joe,

I like the way you started out your post with a pompous 25 cent word....

One of my most recent projects was a retrofit to a machine that was originally built around Schneider products (PLC, HMI, VFD's), We've had this machine about a year, and it spent more time in the shop than on the production floor.

We have a high "moisture content" (food plant, gets washed top to bottom every night), I would bring this machine into the shop and dry it out, but every time it went back on the floor we would have issues with it.... and unfortunately I was not afforded the various pieces of Schnieder software needed to properly troubleshoot the issues (and the OEM lost interest in helping).

Anyway, the plant manager asked me what we could do to eliminate the problems, I convinced him to let me strip out the Schnieder stuff and replace it with A-B VFD's and Unitronics PLC (V570 up where the old HMI was and connected to an IO-D16A3-TO16 down in the control panel).

I did MODBUS control to the six A-B VFD's, and found that I needed to stagger the MODBUS calls else the commands would flounder, and some of the VFD's didn't get their command. I did a sequence routine, based on time, I put a two second pause between each sequence Increment for the startup or stopping of each VFD (which fit right into the OEM's suggested start/stop
 sequence).

I also lessened the MODBUS call burden by storing the previous command to an MI, and compare statements that if the command was not different from the last time, then skip that step.....

Lastly, each of the 12 MODBUS calls (6 for start/stop, 6 for speed) each went to it's own subroutine, which returns after the MODBUS FB "Function in Progress" bit clears, or if the FB returns a comms error so it doesn't hang over a lost comm......

so far, working well......

 

It would be interesting to hear how others use sequenced operations.......

JohnR

Link to comment
Share on other sites

  • MVP 2023

Further commentary would be most interesting!

 

Any of us who consider ourselves real programmers are always looking for the most efficient, economical, and clever solution to a programming problem. But in reality, it's probably more important to write the code in such a way that it is clear and understandable to another person trying to read it. I like using a State Machine, when applicable, because it's very clear what is happening and it's very clear where in the algorithm you are when online with the PLC. Another side benefit is that it forces you to do what Joe Tauser suggests - organize and document your logic.

 

I do other things, such as group similar or related short pieces of code using SB1, as follows:

 

  SB1

--| |-- <one line of code>

        |

         <another, related line of code>

        |

         <yet another, related line of code>

 

This minimizes the number of ladder rungs and helps readability.

 

Another important thing to do that surprisingly many people don't is to name the Operands in such a way that it is VERY obvious what that operand does. A few extra keystrokes today can dramatically reduce the time spent trying to understand what you were doing two years ago when you wrote the code and now have forgotten.

 

I learned early on NOT to use the "Links & Jumps" tab to link operands on pushbuttons to jump to different displays. You will inevitably forget to enter one in the provided field. What I do is have a Subroutine called "HMI PBs" where I enter every display jump condition (I use a single MB for each display I will be wanting to jump to) and then everytime I have a jump PB in any HMI display, it will already be in the code and there is nothing to remember to make it work.

 

I have an HMI screen where I display the vlp filename and the Visilogic version number so that I know what file and version was used when the PLC program was last downloaded. It also includes important information like the PLC Name and the Ethernet address, when applicable, so that it is handy.

 

I always use Input and Output buffering, this can be important when the hardware unexpectedly has to be changed (change I/O module, or the technician mis-wired something). It also makes it easy to troubleshoot using an extra bit that can be controlled while online, or on a special screen, without having to "Force" I/O.

 

I turn OFF access to INFO mode (set SI50 to zero) to the customer, unless properly logged in (I use multiple login levels on every program I write). This helps prevent problems. When access to INFO mode is needed I use SB36 instead of changing SI50. I also provide a button for a soft reset (using SB300) so the customer doesn't have to power cycle in the event of a system change or problem that necessitates a restart.

 

This is off the top of my head - I'll add more stuff as I think of it.

  • Upvote 1
Link to comment
Share on other sites

Hi All..

I stumbled across this conversation by accident. I started my programming life with Machine Code and Assembly Language in the Mid 70's on Mini Computers and early Microprocessors. We were all teaching each other back then. The two pieces of "advice" that were regularly bandied about were

 

The last part is the coding, plan it , write it down, Flow Chart it, then don't top down code, implement your plan.

 

Write your code like its meant to be understood by someone in 20 years time, 'cause you just might be the someone.

 

I don't code a lot these days, mostly play with Networks and GPON but the rules still apply!

 

Nice to see a proper flow chart Joe.

Link to comment
Share on other sites

  • 4 years later...

Simon, I'm a newbie to Unitronics, but not with other plc's, specifically Omron and Koyo.  My preferred method of programming is sequence control.  Omron: STEP\SNXT,  Koyo: SG\JMP.  Would you be willing to share a small program you have written in this style (using as you say, subroutines) in Visilogic, to help me learn how to do it with Unitronics?

Link to comment
Share on other sites

  • MVP 2023

The state machine method I always recommend has it's roots in the Koyo SG/JMP method, and it's more powerful as you can take direct control of the program pointer.   It can be used in any brand of PLC, and I use it even when I'm working with a Japanese PLC because the numeric pointer value is easier to relate to than a bit in a sequence memory array.

I've attached an example of this programming style.  If you search the forum for "state machine" you'll find other posts discussing it.

Joe T.

Statemachineexample.vlp

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...
  • MVP 2014

Sorry, I missed all this.  Thanks for the tag @Ausman

Hi @EladLending The example posted by @Joe Tauser  follows the approach I would use.  A variation would be to put all the code for each machine state inside a subroutine, to separate the application code from the state machine logic.  However that's just an efficientcy and style question, the underlying structure and flow doesn't change.

There is no access to the system "program pointer" or anything like that.  In Joe's example the "pointer" is MI0.  It is explicitly set up and controlled by the ladder application.

Hope this helps,

Link to comment
Share on other sites

  • 3 years later...
On 7/13/2020 at 8:52 AM, Joe Tauser said:

The state machine method I always recommend has it's roots in the Koyo SG/JMP method, and it's more powerful as you can take direct control of the program pointer.   It can be used in any brand of PLC, and I use it even when I'm working with a Japanese PLC because the numeric pointer value is easier to relate to than a bit in a sequence memory array.

I've attached an example of this programming style.  If you search the forum for "state machine" you'll find other posts discussing it.

Joe T.

Statemachineexample.vlp 88.32 kB · 69 downloads

Just curious as to how you would handle setting multiple steps using this technique?  See attached pdf.

2023-12-24_17-18-46.pdf

Link to comment
Share on other sites

  • MVP 2023

I would use more than one pointer, with state 2 in one path and 3 and 4 in another.  You can kill the path by setting the pointer to zero when it's no longer needed.

I always have a separate subroutine called "Outputs" where all my coils go and they are turned on and off by the values in the pointers.  There's no rule that you can't have more than one compare block controlling a coil.

What are your exit transitions for states 3, 4, 5, and 6?

Joe T.

 

Link to comment
Share on other sites

@brooksg44 You can simply click and type, and you can reply to a specific person by typing @ followed by the person's name; a selection box will open up allowing you to click the name of the person you wish to rely to.
Another way is to click 'Quote' at the bottom of the post you wish to reply to. A new post will open including the quoted text - you can edit the quoted text, for example highlight and delete what is not relevant.


image.png

image.png

Link to comment
Share on other sites

  • MVP 2023

I sent a PM to brooksg44 asking further details and learned this question is based on a whitepaper describing converting Petri net to of three of the Five 61131-3 languages. 

https://abcm.org.br/symposium-series/SSM_Vol5/Section_IV_Industrial_Informatics_Discrete_and_Hybrid_Systems/24333.pdf

For summary and illustration purposes, I took screenshots of relevant sections.

Here's the example process and description (page 6:

image.png.6e3818b232a2f95e997bdcce618aed7d.png

The I/O consists of three lamps, a button, three valves, three level switches, a motor, and a heater.

Nothing is really mentioned about the heater and temperature control loop.  This is an academic exercise,  so I'm going to assume that the heater is a frictionless device with the power of the sun that instantly brings the process to whatever setpoint temperature is specified. 😁

 The file brooksg44 posted is a Grafcet diagram, which is a fine way to represent to the process.  I brought his diagram into Visio and added some annotations to help with the State Machine program:

image.png.8661c2183e74b7ecb76b5777c28b95d6.png

 

On 12/25/2023 at 8:45 PM, Joe Tauser said:

I would use more than one pointer,

I only needed one pointer.

By putting the Outputs in their own subroutine, they are easily referenced from the state sequence logic.  I did this example program in Visilogic 9.8.65, because that's still my favorite version and anyone should be able to open it.  I also used a 7" Samba because those seem to be very popular these days.

I didn't spend much time on the graphics.  If someone wants to make it pretty and upload a fancy version knock yourself out.

Joe T.

 

 

P. S.  - Take a look at the LD program in that paper.  Yow!  

 

Tank Fill State Machine JT.vlp

Link to comment
Share on other sites

@Joe Tauser Creative and Intuitive Solution.  One could argue that you modified the Grafcet. But what I was looking forward to seeing is how you implement multiple pointers for handling the And Branches.  Here is another Grafcet with 3 independent branches.  Also a Codesys snippet of my solution.  I was curious as to what your solution would be.

IntroToSFC.pdf CodesysSnippet.pdf

Link to comment
Share on other sites

  • MVP 2023
11 hours ago, brooksg44 said:

One could argue that you modified the Grafcet

I did draw squares and a circle on it.  What would your argument be?

For your second question, your solution would match mine.  Using the same technique with different pointers, you can step through each branch independently.  You set your "step" tag to 99 at I5, which is a holding state for the main loop.  Now all you have to do while your main pointer = 99 is have an AND statement looking for each sub-loop pointer to reach its final value.

Joe T.

 

Link to comment
Share on other sites

@Joe Tauser See attached Grafcets.  Although both of these Grafcets drive the outputs the same. The logic to get there is totally different.  That's why I said your solution was intuitive and creative.  The question was designed to see how you do EQU/MOVEs when a sequence have AND branches or does multiple things at the same time.  I was hoping you had better solutions than what I have come up with for the branches.  I personally think this is where this methodology becomes ugly and less intuitive and why I prefer Sets/Resets. Every programmer does it different. I don't have a ready made solution for MixerTank but have attached one for IntroToSFC, Net6 starts the and branch and Net 13 concludes the and branch.  I base these methodology from Asian PLCs that have STL (Step Ladder) capability.  Same as Domore Stage programming if you are familiar with that line. 

IntroToSFC_Original.pdf IntroToSFC_Modified.pdf Codesys IntroToSFC STL.pdf

Link to comment
Share on other sites

  • MVP 2023

Point taken on me modifying the Grafcet.  Your drawing illustrates it perfectly.  But I modified it to suit my needs as a programmer. 

My State Machine style grew from my frustration with PLC Direct 400 series Stage Programming (now I'm really dating myself).  Each stage was controlled by an S-bit  and if you wanted to see where you were in the program you had to pull up a watch window and then remember what stage did what.  The bits were in sequential order and if you need to insert another operation between stages then you needed to either re-number all your sequence bits or use a fresh bit that was out of order with the others.  The graphical representation of the software at the time didn't like the bits being out of order.  Plus, there was a limited number of them.

My work these days consists primarily of Unitronics (I am a distributor / system integrator) but also a fair amount of older Allen Bradley.  Neither of these has stage programming or is even close to IEC 61131-3 compliant, so the other languages aren't really on the table for me personally.

Both brands have ladder.  Both brands have Equal and Store (MOV) blocks.  Your CODESYS solution with Sets and Resets is only slightly removed from my pointer method, and it uses functions common to all PLCs.  Which is good.  The main reason I don't program that way is because when you set a bit you have to remember to reset it.  A numerical pointer takes care of itself and you can look anywhere in your sequence and see what the value is.

2 hours ago, brooksg44 said:

I personally think this is where this methodology becomes ugly and less intuitive

It most certainly can.  If I've got separate State Machines running in parallel I clean up the code by putting them in their own subroutine that is always called.  Each one has its own Idle state, and the Main loop starts them by pushing a value into their pointers, as you did in your CodesysSnippet document.

I'm sorry I don't have the magic bullet you're looking for.  I can tell you're an accomplished programmer and this is a most interesting discussion.

Joe T.

  • Like 1
Link to comment
Share on other sites

@Joe Tauser  It has been  quite interesting.  I sent you a connect request on Linkedin.  So is Unitronics PLCs that popular in St. Louis enough for you to make a living at it?

I don't really like the fact that Unitronics doesn't have a built in simulator.  But I might be interested enough to purchase one to evaluate for my curriculum.  I would be looking for something low end with Ladder Logic and HMI for maybe incorporating it into my curriculum.  I teach Electrical Automation at Idaho State University. This is my retirement job.  Send me some specs and a quote to my work email broogreg@isu.edu

Regards,

Gregory Brooks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.