Quick Question on the G-Code - G3 and K, and 3D Milling

This forum is dedicated to those Hobbyists Interested in CNC machining in their home shops. (Digital Read Outs are also topical, as is CAD/CAM as it relates to CNC)

Moderator: Harold_V

User avatar
ctwo
Posts: 2522
Joined: Tue Mar 27, 2012 12:37 pm
Location: Silly Cone Valley

Quick Question on the G-Code - G3 and K, and 3D Milling

Post by ctwo » Tue Jan 19, 2016 1:17 pm

I'm getting a little presumptuous with my mill and set out to code my first test part (which there may be a better one that I have envisioned).

The part is simply a 1" aluminum bar stock, stood vertically on end. I will use a 1/4" cutter to mill 0.250 off the OD (0.125 DOC) with a G3 command, and I wanted to vary the Z to make two high points and two low points in an arc shape as the diameter was being milled.

When I add the K value to the arc segment command and import the code, Mach3 complains of K value in a 2D move. Now, I realize that G3 is listed as a 2D move and a K value can be specified, so how does one get 3D arc segments?

I have modified my idea to move Z linearly as shown in the diagram at center - and I have compensated the Z tool path by 12.5 thousands above the part to the peaks come to a point at the surface of the part rather than a flat like the valley.

After this OD is milled, I will return to 0,0,0, then move back to 0.125" off center of part, then start a 1/4" diameter plunge cut to bore out the center to 1/2" ID at the same depth of OD cut.

I am hoping this part will tell me how accurate the machine is by measuring how concentric and round the two circles are with respect to each other, and the peaks and depth of valleys of each diameter (and I just thought that I should adjust the Z height so that there is a 1/4" OD/ID proud that I can measure - diagram at right).

OK, so the question is really, to get 3D milling do you have to program each and every linear point/step (XYZ) to move?
Attachments
test-part.png
test-part.png (2.21 KiB) Viewed 2056 times
Standards are so important that everyone must have their own...
To measure is to know - Lord Kelvin
Disclaimer: I'm just a guy with a few machines...

User avatar
Rick
Posts: 410
Joined: Sat Jan 04, 2003 8:34 pm
Location: Stone Mountain, Ga.

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by Rick » Tue Jan 19, 2016 7:31 pm

Some cnc systems can do a helix, I am not familiar with Mach3 and if capable of that. 3d arcs and such are really splines which are pretty complex mathematically. All the Cam systems I have any experience with break these up into small linear segments, you typically control The amount of deviation The linear segment can have from the spline. The smaller The allowed deviation The more segments. The programming then becomes 3 axis linear coordinated moves. Hope this helps. Bill Shields may have more insight.
Rick

“We make a living by what we get, but we make a life by what we give." Sir Winston Leonard Spencer Churchill (1874-1965)
"Peace is that brief glorious moment in history when everybody stands around reloading". Unknown
Murphy's Law: " If it can go wrong it will"
Rick's axiom: "Murphy was entirely too optimistic"

RET
Posts: 679
Joined: Wed Jun 07, 2006 8:36 am
Location: Toronto, Canada

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by RET » Tue Jan 19, 2016 9:06 pm

Hi ctwo,

This might do enough of what you are looking for to give you the idea. It will give you climb milling which produces a better finish and is easier on the cutter.

%
(start and end your program with a % sign)
(G2 is a curve to the right)
(G3 is a curve to the left)
(G1 is a straight line)
(all of these can be either 2D or 3D)
(items in brackets like these are comments and are not read as code)
(For this code, Home is 0, 0, 0 and is at the center of the part)
(try to read the following code and understand what it does)
(f is the feed rate and is required on codes other than G0)
G40 G90
x 0 y 0 z 0
G0 x .5 y .5
z -.5
G1 y 0 f .1
G2 x 0 y -.5 z -.6 R .5
G2 x -.5 y 0 R .5
G2 x 0 y .5 z .5 R .5
G1 x .5 y .5 Z .5 f .2
G0 x0 y 0 z 0
%

I haven't tried this one, but code written like this works just fine with the Sherline software and if you vary the z axis at the same time, you will get a spiral either up or down. You don't have to put G at the start of each G 0 or G1 line, just when you are changing G code type. If you are going to have an arc of more than 180 degrees, you have to change the radius to a negative number.

Sherline runs on top of EMC2 and the Sherline front end is quite user friendly, MUCH better than the original EMC2 interface. Let me know how you make out.

Richard Trounce.

User avatar
ctwo
Posts: 2522
Joined: Tue Mar 27, 2012 12:37 pm
Location: Silly Cone Valley

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by ctwo » Tue Jan 19, 2016 11:08 pm

Thanks for the responses. I think a helix is a linear Z movement - just a straight line wrapped around a circle...

RET, I read that it is better to specify IJK, but R is regarded easier. I also realized that a 2D arc can be on any plane, so I could specify IJ, IK, or JK, etc., but I do not yet understand the math to get the kind of arc I wanted.

If I move in 3 dimensions and specify R, my question is, in which plane is R determined? If I specify to move from any point on a 3D sphere to any other point on that sphere, will the radius determine that sphere and the tool follow the shortest path on the surface of that sphere? After asking that, it seems the answer should be, of course!

I've seen some G-code with N number on each line. I guess these are mostly just line numbering and ignored; however, some of the line numbers I've seen had letter that did not make sense to me. For example: N16S3400M03
Standards are so important that everyone must have their own...
To measure is to know - Lord Kelvin
Disclaimer: I'm just a guy with a few machines...

User avatar
ctwo
Posts: 2522
Joined: Tue Mar 27, 2012 12:37 pm
Location: Silly Cone Valley

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by ctwo » Wed Jan 20, 2016 12:42 pm

I think I know what I need to do to get the shape I wanted. It was actually a sin curve that I wanted in Z around the circle.

The code below starts at 0 degrees and steps in 5 degree increments around the circle.

Code: Select all

(comment: x=rcos(t)+1	y=rsin(t)-1	z=(rsin(2t+pi/2)-1)+depth/2)
G1
X2.0000 Y-1.0000 Z0.0000
X1.9962 Y-0.9128 Z-0.0008
X1.9848 Y-0.8264 Z-0.0030
X1.9659 Y-0.7412 Z-0.0067
X1.9397 Y-0.6580 Z-0.0117
X1.9063 Y-0.5774 Z-0.0179
X1.8660 Y-0.5000 Z-0.0250
X1.8192 Y-0.4264 Z-0.0329
X1.7660 Y-0.3572 Z-0.0413
X1.7071 Y-0.2929 Z-0.0500
X1.6428 Y-0.2340 Z-0.0587
X1.5736 Y-0.1808 Z-0.0671
X1.5000 Y-0.1340 Z-0.0750
X1.4226 Y-0.0937 Z-0.0821
X1.3420 Y-0.0603 Z-0.0883
X1.2588 Y-0.0341 Z-0.0933
X1.1736 Y-0.0152 Z-0.0970
X1.0872 Y-0.0038 Z-0.0992
X1.0000 Y0.0000 Z-0.1000
X0.9128 Y-0.0038 Z-0.0992
X0.8264 Y-0.0152 Z-0.0970
X0.7412 Y-0.0341 Z-0.0933
X0.6580 Y-0.0603 Z-0.0883
X0.5774 Y-0.0937 Z-0.0821
X0.5000 Y-0.1340 Z-0.0750
X0.4264 Y-0.1808 Z-0.0671
X0.3572 Y-0.2340 Z-0.0587
X0.2929 Y-0.2929 Z-0.0500
X0.2340 Y-0.3572 Z-0.0413
X0.1808 Y-0.4264 Z-0.0329
X0.1340 Y-0.5000 Z-0.0250
X0.0937 Y-0.5774 Z-0.0179
X0.0603 Y-0.6580 Z-0.0117
X0.0341 Y-0.7412 Z-0.0067
X0.0152 Y-0.8264 Z-0.0030
X0.0038 Y-0.9128 Z-0.0008
X0.0000 Y-1.0000 Z0.0000
X0.0038 Y-1.0872 Z-0.0008
X0.0152 Y-1.1736 Z-0.0030
X0.0341 Y-1.2588 Z-0.0067
X0.0603 Y-1.3420 Z-0.0117
X0.0937 Y-1.4226 Z-0.0179
X0.1340 Y-1.5000 Z-0.0250
X0.1808 Y-1.5736 Z-0.0329
X0.2340 Y-1.6428 Z-0.0413
X0.2929 Y-1.7071 Z-0.0500
X0.3572 Y-1.7660 Z-0.0587
X0.4264 Y-1.8192 Z-0.0671
X0.5000 Y-1.8660 Z-0.0750
X0.5774 Y-1.9063 Z-0.0821
X0.6580 Y-1.9397 Z-0.0883
X0.7412 Y-1.9659 Z-0.0933
X0.8264 Y-1.9848 Z-0.0970
X0.9128 Y-1.9962 Z-0.0992
X1.0000 Y-2.0000 Z-0.1000
X1.0872 Y-1.9962 Z-0.0992
X1.1736 Y-1.9848 Z-0.0970
X1.2588 Y-1.9659 Z-0.0933
X1.3420 Y-1.9397 Z-0.0883
X1.4226 Y-1.9063 Z-0.0821
X1.5000 Y-1.8660 Z-0.0750
X1.5736 Y-1.8192 Z-0.0671
X1.6428 Y-1.7660 Z-0.0587
X1.7071 Y-1.7071 Z-0.0500
X1.7660 Y-1.6428 Z-0.0413
X1.8192 Y-1.5736 Z-0.0329
X1.8660 Y-1.5000 Z-0.0250
X1.9063 Y-1.4226 Z-0.0179
X1.9397 Y-1.3420 Z-0.0117
X1.9659 Y-1.2588 Z-0.0067
X1.9848 Y-1.1736 Z-0.0030
X1.9962 Y-1.0872 Z-0.0008
X2.0000 Y-1.0000 Z0.0000

Does that work?
Last edited by ctwo on Wed Jan 20, 2016 1:51 pm, edited 1 time in total.
Standards are so important that everyone must have their own...
To measure is to know - Lord Kelvin
Disclaimer: I'm just a guy with a few machines...

RET
Posts: 679
Joined: Wed Jun 07, 2006 8:36 am
Location: Toronto, Canada

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by RET » Wed Jan 20, 2016 1:50 pm

Hi ctwo,

I'm not sure what K is for but the Sherline software doesn't need line numbers although they can be useful when debugging programs. Your line number which ends in M03 is an M code that tells the motor to turn counterclockwise. I'm guessing, but I don't think a parallel port connection would have enough outputs to handle tool changing, motor speed & direction, etc. The basic 4 axis control does all I need and does it well. It is accurate to a couple of tenths.

In G code programming, the only conic figure allowed is a circle. If you want to do a sine curve or an ellipse, you have to break it up into little pieces with x y z coordinates for each intersection. The smaller the pieces, the more accurate the curve will be, but its still an approximation of the true curve. The table you have created should work (its breaking up the sine curve into little arcs) but I would enter 5 decimal places, not 4. Remember, the Sherline software at least microsteps so it can do tenths. The short answer to your question about the surface of a sphere is "No." The G code software just isn't that sophisticated. Remember, G code is something like 30 years old but what you can do with it is still very impressive in spite of its limitations.

At the start of each program, there should be a set of initiation codes; G 40 (cancel cutter diameter compensation), G 20 (inch system - G 21 is mm.) and G 90 (absolute distance mode). These make sure the program cancels any previous setup mode and will do what you want.

In addition, G 17 (xy plane selection - which is the default selection) can be useful. G 18 (xz plane selection) and G 19 (yz plane selection) are the other two choices. When you use G 2 or G 3, the arcs will be in the current plane selected. If you want to get really fancy or if you need to, you can change the plane selection between code commands.

You really need "Backplot" which is Sherline's 3D representation of the code as it runs. Perhaps Mach 3 may have an equivalent. Sherline I know and find easy to use, the others I'm not familiar with.

Writing your own code may seem difficult at the start, but once you get your head around it, it isn't really difficult. I've been doing this off & on for more than 10 years now. "Richard's latest project" shows what I'm doing at the moment with hand written code.

One essential is to have a CAD program that is very accurate so you can get the coordinates to the required degree of accuracy. Mine shows 8 decimal places, but it keeps track of 12. I find if I enter coordinates to 5 decimal places, the system works, less than that it gets picky.

Hope this helps.

Richard Trounce.
Last edited by RET on Wed Jan 20, 2016 1:59 pm, edited 1 time in total.

User avatar
ctwo
Posts: 2522
Joined: Tue Mar 27, 2012 12:37 pm
Location: Silly Cone Valley

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by ctwo » Wed Jan 20, 2016 1:59 pm

Thanks RET. I just edited my post above to include the XYZ axis on each line. It should work, but you would still need to add the initialization.

I find the conceptual aspect of G-code easy, but will certainly need to get through the execution phase.

You may have introduced my next topic, which is cutter compensation. I will illustrate that by also answering what is K, in just a few moments...
Standards are so important that everyone must have their own...
To measure is to know - Lord Kelvin
Disclaimer: I'm just a guy with a few machines...

RET
Posts: 679
Joined: Wed Jun 07, 2006 8:36 am
Location: Toronto, Canada

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by RET » Wed Jan 20, 2016 2:16 pm

Hi ctwo,

I just edited mine as well. To answer your potential question, I started off using cutter diameter compensation, but now I don't bother with it. It can be hard to live with because of its limitations.

I've found it preferable to just have the CAD program draw a half cutter diameter offset to the curve I want to make and use those coordinates. To use cutter diameter compensation, you have to have a straight line off the part which is long enough for the compensation to occur and then it doesn't like internal corners with radii that are less than half the diameter of the cutter. They have to be bigger. It is simpler not to use it because then you can file the inside of the corner to the desired shape afterward.

I don't know what kind of a CNC mill you are using, but I've found that any CNC machine has to be ABSOLUTELY rigid with no play of any kind anywhere. Even if you can't see of feel it, the slightest give makes a big difference in performance.

The machine I made works quite well (even better than I thought it would) but even with that, I'm limited to 3/8" dia. end mills. For 1/2" dia. end mills, I would need to increase the Thomson shaft diameter to 1 1/4" dia. and for 3/4" diameter end mills, it would have to be 1 1/2" dia. with corresponding increases in the ball screws.

I learn something every day.

Richard Trounce.

User avatar
ctwo
Posts: 2522
Joined: Tue Mar 27, 2012 12:37 pm
Location: Silly Cone Valley

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by ctwo » Wed Jan 20, 2016 2:46 pm

Hi RET, I will be using a Bridgeport BOSS3 CNC that is retrofitted with Mach3 or LinuxCNC. K is supposed to be used just like I and J, but in conjunction with the Z axis. I get that G2 and G3 are limited to 2D arcs, but the arc can be in any 2D plane defined by using a pair of XY and IJ, XZ and IK, or YZ and JK coordinate moves.

I have yet to find cad/cam programs, so am just starting to look at hand coding G-code using mathematical derivations of shapes. This means that if I do not use the machine tool compensation, I will need to calculate that myself. Below is a sin curve along the X-axis. The blue curve is the derived shape (the actual Z points in my G-code above - straightened out into a line), but the red curve is the actual approximate cut because of the cutter diameter (~1/4" square end mill, I did not actually determine the formula for the red curve, so it's just a close illustration of the effect). This happens because of the leading and trailing "edges" of the cutter are cutting before the very center point.
Attachments
sin.png
Standards are so important that everyone must have their own...
To measure is to know - Lord Kelvin
Disclaimer: I'm just a guy with a few machines...

RET
Posts: 679
Joined: Wed Jun 07, 2006 8:36 am
Location: Toronto, Canada

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by RET » Wed Jan 20, 2016 4:25 pm

Hi ctwo,

Yes, now I remember what K is. I use r instead, even although it is recommended that you use i, j & k. For me r works. It probably would still be a good idea to use G 17, 18 & 19 to specify the plane as well instead of just relying on i, j & k. Don't know if it would work without G 17, etc.

The Bridgeport would be a nice toy to use for CNC. I have a 1955 machine, but I figured it would be too expensive and time consuming to convert it. I have a 3 axis Sony readout on it so the machine is capable of some good work. I used it to make the little CNC mill.

Back when I bought the Sherline software, it cost $25.00 for the installation disk which had EMC2, the Sherline overlay and Ubuntu plus several other programs. To me, that was a bargain. If you have an old computer lying around, it might be worth considering installing the Sherline software on it to run the mill. Check with Sherline to see if their "front end" comes with it.

For my ordinary 2D CAD drawing, I use an old DOS copy of Anvil1000 which I run on a computer that uses Windows98. I'm pretty sure you can get a version that would run on newer Windows operating systems or even maybe on Linux. If money is no object, probably the best choice would be Mastercam 3D, but you are looking at thousands for a copy plus a very steep learning curve. For most of us (including me), that simply isn't an option, but it still can be nice to dream.

For 3D drawing, I use a program called Synergy from Weber Systems. The learning curve is steep, but it will generate "G" code to actually produce the part. If you want to see an example of what's possible, in the Live Steam section of this website, look at the thread "Union Pacific Big Boy in 3/4" scale." The shield on the front of the locomotive (4004) is produced by the code that Synergy created (over 900 lines worth!). There is no way that you could write that code by hand.

I hope this helps.

Richard Trounce.

hwboivin3
Posts: 544
Joined: Thu May 03, 2007 3:14 pm
Location: OHIO

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by hwboivin3 » Wed Jan 20, 2016 8:50 pm

G17 is X,Y plane and milling an arc, you will use I and J along with X and Y......and can use Z for depths
G18 is a X,Z plane Milling an arc using I and K along with X and Z.
G19 is a Y,Z plane Milling an arc using J and K along with Y and Z.

Using K in G17 will not work.......as far as I know.


this is a drawing that I keep on hand to remember which way is which. I sometimes forget the if i need G3 or G2 on the G18 plane, because it's viewed from the back (Y+) side......which I'm on the Y- side when looking at the machine.
Attachments
wp186398b3_06.png
wp186398b3_06.png (4.56 KiB) Viewed 1991 times

DavidF
Posts: 188
Joined: Wed May 14, 2014 12:28 pm
Location: Delaware

Re: Quick Question on the G-Code - G3 and K, and 3D Milling

Post by DavidF » Wed Jan 20, 2016 9:14 pm

I took the easy way out for starters and bought a cam program, but now that i have started getting more into it I would like to learn more about g code manual programming, so ill be following along :)

Post Reply