The HOW’s and WHY’s of CNC Sub Programming

If you have done any manual G code program creation, you know you are always looking for some shortcuts  that can not only help cut down the data input … but would also help eliminate errors. Whether they be typing errors or movement errors … the less chance to create one the better.

One of the more powerful tools available to a programmer is the use of SUB PROGRAMMING. In this Making Chips post … we would like to touch on some of the basic ideas, concepts and uses for sub programming. This post will illustrate the Fanuc / Haas coding format … but check out the end of the post for Okuma explanations as well.

What is a Sub Program?

Basically, a sub program is a G code program that is called from another G code program. The contents of the sub program is not limited and can contain tool calls, spindle calls … just about anything any other G code program can contain. The sub program itself resides in memory under it’s own program number … and is separate from the “main program”.

Conversational

Why Would I Use a Sub Program?

As mentioned above … the less data entry means less chance for a mistake. let’s take this example scenario where we have to let’s say spot drilling then drill then chamfer then tap a series of holes. The less times we have to re-type those hole locations, the less chance we will have a typo and / or put a hole in the wrong place. If we can store the X / Y coordinates of the hole locations in one location and call them out as needed … that saves data input and reduces our chances for errors. This is a good example of how a sub program ( in this case it would be the program that stores the hole locations ) can be a big help.

How Do I Program and Call a Sub Program?

A sub program scenario consists of a main program and the sub program. The main program consists of all the code that doesn’t repeat itself … the sub program consists of all the data that will be repeated. In our above example … the tool calls, spindle calls, drilling cycles will all be different for each hole … so we will store that in the main program … but the hole locations will be the same so we will store them in the sub program.

When you create a sub program … it is done just like you would create any other program. On Fanuc / Hass controls you start out with an O number … and type the program as normal. Let’s take our above example of hole locations … the sub program might look like this :

O1234
X1.1 Y1.1
X2.2 Y2.2
X3.3 Y3.3
X4.4 Y5.5
M99
%

Notice that we have an M99 at the end … not an M30 or M02 like a normal program. This indicates that this is a sub program … we’ll explain the M99 command a little later.

This program is entered in the control as any other program … and resides in it’s own memory space.

When a Fanuc / Haas control wants to call a sub program to run … the programmer issues an M98 command in the Main Program. The M98 command is also followed by a P address … which is the “O” number of the external program to run. Our above sample sub program would be called with the command :

M98 P1234

When the main program reads the M98 command … it jumps out of the main program and starts to execute the sub program … in this case program O1234. When it reads the M99 command at the end of the sub program … it jumps back to the main program to the line after the one through which it left. In other words, it jumps back to the line after the M98 command.

The Complete Story

Let’s take a look at the full program and the sub program calls … see if you can follow the path.

Main Program

O0001
G00G91G28Z0
G28X0Y0
M01
N0001
(SPOT DRILL)
G00G91G28Z0
T01M06
G90S3500M03
G00X1.100Y1.100
G43Z.500H01M08
G99G81Z-.130R.050F20.0L0
M98P1234
G80
G00G91G28Z0
M01
N0002
(DRILL)
G00G91G28Z0
T02M06
G90S3000M03
G00X1.100Y1.100
G43Z.500H02M08
G99G73Z-875R.050Q.125F20.0L0
M98P1234
G80
G00G91G28Z0
M01
ETC.    ETC.    ETC.
M30
%

Sub Program

O1234
X1.1 Y1.1
X2.2 Y2.2
X3.3 Y3.3
X4.4 Y5.5
M99
%

Can you follow the path as the program jumps to the sub program?
Here is an in-depth explanation.

N0001
(SPOT DRILL)
G00G91G28Z0
T01M06
G90S3500M03
G00X1.100Y1.100 ——————— Position to our first hole.
G43Z.500H01M08 ——————— Bring the Z axis to the clearance plane.
G99G81Z-.130R.050F20.0L0 ———- Call our canned cycle … but use L0 which means the control will hold the data … but will not execute the cycle.
M98P1234 —————————– Jump to our sub program O1234 which will cause a hole to be spotted at each X / Y location in the sub.
G80 ————————————- When the M99 is read … the program will jump back to here.
G00G91G28Z0
M01
N0002 ———————————- This sequence basically does the same thing …except we are establishing a different canned cycle before we jump to the sub program.
(DRILL)
G00G91G28Z0
T02M06
G90S3000M03
G00X.100Y.100
G43Z.500H02M08
G99G73Z-875R.050Q.125F20.0L0
M98P1234
G80
G00G91G28Z0
M01

Another Example …

Once you are able to follow the above … here is another scenario.

You can also call a sub program and have it executed a set number of times. Let’s take the example where we want to execute a program on our lathe to make a washer (3) times. We will enter the main program and sub program as below.

Main Program :
O0001
M98 P1234 L3
M30
%

Sub Program :
O1234
*********
between here is the complete machining program that includes
tool calls … spindle calls
the feeding of the stock
the machining of the part
the cut-off of the part
*********
M99
%

The cycle start is executed with program O0001 … which calls the sub program O1234 and executes that program (3) times … the L in the M98 line. This feature is different for the various Fanuc controls but is usually commanded either :

M98 P —- L
or
M98 P****$$$$ where **** is the program number and $$$$ is the number of times to repeat.

Differences Between Fanuc / Haas and Okuma OSP

The basic ideas of calling and executing a sub program is the same between these controls … the G code commands are a bit different. Those differences are outlined below.

Sub Program Call
Fanuc / Haas : M98
Okuma : CALL
Example : CALL O1234 will call sub program O1234

 Sub Program End

Fanuc / Haas : M99
Okuma : RTS

Sub Program Call with Repeat
Fanuc / Haas : M98 P1234 L5 or M98 P12345
Okuma : CALL O1234 Q5 with the Q value being the number of repeats.

That’s basically it … just some G code differences but the basic idea and execution is the same.

**************************************

Sub programming is a powerful tool … even if you are not trying to avoid re-typing and repeated data entry. Hopefully this Making Chips post will get you thinking and exploring all the ways sub programs can make you a better programmer.

Happy Chip Making !!

Check out our Real World World machine shop software at www.KentechInc.com

Conversational CAD/CAM

Quoting & Estimating

G Code Conversion

CNC Training

…. and MORE !!!

One thought on “The HOW’s and WHY’s of CNC Sub Programming”

Comments are closed.