CNC - G-Code
I have to admit, things seem to be getting along at a pretty good clip. I am now at the point I need to figure out a few things so I can continually progress. So far I have taken an inside out approach to this project. Working out from the machine itself I come to the software that controls it. There are several available cnc software control packages out there. Some better some worse, but it is way too early in the game for me to commit to one. So, the next logical step for me is to look at what they all have in common. That common ground is the ability to parse G-Code and send the instructions as motor pulses and other related tasks to your machine. Well to make a long story short, I decided that the next best thing I could do for myself was to learn a little G-Code and try writing some for myself. I thought I would document some of that here for those folks who don't know the difference between G-Code and the Common Cold. HEHEHE (Sorry).
What is G-Code?
One definition I found on the Internet from 'Wikpedia' is: G-code is a common name for the programming language drive NC and CNC machine tools. It was developed by EIA in the early 1960s, a final revision was approved in February 1980 as RS274D.
Well, that was really helpful! G-Code it seems is a language used to program machines. The Machine it turns out can be anything really, like a lathe, a robot like a line picker, or a CNC Router. The language as it was initially written was supposed to follow a strict set of rules or standards, These standards were the RS274D. These standards defines what each code would do. For example a G01 would be a linear feed and that sort of thing. Now that we know, at least rudimentary, what G-Code is, what does it look like and how do you write it?
Before we get too deep into this discussion, I would like to point out a couple of tools you can get to help you along the way.
1st - You need something to edit your G-Code with. If you use Windows, you could use Notepad or Wordpad. With Un*x you could use VI or something. If you are on a Windows machine, there is a nice little free program called Auto Edit NC. You can download it from here.· For a tutorial on using Auto Edit NC you can go here.
2nd - You would like to have something to visualise your cuts with. There are a few programs that can do this for you. The one I have been using is called CutViewer and a free 30 day trial can be downloaded from here. Another is called CncSimulator which is free and can be downloaded from here.
OK, it has been a little while since I added anything to this page and thought I would continue the discussion a bit further. One thing I feel that I should point out, I am no expert in any of this, I am only passing on information as I understand it. I am very human, so the information I am passing on may be incorrect. Use it at your own risk.
OK, before we start our discussion about G Code we need to come to some common ground. The reason why I say this is because it seems that everything about these machines is relative to something else. So, if we are on the same page about a few things the information I am trying to convey will be much easier to understand.
Axis names and direction
I will be following the right hand rule for my axis naming. The right hand rule works like this. If you take your right hand with your palm facing up, point your index finger away from you and your thumb to the right. Now take your middle finger an point it up towards the ceiling. Now each of your fingers are pointing in the positive direction for the three axis we will be talking about. Your thumb represents the X axis, and moving this axis to the right is going in the positive direction. Your index finger is the Y axis, and when it moves away from you it is moving in the positive direction. Finally your middle finger is the Z axis and when it moves up it is going in the positive direction.
Relative and absolute positions
It seems we have two choices when we talk about positioning the tool. These choices are relative and absolute positioning. Absolute position means that the place we want to cut is listed, for example, when we were in school and learning linear algebra, we used a Cartesian plane (a X and Y axis with an origin at the center) When we solve the equation we would substitute a value for x and solve for Y. The result was a pair of numbers that we would plot on the plane. If the answer we had was (3,4) then we would go to the point where X=3 and Y=4 and plot it. This was an absolute position. The G Code that we write can work in the same matter. If I wanted to move my cutter to a position over the workpiece that was 2 inches to the right and three inches up from the origin, I would tell it something like G00 X2.0 Y3.0 and the machine will move to this position. If my next command was G00 X5.0 Y2.0 it would move to that position. The other way to move the machine is relative to its current location. For example, let's say that our cutter is at position x=10 y=5 and I told the machine to move like this G00 X-3 Y2 the new position of the cutter would be x=7, y=7. You see that the move is relative of the position in which the cutter is in. The move statement above says move the x axis to the left 3 units (X-3) and move the Y axis forward 2 units (Y2). This may sound a little confusing but a little play will set the concept pretty firm.
The G Codes that determines how things are moved are G90 and G91. Using G90 tells the machine that the coordinates we are giving are absolute. Using G91 tells the machine that the coordinates we are using are incremental (offset from the current position). We will use these commands when we start writing G Code.
Units of measure
We live in a world that is dictated by basically two different measuring systems. These are the Inch and Millimeter. Now I will admit the metric system is superior in a lot of ways to the English imperial system, but I am not going to discuss them here. Suffice it to say that I grew up on the English Imperial system so that is what I will be using. Now the G Code language can work with either system. The commands for these are G20 and G21. G20 sets the machine to use inches and G21 sets the machine to use millimeters.
Making things move
There are several ways to make the machine move, but we are going to learn G Code from the most basic form and add more as we go along. The two basic types of movements are fed movements (where we are moving the cutting bit into the work) and rapid moves, where we are just moving to a new location on the table to workpiece. So the first thing we are going to talk about is rapid moves. Rapid moves tell the machine to go to someplace as quickly as you can go. The G Code that performs this is G00 (G zero zero) So the code fragment G00 X2 Y3 tells the machine to go to position 2,3 as fast as you can go.
Putting it together
Putting what we have learned together so far will allow us to write a little code, but first let me side track a little. Code written in any kind of language can be hard to understand, especially if it was written by someone else or code that we wrote but has been a long time since we looked at it. To help remedy this, comments can be placed inside your code to help you remember what it was you were trying to do. Comments in G Code can be either put inside parenthesis '()' or you can use a semicolon ';'. The choice is yours but if you use '()' to make comments I think it would be better to put them on a line by themselves. Finally, each line f a program can have a line number. I do not think they are required but for discussion it can help to refer to a line number. Line numbers have the format L### where ### is the number of that particular line. Now that we have that out of the way, lets write a little code.
N001 G20 ;Set units to inches.
N002 G90 ;Set movement to absolute coordinates.
N003 G00 X0 Y0 ;Move the X and Y axis to zero.
N004 G00 X5 Y6.5 ;Move The X and Y axis to coordinate 5, 6.5
N005 G00 Z0.5 ;Move the Z axis to 0.5
The above program is pretty simple. The first line of the program (N001 G20) tells the cnc software that the commands we will be using will be moved in inches rather than millimeters. The next line (N002 G90) tells the software that we will be using absolute positioning rather and relative positioning. The last three lines moves things around. (G00 a rapid move) Line 3 (N003) tells the software to move both the X and Y axis to position 0,0. Line 4 tells the software to move the X and Y axis to position 5, 6.5 and line 5 tells the software to move the Z axis to 0.5.
Let's expand on what we know a little bit. So far we have been talking about G codes, there are other codes as well in the command toolbox other than G codes. The next I want to talk about is a M code. M Codes are miscellaneous functions. For example, when we have finished our program, we use an M code to tell the cnc software we are finished. The code that does that is M02 and M30. You can use either one but let me tell you what each means. M02 = end of program and M30 = end of tape. I guess years ago, NC machines ran from codes punched on paper tape. The M30 command would tell the NC control software that the tape had reached the end. (I think it also tells it to rewind it, I really don't know) For our discussion I will use M02, but I think you really can use either.
Another M code I want to talk about is M03 and M04. These commands tell the controlling software to turn the spindle on. (The spindle holds your cutting bit). M03 tells it to turn it on CW (clockwise) and M04 tells it to turn it on CCW (counter clockwise). Now my machine is not set up to let the software run the spindle. And even if it were the best I could do was turn it on in a CW direction, my router does not reverse. So if your CNC router is set up the same way, you will not need to use this command in your G Code, you only need to turn your router on and run your program, but for CNC mill to show you what is being cut, you will have to use this command in your file. That, brings us the question of what happens if we run the command and our cnc router does not support it? The simple answer is nothing at all. Under TurboCNC, if you run this command, it will pass over it and continue running.
Now to make things really fancy, some CNC machines allow you to specify the spindle speed as well as direction. This is done by using a S command. S stands for spindle speed. So if we wrote the command M03 S10000 the result would be the spindle would be turned on in a clockwise direction at a speed of 10,000 RPM.
Page 2 of 2