Critique My Ladder to ST Conversion

dYoast

Lifetime Supporting Member
Join Date
Mar 2008
Location
Kansas City
Posts
252
I finally have had a chance to work with ST. For what its worth, I am using a GE-IP PAC Rx3i.

Please look at the attached pdf of ladder, then critique this code. There may be better/simpler/easier/cleaner ways.

Thanks in Advance!

Code:
[FONT=Courier New][FONT=Courier New]'Logic for Leaked Three Count[/FONT]
[FONT=Courier New]    IF Leak AND NOT Block THEN[/FONT]
[FONT=Courier New]           ctrLeak := ctrLeak + 1;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    IF (Reset AND Leaked) OR (NOT Leaked AND ((Leak AND Block) OR NOT Leak)) THEN[/FONT]
[FONT=Courier New]           ctrLeak := 0;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    Leaked := (ctrLeak >= 3);[/FONT]
 
[FONT=Courier New]'Logic for Blocked Three Count[/FONT]
[FONT=Courier New]    IF NOT Leak AND Block THEN[/FONT]
[FONT=Courier New]           ctrBlock := ctrBlock + 1;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    IF (Reset AND Blocked) OR (NOT Blocked AND ((Leak AND Block) OR NOT Block)) THEN[/FONT]
[FONT=Courier New]           ctrBlock := 0;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    Blocked := (ctrBlock >=3);[/FONT]
 
[FONT=Courier New]'Logic for Both Three Count[/FONT]
[FONT=Courier New]    IF NOT Leak AND NOT Block THEN[/FONT]
[FONT=Courier New]           ctrBoth := ctrBoth + 1;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    IF (Reset AND Both) OR (NOT Both AND ((Leak AND Block) OR (Leak AND NOT Block) OR (NOT Leak AND Block))) THEN[/FONT]
[FONT=Courier New]           ctrBoth := 0;[/FONT]
[FONT=Courier New]    END_IF;[/FONT]
 
[FONT=Courier New]    Both := (ctrBoth >=3);[/FONT]
[/FONT]
 
Do the counters in the ladder increment their count based on levels rather than edges ?

I would use another variable for the detection logic which you can then use inverted in the reset logic. The ladder could be simplifed in the same way.

Code:
'Logic for Leaked Three Count
 
    LeakDetect:=Leak AND NOT Block

    IF LeakDetect THEN
           ctrLeak := ctrLeak + 1;
    END_IF;
 
    IF (Reset AND Leaked) OR (NOT Leaked AND NOT LeakDetect) THEN
           ctrLeak := 0;
    END_IF;
 
    Leaked := (ctrLeak >= 3);
 
'Logic for Blocked Three Count

    BlockDetect:= NOT Leak AND Block

    IF BlockDetect THEN
           ctrBlock := ctrBlock + 1;
    END_IF;
 
    IF (Reset AND Blocked) OR (NOT Blocked AND NOT BlockDetect) THEN
           ctrBlock := 0;
    END_IF;
 
    Blocked := (ctrBlock >=3);
 
'Logic for Both Three Count

    BothDetect:= NOT Leak and NOT Block

    IF BothDetect THEN
           ctrBoth := ctrBoth + 1;
    END_IF;
 
    IF (Reset AND Both) OR (NOT Both AND NOT BothDetect) THEN
           ctrBoth := 0;
    END_IF;
 
    Both := (ctrBoth >=3);
 
Last edited:
The counters in ladder increment on edges.

My intention for either the ladder or ST is to use it as a function block. It will be called with a positive transition.

I like the simplification!
 
One reason why there are multiple programming languages is that some tasks are better done with one language and some are better doen with another. When I see ST code with a lot of IF/THEN constructs and a lot of Boolean operators, my initial reaction is that ladder logic would be a better language choice.

Tasks where you need to do a lot of calculations or a lot of indexed instructions will probably turn out to be more efficiently done in ST than ladder.

And, as Sergei points out, when you move from ladder to ST, you've given up ladder's ease of online troubleshooting.
 
One reason why there are multiple programming languages is that some tasks are better done with one language and some are better doen with another. When I see ST code with a lot of IF/THEN constructs and a lot of Boolean operators, my initial reaction is that ladder logic would be a better language choice.
I think dYoast did well using ST as it should be used.
I like this
Leaked := (ctrLeak >= 3);
I encourage this kind of programming for those that are ladder programmers.

L D[AR2,P#0.0] cleaned up the IF THEN code a lot so it is relatively simple.

Tasks where you need to do a lot of calculations or a lot of indexed instructions will probably turn out to be more efficiently done in ST than ladder.
If you are programming a S7-3xx you don't have much choice. In motion control only ST in a state machine frame work like SFC makes sense. I see a lot of FBD examples but they don't do a lot of calculations.

And, as Sergei points out, when you move from ladder to ST, you've given up ladder's ease of online troubleshooting.

It isn't the language that is at fault. It is the kind of programming you do with it. Procedural languages like ST are best debugged off-line with a debugger that can single step through the code. Only when the code is ready should it be integrated into the running code. I have found that debugging ladder code in a loop is difficult. It makes no difference that all the values are updated on the screen. The screen cannot update fast enough to make debugging loops in ladder useful.

Now for a little boasting. We log ALL the calculations and where they happened so one can go back through the log to see what happened. The log can be quite long but we have filters so one can log only what they are interested in. We could see that the traditional ladder method of debugging would not work because the variables would change too fast to see on the screen. My/our experience is with In Circuit Emulators where a real time trace logs the last 2000 or so machine code instructions inspired this.
 
One reason why there are multiple programming languages is that some tasks are better done with one language and some are better doen with another. When I see ST code with a lot of IF/THEN constructs and a lot of Boolean operators, my initial reaction is that ladder logic would be a better language choice.

Tasks where you need to do a lot of calculations or a lot of indexed instructions will probably turn out to be more efficiently done in ST than ladder.

And, as Sergei points out, when you move from ladder to ST, you've given up ladder's ease of online troubleshooting.

I haven't fully decided on using the ladder or the ST for my project. If a person really wanted to, they could have both in the project and enable/disable as needed to be able to troubleshoot. Either way I can used the suggestion of L D[AR2,P#0.0] to simplify.

I like this
Leaked := (ctrLeak >= 3);
I encourage this kind of programming for those that are ladder programmers.

Unfortunately, I can't take credit for this. In the GE-IP PAC Systems CPU Reference Manual, there was a similar example. It does make perfect sense.


Thank you all for the comments and suggestions.
 
There is very little that is new.

Unfortunately, I can't take credit for this. In the GE-IP PAC Systems CPU Reference Manual, there was a similar example. It does make perfect sense.
I didn't want to imply this is original, it is just a good way.
Back in the dark ages we could
Leaked = (ctrLeak >= 3);
in C.

There is an example of doing something similar in the C programming language page 41 by Kernighan and Ritchie in 1978. I bet there are even older examples in earlier languages that have come and gone.
 
Yeah, I've done that numerous times. What's more fun is doing something like this in java:
Leaked = ctrLeak >= 3 ? 1 : 0;
To praaphrase: Leaked is equal to (if ctrLeak is greater than or equal to 3) 1 else 0.

I highly suggest anyone who does ST programming to learn some actual computer programming. Learning to program a computer gives you a chance to learn how to write code like this better, without the need to deal with hardware (well, not in the same way, anyway). Sure, the syntax may be a bit different, but the procedural coding style and thinking is applicable to both computer programming and ST. Things like learning how to optimize an algorythm or learning other ways of dealing with data and such (filtering, sorting, ways of shuffling data around etc.) can be done on a computer even when a PLC isn't available, making it easy to learn these things in an environment where even if something doesn't work, it's not going to cost you (or another company) money, taking the risk out of trying something new.

Sure, not everything in computer programming is applicable in ST, but a lot of it is.
 
Last edited:

Similar Topics

Hi. I have taken over a Building Management System that hasn’t performed well since installed. It’s a ControlLogix main with wireless messaging to...
Replies
21
Views
6,014
Hey everyone! I work for a small family business and am working on adding automation capabilities to our surface mining operations, for a little...
Replies
41
Views
11,588
(*convert rpm into inches per sec //1.5708 * rpm /1800 * 12"*) BridgeWriteSpdInSec:= 1.5708 * INT_TO_REAL(Bridge_write_speed_rpm) /1800.0 * 12.0...
Replies
3
Views
1,969
(* convert the inputs to floats *) input_min:= raw_min_real; input_max:= raw_max_real; scaled_min:= eu_min_real; scaled_max:= eu_max_real...
Replies
2
Views
2,125
For practice, I designed a system using autocad electrical with the following specs, and I would like some critiques on my design. I'd like to...
Replies
22
Views
7,994
Back
Top Bottom