Sign in to follow this  
Followers 0
baldyspate

Coding Efficiency?

22 posts in this topic

Hi: I'm a newbie to writing. Self educated or semi ignorant... I've read or heard that (AB PLCs at least) will only evaluate a rung to the first false statement, update the rungs output, and then go to the next rung. Is this true? If so, it could make a lot of difference and be good practice to put the most often false arguments at the beginning of a rung. Also true??? thanks.

Share this post


Link to post
Share on other sites
I believe each rung is executed as normal upto the first false statement, after this all instructions are still read (i.e. it does not jump to the end of the rung) but not acted upon. Where branches are concerend, if there is no output on the branch then the branch terminates when a true condition is found. Also, branches take up more time than new rungs.

Share this post


Link to post
Share on other sites
On a related note.. I worked with a guy that I regarded as a very good programmer. He didn't like to use multiple program files because he said they slow the scan down way too far, and aren't an efficient coding technique. So he'd have one big ladder file, sometimes with 1500-1600 rungs(!) The way he made sense of it all was to separate the functional sections by an unconditional rung with an OTE to an unused bit address. Each section's identifying rung was a multiple of 20.. B3/20, B3/40, etc. That way you could search for the bit and it would quickly take you to where you wanted to go. I still prefer a more structured approach, but he pulled off jobs that you wouldn't believe a 5/03 could handle..

Share this post


Link to post
Share on other sites
One thing to be wary about is that if you over stress a PLC and optimise too much then a time may come when no logic is skipped and the watchdog timer may be exceeded. I'm not sure a JSR uses very much time at all. I think there is very little difference between a program with 1500 rungs in it and presumably 50 or so 'unconditional OTE's' and just putting 50 JSR's instead. It certainly makes the program easier to read!

Share this post


Link to post
Share on other sites
The quick look I made at older manuals showd the JSR on the order of 100 times longer than an OTE. Might re-check you assumptions.

Share this post


Link to post
Share on other sites
Timings in micro seconds        5/01  5/02  5/03    5/04 JSR    46    28    131     112 SBR    2     4     0.25    0.18 RET    34    20    23      20 total  2     52    154.25  132.18 OTE    18    11    0.63    0.56 Diff.  x4.6  x4.7  x245    x236 It appears I should eat my own words! These are the best instruction timinigs I could find and it a JSR (and SBR, RET) instruction is 245 times slower than an OTE! (excluding new rung overheads) Given the example above of 50 JSR's in a file verses 50 OTE's that would be a saving of 7.7ms which I concede is a considerable amount of time. Edited by Spedley

Share this post


Link to post
Share on other sites
I'm surprised that I'm actually following this. If your still there... I'm making changes to a filler program that I inherited from another, apparent, amateur. I ( find myself trying to paraphrase, "the blind following the blind" but ain't a poet neither). I find timer resets that would seem to reset every scan for up to a couple of seconds, and retentive timers in places that would seem better suited to nonrentative timers and need resetting. I'd like to make this finicky process as "idiot proof" for the very busy operators as possible, adding lots of logic. It's only about 50 rungs long, with up to 9 arguments per (avg:~4), and with no JMPs, JSRs, or MCRs. It needs at points to be acurate to around .01 secs. I'd like for my own practice to make it efficient, but this is a business and I am wondering if modifications (can't start from scratch...nervous boss) tripling or quadrupling the length would benefit from time saving mods. Micrologix 1000, no spare outputs, one input left....... thanks

Share this post


Link to post
Share on other sites
Use page titles in RSLogix. You can keep down the number of JSR's and page titles allows you to separate group of rungs in a files. Every time you need to go a section of rungs, press CTRL-D, select a file, select the desired page title, and you're there.

Share this post


Link to post
Share on other sites
Having a quick look, I get the impression that a MicroLogix 1000 has a 'typical scan time' of 0.008 seconds (8ms) / kilobyte of program. I don't know how long it spends refreshing the I/O etc but I think a 50 rung program is well below it's limits. I wouldn't worry too much about efficiency, get it done (you will leanr a lot doing that) and then worry about efficiency if you need to. Edited by Spedley

Share this post


Link to post
Share on other sites
Sometimes starting over from scratch is the quickest, simplest, most efficient way to handle a machine that isn't performing, with logic that defies logic. Unless your boss is the original coder and is likely to be offended (I had a situation like that before.. yech) You should try to convince him that this is the way to approach it. I assume YOU are the one that is going to support this machine from now on.. It is important that the program makes sense to YOU so that you can do that. Hell, if its only 50 rungs.. :)

Share this post


Link to post
Share on other sites
It may seem like a great idea to optimize PLC code for the best scan time. It's not. If you really have a scan time issue, then going through the code, and (cough) streamlining it to squeeze out every possible microsecond will only make it completely unmaintainable. If you really have a scan time issue, saving 7.7 ms by eliminating JSR's won't really do much except make it code unreadable by anyone except the programmer; and even the programmer won't understand it well in six months. (I guess it is good for job security though). Keep the Subroutines, but if need be, scan them conditionally. Run a 'phase counter', say 0 through 10, and scan the less critical subroutines every 10th scan. And never all in the same scan, do one when the counter is 0, next when it's 1, etc. This is particularly good for values that are scaled for HMI's, as they generally don't have to update much faster than once a second. DO trim fluff out of any interrupt routines. Upgrade the processor one notch. Don't use expensive instructions 'Just because You Can'. CMP and CPT in AB's are expensive, but useful. Factor down any constants in them. It may be easy to make a computer that is "Roll_Circum_Feet = Roll_radius_Inches * 2 * 3.1415 / 12", but it's cheaper to put the actual formula in the comment, and just do "Roll_Circum_Feet = Roll_Radius_Inches * 0.52359" -------------------------------- It is of course, only my opinion, but I much prefer code that is understandable, and maintainable rather that 'cool'. Note that this doens't only apply to PLC logic, but also to any programming language. I laugh at people that use every trick they can to 'optimize' C/C++, without even looking at a profiler to see what requires optimization.

Share this post


Link to post
Share on other sites
With AB PLCs it IS possible to trim scan time by moving mostly false logic to the beginning of a rung. I have done this to a 3000 rung PLC5-40 program and trimmed average scan time from 50ms down to 43ms. Not a huge change, but helpful. I have the habit of writing code in this way from the beginning. Don't use unconditional math instructions when you can "1-shot" them. True bit instructions are about 13 times faster than word instructions. Put mostly true logic (stuff like XIC "Auto Mode") as for to the right as possible. Do your scaling with the HMI and work with raw units in the PLC. Use lots of comments to explain what's going on...they don't affect scan time. (These things are imprtant to me, probably because I learned to program in assembly on an Atari400 with 16Kram and a super slow CPU.) Just my opinions.

Share this post


Link to post
Share on other sites
Heck you were spoiled.. I had one of the original 8K 400s. Later "upgraded" to 16K. Later "upgraded" to 64K. Later moved to a shelf in the basement where it currently sits :) Not much room for BASIC code with 8K of RAM! And hey the CPU wasn't "super slow".. It was 1.79 MHz when everyone else was running at 1 MHz!

Share this post


Link to post
Share on other sites
Mmmm... Atari Computers... I LOVED those things. I had a 400, then got an 800, then started buying them up at flea-markets for dedicated controller projects. Step one was always replace the 6502 with NEC's 65C02 (enhanced instruction set... it actually had a store-zero instruction saving a couple clocks woot! (STZ)) Step two was usually cut a slot out of the back of the solit aluminum housing to gain access to the edge connector with the full CPU bus on it. They were actually pretty advanced; The ANTIC (Alpha-numeric television interface chip) was an amazing little processor in its own right. To this day, it was about the only 'video driver' that could allow mid-scan-line resolution/'bit depth' mode changes, and you could actually control the display scan line by scan line without bothering the main CPU. /sigh those were the days.

Share this post


Link to post
Share on other sites
I was making my first steps on home made ZX81 with only 1k. The US version had whole 2k memory. It was so unfair...

Share this post


Link to post
Share on other sites
I guess I was spoiled with my Atari. I was fourteen at the time, so my interest was video games. I learned dozens of tricks for packing more detail into my creations. Character set swapping, multiple display modes, VBI routines, etc. I used to fantasize about having 256KRam and a disc drive. (I only had a cassette player.) Could you imagine modern PC hardware with the Atari architecture? They were very advanced for their time. I sold mine for $25 dollars at a garage sale 15 years ago. I wish now that I'd saved it for my kids to learn the nuts and bolts of computing the way I did. Paul C.

Share this post


Link to post
Share on other sites
And yet again...Thanks. I've picked up a lot here (measured against what I know). There're badly bruised egos lying all over the plant floor over the two lines that I'm attempting to work on. The good news is that MY boss is on my side. And he is coming around on the rewrite after a couple aof successful changes which the operators love. The filler line is far and away going to be the easiest. It produces as much product as can be shipped from here and the issues are economy and spills. I'm certain that I can reduce the number of spills and the amount of spills when they happen without too much trouble. The operators are good hands but are working with a system that has no provisions for less than perfect sequences of events. There are no lights, gauges, bells, or whistles for the operator to glance at before he hits the E-stop, so often he has no clues to give the maintenance people when they show up, while there's plenty of data fed to the plc which can be used to evaluate events and call on fault routines. I was a pretty good hand with my trusty C-64 and it's poking and peeking around the interpreter back when it was Rex but haven't done any programming between then and very recently, after a short course on ladder logic. After I sharpen my teeth (those left) on the filler I'll go after the real grizzly and will probably be.... right ...back... here..................

Share this post


Link to post
Share on other sites
That's always a good move. Please the operators and they sing your praises. If you've ever worked on a machine for several hours and there is something not quite right, it drives you mad! Often these things have very simple solution but they can put productivity and operator happiness way up.

Share this post


Link to post
Share on other sites
Yep. I'm new to PLC programming and somewhat to industrial maintenance, but have been doing basically this type of work for a long time. (haven't worked on the Shuttle yet but...) Happy users are more important than many, who should know, seem to. For a long time here it was, "...take what we give you..." That's changing but with some pain, for some. The mods that I've done so far are the result of spending some time with operators. In one case almost just translating and sticking what had been a fellas wishful thinking into a ladder. He'd been wanting it for that long. What's "driving me mad" at this point is probably stuff that you solve without hardly a thought, but I'm improving (I think), and t'were it easy t'would would also soon be boring.

Share this post


Link to post
Share on other sites
Oh don't remind me about the 410 cassette drive, I had almost put all the bad memories out of my mind. I had many BAD experiences with the 410 and very few good ones! Still got it.. belts slipped like crazy last time I tried tho.. maybe someday I'll try to rebuild it and see if any of those old tapes still work!

Share this post


Link to post
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
Sign in to follow this  
Followers 0