Introduction To G-Codes
Abstract
|
| Not so long ago |
| CAD: Computer-Aided Drafting |
| CAM: Computer-Aided Machining |
| CNC: Computer Numeric Control |
| Post Processing |
| G-Codes |
| M-Codes |
| The Other-Letter Codes |
| Gerber Files |
| Excellon Files |
| Etching vs. Milling |
| Selected Links |
Not so long ago |
Not quite so long ago, we would be using techniques that, to some degree remain quite appropriate for circuit construction—I refer to a variety of bread-board, point-to-point, wire-wrap, or dead-bug construction. Printed circuit boards (PCB) are increasingly required for some circuits, have become relatively inexpensive, and can accommodate many of the prototyping techniques. Surface-mount components inevitably require PCBs. The small, light, reliable, and easy-to-copy attributes of the PCB are offset to some degree by the difficulty of modifying and the greater effort of planning and execution that the PCB requires.
The Community PCB Mill Project lies towards the end of a chain of tools that are demanded by the greater effort that the printed circuit board requires. The purpose of this article is to describe the tools that lie between the circuit concept and its rendering by the PCB Mill, whether the Mill drills a chemically etched board, or the Mill both etches and drills the board.
CAD: Computer-Aided Drafting |
More recently, we use the computer to enforce the rigor of the process of capturing the conceptual design and rendering it (eventually) onto paper while simultaneously allowing us to revise the concept relatively easily. The design capture tool, Computer-Aided Design (CAD) software, is essentially a tool that allows us to draw pictures on a computer, save that picture, and alter that picture, and (if the need presents itself) actually print that picture on paper. Depending on the focus of the CAD tool, the design that is captured can be as varied as the manual processes allowed—for this discussion, we will be limiting ourselves to the CAD tools that permit us to capture pictures of a PCB:
A large body of knowledge specific to electrical circuits and devices is embedded in the CAD tools mentioned. This allows the CAD tool to act as an auditor for the designer of the circuit. To some degree, the CAD tool has sufficient understanding of the specific problem domain that circuits can be automatically generated after the tool has asked a few questions of the user (for a simple example, take a look at this 555 timer designer; and a more complex example, check out this power supply design tool).
We will use, by way of example, information that Parallax publishes about the Basic Stamp 1 OEM.
CAM: Computer-Aided Machining |
More recently, we have applied machinery to allow us to faithfully produce many copies of the design.
Even more recently, we have developed means to have the computer transfer the design articulated to the CAD tool to the machines so that the machines can quickly and inexpensively mass-produce identical copies of the design. This process of having the machines render a physical instance of a design is called Computer-Aided Machining (CAM). We will limit the focus of Computer-Aided Machining for this article to the production of printed circuits boards. The completed circuit board has the components connected and arranged according to the CAD design of the circuit. We will further limit, for this article, the focus of CAM to the production of the circuit board itself.
In the same way as for CAD, a vast body of machining knowledge can be embedded into the CAM tool that will influence the tool's output. Sometimes, this machining knowledge can be very machine-specific. Once a particular machine has reached a degree of popularity, the CAM tools from various vendors will support that machine. This has the effect, then, that when another machine-tool manufacturer wants to introduce a new machine to the marketplace, the CAM interface of the popular tool is built into the new machine.
Thus, it is that certain methods of communicating the design to the machine have become standards that the machinist and tool-builders cannot generally ignore with impunity.
CNC: Computer Numeric Control |
By way of analogy, a programmer uses a CAD tool (whether a simple ASCII editor to a full-blown UML graphical tool) to capture the design of a software design, and uses a CAM tool (the compiler/assembler and the downloader) to communicate that software design to the microcontroller to execute that design. The machine code that is squirted into the microcontroller is the analog of the CNC language.
In essence, the CNC language is a script of motions that the machine is to follow to make an instance of the design. The CAM tool processes the CAD information about the concept into this motion script, sends it to the machine; assuming that the machine is a PCB mill, that Milling Machine then follows the CNC script to move its cutting tool so as to cut the outline of the PCB traces, and then to drill the pad-holes through the board.
For a variety of reasons, the defacto standard (and one of the most wide-spread) of CNC language is called G-Codes. G-Codes are also called by the name of the ANSI standard number RS274. I hasten to point out that there are other CNC languages, many of which tend to be isolated to rather vertical applications.
The manufacturing of printed circuit boards is a rather vertical industry. While general-purpose CNC machines are present, specific purposed machines quickly were developed. As the machines specialized, subsets of the G-Codes language were developed. Two of these subsets are known as Gerber format and Excellon format.
Post Processing |
Let's assume the schematic capture CAD tool we have only outputs Gerber files. The commercial board house we have selected requires Excellon files for drilling our PCBs. Instead of getting a new CAD tool, or a new board house, we can elect to just do a Post Process conversion of the available Gerber file into the required Excellon file.
G-Codes |
Back in the days when punched paper tape was all the rage for CNC program storage, it was also realized that any reduction of the amount of data to be stored would yield a significant savings, if not money directly, certainly in filing cabinet volume. The G-Code motion scripting language was purposefully invented as a terse, but rather flexible, language.
The basics of the G-Code language syntax can be summarized as follows. The command line is composed of a series of numbers interspersed by letters. These all have quite specific meaning. Nearly all of the letters of the English alphabet are used—these are little more than names of specific registers in the CNC machine. The number that follows any letter is the value to store in that register. As the CNC machine parses the G-Code input, it stores the stated values into the named registers. Typically, when the parser reaches the end of the line, the machine executes the motion command based upon the current content of all of the machine registers. The scripting language allows that the G-Word (or many of the other Words) need not be repeatedly specified for sequential similar action—one only needs to identify the desired change in axis position to re-trigger that command.
The G-Word (the combination of the letter 'G' and a number) is called a preparatory function. All this means is that the G-Word is the command that tells the machine what it is to do; other words are essentially parameters for this action, and the action is triggered when, typically then end of the command sentence is reached. Approximately 100 preparatory functions are defined in the RS274D G-Code standard; machine tool manufacturers sometimes extend this list.
Some of the G-Codes deal with commanding motion in a certain way; others toggle between Inch/Metric measurement systems or act as switches to influence the machine's motion planning logic. Still others deal with calling subroutines, or determine the compensation to motion planning that must occur due to geometry of the cutting tool.
Of all of these, I'll discuss in detail the ones that intimately involved in command linear motion.
These are the G-Codes that would be the basis of PCB milling or drilling. Do understand that all commanded motion
starts with the current position of the axes, and axis words specify the end-point of the motion.
| G-Code | Description |
| G00 |
Point-to-point positioning.
Move all axes named in conjunction with this G-Code to their specified positions. The axes can all move independently, their motions are not coordinated; typically each axis moves at its maximum velocity. |
| G01 |
Linear interpolated positioning.
Move all axes named in conjunction with this G-Code to their specified positions. In this case, all the axes must move in a coordinate fashion; specifically, the axes involved all start their motion at the same time, and the velocity of each axis is adjusted so that all axes arrive at their destination at the same instant. This is how straight lines are made with the tip of the tool, when moving in more than one axis. |
| G02 |
Circular (Clockwise) Interpolated Motion.
Either the end-point and radius of the arc is specified; or the arc's end-point and the arc's center-point (relative to the starting position) is specified. In the first case, additional information must be given so that the machine knows whether to take the long-way or the short-way around the circle that the arc lays upon. The motions of the involved axes are coordinated so that a perfectly circular path is described for the arc. |
| G03 |
Circular (Counter-Clockwise) Interpolated Motion.
As for G02, but moving in the opposite direction relative to the planes of motion. |
| G04 |
Dwell.
That's it; just make no movements until the time specified has passed. |
| G54 |
This is one of the G-Codes that is implemented differently by different toolmakers.
For normal CNC machines, this selects on of the several frames-of-reference, or coordinate systems, for following X,Y,Z positions. For Gerber CNC machines, this signals that the aperture disk is about to receive a command. The D-Word that follows contains the tool changer command. |
| G90 |
Absolute Programming
From this point, X,Y,Z Coordinates specify points on the active coordinate system (typically the machine coordiate system. Mutually exclusive to G91. |
| G91 |
Incremental Programming
From this point, X,Y,Z Coordinates specify an offset relative to the last specified position. Mutually exclusive to G90. |
| G92 |
Set Absolute Position
Used to specify an absolute position using, for example, X,Y,Z Coordinates; can also be used in conjunction with the S-Word or the F-Word to specify maximum spindle speeds or feedrates (within the physical limits of the axis/spindle), respectively. |
As already mentioned, there are about a hundred variations on the G-Word, depending on the particular machine's implementation. You can look up the meaning of the additional preparatory functions in the references provided below.
M-Codes |
Other of the miscellaneous functions are enumerated in a number of the links below. Just like the G-Codes, depending on the particular machine's implementation, upwards of 100 or more M-Codes may be defined.
The Other-Letter Codes |
The T-Word is used to identify a particular tool (and sometimes tool geometry information) that the machine is to select for the following operations. The D-Word is often used to specify either an additional angular motion, a feed function, or a function secondary to the current tool—D-Codes specific to PCB manufacturing are discussed below.
The F-Word establishes the effective feed-rate of the tip of the tool (how fast it moves); for interpolated motions in multiple axis planes, this is the velocity of the resultant movement vector. The S-Word sets the rotational velocity of subsequent spindle commands.
The N-Word is kind of label within the G-Code file—it is similar to the BASIC line number/label. The O-Word identifies particular subroutines—again comparing to BASIC, this would be the label that the GOSUB would call. Differing levels of branching and program structure capabilities are present in the various G-Code processors, depending on the toolbuilders requirements; some provide rather complete scripting capabilities.
The machine's linear axes of motion are labeled, typically as the X-Axis, Y-Axis, and Z-Axis. U, V, W, and P, Q, R are additional labels for axes, parallel to the X, Y, Z axes, respectively. A, B, C are labels of axes that rotate around the X, Y, Z axes. Each of these axes has a corresponding register. The meaning of the value stored in these axis registers is nothing more than where the machine should have positioned the tool/work-piece on that axis by the end of the movement.
The I, J, K words are mostly used as interpolation parameters. These will be often found in the circular interpolation commands G02 & G03. Think of them as just specifying a secondary coordinate on the X, Y, Z axes for some motion sequence.
So, that pretty well uses up the alphabet. With this brief review of the G-Code commands, let's move on to how these are used in PCB fabrication.
Gerber Files |
The Gerber data file format is an adaptation of the G-Code standard. Much of what has been said about G-Codes applies directly to Gerber Files. The tools that are used by the photoplotter are the apertures; these are selected by the G54 G-Code. The D-Codes are used to draw the image on the film.
| D-Code | Description |
| D1 | Move the image point to the specified X,Y position, with the imaging shutter open. In other words, using the current aperture, trace on the film what will become a conductor on the PCB. |
| D2 | Move the image point to the specified X,Y position, with the imaging shutter closed. This action leaves no trace. |
| D3 | Move the image point to the specified X,Y position, with the imaging shutter closed, then flash (that is, open and then close) the aperture image on the film. This technique is used, for example, for making the solder pads. |
| D10 | Codes 10 and higher identify specific apertures; these are the defined shapes with which the photoplotter draws or flashes. |
Recently, there has been a push to advance the Gerber file standard towards what is known as RS274X, an extended Gerber format. RS274X only really differs from RS274D specification by the definition of some new parameters (for more information, see the "Gerber RS-274X Format" in the Selected Links. Remove these parameters; you have a standard G-Code file. It seems that one big advantage to the extended Gerber format is that what used to be several files that described a circuit board layer is now combined into one file. Fewer separate files mean that there is less to go wrong in the communication between a customer and the board fabrication facility.
Let's take a look at the Gerber files
that details how to make a Basic Stamp 1 OEM circuit board
(for this tutorial, the APT, GDG, & GDD files will be ignored).
Let's pick, say, the Top Pad Master (GPT).
bs1Gerber.zip |
|
|
*%FSLAX24Y24*% *%MOIN*% G01* ... %ADD21C,0.0450*% |
|
G54D21* X93450Y79540D02* D03* X94050D02* D03* |
This pattern of selecting an aperture, and flashing that image on the film at a series of positions is repeated until all of the pads for the top layer have been imaged on the film.
Having looked a bit into the innards of the Top Pad Master (GPT) file, we can now peek into
the Top Copper Layer (GTL) file. We can see the same
extended Gerber parameters included in this file.
|
*%FSLAX24Y24*% *%MOIN*% G01* ... %ADD14C,0.0080*% |
|
G54D14* X104030Y86430D02* Y86830D01* X104201D01* X104259Y86811D01* X104278Y86792D01* X104297Y86754D01* Y86716D01* X104278Y86678D01* X104259Y86659D01* X104201Y86639D01* X104030D01* X104163D02* |
|
You should, by now, be able to decipher the meaning of the other Gerber files for the Basic Stamp 1 OEM
circuit board.
In fact, if you were so inclined, you could get pencil & graph paper (or use this spreadsheet graphing trick) and puzzle out the Top Layer Silk Screen (GTO)— what do you think it will look like?
(Hint: The 36 lines starting with X92471Y86590D02* creates the
|
Excellon Files |
The most significant difference between Excellon Files and Gerber files is that the Excellon format files script out a specific drill bit selection, and each X,Y coordinate in the drill file specifies the location of a hole to be drilled. In other words, the Excellon drill machine selects the bit commanded, and then moves the drill to the specified coordinate, plunges the drill to create the hole, and retracts the drill. This process is repeated for all holes that use that particular bit. Then the next size of bit is selected, and all the holes for that bit drilled.
See, just a machine process simplification, or subset, of what is can be now viewed as a standard G-Code motion scripting language. It makes sense for something like PCB drilling, but the Excellon script might be inadequate for a number of other types of machining processes. Incidentally, most anyone that makes PCB drilling machines enables their machine to understand the Excellon motion scripting language.
Going back to the Basic Stamp 1 OEM example, let's now examine the Excellon Drill Files (in the same ZIP file as the Gerber Files).
bs1Gerber.zip |
|
The ASCII Excellon Drill File (TXT) starts out with
this preamble.
|
M72 M48 T1F00S00C0.026 T2F00S00C0.038 T3F00S00C0.055 % |
Then it moves right on to the business of drilling the holes: use the T-Word to select the drill bit,
and then enumerating all the holes to be drilled with that size of bit (one hole per line).
The instructions for all of the first size of hole are shown.
|
T01 X0155Y01468 X016Y01418 X0165Y01468 X0123Y0165 X0117 T02 |
The binary Excellon Drill File (DRL) has not been included by Parallax in this collection of the Gerber files that detail the fabrication of the circuit board. In this case, it is likely that the board house they have selected for production runs of these PCB use the ASCII format. The binary Excellon file has essentially the same information as the ASCII file, but a format compatible with paper tape readers from the time when paper tape reigned as the NC storage media of choice. Don't you think that the ASCII G-Codes are much nicer, anyway?
Etching vs. Milling |
There is (typically) no difference in the Gerber/Excellon files that define positioning of the holes to be drilled in the PCB.
And since this Community PCB Mill project is oriented for the hobbiest/prototyper, we are largely not very interested in the solder mask and silk-screen layers. In any case, these would be applied by a process other than the PCB Mill.
The biggest difference is in the G-Code files that specify the circuit paths and pads. The PCBs destined to be chemically etched have the traces drawn on the board's chemical resist—that stuff that prevents the copper layer from being dissolved by the etching chemical. When the board etching is completed, the chemical is flushed off, and the resist layer washed away. What is left is a network of flat wires in the PCB. Chemical etching is the process assumed by the Basic Stamp 1 OEM circuit board Gerber Files.
For PCBs that are going to be fabricated by a milling process, the G-Code files that are required, instead of the tracing path, actually an outline of the trace's path. This difference is subtle but important. Both methods involve removing copper metal from the PCB substrate to isolate one trace from another. The PCB mill must do this by drawing around the trace. Perhaps a picture will help—take a look at the first page of this PCB prototyping tutorial.
Some schematic capture tools emit the set of G-Codes required for PCB Milling operations. Lacking this, there are converters that will read a set of Gerber Files for chemical etching and output a set of G-Code files for milling PCBs; it is probably better, however, to get a software package that will process the required PCB milling G-Codes direct from a CAD file, such as DXF, or direct from the schematic capture tool's data file..
But specific software is outside the scope of this article; which ends with this list of reference links.
Selected Links |
An (incomplete) annotated list of references.There is much information, both on and off the web.These links are not an attempt to make a comprehensive survey of the field; there is Google and conventional research tools for that. Rather, these are some that have been helpful to me in the construction of this introduction. | |||||||
|
Parallax
SX SX/B Interactive Thread Printable Thread | Links to Parallax, and to their SX microcontroller and the SX/B compiler. This is the central core of the motion processor of this PCB Mill project. Also links to the thread in the Parallax The Sandbox support forum that documents the development of the idea of the SX Community PCB Mill. | ||||||
|
CAD
HPGL DXF CAM Milling Machine NC CNC G-Code Schematic Capture Place & Route PCB Photoplotter PCB Milling Gerber Files Excellon Files | wikipedia.org is an interesting project attempting to catalog information into an open encyclopedia. These are links to some entries pertinent to the purpose of this introduction. These entries are heavily cross-referenced to other entries in the wikipedia; unfortunately, some of the entries are quite sparse. | ||||||
|
The Key Concepts Of Computer Numerical Control
| MMS Online has made available this series of ten articles that introduces the basic concepts of CNC. This is an excellent review of the basics of computer controlled machining. | ||||||
|
G-Codes to Know
RS274D - alias 'G-Code' | These two links each provide a "one-page" description of G-Codes. Each link is from a different web site. | ||||||
|
The CNC Programming FAQ
The Dreaded G2 & G3 | NFR Partners Inc. produces an editor for G-Codes. They also have produced a pair of tutorials about G-Codes that I have had bookmarked for some time. It is fast-paced introduction to the basic G-Codes, and points out some of the things that are not obvious at first exposure. | ||||||
|
Gerber RS-274X Format located at www.ucamco.com/downloads.php | On that page, click on the link "RS-274X Extended Gerber Format Specification". Ucamco owns the copyright on this PDF document, and makes the most recent version available at their downloads page. This might be viewed as the definitive reference to the 'Gerber format'. | ||||||
|
Machine Manuals
Part Programming Commands | These are links to the Excellon Automation CNC-7 drilling machine. What better place to get a complete description of the Excellon file format? | ||||||
|
Required Files
Gerber File Info Excellon Drill File Info |
PCB Express is one of many board houses. Basically you send to these board houses a set of Gerber files, Excellon files,
or even the native file-format from the multitude of PCB layout/routing packages. They return to you completed PCBs.
As much of the interaction is automated and web- or email-based, these board houses go to some length to explicitly
help their customers understand what the importance of the file-formats in use.
While you are here, you may as well take a peek at their PCB Tutorial; after all, the initial impetus for this project was to manufacture a PCB. While a PCB mill uses mechanical means of PCB processing, it is instructive to understand what goes on during PCB production at the board houses. | ||||||
|
File Format Info
NC (Excellon) Info Gerber Data Info |
AP Circuits is another of many board houses.
As with most board houses, you must send a description of your board design in the form of
Gerber files, Excellon files, and whatever other formats they might accept. These links take you to
the AP Circuits' descriptions of those file formats.
Follow the secondary links on these pages; they are replete with clear examples and explanations. | ||||||
|
D-Codes, Apertures & Gerber Plot Files
What's all this about RS274X Anyway? |
Artwork Conversion Software, Inc. is in the business of
file format conversion software. Steve DiBartolomeo provides several application notes, two of which I reference here.
The first is a tutorial on the basics of Gerber data, especially on how it is processed by board houses. The second is a discussion of the differences of the older RS274D G-Code standard and the newer RS274X G-Code standard. | ||||||
|
Homebrew_PCBs
DIY-CNC CAD_CAM_EDM_DRO jobshophomeshop | These are several of the CNC-related discussion groups (in no particular order) that I lurk on; there are many other machining groups. One can learn a lot just by eavesdropping on those with broad experience and who are willing to encourage newcomers to the field. | ||||||
|
EMC - a brief overview
EMC Handbook RS274/NGC Interpreter | LinuxCNC.org is the home of the open, Linux hosted, Enhanced Machine Controller (EMC). | ||||||
|
Learning CNC
Sherline's links to free CNC software | Sherline Products, makers of small metalworking milling machines and lathes, has embraced the open EMC software for their CNC adaptations of their workhorse machines. I find that Sherline's Joe Martin writes about sometimes-hard subject matter in a way that many can grasp the concepts. | ||||||
|
CAD_CAM_EDM_DRO #34107
| Ian Eagland, in a message on the CAD_CAM_EDM_DRO group, describes the process he went through to adapt his existing milling machine into a PCB mill. | ||||||
|
Mechanically Etched PC Boards
Plotting Gcode using Excel |
Scott Holmes describes how he built up a software toolchain to do PCB milling,
after being inspired by Ian Eagland's description of the process;
all this before he had a mill setup to do the work.
He also describes a means of using the spreadsheet Excel to verify> the resulting G-Codes.
|
Miller's University - Required Reading
| New to PCB Prototyping? Why Accuracy Counts The Importance of Control
Several interesting discussions that are germane to the Community PCB Mill
project are presented. Many of the key points have been discussed to some degree
on the project's thread.
|
Printed Circuit Prototyping
| Etching Techniques
Think & Tinker, Ltd. provides an interesting discussion of the
issues surrounding PCB prototyping, both of chemical etching and mechanical milling processes.
The first link is to their reference library; the second link is to the start of the discussion
of the two types of etching processes.
|
Tabletop Machining
|
Machine safety is an important issue that should not be overlooked. I included this link because it is concise; there are many such cautionary admonitions available.
| Also discussed is tooling that comes in handy (necessary?) for a hobbyist machinist. |