Using Subroutines Within G-Code Programs for Quicker Coding

March 7, 2019 4:45 pm

 

To speed up the programming of a part using G-Code, we can reuse sections of the code and recall it within the program. These sections are known as subroutines.

The best way to explain subroutines is with an example. Let’s take a look at how we can write a roughing and finishing cycle on a CNC lathe writing the profile once.

 

The Roughing Cycle

 

This block of code will turn a 12mm spherical radius and turn a 24mm diameter at 20mm length.

 

G71 U0.1R0.1;

G71 P100 Q200 U0.0 W0.0 F0.2;

N100 G0 X0.0;

G01 G42 Z0.0;

X0.0;

G03 X24.0 Z-12.0 R12.0;

G01 Z-20.0 F0.1;

N200 G01 G40 X30.0;

 

Note: The example above is not the full block of code needed to program the cycle, it is written to demonstrate calling a subroutine within a G71 roughing cycle. For a full explanation of how to program a roughing cycle and any operation on a CNC lathe check out this.

 

The G71 lines are our roughing cycle. The thing to note here is the P100 Q200 commands. These commands define the start (P100) and end (Q200) of our subroutine. These values can be any number but they must be identical to the ‘N’ numbers found at the start and end of our profile subroutine.

P100 tells the control to read the subroutine that starts at N100, while Q200 defines the last line of the subroutine that ends with N200.

In the old days (now I’m showing my age), we used to add ‘N’ numbers on every line. Now, we just tend to use them as a way to jump around the program.

You will notice that I start my blocks of programs with N1, N2 etc. This is so I can search quickly to that section of code. I usually keep these ‘N’ numbers the same as the tool number, so if I am cutting with T05, then I will start the section of G-Code with N5.

 

For more on roughing cycles, check out my article called “G71 roughing cycle on a CNC Lathe“.

 

Finishing Cycle

 

Now that we have our profile already written, we don’t need to write it out again when we use a finishing tool, we just need to recall the G-Code subroutine.

To do this, we just need this simple line of G-Code.

 

G70 P100 Q200;

 

G70 is the G-Code used to indicate that we are using a finishing cycle and the ‘P’ and ‘Q’ values point the program to the start and end lines of our subroutine.

Below is the full program of the roughing cycle and finishing cycle that makes use of a subroutine. This program produces a 12mm spherical radius and a 20mm long by 24mm diameter feature. As shown in this drawing.

 

N1 (ROUGH TURN);

G50 S2000;

G99 G54 G18;

G00 G96 S180 T0101 M03;

X30.0 Z0.1 M08;

G71 U0.1R0.1;

G71 P100 Q200 U0.0 W0.0 F0.2;

N100 G0 X0.0;

G01 G42 Z0.0 F0.08;

X0.0;

G03 X24.0 Z-12.0 R12.0;

G01 Z-20.0 F0.1;

X30.0;

N200 G01 G40 X30.0;

G53 X0.0 Z-200.0 M09;

M05;

M01;

;

N2 (FINISH TURN) ;

G50 G54 S2500;

G00 G96 S200 T0202 M03;

X26.0 Z0.0 M08;

G70 P100 Q200;

G53 X0.0 Z-200.0 M09;

M05;

M01;

M30;

 

In conclusion, subroutines are a simple and efficient way to call upon sections of code and reuse them in order to help you speed up the programming process.

 

Author: Marc Cronin, Senior CNC Machine Tools Engineer, and founder of GCodeTutor.com

LEARN MORE ABOUT GCODETUTOR.COM

 

Categories:

Leave a Reply