Basics of CNC Part 1 - G Code: Side Note
Basics of CNC Part 1 - G Code: Side Note
A program starts with various code to set the machine up Inch/Metric, Absolute/Incremental coordinate system, etc. This varies a bit from machine to machine. After that, the fun starts (the machine starts moving). The main thing you should know about G-code is that each line is an instruction that pulls the machine from [wherever it is] to a new coordinate. Along with the new position, you can describe the shape of the path (linear or in an arc) and the speed at which to travel a.k.a. feed rate. That's pretty much it. Of course that's never really true though is it? The fact is, there are tons of things and several ways you can tell a CNC machine to do something and it can become fairly complicated if you want it to. Luckily, most things can be accomplished with pretty simple code. Here are the basic commands in a nutshell. G0 (G zero, not go) This is the command for rapid position. With this command, the machine motors will travel as fast as they are set up to move toward whatever coordinate you attach to it. For example, if the machine is sitting at coordinate X3.0 Y2.0 Z-2.5 and you command: G0 X0 Y0 Z0 it will simply move as fast as the motors are allowed to spin, stopping at X0 Y0 Z0. This is a pretty uninteresting command but it will be used all the time. G1 Linear interpolation. This command will coordinate the motors to move the machine in such a way that it travels from [wherever it was] in a straight-line path to the new position. You also need to tell it how fast to travel along this path. i.e. machine is sitting at X0 Y0 Z0 and you command: G1 X4.0 Y4.0 F10.0 The machine will travel in a straight line at 45 toward the point (4,4) at a speed of 10 inches per minute. Note that there is no Z coordinate included here. That means that only X and Y will change and Z will stay wherever it was before the new command. No new info = no position change. Side note: Also note that when I specified zero above, I just wrote 0 with no decimal point. All controls understand what zero is and a decimal is never necessary. However, some controls interpret values differently some read 4 to mean .0004 and some to mean 4.0 for this reason, I always write a decimal point when specifying values other than zero so that it can be understood by any machine. It's just good practice. You can leave off the trailing zero (I usually do). Machine controls interpret 4. and 4.0 to mean the same thing, but just to be clearer I'll leave the trailing zero on for this presentation (unless I forget). So G0 and G1 are the two most basic motion commands.
Now to get a little more meaty... You often need to mill arcs and circles. These are covered by the next two commands. G2 Clockwise circular/helical interpolation G3 Counter-Clockwise circular/helical interpolation These commands will coordinate the motors of the machine to move in a smooth circular path. There are a couple different ways to do it. 1) You can just tell it a radius and it will figure out the rest, but it is limited to arcs of 180 or less. 2) You can tell it the arc's center point and it will do any arc between 0 and 360 For the following examples, we will assume the machine is sitting/starting at the point X0 Y0 Z0 Radius method: You specify the radius and the machine figures the rest. G2 X3.0 Y0 R1.5 F20.0 will travel in a CW semi-circle and end at (3,0) at 20 IPM G2 X3.0 Y0 R3.0 F20.0 will travel in a CW arc shallower than the one above and end at (3,0) G3 X4.0 Y0 R2.0 F20.0 CCW semi-circle ending at (4,0) trick question - what will happen with the following command? G3 X4.0 Y0 R1.0 F20.0 Center Point method: You specify the arc's center point and the machine figures out the radius. This is a little tricky at first, but you can get the hang of it pretty quickly. With this method, you command the end point as usual, but the center point is an incremental distance from the starting point. This is ultimately simpler than dealing with a center point's absolute position, but since it's something new to wrap your head around, it may be a little tricky until you get used to it. In this case, the incremental distances are described with the letters I, J and K, which correspond respectively to X, Y and Z. G2 X3.0 Y0 I1.5 J0 F20.0 will travel in a CW semi-circle and end at (3,0) at 20 IPM G2 X3.0 Y0 I1.5 J-1.0 F20.0 will travel in a CW arc shallower than the one above and end at (3,0) G3 X4.0 Y0 I2.0 J0 F20.0 CCW semi-circle ending at (4,0) Note that J can be omitted here. Either I or J can always be omitted if its value is zero. However, omitting both of them will error the machine. You can also omit the Y value here as well as the previous examples since its value does not change, i.e. starts and ends at zero. So the exact same command can be written: G3 X4.0 I2.0 F20.0 (again, assuming the machine is starting out at X0 Y0 Z0) Until you get used to it, it is sometimes easier to understand what's going on in your code if you go ahead and specify all values though whether they have to be included or not. Try to draw a sketch of what paththis code will make (again, assuming machine starts at (0,0,0): G3 X4.0 Y4.0 I2.0 J2.0 F15.0 How about this one: G2 X0 Y0 I2.0 F15.0
Helical Interpolation: This is simply a circular interpolation with the addition of some Z motion. The machine will coordinate the motors to move X and Y in an arc, and Z will move steadily toward its end point all along this arc path. Pretty easy just add a Z value (different than the starting Z point) and it follows a helical path. G2 X0 Y0 Z -.5 I2.0 F15.0 This will be the same as the last test example on the previous page, but Z will move down 1/2 by the time it reaches the end point. Think of it maybe as one revolution of a bolt thread. In fact, with the correct cutter, you can use helical interpolation to cut threads. Maybe you need a large threaded hole of a strange size in a plate to fit a camera lens to it. Lets walk through this interesting example. First, use a standard end mill to cut a basic hole, then use a 60 V-shaped cutter to make the threads. The actual dimensions get technical and you can look it up in a book or on the web. It not only involves trigonometry, but also clearance issues and thread crest widths and all sorts of other thread form geometry. But to make it simple, lets just assume we need to make a threaded hole for an antique camera lens which is a little larger than 2 diameter and has really fine threads. Using a 1/2 diameter end mill, we can command a few lines of helical interpolation to rough out the hole. Lets assume the hole needs to be 2 diameter and the center is at (0,0). Note that because the cutter is 1/2 diameter, the circular path needed to make a 2 hole will be 1.5. Lets assume our plate is 1/4 thick. Bring the cutter close to the work. Move it to the edge of the hole we will cut. Use circular interpolation to cut the hole. Pull back out. G0 X0 Y0 Z.1 (assume the top of the plate is Z0 standard practice) G0 X-.75 Y0 (position to the edge of our hole) G1 Z0 F5.0 (safely feed to just be touching the plate) G3 X-.75 Y0 Z-0.1 I.75 F10.0 G3 X-.75 Y0 Z-0.2 I.75 F10.0 series of helix paths to cut out a circle G3 X-.75 Y0 Z-0.3 I.75 F10.0 G0 X0 Y0 G0 Z1.0 There! Now you have cut out a 2 diameter circle from the plate. Cake.
Now change the tool to a 60 fly cutter. Cheap solution. Lets assume it swings a diameter of 1 inch. We need it to cut a little into the sides of the hole to make threads (lets say .025 deep) and our thread's pitch can be .040 (or 25 threads per inch). Our finished diameter will be 2 + 2X the thread depth of .025, so 2.050 is our target diameter. Our cutter is 1 diameter, so the path it needs to follow is a circle of 1.050 diameter = .525 radius. Our code will be very similar to what we used to cut the hole. Remember our thread pitch is 25 TPI or .040 per revolution. G0 X0 Y0 Z.1 G0 X-.525 Y0 G1 Z.04 F5.0 (feed to a point a little above our plate) G3 X-.525 Y0 Z0 I.525 F10.0 G3 X-.525 Y0 Z-0.04 I.525 F10.0 G3 X-.525 Y0 Z-0.08 I.525 F10.0 G3 X-.525 Y0 Z-0.12 I.525 F10.0 G3 X-.525 Y0 Z-0.16 I.525 F10.0 series of helix paths to cut out our threads G3 X-.525 Y0 Z-0.20 I.525 F10.0 G3 X-.525 Y0 Z-0.24 I.525 F10.0 G3 X-.525 Y0 Z-0.28 I.525 F10.0 (last move to go a little past the bottom of the 1/4 plate) G0 X0 Y0 G0 Z1.0 There ya go. Used two cheap tools to cut a weird threaded hole that you'd have to pay probably $300 to have some shop take a month to make a tap for you to cut it. Looks much better than epoxy and duct tape too. Well that's enough G-code for now. Most anybody in the DIY crowd will be using Mach3 for their control. You can download their user manual and see instructional videos on their site for lots more information. http://www.machsupport.com/documentation.php http://www.machsupport.com/videos.php We're just scratching the surface. Hopefully more fun to come in the future.