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
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
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
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
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
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
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.