Get with the program of writing G-code

Publish date:

Though software programs are becoming more and more user friendly, this leaves the user more and more dependent not only on the program they are using, but on many other software programs that are becoming necessary to complete a task. Nowhere is that more evident than in the CNC industry.

First there is the CAD program that changes an idea into a vector-based drawing. Then there’s the CAM program that changes the vector drawing into step-and-direction files (G-code), which are fed into a controller and converted into electrical signals that drive the stepper or servo motors to produce a part.

While much of this is done automatically, the first two steps certainly require input from a skilled operator. By skipping those steps, overall production increases. But the operator has to be able to program CNC function with G-code.

In my previous column (November 2019), the base elements of G-code were introduced, as was the technique of subprograms which can control the CNC to execute the same program over and over without any operator intervention. Subprograms use the functions M97, M98 and M99. The M97 function uses a subprogram that is within the main program (local subprogram), while the M98 function calls a subprogram that is stored in the controller. The M99 function ends the subprogram and transfers the processing back to the main program.

Another set of G-code functions, known as work offsets, program zero or fixture offsets. These are generally G-54 through G-59, though depending on the make of the controller, there may be up to 255 work offsets.

A work offset is an envelope within the machine’s total work area. All work offsets are relative to the machine’s home position (0,0,0).

Work offsets increase productivity if a shop needs to manufacture 50 radiused top rails, for example. A fixture could be made to hold six top rails and the location of the work offset program zero could be determined for a corner of each rail. The program zero could be found through the use of an electronic probe, such as the Haimer 3D manual probe, or an electronic touch plate that is connected to the controller.

The electronic touch plate is the most functional of the three alternatives as it can be used to set work offsets for the X-, Y- and Z-axes. It is also the least expensive option. However, the controller needs to have additional ports and pins to be able to hook the wiring from the plate to the controller breakout board.

Also, the programming within the controller must be able to have a small program added to it so the controller recognizes the electronic plate and its operation.

In our example, the work offset must be identified for each of the six slots on the fixture that holds the uncut rails for the benefit of the controller’s memory. While this example has only six doors cut at a time, fixturing could be developed that covered as much of the CNC router’s table as possible.

M97 local subprogram format:

M97 Pxxxx Lnn
‘Pxxxx’ indicates which line number within the main program the subprogram is located and ‘Lnn’ is the number of repetitions for the subprogram.

Main program:

O01234 (Program number and start of main program)

N0001 T02 M06 (T02: use tool No. 2; M06: tool change)

N0002 M03 S20000 (instructs spindle to run at 20,000 rpm)

N0003 G90 (G90: absolute distance mode)

N0004 G00 Z0.50 (G00: non-cutting rapid move; Z0.50: Z-axis set 0.50” above stock)

Portions of the main part program:

N019 G54 (First work offset coordinates)

N0020 M97 P0115 (Program jumps to the P address, line 115)

N0021 G55 (Second work offset coordinates)

N0022 M97 P0115 (Program jumps to line 115)

N0023 G56 (Third work offset coordinates)

N0024 M97 P0115 (Program jumps to line 115)

N0025 G57 (Fourth work offset coordinates)

N0026 M97 P0115 (Program jumps to line 115)

N0027 G58 (Fifth work offset coordinates)

N0028 M97 P0115 (Program jumps to line 115)

N0029 G59 (Sixth work offset coordinates)

N0030 M97 P0115 (Program jumps to line N0115)

Finish part program:

N0114 M30 (End of main program, return to top of program)

N0115 (Identifies the start of the local subprogram called up by M97 P0115)

Local subprogram portion of main program:

M99 (Instructs program to loop back to the main program and execute next line or return to the top)

This article originally appeared in the January 2020 issue.

Related Articles

With CAD and optimizing software, the job gets easier

Making a decent profit on a job is a skill based on years of experience and includes figuring how to get the most out of sheet goods and dimensional lumber.

It’s all about the details with a digital probe

Most woodworking shops with a 3-axis routers are setup to cut cabinet and furniture parts from flat panels and solid wood.