John Haine | 09/09/2021 17:13:21 |
5563 forum posts 322 photos |
Thanks Dave. Does "print" put a newline after each instance or should that be added? And I notice you are using a "w" method, would it be better to use "a"? |
Dave S | 09/09/2021 17:17:23 |
433 forum posts 95 photos | w is write, and will trash an existing file of the same name, a is append - so open if the file is there and write to the end, otherwise create a new one. It makes little difference if you are creating the whole file at once. My earlier example was for a rolling log file, where you actually want to keep the existing data - just the handiest example I had around. edited to say I think in Python there is an implicit newline in a print
Dave
Edited By Dave S on 09/09/2021 17:18:11 |
John Haine | 09/09/2021 19:21:13 |
5563 forum posts 322 photos | Thanks! |
SillyOldDuffer | 09/09/2021 19:33:11 |
10668 forum posts 2415 photos | By default print() terminates with end-of-line It can be turned off, or changed to something else, by adding an end parameter: print('hello', end = "" ) # end-of line is nothing Dave pesky smiley! Edited By SillyOldDuffer on 09/09/2021 19:34:28 |
John Haine | 12/09/2021 09:49:50 |
5563 forum posts 322 photos | Well, thanks to SoD and Dave S I have written a little Python wizard to generate g-code for knurling for any length. The first try for simplicity as shown above I just cut a full lead length, which for a diamond knurl is pi x diameter of stock. Even for 12.7mm this is nearly 40mm long which is much too long for practical purposes - larger diameter knurls would be very wasteful. I realised though that if one made a knurl with an integer number of diamonds in the axial direction you can cut each "tooth" with just a zig-zag motion where the work rotates for say N teeth axially while it moves N diamonds axially, then continues to rotate for another N teeth while it moves back N diamonds. What one would then like to do is just cut another zig-zag an carry on until one has gone right round the blank, without having to make a pure rotational move to index to the next tooth. With a bit of playing around for this to happen the number of teeth and number of diamonds have to be "relatively prime", that is have no common factor other than 1. Or, one can just make the number of teeth "M" prime, in which case any number of axial diamonds up to (M-1) can be cut just with repeated zig-zag moves. I think this is the easiest approach since there isn't a particular reason to have any specific number as long as the knurl looks and feels right. For my previous example M=37 is a nearby prime to 42 - I wanted something rather smaller than 42 to get a coarser pattern, 37 gives about a 1.08mm pitch. Here's the Python code. import math # Now do geometry calcs ******ing Smileys! They should be closing brackets. Thonny is free and comes as the built-in tool on the R-Pi, but I also use it on my PC. When you run it it asks for various parameters of the cut as listed, and the name of the file you want to put the results in (which will be in the same folder as the code). To get a text file include .txt on the end of the name. Note that the machine should be referenced so that X=0 is at the start or the knurl and Y=0 is on the A-axis. Z=0 for the tool tip is the machine table.
Edited By John Haine on 12/09/2021 09:50:51 Edited By John Haine on 12/09/2021 09:57:17 Edited By John Haine on 12/09/2021 09:58:32 |
John Haine | 12/09/2021 09:55:15 |
5563 forum posts 322 photos | Here's a g-code example for 12.7mm diameter and 37 teeth. G0 G49 G40 G17 G80 G50 G90 I've taken out a big block of lines in the middle to save space. Note that the code puts the "zig-zag" number in as a comment so you can keep track of what's happening on the Mach3 screen. For this example it counts 0 to 36, or 37 zigzags. (I dunno why the smileys don't appear here...) This is about as short as the code can be without using a subroutine but can be used on a controller that doesn't support these. Edited By John Haine on 12/09/2021 09:59:12 Edited By John Haine on 12/09/2021 10:02:06 |
John Haine | 12/09/2021 10:03:17 |
5563 forum posts 322 photos |
And the finished result. |
Dave S | 12/09/2021 16:55:44 |
433 forum posts 95 photos | Looks good. Only real difference I can think of is knurling doesn’t “sink” below the original surface - which may or may not matter. At least with this you can guarantee a good knurl and not worry about it not tracking in. Got to love the forum smilies in code Dave |
SillyOldDuffer | 13/09/2021 14:31:05 |
10668 forum posts 2415 photos | Posted by Dave S on 12/09/2021 16:55:44:
... Got to love the forum smilies in code Dave Here's John's code in glorious technicolor, smiley's exterminated: Couple of small points. May not make any difference but I would use: myFile = open( name, 'w' ) rather than: myFile = open(name,'a' ) 'a' opens the file 'name' in append mode, and if name already exists, the new G-Code will be tacked on the end, which probably isn't wanted. The 'w' open() always creates an empty new file so there's no danger of confusing the machine tool by feeding it two or more blobs of g-code in the same file. Second, again not making any practical difference, John uses format strings mildly inappropriately in his print statements. print(f"G91", file = myFile) This is unnecessary: print("G91", file=myFile) f"strings" useful for another purpose: print(f"G01 Z", -1-Z_feed, " F", Z_feedrate, file = myFile) can be written: print(f"G01 Z {-1-Z_feed} F {Z_feedrate}", file = myFile) The second form is particularly neat because formatting, not required in John's program, can be applied to whatever is between the curly brackets. Dave PS I like Thonny too.
Edited By SillyOldDuffer on 13/09/2021 14:31:45 |
John Haine | 13/09/2021 14:39:56 |
5563 forum posts 322 photos | Ah, so that's what the f means! Noted, thanks! I thought it had something to do with "file"... I thought, since the programme asks for a filename, I might as well use append mode. If write mode is used on an existing file, does it overwrite everything in the file? If not one still has the risk of having spurious stuff from a previous run. One thing I haven't figured out - g code contains statements line "G01 X5" to move the X axis 5mm. My code generates "G01 X 5" with a space between the X and the digit. It doesn't seem to matter form Mach3 but might for other controllers, is there a way to suppress the space please? Maybe using that formatting trick? |
John Haine | 13/09/2021 15:05:26 |
5563 forum posts 322 photos | Aha!! I see, it does just what I want, thanks for the suggestions Dave. |
John Haine | 15/09/2021 11:57:20 |
5563 forum posts 322 photos | Latest version with above changes. Also asks for the width of any "flat" on the end of the cutter (reducing feed appropriaetly, and corrects the formula for the depth as I initially misunderstood the geometry. Posted pic to avoid the smileys, but happy to provide file on request, or download from **LINK**
|
Please login to post a reply.
Want the latest issue of Model Engineer or Model Engineers' Workshop? Use our magazine locator links to find your nearest stockist!
Sign up to our newsletter and get a free digital issue.
You can unsubscribe at anytime. View our privacy policy at www.mortons.co.uk/privacy
You can contact us by phone, mail or email about the magazines including becoming a contributor, submitting reader's letters or making queries about articles. You can also get in touch about this website, advertising or other general issues.
Click THIS LINK for full contact details.
For subscription issues please see THIS LINK.