Jump to content
Sign in to follow this  
zhaolinger

Regarding the use of sub-routines in VisiLogic to model states machine....

Recommended Posts

I have a idea of making use of sub-routines to model a state machine since the sub-routines can be make to only be called when its inputs ladders condition is true.

But to do that, I need to fully understand how a relay that got energized within a sub-routine will behave after the calling of that sub-routine is isolated.

To further explain my question, I will create a scenarios of a simple ladder program here.

Let say we have Input 1, Sub-routine "Sub1", Relay 1, Timer (Delay) 1, Output 1. See the attached picture for the ladder connections.

Main Module

post-9148-0-20223300-1347260716_thumb.jpg

Sub-routine 1

post-9148-0-64036500-1347260738_thumb.jpg

My first question is can I assume that both the Timer 1 and Output 1 will not be active until Input 1 is on, because Sub1 won't be scanned until Input 1 energized Relay 1. Correct me if I'm wrong here.

Next, after the sub-routine Sub1 become active, Timer 1 will be activated and start countdown, Output 1 will be energized. After Timer 1 finished its countdown, it will cut-off the source of Relay 1 (de-energize) and isolate Sub1 from being call the next scan onward.

So, my next question is after that will Output 1 remain energized (because its inputs condition still hold true), or be de-energized (because the sub-routine that energize it no longer be called).

Experts over here, please enlighten me on this, because there is no software mode simulation in VisiLogic to prove whether my thought is feasible.

Share this post


Link to post
Share on other sites

I'm sure someone more authoritative will come along, but in my experience, any coil in a subroutine that is not executed will remain in the state it was in when the subroutine was last called. This is similar to and would be the same for any operand in the PLC that has no coil associated with it at all (i.e. remnants from a previous program) - they will always remain in the state they are in until a ladder rung with that coil is explicitly executed. This is why it can be important to initialize the PLC when loading a new program.

Share this post


Link to post
Share on other sites

Don't use subroutines for the individual states for the reason you mentioned- the outputs will stay on when you stop calling the subroutine and you'll spend an inordinate amount of time cleaning up after yourself resetting coils.

I break my programs into collections of state machines and each machine has a beginning "idle" state. I put an entire state machine in a subroutine.

I use a pointer register and equal blocks to jump between states and the subroutine is always called when the state machine is not idle; this way the coils will get written properly and not be left on when I leave the subroutine.

Joe T.

Share this post


Link to post
Share on other sites

I swear every time Joe Tauser makes a comment on this forum I learn something. Joe, can I trouble you to elaborate a bit on what you mean by "I use a pointer register and equal blocks to jump between states and the subroutine is always called when the state machine is not idle; this way the coils will get written properly and not be left on when I leave the subroutine"? I think I understand the first part - each state has a number associated with it and you call the subroutine when the state equals a specific number, but I don't understand what you mean by "the subroutine is always called when the state machine is not idle; this way the coils will get written properly and not be left on when I leave the subroutine". Also, what is included, generally, in the "beginning 'idle' state"?

  • Upvote 1

Share this post


Link to post
Share on other sites

It was easier to write a state machine example program than to try to go all verbose and explain visual concepts with words. I also want to add an addendum to my state machine calling criteria - often you can just call the state machine all the time and just let it "idle". You don't really gain anything in scan time for simple sequences and then you don't have to worry about leaving anything on because the subroutine isn't being called. This is the best path to take if you haven't done one before.

Take a look.

Joe T.

State machine example.vlp

  • Upvote 1

Share this post


Link to post
Share on other sites

Joe, that was a great example - thank you for posting the example program. Just one more question: how do you normally handle placing the machine into the initial idle state (MI0 = 10)? Would you normally set the "Power-Up Value" to 10 or use some logic to pre-determine which state the machine should start up in?

Share this post


Link to post
Share on other sites

You are exactly right about setting the power-up value to 10 - I forgot to to do this. The problem would have shown itself when I went to start up the machine.

I keep telling people my stuff NEVER works right the first time. Even in an example :)

Joe T.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...