Author Topic: NPT Thread Milling  (Read 5802 times)

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
NPT Thread Milling
« on: March 29, 2016, 02:39:00 PM »
Recently I was poling around the net and got distracted. I luckily ran across this little nugget on NPT thread milling with a single point cutter. Seems this guy wrote a general purpose macro to mill pipe threads with a single point cutter.

I started digging into it to understand how it works, and quickly found out that it has been too many moons since I've done serious programming. I couldn't figure out how the variables on the right side of the equation were getting evaluated, and there's more. I'm not fluent in G code programming, do I had made the assumption that a #number argument was actually a variable. Well, back to the internet to get help.

I seems the variables had to be passed into the macro call somehow, but how did it actually work. I found a very helpfull in a CNC Cookbook page about the G65 macro call, then a lot of lights turned on. Unfortunately, Mach does not support G65. Fortunately there is another way says the CNC Cook Book page, but again they were way too vague about it for mu G code skill level.

I think this would be a very helpful tool for us here on MEM, and If someone could help sort this out with me, I think it woukd be a good thing to post for all.

The link:

https://www.cncci.com/resources/tips%20form.htm

The code:

In main program:
G65 P9000 U0. W0. A.375 R5. E10. Z-.5 V18. F10.

  * O9000 (PIPE THREADS)
  * (U IS X LOCATION)
  * (W IS Y LOCATION)
  * (A=STARTING RADIUS)
  * (R=NUMBER OF MOVES PER CIRCLE)
  * (Z=DEPTH)
  * (E=NUMBER OF PASSES [thickness / pitch])
  * (V=THREADS PER INCH)
  * (F=FEED)
  * #3=0.0
  * #10=360 / #18
  * #109=#10
  * #110=1 / #22
  * #111=0.0625 / #22
  * #3=#18
  * G00 X#21 Y#23
  * G01 Z#26 F#9
  * #19=#1 + #21
  * G01 X#19 Y#23 F#9
  * N2 #26=#26 + #110 / #109
  * #24=COS[ #3 ] * #1
  * #25=SIN[ #3 ] * #1
  * #24=#24 + #21
  * #25=#25 + #23
  * G01 X#24 Y#25 Z#26 F#9
  * #3=#3 + #18
  * #1=#1 + #111 / #109
  * IF [ #3 LE 360.00000 * #8 ] GOTO2
  * G01 X#21 Y#23 F10.
  * G00 Z1. M09
  * M99

Any help would be appreciated.

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline kvom

  • Full Member
  • *****
  • Posts: 2649
Re: NPT Thread Milling
« Reply #1 on: March 29, 2016, 06:37:48 PM »
The g-code needed would be slightly different depending on whether you want to mill an external or internal NPT thread, but writing a program to generate a g-code fragment for a single thread would not be too difficult.  Other than the lead in and lead out, the code would be a long series of G1 straight moves in X, Y, and Z.

Assuming climb milling, the tool would turn about the work in a clockwise direction for external thread and anti-clockwise for internal.


Offline kvom

  • Full Member
  • *****
  • Posts: 2649
Re: NPT Thread Milling
« Reply #2 on: March 29, 2016, 08:42:13 PM »
I had been thinking about this earlier and decided to do a little coding in Java.  The program to generate the G01 moves is relatively compact.  My first cut program is attached.  Anyone who downloads the Java development/runtime could modify and run it as needed.  Or else convert the logic to his own preferred language.  I loaded the output into Mach3, and the toolpaths look reasonable.  Obviously the actual code needed will depend on modifying the parameters at the start of the program.

The Java code is pasted here as well.

import java.util.*;

public class ntp
{
    static boolean external = true;
    static int numSegments = 36;
    static double toolDiameter = 0.2;
    static int threadsPerInch = 27;
    static int numThreads = 7;
    static double stockDiameter = .3125;
   
    public static void main(String[] args)
    {
   // NPT angle is 1 degree, 47 minutes or 1+47/60 = 1.7833 degrees
   double nptAngle = Math.toRadians(1.783333);

   int degIncrement = 360/numSegments;
   double threadPitch = 1.0/threadsPerInch;
   double threadDepth = threadPitch*Math.cos(30.);

   // how much Z changes with each XY move
   double zIncrement = threadPitch/numSegments;

   // how much the toolpath radius changes with each XY move
   double rIncrement = zIncrement*Math.tan(nptAngle);

   // compute initial radius of the toolpath
   double r = stockDiameter/2. + toolDiameter/2. - threadDepth;

   // Assume Z0 is top of stock
   double z = 0.;

   // generate the G01 moves
   for (int t = 0; t<numThreads; t++)
   {
       double degrees = 0.;
       for (int i=0; i<=numSegments; i++)
       {
      degrees = i*degIncrement;
      double radians = Math.toRadians(degrees);
      z -= zIncrement;
      r += rIncrement;
      double x = Math.cos(radians)*r;
      double y = Math.sin(radians)*r;
      System.out.printf("G01 X%4.4f Y%4.4f Z%4.4f", x, y, z);
      System.out.println();
       }
   }
    }
}

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #3 on: March 29, 2016, 09:46:49 PM »
KVOM, thank you so much for the post. I understand your code much better than the G code I posted. Another thing
Mach doesn't is support conditional branches (i've been poking through the G Code), so the loop will have to be unwound.
Nasty.

I have a few questions about your code, I would like to use it.
First, is the Java runtime/dev environment a Microsoft thing that can just be downloaded?
And hopefully it comes with a manual.

Where does the output go? is the
System.out.printf("G01 X%4.4f Y%4.4f Z%4.4f", x, y, z);
 System.out.println();   opening a file? or does it go to the screen?

I assume your first variable (static boolean external = true;) is for external vs internal threads?

I don't remember where I got my information, but I remember pipe threads being 11 turns, why 7?

You assume the top of the stock is Z=0.0, so The part of the program we contribute is to do the tool moving
before and after the threads are cut??

Can't wait to try it, thanks again,
maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline kvom

  • Full Member
  • *****
  • Posts: 2649
Re: NPT Thread Milling
« Reply #4 on: March 30, 2016, 12:36:43 AM »
The Java runtime is downloadable from the Sun/Java website.  The JDE contains the compiler and runtime.

Once the JDE is installed, I start up a win command window and issue the following command via a .bat file:

path=c:\program files\java\jdk1.8.0_31\bin

The jdk dir name will depend on which version you've installed.

To compile the program:

javac ntp.java

To run the program

java ntp > gcode.nc

This produces the output file, which can have any name.  If omitted the output goes to the terminal window.

While NPT taps and dies may have 11+ teeth, the number that can be cut depends on the wall thickness of the tube among other things.  In the ideal world 7 threads yields a hard-tight fit, but for model brass pipe it makes more sense to have the pitch diameter of the first thread larger and cut fewer threads.  A PMR brass elbow wouldn't allow 7 threads.

The external boolean is just a placeholder for future use for internal threads.  For those a number of the variables would need to be computed differently.  Among the differences:

Start at bottom of the hole, so initial z value would be threadPitch*numThreads
degIncrement and zIncrement negative

The CAM plugin I use normally for straight threads uses a semicircular arc for external lead in and leadout.  A straight tangent leadin and out is likely just as good.

An option I considered for this is to have it calculate the feedrate given the SFM, chip load, and number of flutes.

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #5 on: March 30, 2016, 01:26:54 PM »
KVOM, thank you for the detailed information on getting Java up & running. It went a lot smoother than I was expecting.

But, at the expense of exposing how dense I really am, I need to ask a couple of dumb questions.

I created a threadmilling folder on c:. Then I did the .bat file and put it in the folder. A well, I put your code into a .java file
in the same folder. Also, I included the javac compile command in the .bat folder for convenience.

The .bat executed ok, java came up, but it gripes about not being able to find NPT.java.

I tried: putting the whole path on the command line, using the -sourcepath option, and adding a second path command to
the .bat file. It seems Windows can only handle 1 path at a time.

So, what obvious thing am I missing?

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline kvom

  • Full Member
  • *****
  • Posts: 2649
Re: NPT Thread Milling
« Reply #6 on: March 30, 2016, 01:36:06 PM »
Did you cd to the threadmilling folder with the java file?

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #7 on: March 30, 2016, 01:53:54 PM »
KVOM, that was an obvious one. Guess I am going to have to familiarize myself with the command window commands. Are they like unix? used that for years.


I get an error saying "class npt" is public, should be declared in a file npt.java.
Seems it is declared in your code, are we being case sensitive on the filename?

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #8 on: March 30, 2016, 02:55:19 PM »
KVOM, I got it to run, got the g code and ran it in Mach. Agree, the tool movements look ok, so a little more playing with the lead in & out then off to the machine.

I'll post the results, thanks for all the hand holding.  I took a java class about 15 years ago , but I never actually wrote a real program in it.

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline tvoght

  • Full Member
  • ****
  • Posts: 1003
  • Indiana
Re: NPT Thread Milling
« Reply #9 on: March 30, 2016, 04:03:51 PM »
I like to generate g-code programatically as kvom has done here in Java, and I use Python language tools I've developed to do most of my CNC work. While I'm not ready to share much of my work (it's not ready for public consumption), I can recommend a tool for doing this: gcmc - G-code Meta Compiler.

From the website:
Quote
Gcmc is a front-end language for generating G-code, SVG and DXF for CNC mills, lathes, laser cutters and other numerical controlled machines employing G-code, SVG or DXF. The language is a context-free grammar created to overcome the archaic format of G-code programming and aims to be more readable and understandable. Gcmc makes extensive use of vector mathematics to support the 3D nature of CNC machining.
An introduction document is available. The introduction goes through basic syntax and program outline and describes an example to create involute gears in under 200 lines of code (under 100 lines when excluding source code comments).

If this seems interesting to anyone, I highly recommend checking it out (it's free and open source). Check out the examples and screenshots on the web page!

http://www.vagrearg.org/content/gcmc

--Tim

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #10 on: March 30, 2016, 10:20:54 PM »
Tim, thanks for the info.

KVOM, Great progress this afternoon. I found a couple of things with the program, I know it was just a first cut, thought you might want to know.

I was getting the first thread Dia too large, did some poking and found the Math.Cos function for calculating the initial radius different from the one later in the program. I suppose it always wants radians.

I had to add a "-" in the angle calculation. The path was going CCW instead of CW.

Also, I think I want to start the cutting 1 thread above the top of the stock to get a nice helix starting at the top. It was leaving stock at the stock OD
before the first thread started. Working on that now.

Minor things, thanks so much for the code. It would have taken me a very long time to write that.

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Offline kvom

  • Full Member
  • *****
  • Posts: 2649
Re: NPT Thread Milling
« Reply #11 on: March 31, 2016, 12:13:13 AM »
Good catch.  I always forget positive rotation is counter clockwise.

On my thread mills. I zero the stock to its bottom, hence 1/2 thread pitch high.

As for the width at the top, I've had to adjust the tool diameter to make the thread correct on straight threads, since a given mill can be off by a few thou.  I just jeep adjusting until I get a thread that works.  Then my programs are good for that thread and threadmill combination.

The Math class trig functions all use radians.  If you want class documentation, the URL is https://docs.oracle.com/javase/8/docs/api/
« Last Edit: March 31, 2016, 12:16:20 AM by kvom »

Offline maury

  • Full Member
  • ****
  • Posts: 515
    • Lone Star Engine Works
Re: NPT Thread Milling
« Reply #12 on: April 01, 2016, 08:12:02 PM »
Well, after smashing my zeroing tool into my vise and having to reset the zero on all of my tools, I am finally back in business.

I have made a few mods to KVOM's program and added a header and trailer to the G code from one of my other programs. The way I
get the tool dia is to just measure it and edit it into the variable in the code. I did have to also measure the diameter of the first thread on
one of my other nipples and edit that into the stock diameter variable. Additionally, like KVOM said, that needed to be tweaked a bit to get
a nice fit.  THat's about it. There weren't many responses, so maybe nobody actually wants to do this. If there is interest in the code or if there
are other questions about it I will be happy to post the answers or the code. THe Java environment KVOM posted is about as easy to use as
any I have ever seen. Really no learning curve. My hhanks fro KVOMs' help on this.

THere is a nipple I made today.

maury
"The trouble with socialism is that you eventually run out of other people's money."... Margaret Thatcher

Online Vixen

  • Full Member
  • *****
  • Posts: 3096
  • Hampshire UK
Re: NPT Thread Milling
« Reply #13 on: April 01, 2016, 09:22:39 PM »
Nice clean thread Maury.
It is the journey that matters, not the destination

Sometimes, it can be a long and winding road

Offline tvoght

  • Full Member
  • ****
  • Posts: 1003
  • Indiana
Re: NPT Thread Milling
« Reply #14 on: April 01, 2016, 09:49:21 PM »
Well, after smashing my zeroing tool into my vise and having to reset the zero on all of my tools, I am finally back in business.

That's a bummer, and I've done it myself. I handled it a little differently though:

I touched off the new length of the zeroing tool and zeroed there, then touched off one of the other tools. The difference between the known tool's offset and its entry in the tool table gave me a new offset to plug into the tool table for the zeroing tool only.

--Tim

 

SimplePortal 2.3.5 © 2008-2012, SimplePortal