DonNH

FOR - NEXT loop basics - how to call?

12 posts in this topic

Again, I'm missing something basic here . . . .

So the Sysmac reference materials I've looked at give some details about the FOR function block, but I haven't seen anything telling me how I would actually call it up.

The FOR and NEXT functions have to be attached to the left side of the ladder, so I can't insert an input to initiate the loop. 

Can anyone give me the basics of how I would place a loop like this in a ladder program, or direct me to more info? As is, it will run as soon as the PLC (or Simulation) is fired up.

 

 

 

For_loop.png

Share this post


Link to post
Share on other sites

Posted (edited)

You are correct that you can't create an input condition to the "FOR" loop using the "FOR" instruction, so it runs every time the task is scanned. However you can easily just use inline ST (structured text) and have input conditions before the inline ST block.

Edited by chelton
typo

Share this post


Link to post
Share on other sites

Seems like that severely limits the usefulness of the function.

I want to use it to check to see if an ARCL function block actually succeeded (I'm getting occasional short network drops), and reissue the command if necessary..

I believe these function blocks can also be called through ST, but the documentation doesn't show the syntax.

I've worked out the ladder code to run through a couple passes just by repeating the command, but that would get pretty clunky if I want it to try several times.

Share this post


Link to post
Share on other sites

Do you really need to loop within the same scan cycle? If it's comm's responses your wanting to see if it succeed or not i don't think a "for" loop is the correct solution.

Share this post


Link to post
Share on other sites

So I guess maybe I'm looking at this wrong - will a For , Next   ladder loop always happen within a single scan cycle?

I actually want to wait about a second after the function block is issued, then check its Status variable and act accordingly.

I just want the check and command reissue to happen within a loop rather than duplicating the code multiple times.

Is there a different looping structure to use within ladder?

 

Share this post


Link to post
Share on other sites

Posted (edited)

@DonNH

Use the Omron help for any functions, they can be very informative. Select the function and F1 to go to the help.

FOR loops do execute the entire loop within a single PLC scan so like @chelton said it is probably not the correct solution for what you are wanting to do.

Without really seeing how you are doing you commands, if you are wanting to check a condition every second then maybe you can just use the Get1sClk instruction.

 

65e5d425a2d08_Screenshot2024-03-04080057

 

Would look something like this. 

This assumes the Start_enabled bit is latched on the entire time after you execute your function. on a Failed state you would probably reset your function execute and then issue the command again if it has been turned off.

 

 

 

 

Screenshot 2024-03-04 075908.png

 

Edited by Str8jCkt

Share this post


Link to post
Share on other sites

In ST, I think this is what it would basically look like.

For_loop2.png.4973971feb38aa20440b7f7462

The ARCLGotoXXX would be the function block I'm currently using, in the correct ST syntax. Ladder syntax shown here.

Goto.png.f82899b3a5425d0730c485a08c27c11

TP_3 line is just a 1 second wait before verifying command receipt.

IF function checks to see if the function block's Status variable is showing 'Going to Goal'.  If not, then there was a communication loss at that moment, so we'll count the failure then loop back & try reissuing the command, in this case up to 5 times total.

Share this post


Link to post
Share on other sites

@DonNH You shouldn't use timers within a FOR loop. In your case your timer will never timeout. The loop will iterate the 5 times instantly then finish. You need to monitor the status of the FB after you have executed, then if you don't get the desired result after xxx time/counts then re-execute the FB.

Share this post


Link to post
Share on other sites

 

Ok, but I have no idea what that would look like.

Share this post


Link to post
Share on other sites

So I added ladder logic to the existing program which checks the status once, and if it's not correct, tries again before stopping.

However, it's now giving me a weird problem:

The Status variable of the function block typically returns as "Command Sent" when first triggered, very quickly changing to "Going to Goal", once the 

robot responds that it has received the command.

However, once I added the code to act upon that info, it stays on "Command Sent" indefinitely.  I can't see anything I've done to change the way it is called - will look harder.

When I reload the original program, I'm back to normal.

Looks like a variation on the Heisenberg Principal - you can observe the status or act on it, but not both.

Share this post


Link to post
Share on other sites

I haven't looked at this thoroughly, so apologies if I have misunderstood. But instead of using a For Loop, why not just have a integer variable that is your incrementing counter, and then add an additional condition to your IF statement where that integer must be <=5 (less than or equal to 5) for the statement to proceed. The integer would be set to 1 (or 0, depending on what you need) using a one shot of the start_enabled bit to initiate the process. This way you can incorporate the timer side of things quite easily.

1 person likes this

Share this post


Link to post
Share on other sites

Posted (edited)

So I obviously got hung up on the fact that there is a FOR loop within Sysmac, and didn't realize its limitations.

After pondering on the comments here, it finally clicked that I should be able to create a fairly simple While loop.

I think this should work:  (Conceptual ladder program, using a TP as a stand-in for the ARCL Goto function block, and taking some liberties with data types in order to keep it visually clean).

 

65e74ac9cc173_Whileloop3-Gotodummy.thumb

 

 

I still need to figure out why the Status variable isn't kicking back correctly, as noted in my last post - maybe this structure will work correctly for some reason.

 

 

 

 

 

Edited by DonNH
update ladder diagram

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