Safety first when it comes to programming

Programming the safety line.

The real art to using G-code to perform tasks on a CNC router takes some patience and practice. I’ve covered the basics of G-code commands in the past and offer a quick review here, along with important programing information about safety.

A line of G-code is commonly called a block. A block contains words, which are letters in the alphabet (A to Z). Following the words is a string of numbers called an address. The words and addresses are the instructions for the CNC machine’s controller.

An example of a block of G-code looks like this:

N## G## X## Y## Z## F## S## T## M##

(N represents the Line Number; G – Machine Motion; X – Horizontal Position; Y – Vertical Position; Z – Depth; F – Feed Rate; S – Spindle Speed; T – Tool Selection; M – Miscellaneous Functions)

Subprograms are small programs within a main program that can be used over and over to be able to cut multiples of parts, for example.

Most often it is best to use a M97 command for a local subprogram that is within the main program, plus a M99 command that ends the subprogram and transfers the processing back to the main program.

Here is the M97 local subprogram format:

M97 Pxxxx Lnn

(Pxxxx is the instruction within the subprogram that indicates which line number within the main program the subprogram is located and Lnn is the number of repetitions for the subprogram.)

Another set of G-code commands are extremely useful for programming directly to the CNC machine. These commands are known as work offsets, also called work coordinate systems, program zero or fixture offsets. These are generally G-54 through G-59. A work offset is the initial location of a work envelope within the machine envelope. The machine envelope is the total area where the machine can work. For instance, on a typical 4’ x 8’ CNC router work can be done within that entire area. All work offsets are relative to the machine home position: 0,0,0. This machine envelope home position is work offset G54 and is the default work offset.

G90 instructs the CNC router to go into absolute distance mode, a normal operational mode, where axis numbers (X, Y, Z) usually represent positions in terms of the currently active coordinate system or work offset; typically 0,0,0, though there are exceptions.

G91 instructs the CNC router to go into incremental distance mode, where axis numbers (X, Y, Z) are the current position of the cutterhead. In the incremental mode, an instruction such as X5, Y5, tells the spindle to move 5” from its present X-axis location and to move 5” from its present Y-axis location.

The very beginning of every G-code program should have a start-up block or safety line. This ensures that when a G-code program begins and after each tool change, the CNC router is set to its default conditions rather than assuming the CNC router is in that state.

A typical safety line is:

N1 G98 G94 G90 G00 G17 G20 G40 G49 G54 G64 G80

N1 is the first line in a program.

G98 will retract the Z-axis to the initial safe Z height after a canned cycle. This is so the axes can move at rapid speeds without colliding with a jig or material being machined. Be careful though, that all depends on how your post processing is set up or how you have written the program. All G98 guarantees is that the Z-axis height will return to the initial safe Z position when the canned cycle began.

G94 tells the machine that feed rates will be in inches per minute.

G90 selects the absolute or default mode of movement.

G00 selects rapid movement as the initial axes movement speed mode.

G17 sets the working plane as the XY plane. For a CNC router, this is the default mode.

G20 selects inches [English units] as the default movement unit. The machine controller is probably programmed with English units as the default mode. However, G21 will set the movement mode to metric, often discovered after a program has been run for a super rush job that should have been in inches. Guaranteed.

G40 cancels any active cutter diameter compensation. Cutter compensation is used in the instance that a tool has come back from sharpening. For instance, a 1” bit may be 0.984” after sharpening, then the cutter compensation helps to access the new diameter without changing any G-code.

G49 applies to the Z-axis. It cancels any tool length offsets that may be present in the machine controller. If the machine has automatic Z-zero setting, this may not be necessary but should be used for safety reasons, particularly after tool changes.

G54 sets the default work offsets positions: 0,0,0.

G64 sets the continuous cutting or ‘normal’ mode. This cancels other cutting mode commands which means acceleration and deceleration will be done normally and the feed rate overrides will be effective.

G80 cancels any active fixed or canned cycles. Fixed cycles are used to eliminate the repetition in manual programming and allow easy program data changes; they are usually associated with machining holes. 

This article originally appeared in the July 2020 issue.

R.W. LeeAuthor