Using o-words with PlanetCNC TNG software: Subroutines

Some CNC jobs demand repetition of the same motion over and over again, e.g.: drilling of a 100 holes. If we want to re-use the existing g-code pattern throughout the main program,  we can use subroutines.

Subroutine is basically a program block which is executed the moment it gets called from main program.

Subroutine program can be located within the same g-code program from where it gets called, or it can be standalone program file.

Subroutine o-word commands:
o-sub – Begin subroutine
o-endsub – End subroutine
o-call – Call subroutine
o-return – Exit subroutine

 

o-call:
Subroutine is called using o-call command.

o-sub and o-endsub:
Subroutine program block is defined with o-sub and o-endsub brackets. Everything within these brackets will be executed when related subroutine is called.

When o-endsub is encountered, program jumps back to the line where subroutine was called and continues with execution or remaining main program.

o-return :
User can achieve the same effect as o-endsub also with the use of o-return command.

 

Example of subroutine call:
Program below will call three subroutines from main program.
First and second subroutine will print to log window while third one will print nothing since o-return command will be used.

To observe the subroutine behavior open log window: Help/Show Log

 

%
(Main program)
(PRINT,Start main program)

(PRINT,Calling subroutine 1)
o100 call
(PRINT,End of subroutine 1, return to main program)

(PRINT,Calling subroutine 2)
o101 call
(PRINT,End of subroutine 2, return to main program)

(PRINT,Calling subroutine 3)
o102 call
(PRINT,Preemptive end of subroutine 3,o-return command was used)

o100 sub
(PRINT,Subroutine 1)
o100 endsub

o101 sub
(PRINT,Subroutine 2)
o101 endsub

o102 sub
o102 return
(PRINT,This should never be printed)
o102 endsub
%

Log window will display this:

User can also call external subroutine program files.
In such case, syntax is slightly different than when calling subroutines that are located withing the same g-code program.

Subroutine program file location is defined in settings: File/Settings/Program Options-> G-code folders. Insert pathway to files location:

 

Subroutine program file should have an filename.sub file extension.

Subroutine program file code is defined with o<filename> sub and o<filename> endsub brackets.
When o-endsub is encountered, program jumps back to the line where subroutine was called and continues with program execution. User can achieve the same effect with the use of o-Return command.

Subroutine program file is called using o<filename>call command.

 

Example of subroutine file call:

Create file: Sub_example.sub

Open it with text editor and write:

o<Sub_example> sub
(PRINT,Subroutine file program)
o<Sub_example> endsub

Main program will call subroutine file:

%
(PRINT, call subroutine file)
o<Sub_example> call
(PRINT,This should be printed last)
%

Program above will call external subroutine file from main program.
To observe subroutine file behavior open log terminal: Help/Show Log

Log window will display this: