Once upon a time, G-code programs were simple. The controller read them from the tape, beginning to end, and moved the cutting tool, step-by-step, to make a finished part.
With the advent of “conditional branching” and “variables,” the controller could create complex movements using only a few lines of code. These two functions—branching and variables—enable programs to repeat themselves while overwriting their values, making the controller “Turing complete.” (Turing refers to English mathematician Alan Turing, who is credited with breaking the German Enigma Code during World War II.) “Turing completeness” describes the set of systems that can simulate each other.
In theory, Turing completeness means that a CNC can do anything any other computer can do. Want to turn a milling machine into an arcade game? It’s possible—but your boss will get mad if production stops because you ran out of tokens.
Using macros greatly improves programming productivity.
Macros are pieces of instruction that provide shortcuts. The simplest milling macro, for example, tells the machine to repeat a cutting pattern for multiple depths:
(MILL A PATTERN TO Z-0.699)
N100 VC1=[0] (INITIALIZE VARIABLE)
N110 VC1=[VAR-0.25] (1/4"DOC)
N120 G0X0Y0 (STARTING POSITION)
N130 G1Z=[VC1]
<<repeated milling pattern>>
N990 G0Z0.1 (RETRACT)
N1000 IF[VC1 GT -1.0]N110 (LOOP
UNTIL Z=-1.0)
This code sets a variable for the starting DOC (N100). The code then decrements it (N110), feeds the tool to the depth for each step (N130), mills the pattern, then retracts the tool (N990) and checks to see if the program should decrement again (N1000).
Note that the program uses the two functions of Turing completeness: a variable (VC1) and a conditional branch (IF). It also makes excessive use of comments—notes added to the code in parentheses that cause the controller to ignore them. Comments make it easier to understand more complicated programs when you inevitably have to go back to debug them or extend their functionality. They also help in the often-used practice of reusing code, aka copying and pasting.
But rather than copying and pasting, it’s better to create subprograms. Subprograms are a way to encapsulate a toolpath in a single block of code and then use that code for multiple programs. Subprograms make your code shorter, cleaner and easier to maintain—when written properly.
As with all code, you should write subprograms defensively. Try to envision as many different situations as possible where you could use the code. Should the code apply to both G90 (absolute coordinates) and G91 (relative coordinates)? Do you need to check any other modal values, such as whether the spindle is running or the current coordinate system? Do you need a clearance zone? Take a few moments to code in as much flexibility as possible and you will save hours of work later.
Following are a few coding best-practices for maximizing flexibility:
- Write each macro to perform just one essential function. If a single tool performs two operations, write a macro for each operation and execute one after the other. If these two operations are performed on multiple workpieces, write one macro for each operation and a third macro that executes the two operations one after the other. Execute this third macro once for each workpiece.
- Use local variables instead of global variables. In production code, all variables in calculations should only exist within the context of the subprogram. This keeps them from being overwritten as control passes from one macro to another. If the variables in a macro can be overwritten, there’s no guarantee the values will remain sensible.
- It’s wise to have error checking in production code. A few lines at the beginning of a macro can compare the argument variables to expected values, generating an error if the values are unexpected or missing. This step may seem unnecessary now, but you will thank yourself down the road.
- When possible, code should incorporate cutter compensation for fine adjustment of feature dimensions through the use of the offset values. It also allows you to apply a tool with a different diameter to create the same part features.
Once the toolpaths have been created, the post-processor creates massive linear programs that are read from beginning to end, wasting the variable and conditional-branching capabilities of the CNC. You will often find that modeling a part in CAM software, creating the toolpaths and generating, say, 500 lines of G code takes more time than writing an equivalent 50-line program by hand. Macros can save time and increase productivity.
Related Glossary Terms
- clearance
clearance
Space provided behind a tool’s land or relief to prevent rubbing and subsequent premature deterioration of the tool. See land; relief.
- computer numerical control ( CNC)
computer numerical control ( CNC)
Microprocessor-based controller dedicated to a machine tool that permits the creation or modification of parts. Programmed numerical control activates the machine’s servos and spindle drives and controls the various machining operations. See DNC, direct numerical control; NC, numerical control.
- computer-aided manufacturing ( CAM)
computer-aided manufacturing ( CAM)
Use of computers to control machining and manufacturing processes.
- cutter compensation
cutter compensation
Feature that allows the operator to compensate for tool diameter, length, deflection and radius during a programmed machining cycle.
- gang cutting ( milling)
gang cutting ( milling)
Machining with several cutters mounted on a single arbor, generally for simultaneous cutting.
- milling
milling
Machining operation in which metal or other material is removed by applying power to a rotating cutter. In vertical milling, the cutting tool is mounted vertically on the spindle. In horizontal milling, the cutting tool is mounted horizontally, either directly on the spindle or on an arbor. Horizontal milling is further broken down into conventional milling, where the cutter rotates opposite the direction of feed, or “up” into the workpiece; and climb milling, where the cutter rotates in the direction of feed, or “down” into the workpiece. Milling operations include plane or surface milling, endmilling, facemilling, angle milling, form milling and profiling.
- milling machine ( mill)
milling machine ( mill)
Runs endmills and arbor-mounted milling cutters. Features include a head with a spindle that drives the cutters; a column, knee and table that provide motion in the three Cartesian axes; and a base that supports the components and houses the cutting-fluid pump and reservoir. The work is mounted on the table and fed into the rotating cutter or endmill to accomplish the milling steps; vertical milling machines also feed endmills into the work by means of a spindle-mounted quill. Models range from small manual machines to big bed-type and duplex mills. All take one of three basic forms: vertical, horizontal or convertible horizontal/vertical. Vertical machines may be knee-type (the table is mounted on a knee that can be elevated) or bed-type (the table is securely supported and only moves horizontally). In general, horizontal machines are bigger and more powerful, while vertical machines are lighter but more versatile and easier to set up and operate.
- toolpath( cutter path)
toolpath( cutter path)
2-D or 3-D path generated by program code or a CAM system and followed by tool when machining a part.