If you own a 3D printer, CNC router, or basically anything else that makes coordinated movements with a bunch of stepper motors, chances are good that it speaks G-code. Do you?
If you were a CNC machinist back in the 1980’s, chances are very good that you’d be fluent in the language, and maybe even a couple different machines’ specialized dialects. But higher level abstractions pretty quickly took over the CAM landscape, and knowing how to navigate GUIs and do CAD became more relevant than knowing how to move the machine around by typing.
Strangely enough, I learned G-code in 2010, as the RepRap Darwin that my hackerspace needed some human wranglers. If you want to print out a 3D design today, you have a wealth of convenient slicers that’ll turn abstract geometry into G-code, but back in the day, all we had was a mess of Python scripts. Given the state of things, it was worth learning a little G-code, because even if you just wanted to print something out, it was far from plug-and-play.
For instance, it was far easier to just edit the M104 value than to change the temperature and re-slice the whole thing, which could take an appreciable amount of time back then. Honestly, we were all working on the printers as much as we were printing. Knowing how to whip up some quick bed-levelling test scripts and/or demo objects in G-code was just plain handy. And of course the people writing or tweaking the slicers had to know how to talk directly to the machine.
Even today, I think it’s useful to be able to speak to the machine in its native language. Case in point: the el-quicko pen-plotter I whipped together two weekends ago was actually to play around with Logo, the turtle language, with my son. It didn’t take me more than an hour or so to whip up a trivial Logo-alike (in Python) for the CNC: pen-up, pen-down, forward, turn, repeat, and subroutine definitions. Translating this all to machine moves was actually super simple, and we had a great time live-drawing with the machine.
So if you want to code for your machine, you’ll need to speak its language. A slicer is great for the one thing it does – turning an STL into G-code, but if you want to do anything a little more bespoke, you should learn G-code. And if you’ve got a 3D printer kicking around, certainly if it runs Marlin or similar firmware, you’ve got the ideal platform for exploration.
Does anyone else still play with G-code?
16 thoughts on “Speak To The Machine”
I learned gcode in a proper tool n die shop about thirty years ago. Not working with those cnc machines cutting steel now, it is so much fun having a 3d printer and being able to read the code my slicer provides!
I, and many others with CNC mills still use simple GCODE commands for simple things. I took the hand wheels off of my mill and so if I just need to do some straight cuts on a part, it is often simpler to enter GCODE at the console than to fire up the CAM system. Second, I have also written a couple of utility programs in GCODE for common repetitive tasks. The most common is when I use a facing mill to clean up a surface. I can set a variable to the depth of cut I want and another to the total depth I want to cut and Bobs your Uncle. When I need to do curves or cutter offsets become important, I generally goto the CAM program.
I’ve taken this route too. Because GRBL doesn’t let you define G-code macros, I ended up writing a Python routine that simply sends one command after the next, and defines a bunch of simple operations like rectangles and circles (inner-diameter and outer-diameter, so I don’t have to do the cutter compensation) and also a simple surfacing routine.
So I run it, type “do home”, jog the head with G-code, and then “do probez”, etc. A few canned routines buys a lot. Wrote a simple sender program as well that optionally runs the head around the bounding box at a clearance height. That helps a lot when it gets tight.
But then, that’s really my point — knowing a tiny bit of G-code makes it super easy to automate simple things like that. If I only knew CAD/CAM, I would think it was black magic.
Of course, when it gets complicated, then it’s CAD/CAM all the way. But I still double-check the G-code. :)
Most GCode senders (that you need to run a Grbl job) support macros. First one is usually “Go To 0,0” and sometimes probing is implemented that way.
I wrote custom G-code for my gear cutter since it’s basically nested loops. Enter all the appropriate values at the top of the script and push the go button.
That was probably 15 years ago. I haven’t touched hand written G-Code since I discovered CamBam (No association)
Excellent primer for all who pretend to be CNC machinists! Now one only needs to learn how to read blueprints, use of proper feeds/speeds, understand interelated tolerancing, metallurgy (heat treatment), correct use of precision measuring tools, jigging/fixturing/tooling, precision & semi-precision layouts, trignometry, tool deflection compensation, rational material selection, complex setups and creative problem solving, machinists lingo, and how to machine teflon without literally going insane! And in 20yrs maybe you can finally call yourself a machinist.
What a crock of crap. Sure it takes years to learn a complex trade. Who said it doesn’t? Are all machinist actively condescending like you, or do some of them encourage people to explore their profession? (Also, your attempts to show off your knowledge make you seem insecure).
‘Machinist’ is slur.
Just ask any tool, mold and die maker.
You call one of them a machinist and it’s on.
‘Machinists’ implies ex-cons running manual Jet lathes. Doing the same thing over and over. Don’t really exist in the USA anymore.
We had fantastic machinists at a corporate research lab that I worked at, and they called themselves machinists. They would help you with your design and tell you how to change it to make it more manufacturable in any of the numerous techniques with which they were familiar.
Don’t think much of hobbyists do you? I guess that attitude is why we had to wait for the patents to run out before we could have 3d printers in the home.
While you can use a modern CNC Router, Laser Cutter, Plasma, Lathe, … without knowing GCode, it really does enhance your experience to know it. There are only a handful of commands to learn and, as a language, it is utterly simple. More akin to a sequence of calculator operations than a programming language. Yes, the language definition has things like branching and subroutines (canned cycles) but those are pretty uncommon – a lot of controllers don’t support them, just basic GCode sequencing.
I use GCode directly to do things like jog to specific locations to check clamp clearance, for example. I’ve even done a little “manual” milling with GCode commands.
> And if you’ve got a 3D printer kicking around, certainly if it runs Marlin or similar firmware, you’ve got the ideal platform for exploration.
Marlin has an excellent online reference for their GCode: https://marlinfw.org/docs/gcode/G000-G001.html
Old story, but it still amuses me:
Back in the 1980s a company with a team of master machinists bought an early CNC mill. Instead of resenting it, the machinists thought it was an interesting new tool. With no official support, they learned G-code from watching it scroll by as the machine worked.
Along the way, they saw that the program was garbage.. far too slow, bad cutting patterns, you name it. So they wrote a new program that produced better quality parts in less than half the time. Management noticed the increased productivity, found out what happened, and responded by replacing the old program and building a chain link fence around the machine (inside the shop) with a locked gate.
Protip: a locked gate doesn’t stop a team of master machinists. They made their own keys and kept tinkering with the machine. At the beginning of a shift they’d install their program, run the required number of parts, then spend the rest of the shift exploring what they could do with the machine. They also began designing accessories that would improve the machine’s capability.
When the company eventually fired the machinists, they opened their own shop. The company that fired them went out of business within a few months, and they bought all the shop equipment at bargain prices. They built and installed the accessories they’d designed, and started doing things the vendor thought impossible. They ended up with an ongoing consulting contract to test and improve the vendor’s whole line.
It’s so powerful knowing G-code, like there are plugins for my slicer to stop the program at a particular Z-height (say, to drop-in some nuts or whatever), but it’s so much faster to just open the sliced gcode file, ctrl-F for the G0 Z line of interest, and insert an M0 after it.
Crucially, I can easily also move the printhead out of the way, sling the bed forward, or otherwise make things easy on myself, that the plugin doesn’t do.
A couple years ago, I had a little CNC mill engraving the company logo in some aluminum bars. The toolpath was originally generated by VCare and it worked fine, but it would only do one bar a time. For engraving, the cutter is V-shaped so Z-height is critical, and since the bars can differ in thickness ever so slightly, it’s necessary to repeat the Z-probing operation on every new bar. Even if you clamped more bars to the table, the machine can’t Z-probe on its own.
Reason being, the probing operation is electrical, and requires clipping an alligator clip onto the spindle before running it. So someone had to visit the machine every 45 minutes (it was a long engrave job), remove the old bar, fixture the new bar, attach the clip, run the Z-probe routine, detach the clip, and start the program. Super labor intensive, super obnoxious.
So I made a fixture that would hold ten bars, and I figured out how to put some EMI shielding strip (copper finger-stock) along the whole bed, just beyond where the spindle would ever go during the real cut. Then I wrote G-code that would stop the spindle after one engrave job, move to the next slot in the fixture, bump against the finger-stock to ground the spindle, probe to the next bar, move away from the fingerstock, start the spindle, and run the next engrave.
This allowed the machine to run unattended for many hours, and if you came by when it was on, say, slot 7 in the fixture, you could swap out the first 5 bars. I wrote the program to start over at slot 1 and it would just go as long as you kept feeding it bars. (Repeating an already-engraved one was harmless.) it doubled production since there was no downtime, and it reduced the human interaction from about 2 hours per day to about 10 minutes.
Learning G-code is a big deal for getting more out of your machine! (That fixture is all torn apart now, but I might have video somewhere..)
> a trivial Logo-alike (in Python) for the CNC: pen-up, pen-down, forward, turn, repeat, and subroutine definitions.
Out of curiosity, are you familiar with HPGL? What you’re describing here is in essence the language that my oscilloscope, for instance, dumps out to a plotter when you tell it to screendump. It’s still the language that most commercial plotters use, and a lot of printers and PCB manufacturing equipment can work with it as well.
I found an old book from the 80s about CnC machining and have been reading it. I smiled at the part that said software that generates GCode from CAD files would never be practical and it would always be necessary to write GCode by hand.
Don’t get me wrong, it’s nice to learn this for progressing and advanced use. But it reminded me of the old Bill Gates 640k will always be enough for anyone statement that he may or may not have actually said.
Please be kind and respectful to help make the comments section excellent. (Comment Policy)