×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Subroutine Use

Subroutine Use

Subroutine Use

(OP)
I am new at PLC programming and am writing a program to control a traveling cutoff saw. I want to use subroutines but I want to be able to stay in a subroutine until a particular condition has been met. I have a Keyence KV-16AR plc and when I try and put conditions on the return statement I get complile errors. I also tried to call another subroutine from within a subroutine and that doesn't seem to work either. I would appreciate any suggestions or help.

RE: Subroutine Use

nvpual,

If the operation of the saw is primarily a sequence of steps, you my consider writing it into a sequence of the events.  Some PLC's have a sequence type function, however I find them confining in machine control. Most traveling saws that I have been exposed to can be written into a sequence of events.  

One issue to address is that faults (ie over travel) will need to reset the sequence. Most of the time I reset my sequence to a controlled state.

Basically use a counter or register to track your steps of the sequence. As each step of the sequence is completed either increment the count or move sequnce step number into the register. Use the value of the sequence step to turn on desired outputs or other functions. When you have completed all the steps just reset or write a zero to the register. The advantage of this sequence is that you can move or jump in and out of a sequence as you need to. Most machines can be divided into sequences.

I have not programmed your PLC. If it has stage programming you may look at that as another option

RE: Subroutine Use

I would suggest that in a PLC program you should NEVER have a subroutine that does not always exit. That is not the way that PLC software should be written.
If you have a task that needs to be repeatedly executed until some condition is achieved that can easily be handled in other better ways. For example by conditionally executing a finite block of code (with no Loop until type structures) until the condition is achieved.

RE: Subroutine Use

I would suggest conditioning the calls to the subroutine.  Your 'Main' ladder would be very small and therefore execute fast.  You could use flags (latches) in each subroutine to control execution.

You may need an initialization routine to clear all flags.

Your application sounds like you need something high speed, which brings to mind using interrupts if available in the PLC.


Good luck.

RE: Subroutine Use

I would agree with FrancisL. I have been programming PLC's for 20 years. I have been down that path of using subroutines like steps in a sequence. In the end, it becomes more confusing and the result is generally not as good.

The simplest method (put simply) is to move values into a STEP register to go to a step.
eg. IF (STEP==2), AND (Transition Conditions are True) THEN (STEP=3).

This allows you to jump to any step in a sequence. You can then use a comparison on STEP to activate whatever logic you want to do at that step.
eg. Start Saw blade at step 2, and stop at Step 5, you can use the logic (2<=STEP<5) THEN Run SAW
The other logic I would use, which is easier to follow, is program a start and stop bit for each device, then for each step have an activation logic, So for this example,
IF (STEP = 2) THEN START SAW
IF (STEP = 3) THEN (other actions)
  .
  .
IF (STEP = 5) THEN STOP SAW

As you can see, this code is the simplest to fault find, because you can easily see exactly what is happening at each step.

Finally, you can have your shutdown or fault conditions set STEP=0.

Hope this is of some help, and not too long winded.

Cheers

RE: Subroutine Use

Jonesy alluded to a possible suggestion. Make your 'main' as tight as possible (inluding safety issues) and the subroutine call(s) handle the work load. This essentially reverses the main/subroutines relationship. To use an warn cliche, think outside the box.

I constructed something similar years ago when trying to catch a high speed action with a slow processor; 'main' sat in a tight loop waiting for the high-speed triggering action which subsiquently made the sequencing subroutines active.

Two cents on using integer driven sequencing: while flexible, all the required comparison functions slow the scan time. This used to be more of an issue when CPUs were slower - less so today, although high-speed packaging systems can still benefit from the streamlining.

RE: Subroutine Use

Point taken jlada, but I do have a solution to the comparison instruction.

I use one intruction (2 power STEP) to a word. This then gives a word where each bit represents the step number, ie bit 0 is on for step 0 or if sequence is stopped. Bit 5 will be on for step 5.

So I don't actually use any comparison instructions, I just use these bits.

So I still think this is the best way to control sequential logic. You could have these bits activate a jump to subroutine for your step activation logic if that is your preference.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources