![]() ![]() ![]() ![]() ![]() ![]() |
||
![]() |
||
![]() ![]() ![]() ![]() This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc. |
||
![]() |
![]() |
#1 |
Member
![]() ![]() Join Date: Feb 2011
Location: Bolton
Posts: 44
|
Structured text FOR loop in Controllogix
I am trying to learn structured text on the controllogix platform and have managed to fault the processor by creating an infinite loop.
My problem is i cannot understand what is wrong with my code. Started with the following code: Code:
FOR y := 1 TO 10 DO Lamps := Lamps + 1; FOR x := 1 TO 1000 DO x := x + 1; END_FOR END_FOR IF Lamps > 150 THEN Lamps := 0; END_IF I then changed the code as follows: Code:
Lamps := 1; FOR y := 0 TO 15 DO Lamps := Lamps *2; FOR x := 1 TO 1000 DO x := X + 1; END_FOR; END_FOR; IF Lamps > 2048 THEN Lamps := 0; ELSE Lamps := Lamps; END_IF; I just wanted to create a running lamp going through each output in turn. can anybody point me at a good resource for Structured text, and critique my code? |
![]() |
![]() |
#2 |
Member
![]() ![]() Join Date: Nov 2016
Location: Fields of corn
Posts: 2,364
|
What's the processor fault? Some have watchdog timers and large loops will trigger them and not necessarily because of an infinite loop.
You are also missing the "BY increment" in your code. And there's a typo on X with an upper case x. |
![]() |
![]() |
#3 |
Member
![]() ![]() Join Date: Feb 2011
Location: Bolton
Posts: 44
|
The processor fault is a watchdog timer and infinite loop is mentioned in the text. Typo is from when I have typed it into the code box (I am copying it from a picture I took when I was at work) don't have access to the plc at the moment. I didn't use the BY in either loop and the first one executed ok, my understanding was the default step is 1, but I can try adding the BY when I next have access to the plc.
|
![]() |
![]() |
#4 |
Lifetime Supporting Member
![]() ![]() Join Date: Nov 2006
Location: UK
Posts: 6,503
|
Have you posted all of the code? Your FOR loop using x doesn't do anything unless you are intending that to be a time delay - in which case you need a radical re-think of your implementation.
__________________
S7-300 to 1500 conversions done - email to s7conversions@hotmail.com |
![]() |
![]() |
#5 |
Member
![]() ![]() Join Date: Feb 2011
Location: Bolton
Posts: 44
|
Yes this is all of the code, and the x loop is a delay. Without it everything happens to quickly. My intention was to get it working in a simple way, that I can understand and then add an actual timer in to create the delay. I agree that it is not the best way, but when I looked at timer implementation in ST there were lots of options that I needed to find out how to use.
|
![]() |
![]() |
#6 |
Member
![]() ![]() Join Date: Sep 2015
Location: Alberta
Posts: 136
|
My suggestion would be to use a Timer.
When the timer is done, Reset timer and add 1 to Lamp Tag. No For Loop Needed. MyTimer(IN:=TRUE,PT:=X); IF MyTimer.Q THEN Lamps:=Lamps+1; MyTimer(IN:=FALSE); //reset timer END IF IF Lamps>TAG THEN Lamps:=0; //reset Lamps End IF |
![]() |
![]() |
#7 |
Lifetime Supporting Member
|
I put your exact code in my 1769-L30ER, it will not fault the processor. The only thing in the program is that routine, called unconditionally. The max scan time is ~7ms. The default watch dog timer is 500ms. For it to fault, the scan has to exceed the watch dog timer. There has to be more to the story.
__________________
Open source python communications library for CompactLogix/ControlLogix/Micro800 PLC's: https://github.com/dmroeder/pylogix |
![]() |
![]() |
#8 |
Lifetime Supporting Member
|
Possibility 1) x increments by 2 for every pass of the inner loop (for x = 1 to 1000 do), so after 499 passes, x will be 999, and after 500 passes, x will be 1001. So the loop logic never compares a value of 1000 in x against the loop termination criterion of a literal constant 1000. Is it possible that the loop implementation on OP's PLC checks for x equal to 1000, instead of greater than or equal to 1000, and so that inner loop never terminates?
Possibility 2) OP is using an emulator, which runs slower than @dmroeder's 1769-L30ER, and the 5000 passes through the inner loop per scan cycle takes long enough to cause a watchdog timeout in the emulator. Possibility 3).?
__________________
_ Brian T. Carcich i) Take care of the bits, and the bytes will take care of themselves. ii) There is no software problem that cannot be solved with another layer of indirection. iii) Measurement is hard. iv) I solemnly swear that I am up to no good ![]() v) I probably have the highest ratio of forum posts to actual applications in the field (∞). vi) Hakuna matata. |
![]() |
![]() |
#9 |
Lifetime Supporting Member
|
Good catch, I missed that detail.
__________________
Open source python communications library for CompactLogix/ControlLogix/Micro800 PLC's: https://github.com/dmroeder/pylogix |
![]() |
![]() |
#10 |
Lifetime Supporting Member + Moderator
|
One point nobody has mentioned yet is that in the first version there are ten iterations of the outer loop while there are sixteen iterations in the second version. That fact alone means that the second version of the code takes 60 percent longer to execute than the first. Quite likely the source of the watchdog fault.
For debugging, start with, FOR y := 0 TO 9 DO. That makes your second version more nearly the same as the first. Assuming that runs OK, increase the number of iterations until you find out how many it takes to fault the processor. |
![]() |
![]() |
#11 |
Lifetime Supporting Member
|
Whoops.
My "Possibility 1" (loop index does not hit 1000+1 exactly, causing loop to become infinite loop) is unlikely, because otherwise something like Code:
FOR x = 1 TO 10 BY 3 ... END_FOR
__________________
_ Brian T. Carcich i) Take care of the bits, and the bytes will take care of themselves. ii) There is no software problem that cannot be solved with another layer of indirection. iii) Measurement is hard. iv) I solemnly swear that I am up to no good ![]() v) I probably have the highest ratio of forum posts to actual applications in the field (∞). vi) Hakuna matata. |
![]() |
![]() |
#12 |
Lifetime Supporting Member
|
Sidebar:
OP is to be very highly commended for what they are doing i.e. they are learning about structured text by poking around writing random tasks in structured text that do not relate to an actual application, and seeing how it behaves. There seem to me to be a fair number of posts where an OP does not see the code do what they expected it to do, they simply come here and ask a question, instead of investigating the PLC behavior by adding diagnostic code unrelated to the application, or trying to measure performance or timing, or other techniques,
__________________
_ Brian T. Carcich i) Take care of the bits, and the bytes will take care of themselves. ii) There is no software problem that cannot be solved with another layer of indirection. iii) Measurement is hard. iv) I solemnly swear that I am up to no good ![]() v) I probably have the highest ratio of forum posts to actual applications in the field (∞). vi) Hakuna matata. |
![]() |
![]() |
#13 |
Lifetime Supporting Member
|
P.S. @cardoscea's mention of the x/X typo finally clicked; see below. Also, I originally misread the OP and thought both of the examples were faulting with a watchdog timeout. Finally, to @dmroeder, the idea that OP is using an emulator is a guess/possibility, not something gleaned from OP' posts; I just re-read the thread, and actually they mentioned a Logix trainer suitcase, so now I think that is an actual PLC, not an emulator.
Following @cardoscea's sharp observation: in the second of OP's code example, the FOR x ... inner loop is indeed an infinite loop IFF X (uppercase tag) is a tag with a value less than 999. To wit
P.P.S. modifying a loop index is not exactly a best practice; even the x = x + 1; (all lowercase) injects a bit of obfuscation into the code. P.P.P.S. X (uppercase) is almost certainly declared somewhere, otherwise the compiler would fail to compile the code.
__________________
_ Brian T. Carcich i) Take care of the bits, and the bytes will take care of themselves. ii) There is no software problem that cannot be solved with another layer of indirection. iii) Measurement is hard. iv) I solemnly swear that I am up to no good ![]() v) I probably have the highest ratio of forum posts to actual applications in the field (∞). vi) Hakuna matata. Last edited by drbitboy; March 19th, 2023 at 09:02 AM. |
![]() |
![]() |
#14 | |
Member
|
Quote:
"Structured text is a textual programming language that uses statements to define what to execute. • Structured text is not case sensitive. • Use tabs and carriage returns (separate lines) to make your structured text easier to read. They have no effect on the execution of the structured text..." I have to assume the "not case sensitive" applies to tag names, as it does with various ladder elements (e.g., CPT instruction). While there are errors in RA documentation, this would be significant in my opinion. Would be interesting to know if changing the ST 'X' to 'x' fixes the problem. Last edited by Mispeld; March 19th, 2023 at 12:54 PM. Reason: minor wording |
|
![]() |
![]() |
#15 | |
Member
![]() ![]() Join Date: Feb 2011
Location: Bolton
Posts: 44
|
Quote:
Thanks for all your comments guys. The 'X' / 'x' typo was highlighted in the first reply and i tried explain that it was a typo only in the code I had typed into the code box of the web site, in the actual PLC all the 'x' tags are lower case. Pretty sure that I tried different values for the loops and they all still faulted only on the second code sample, but I will check this in the morning. I will get the Processor type as well. There is more code running on the PLC but all other routines are disabled when running the structured text routine, with the exception of the routine that updates the I/O. One thing i did notice was that when running the code the watch window shows the loop values x and y counting up as i would expect but the outputs were not updating (again this is only with the second code sample, the first works perfectly). I will also try putting the loops as the only routine and having no other code on the PLC. I do appreciate everyone's comments, I thought somebody would immediately spot what I was doing wrong, and stop me making the same mistake again. I will also try using a timer instead of the inner loop. Interesting that 'dmroeder' says the watchdog timer is 500mS, as when the program is running it takes quite a while to fault (something else i will get details of in the morning), but it is definitely more than 500mS. |
|
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
ControlLogix structured text translation | AutomationEngineer | LIVE PLC Questions And Answers | 8 | May 11th, 2021 02:15 PM |
TwinCAT Structured Text Programing (LOOP) | Sohaib28 | LIVE PLC Questions And Answers | 5 | November 19th, 2015 01:36 PM |
ControlLogix training – would you like an order of Structured Text to go with that? | Ron Beaufort | LIVE PLC Questions And Answers | 11 | June 9th, 2015 10:15 AM |
ControlLogix 5000 Structured Text | rigsha97 | LIVE PLC Questions And Answers | 4 | January 27th, 2015 04:05 PM |
Documentation on Structured Text | cmulder | LIVE PLC Questions And Answers | 13 | June 11th, 2003 11:10 AM |