Beginner's question: Why does a variable stays true in Codesys V3

Gragtor

Member
Join Date
Feb 2021
Location
---
Posts
2
Hi all,


I'm just starting out in the plc world. For school we had some introduction into codesys v3.5, basically we get given visualizations and have to provide the code either in ladder or in function block. I decided to also try to code the assignments in structured text as an exercise.

Thanks for bearing with me. On to the question:
In the attached screenshot you can see both the ladder and the structured text program. The ladder works fine. I can switch pumps on and off, and when the thermal overload kicks in (via button) the corresponding visual alerts turn on and p1 switches to p2.
I've got the same thing working in ST, but for one thing. the second visual alert (H4) stays true, and I cannot work out why.
Does anyone have an idea?

Capture.JPG
 
Short answer: the two programs behave differently because the two programs are not equivalent.


The first thing to learn about programming is this: the only thing worse than a program not doing what you want it to do, is when it does exactly what you told it to do. Anyone who keeps that in mind has a chance to be successful at programming. Anyone who does not fully embrace that mindset will fail.



TL;DR


Change all of the [ELSIF ...] clauses to [IF ... END_IF];


In the ladder program, every rung, along with its IF-expression-equivalent logic, is performed on every scan.


With the [IF ... ELSIF ... ELSIF ...] construct, only statements in the first TRUE IF or ELSIF expression's clause are executed; in fact, all following ELSIFs' expressions are not even evaluated (or should not be) once one IF's or ELSIF's expression is TRUE. Changing all to IFs makes the ST more similar to the ladder.


Just to be clear on jargon :


Code:
IF expression1 THEN
  clause1statements;   (* this will only be executed if expression1 is TRUE)
ELSIF expression2 THEN  (* this will not even be evaluated if expression1 is TRUE *)
  clause2statements;    (* this will only be executed if expression1 is FALSE AND expression2 is TRUE)
ELSIF expression3 THEN  (* this will not even be evaluated if expression1 is TRUE OR expression2 is TRUE *)
  clause3statements;    (* this will only be executed if expression1 is FALSE AND expression2 is FALSE AND expression3 is TRUE *)
...                     (* [I]et cetera[/I] *)
END_IF;
I have not evaluated the code in detail, but if you make the change from [ELSIF ...] to [IF ... END_IF] and the ST then works, then that was the problem, and doing so takes less time than trying to figure out exactly which ELSIF blocked the later one.


Update: I looked again and now understand which earlier ELSIF is blocking the final ELSIF expression from even being ever evaluated, so it never even gets close to Resetting .H4. However, I leave it to you to find it for yourself, because when you do that for yourself then will either be forced to forever take the mindset recommended above, or give up programming as a career.
 
Last edited:
Code each rung of ladder with ST, e.g. for rung 1


Code:
gv1.P1:=(gv1.S1 OR gv1.P1) AND NOT gv1.Stop AND NOT gv1.S2;
gv1.H1:=gv1.P1;
 
-

Thanks so much, the both of you. You made this into a productive sunday.

Somewhere I had gotten the idea that you cannot simply declare in ST, but that you had to invoke using loops or conditionals. Works perfectly now that I followed the rungs on the ladder.

Also the realization that an elsif is part of the if statement and that only one of these can be executed each scan. Makes perfect sense. Hindsight is 20/20:ROFLMAO:
 
Somewhere I had gotten the idea that you cannot simply declare in ST, but that you had to invoke using loops or conditionals. Works perfectly now that I followed the rungs on the ladder.


Thanks for the feedback.



@LD's solution is the right one, of course, and much cleaner. That said, it's good you now understand where it went wrong; the making mistakes is important i.e. so we don't repeat them (hopefully ;-)).



IFs/ELSIF/ELSE clauses in ST for discrete logic implement the behavior of Set/Reset (from ladder).
 
IFs/ELSIF/ELSE clauses in ST for discrete logic implement the behavior of Set/Reset (from ladder).
I would argue that IF (without ELSE) implements SET/RESET while IF/ELSE implements the AB XOR instruction.

X Y
-] [------( )-



is the equivalent of

IF X
Y := 1
ELSE
Y := 0
ENDIF


X Y
-] [------(Set)-



is the equivalent of

IF X
Y := 1
ENDIF
 
Due to different platforms it is possible it may work in different ways,
Here is some ST for if/else & using the ST SET/RESET functions, you can see they all do what is expected but the code is actually different.
IF My_X THEN
My_Y:=TRUE;
ELSE
My_Y:= FALSE;
END_IF;

IF My_X THEN
My_Y:= TRUE;
END_IF;

SET(My_X,My_Y);
RST(NOT My_X, My_Y);
Note: that using the ST Function SET & RESET is the simplest code wise (Step 17 to 20)

SET_Reset.png
 
I would argue that IF (without ELSE) implements SET/RESET while IF/ELSE implements the AB XOR instruction.

X Y
-] [------( )-



is the equivalent of

IF X
Y := 1
ELSE
Y := 0
ENDIF


X Y
-] [------(Set)-



is the equivalent of

IF X
Y := 1
ENDIF

Actually it's the equivalent of y := x;
 

Similar Topics

Hi all, Writng a FB in ST on Beckhoff TC for a pulser which turns on and off on a cycle, is paused by turning bControlInput to FALSE, but resumes...
Replies
4
Views
99
So I work for a company that doesn’t allow code change without going through a lot of people. Anyway my question is I have a sensor and when it...
Replies
12
Views
1,759
How do you code it to when you push a button attached to X001, it turns on Y001. Then, the next time you push the button attached to X001 it...
Replies
4
Views
1,601
Hi all, this is my first thread on here, completely new to plc. Have been given a 1769 L24ER QB1B to play with. I have a PA2 power supply and a...
Replies
3
Views
1,765
Hello, I'm using a Micrologix 1100 (1763-L16BBB) and have a question on what current I would be getting out of one of the transistor outputs...
Replies
4
Views
2,201
Back
Top Bottom