Getting started with G-Code
Before starting with any actual codes that do something, we need to let the controller know a couple of things about our set up. We should get in the habit of doing this even if we are only writing code to run on an emulator. Eventually, we will want code to run on a real machine so let's start getting in the habit of it.
I like to call these code that make settings to the controller preparatory codes. These codes prepare the controller for what is to follow.
Finally, I want to mention N codes. These are program line numbers. Line numbers have their place. You will need them for sub-routines or some other identifier. LinuxCNC does not require the use of line numbers in your program, other controllers out there may. If you are using something other than LinuxCNC, like Mach3 check to see if your controller requires the use of line numbers. I will cover them next.
Line Numbers
Line numbers begin with the letter N followed by up to five numbers in length and must be positive and no decimals. As i stated earlier, N codes are usually optional and are ommitied. But check your controller to be sure. Examples of line numbers or blocks are as follows:
N1
N2
N3 ...
Comments
Comments are bits of text that you can put in your program to make notes to yourself on what you were doing. Programs should have lots of comments. It is frustrating to go back to a program to make a tweak a month later only to have no idea what you were trying to do. I advise you to comment your code. Comments are ignored by the controller and have no impact on the machining job at hand. Comments in a G-Code program are made in two possible ways. Comments are embedded within a line of code using the parenthesis characters, ( and ). Comments can also be started with the semicolon character, ; and are read to the end of the line. Some examples of comments follow:
(This is a comment on a single line)
;This is a comment on a single line too.
N10 G0 (rapid movement comment) X0 Y3
N11 G0 X2 Y5 ;This comment is read to the end of this line
Dimensions
CNC machines can operate in either millimetres or inches. The controller needs to know what dimensions you are operating in. The difference between 5mm and 5in is rather large and can mean the difference of doing it right and making a horrible mistake. There are 2 G codes that are used to set the units of measurements. These are G20 and G21. G20 means the machine coordinates are measured in inches and G21 means the machine coordinates are measured in millimetres. I think the metric system is probably the best system to use in most things, but having grown up using the imperial measurements, I cannot make the switch all that easy so most of my stuff will be in inches.
Rapid Movements
Rapid movements are one of the most basic G-Codes. It is represented by G0 (That is the letter G followed by the number zero.) Rapids are intended to move to a certain location a fast as the machine can. The actual rate at which the machine travels is very much machine and controller dependent. My JGRO CNC Router for example can only rapid about 30 inches per minute (i.p.m.) if I drive it any faster the stepper motors will start missing steps. It is important to note that rapids are only to be used to move from some place to another, you would never try cutting with a rapid as you will likely snap a bit or much worse. It is also worth noting that when you rapid in more than one axis at the same time, it is up to the controller to decide how that were to happen. Some controllers will do a single axis at a time while others will do all the given axis at the same time. To complicate it further, when doing both axis at the same time, they may be coordinated to arrive at their programmed destination at the same time or they may arrive independently. It is important to learn how your controller works. And one more thing, when doing rapids make sure you have nothing in your path. For example a vice or clamping fixture. It is a bummer when you run into one of these.
Now that we have a little code under our belt, let's write our first program.
(Program to travel in a square staring from coordinate 0,0 with each side being 2 inches)
N1 G20 ;Set machine units to inches
N2 G0 X0 Y0 ;move spindle to origin
N3 G0 X2 Y0 ;do the bottom of the square
N4 G0 X2 Y2 ;do the right side of the square
N5 G0 X0 Y2 ;do the top of the square
N6 G0 X0 Y0 ;do the left side finishing the square
A couple of things to note
Remember I said that line numbers are for the most part these days optional. Another thing to note is that the controller will remember where a given axis is at any time. For example in line N3 above we told the spindle to go to X2 Y0, well it was already at Y0 and the controller knows that. Because of this behaviour we could have left the Y0 off the line. Additionally, the controller remembers what motion mode it is in. In this example we set it a G0 (Rapid Motion) so the additional G0 commands could be left off. So the above program could have been written as:
(Program to travel in a square staring from coordinate 0,0 with each side being 2 inches)
G20 ;Set machine units to inches
G0 X0 Y0 ;move spindle to origin
X2 ;do the bottom of the square
Y2 ;do the right side of the square
X0 ;do the top of the square
Y0 ;do the left side finishing the square
That was a lot smaller wasn't it. We could have left the comments off to making it even smaller. Things like this are good to know to save you time when writing G-Code programs by hand. But it is worth noting that it is OK to put them in there like the first example. Streamlining code to save you time writing it has diminishing returns. Sure, you can write the code faster but the more you do like this the harder it becomes to understand. Do what your comfortable with and most importantly do whatever helps you understand the program a little better. More is usually better than less in this case even if it takes a little longer to type up!!!
In the next installment we will talk about linear feeds. This is a basic G-Code to cut straight lines.