What's new
What's new

# G32 THREADING PYTHON CODE FEEDBACK

#### maguilera

##### Plastic
I wrote this rudimentary code in python to automate the creation of a threading program using G32 with an infeed angle. I used the constant volume formula provided by the Kennametal catalogue to calculate the DOC of each pass.
The shift in Z for each pass is calculated by multiplying the DOC of Nth pass by the tangent of the infeed angle.
I would really appreciate the community feedback on this.

Cheers.

This is the python code:

import numpy as np
rsc = input('External Thread (E) or Internal Thread (I):')
dn = float(input('Thread Diameter (mm): '))
feed = float(input('Thread Pitch (mm): '))
zi = float(input('Z Initial Position (mm): '))
zf = float(input('Z Final Position (mm): '))
nap = int(input('Number of Passes: '))
alpha = 30 # INFEED ANGLE
ap = 2 # CLEARANCE IN X
if rsc.upper() == "E":
xap = dn + ap # APPROACH DIAMETER
ap = 0.61343 * feed # THREAD DEPTH
ap1 = (ap / (nap - 1) ** .5) * (0.3 ** 0.5) # DOC FIRST PASS
x1 = round(dn - 2 * ap1, 3) # DIAMETER OF FIRST PASS
print('G0 X' + str(xap), 'Z' + str(zi))
print('X' + str(x1))
print('G32 Z-' + str(zf), 'F' + str(feed))
print('G0 X' + str(xap))
print('Z' + str(zi))
i = 2
while i <= nap:
apx = (ap / (nap - 1) ** .5) * ((i - 1) ** 0.5)
xp = round(dn - 2 * apx, 3)
zs = (apx - ap1) * np.tan(np.deg2rad(alpha)) # SHIFT ON Z
z = round(zi + zs, 3) # Z SHIFTED FROM INITIAL Z
i = i + 1
print('G0 X' + str(xp), 'Z' + str(z))
print('G32 Z-' + str(zf), 'F' + str(feed))
print('G0 X' + str(xap))
print('Z' + str(zi))
else:
dh = dn-feed # HOLE DIAMETER
xap = dh - ap # APPROACH DIAMETER
ap = 0.54127 * feed # THREAD DEPTH
ap1 = (ap / (nap - 1) ** .5) * (0.3 ** 0.5) # DOC FIRST PASS
x1 = round(dh + 2 * ap1, 3) # DIAMETER OF FIRST PASS
print('G0 X' + str(xap), 'Z' + str(zi))
print('X' + str(x1))
print('G32 Z-' + str(zf), 'F' + str(feed))
print('G0 X' + str(xap))
print('Z' + str(zi))
i = 2
while i <= nap:
apx = (ap / (nap - 1) ** .5) * ((i - 1) ** 0.5)
xp = round(dh + 2 * apx, 3)
zs = (apx - ap1) * np.tan(np.deg2rad(alpha)) # SHIFT ON Z
z = round(zi + zs, 3) # Z SHIFTED FROM INITIAL Z
i = i + 1
print('G0 X' + str(xp), 'Z' + str(z))
print('G32 Z-' + str(zf), 'F' + str(feed))
print('G0 X' + str(xap))
print('Z' + str(zi))

This is what it spits out for cutting with 6 passes a M25x1.5 external thread with an infeed angle of 30 degrees.
G0 X27.0 Z5.0
X24.549
G32 Z-10.0 F1.5
G0 X27.0
Z5.0
G0 X24.177 Z5.107
G32 Z-10.0 F1.5
G0 X27.0
Z5.0
G0 X23.836 Z5.206
G32 Z-10.0 F1.5
G0 X27.0
Z5.0
G0 X23.575 Z5.281
G32 Z-10.0 F1.5
G0 X27.0
Z5.0
G0 X23.354 Z5.345
G32 Z-10.0 F1.5
G0 X27.0
Z5.0
G0 X23.16 Z5.401
G32 Z-10.0 F1.5
G0 X27.0
Z5.0

#### sinha

##### Titanium
I appreciate your efforts, but why not simply use G76?

#### maguilera

##### Plastic
I indeed use G76 as it offers much more flexibility, but recently I’ve been using python more and more to automate stuff and thought this would be a fun project.

Cheers

#### angelw

##### Diamond
Yes, I agree with Sinha,
I indeed use G76 as it offers much more flexibility, but recently I’ve been using python more and more to automate stuff and thought this would be a fun project.

Cheers
If for your own enjoyment, or education then fine, but otherwise I would agree with Sinha, in that it's implied that the project should be an improvement on what exists.

Most machines today are supplied with some sort of User Macro feature and so, whatever you may come up with in python, or other language can be achieved using User Macro. Fine Pitch Threads, such as in your example are OK to do long hand, but get into coarse pitch threads with corresponding deep thread form, the amount of code output becomes significant. In User Macro it makes no difference.

One improvement that can be made for G76, is being able to Index the Thread Start for a Multi-lead Thread without having to change the Z Start coordinate; this can be rather important when the amount of Z Shift of the Z Start point may be limited by a Tail-stock. The G76 Threading Cycle for most controls that use the two Block Fiormat of G76, isn't able to index the Thread Start other than by shifting the Z Start location. Somewhere on this Forum I posted a User Macro program that mimic the function of the standard, two Block G76 Cycle, but is able to index for Multi-lead Threads without shifting the Z Start and can use Thread Form included angel data from Zero to 120 degrees the same as the one Block G76 Format.

Regards,

Bill

#### maguilera

##### Plastic
If for your own enjoyment, or education then fine, but otherwise I would agree with Sinha, in that it's implied that the project should be an improvement on what exists.
Absolutely, this is just for educational purposes.
Most machines today are supplied with some sort of User Macro feature and so, whatever you may come up with in python, or other language can be achieved using User Macro.
I’ve read a little about user macros but never delved deep into it because I’m not sure if my machine has the macro capabilities. There’s any way for me to find out at the machine if it accepts user macro? The controller is Fanuc 21i-T.

I’ve been using Python because it is such a powerful tool and it is open source. I also did a script somewhat similar to this for grooving, where it decides if it will be cut by groove and turn or multiple pass, based on groove width and depth.
One improvement that can be made for G76, is being able to Index the Thread Start for a Multi-lead Thread without having to change the Z Start coordinate; this can be rather important when the amount of Z Shift of the Z Start point may be limited by a Tail-stock. The G76 Threading Cycle for most controls that use the two Block Fiormat of G76, isn't able to index the Thread Start other than by shifting the Z Start location. Somewhere on this Forum I posted a User Macro program that mimic the function of the standard, two Block G76 Cycle, but is able to index for Multi-lead Threads without shifting the Z Start and can use Thread Form included angel data from Zero to 120 degrees the same as the one Block G76 Format.
That’s a cool improvement; I’ll definitely search for this and try to implement it.

Thanks for the feedback!

Cheers

#### angelw

##### Diamond
I’ve read a little about user macros but never delved deep into it because I’m not sure if my machine has the macro capabilities. There’s any way for me to find out at the machine if it accepts user macro? The controller is Fanuc 21i-T.
Hello maguilera,
I would be very surprised if your machine doesn't have the User Macro Option. If your Control has User Macro, you will be able to navigate, via the function buttons of the control, to a series of Macro Variable pages. Alternatively, either in MDI or short test program in memory, execute the following:

#1=100.0 (or any value)

If the Block executes without alarm, then your control has the User Macro function. After execution of the above Block, you can navigate to the Macro Variable pages to view the result. If you execute the above Block in a short, test program, M30 at the end of the program, if allowed to execute, will set #1 (a Local Variable) back to Vacant (Null) and you may think that the Block didn't execute correctly, if you view the Macro Variable and see no number. Accordingly, if you test the Block in a Program, execute the Program in Single Block and view the result after execution of the Macro Block, but before the M30 is executed. You can also put M00 after the Macro Block and before the M30 to allow you to view the result.

Regards,

Bill

#### maguilera

##### Plastic
Thanks for the explanation, Bill. I’ll definitely check this out.
Do you have any recommendation on literature for Fanuc macro user?

Cheers.

#### johnmontrose

##### Aluminum
I would really appreciate the community feedback on this.

Why are you using numpy? Is the standard math library no good?

Right at the beginning, you take input into rsc but you only check it is 'E'. I could put anything into that input, so sanity checking the inputs is a must.

I could try to cut an M2 x 6.0 pitch thread and it would not complain.

Better to be very strict on the user and terminate the program on unreasonable input than to continue and produce garbage output.

#### maguilera

##### Plastic
Why are you using numpy? Is the standard math library no good?
For this particular project the standard math library would be enough, but for more complex projects numpy becomes essential, and because of that I just use numpy as a default for all my python projects.
Right at the beginning, you take input into rsc but you only check it is 'E'. I could put anything into that input, so sanity checking the inputs is a must.

I could try to cut an M2 x 6.0 pitch thread and it would not complain.

Better to be very strict on the user and terminate the program on unreasonable input than to continue and produce garbage output.
Absolutely, this is a very rudimentary script and sanity checks would be one of many improvements that could be done to this project.
Thanks for the feedback johnmontrose, and thanks goooose for the info of the book!

Cheers.

Replies
1
Views
2K
Replies
6
Views
2K