Saturday, January 17, 2026

Sharing Common Code

For my machining projects, I have written lots of C programs to create gCode to operate a CNC mill. These are feature-specific: mkslot cuts slots from specified start x, y to end x, y for specified depth; mkhexagon, mkrectangle for cutting slots that will drop the rectangle out the bottom.   My biggest nemesis have been mkcircle, to cut a circle of specified radius centered at x, y and mkcirclepocket which cuts the circle and gouges out the center. 

I made use of ChatGPT to write C code to do a series of inward circles for the pocket. Because I started from scratch on mkcirclepocket instead of expanding the capabilities of mkcircle, I ended up with a slightly different interface and one of them worked more accurately than the other.  Mkcircle takes a start x, y for the left side of the circle and end x, y for the right side of the circle.  Mkcirclepocket accepts a radius and x, y for the center of the circle.  

I only used mkcircle with a 1/8" diameter endmill and thus did not immediately notice that it was not terribly accurate on diameter with larger diameter mills.  This became enough of a nuisance that today I modified mkcirclepocket to accept a -pocket parameter so that without, it cuts circles and with it, cuts you know, pockets. 

Whenever two programs produce similar results, you should look for ways to make the difference an option.

No comments:

Post a Comment