Direct CNC Control with the Raspberry Pi

If you’re building a CNC router, laser cutter, or even 3D printer, you’ll usually be looking at a dedicated controller. This board takes commands from a computer, often in the form of G-Code, and interprets that into movement commands to the connected stepper motors. Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful. That may not be the case anymore.

A stepstick driver

Thanks to the Raspberry Pi (and similar boards), we now have Linux computers with plenty of GPIO pins. The only thing missing is the software to interpret the G-Code and command the steppers over GPIO, which thanks to [pantadeusz], we now have. Called raspigcd, this software interprets a subset of G-Code to provide real-time control over connected steppers fast enough to drive a small CNC router.

Of course, you can’t directly control a beefy stepper motor to the GPIO pins of a Pi. You’ll let out all the magic smoke. But you can wire it up directly to a stepper driver board. These little modules connect up to a dedicated power supply and handle the considerable current draw of the steppers, all you need to do is provide them the number of steps and direction of travel.

This method of direct control offers some very interesting possibilities for small, low-cost, CNC projects. Not only can you skip the control board, you could conceivably handle the machine’s user interface (either directly via a touch screen or over the network) on the same Pi.

We’ve seen attempts at creating all-in-one Linux stepper controllers in the past, but the fact that anyone with a Raspberry Pi 2 or 3 (the boards this software has currently been tested on) can get in on the action should really help spur along development. Has anyone used this?

Agreed. I run a couple LinuxCNC machines, and even though the kernel is compiled with real-time support, it is still recommended to run a latency test to determine the fastest safe/reliable step rate your computer can achieve.

Even better, you can exclude everything else from running on one core (via kernel command line) and then so long as you don’t go making blocking system calls you can basically treat that core as your own. Hit the gpio pins by memory mapped I/O and you’re good to go.

It’s still non-deterministic though. You don’t know about the number of cycles spent waiting to access shared memory, the precise time it will take any code to execute (the joys of out-of-order execution) etc. There’s a reason that ARM designed the A series and M series cores. The A’s are faster, but you know exactly how long something is going to take on an M, plus it’s been optimised for IO.
A cheap $1 STM32 is going to be far better at the job than any Core A without dedicated hardware.

Last time I looked at the Machinekit the pru driver was written as some “one program does everything”. It ran the stepper algorithm, did some plc like stuf and maybe more (can’t remember well).
Maximum step frequency was some 20 to 30kHz. plenty for “real” steps, but not adequate for high microstepping to get the motors quiter.
Only a single PRU was used, the otther was twidling it’s thumbs.
It was a real bummer when I discovered that.
Another project used the PRU as stepper driver and could get upto 1.5MHz step rate. Plenty and with headroom to spare for high microstepping (On github, can’t rember the project’s name).

If I had known this in advance I would have simply bought a mesa FPGA card and be done with it.
It’s not like EUR100 is a serious difference in a decent CNC machine.

GRBL on Cortex M3 might be an option for me.
Probably fast enough, and software on a lever that I can comprehend and fit into my head.

‘better’- 95% of us just want an easy way to wire up our diy cncs. I’d like to just order the stepper drivers, hook them up to the pi zero and the CD drive steppers, change whatever variables are required (all accessible easily) and fire away with your new baby cnc

I have an ANET A8 3D printer that is making a great paperweight on my shop desk. I’ve never been able to figure out why it freezes up during a print. The heated nozzle and block always seem to leak which sucks. So why am I bringing this up here on this discussion? Well, if I can’t get the 3D printer to print, I’m wondering if there is a way to convert it to a CNC instead? Or if someone wants to contact me with how to fix this thing, I’d really like that. Either way, it’s good to hear someone has overcome the CNC issue presented here. Awesome. Now if I had plans to implement this whole thing, it would be even more useful to me. CNC’s are expensive. DIY would be great.

Even the beefiest 3D printer will make a lousy CNC router/mill. FDM doesn’t put a ton of stress on a frame so there is no need to built it super robust. Random freezes are often an issue of your bootloader or arduino distro. I’d reflash the bootloader (you’ll need an ISP) and then find the appropriately configured Marlin or reprapfirmware and flash that. There was a Ardunio Mega/Taurino bug in the bootloader that would freeze the printer and return a single character serial stream that plagued the 3D printer community for a little bit.

In general, a RAMPS controller with Marlin firmware is the solution for goofy control electronics.
The firmware will check the heating control feedback for anomaly, and shutdown th heaters if thermometers are too slow/disconnected. RepRap setups are very reliable, but to use them for CNC is ridiculous for anything outside engraving (GRBL is the same, but offers more options).

The other option is a Prusa i3 mkII if you want to avoid the pain of setting up your own system.

LOL, yeah.. good luck converting an A8 in to anything close to a subtractive machine. You would be hard pressed to get it to do simple engraving in butter. It is not even close to a rigid machine, and just barely rigid enough for FDM.

Like Jake said , even the most heavy duty, rigid 3D printers are just barely capable of being used as even a rotary engraver. Will be a complete waste of effort.

I can confirm that, I needed to print about two spools of ABS worth of braces and some threaded rod supports to make my A8 even print right. ABS just for the higher glass transition temperature, to keep it from sagging next to the hot end and heated bed. Also makes welding parts together with acetone a possibility if you want braces that are larger than the print bed with no hardware.

It’s a fun project if you know what you’re getting into, but if you wanted to convert it to a subtractive machine I’d go out and get a welder and some scrap metal and begin from scratch instead. For the record, I wanted to get an A8 and improve it instead of simply buying a good printer from the get-go because it seemed like a good way to practice my CAD and mechanical design skills. It was. I definitely learned enough to justify the hours of labor spent.

I’m currently working on putting in a RAMPS board to replace the cornballer of a control board that comes with the A8. No thermal runaway protection? I think my toaster is more advanced than that. Hopefully that’ll be the last safety upgrade it needs. Might even add dual extrusion as well! Who knows.

So far my A8 works as a passably decent filament printer with PLA, ABS, TPU, and PETG, and I printed out a bracket to hold a sharpie so I can use it as a small plotter. Just for funsies. But a plotter or a laser is about as much as you’ll get out of that frame; any kind of rotary tool is going to exceed its structural integrity and lead to rapid unplanned disassembly. This project begs to differ: https://www.thingiverse.com/thing:2508924 but notice just how heavily reinforced it is and how much of the machine is new growth. I don’t know if that’s the route I would take personally.

I DIY’d my own machine with the Shapeoko for about $1100. Since it’s 2’x4′ and already that much money I set it up with a refurb desktop. I feel silly calling it a CNC because next thing I know I’m being asked to make a crankshaft or something, but I find there’s a lot of market segments you can jump in on with a larger format machine.

“Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful”. Ahum… that’s not entirely true, because I’ve been using “Mach3” for many years and it does exactly that.

But what a great development to read about this working on a raspberry pi! Really, very very cool, I’ve been looking for this for a long time, although a few years too late for me perhaps. But in the future it might be of great use I’m sure!

“Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful”

I’d like to add..

Except for every single piece of equipment that uses steppers, from floppy drives to receipt printers and of course LinuxCNC and Mach3, which you mention, that also directly also provide a pulse train to the steppers.

A less than PC sized full G-Code interpreting hackable controller is the holy grail, and there are many contenders. There is GRBL for Arduino, there was an attempt to put a port of LinuxCNC on the Beaglebone and on the the Raspberry Pi. People have made a CNC control for the Propellor chip and I would be amazed if there was not a Pic version or several out there.

There are some interesting standalone China made CNC controls on eBay, some with even built in stepper drivers, but I seem to have a thing for the price point of old off-lease Dells and ebay stepper drivers, combined with the flexibility of LinuxCNC.

I use a kflop on my cnc mill, it can handle all sorts of fun stuff like dual encoders, rigid tapping, pretty much anything you can write in C. i think there is only windows support but you could tie that to a microatx pc or an intel NUC and get a very small system.

So maybe the term “Historically” depends on your perspective here, but running a CNC with something like Mach3 is definitely not common anymore. You need a 32bit Windows OS running on a machine with a parallel port…it’s fair to say there haven’t been many NEW Mach3 installs/setups recently.

For the last 15 – 20 years, you’ve definitely needed a motion controller.

This is new? I thought that Linux CNC was already ported to the Pi and already could use the GPIOs.

Besides, what great expense does this save? An Arduino clone is cheap. Plus you will still want some sort of motherboard to place those step sticks on. I haven’t seen one with ready positioned connectors to plug into a Raspi GPIO header yet. Though… I wouldn’t be that surprised if it’s out there. Comparing the time required to wire up all those StepSticks to their GPIOs, power source and motors… And then.. if there is any chance of latency problems… I’d rather just buy the RAMPS/Arduino combo and not worry about all that.

Also, does anyone have experience with the RTL kernel extensions? If the stepper control is guaranteed real time does the UI suffer? Does it trade motor jitters for UI responsiveness issues?

You guys might want to check PyCNC out, https://github.com/Nikolay-Kha/PyCNC. While its sparse on the front end it has no dependencies to run or maintain, it just needs a nice pi Hat for some stepper chips and pwm spindle control.

This type of use of a RaspyPi is a engineering non sense:
RT kernel is not enough to ensure proper fail safe RT timing, some DMA access must be implemented for GPIO.
Much more elegant solution is to drive with a RaspyPi a standard microcontrollor like with klippers FW.

Engineering is not about building hard-realtime failsafe systems for small hobby CNC mills that are made out of plywood. Using just a raspberry is cheaper and is on the level of cheapness of the rest of the machine.

It is Frezia prototype, work in progress. It works very well in plywood, wood, plexi and mdf. It can also cut in 1mm brass and 3mm alloy (verry slowly, but successful enough). Right now there is no other information about it than https://www.facebook.com/FreziaTool (in Polish language) and github repo (https://github.com/pantadeusz/raspigcd). The future (when it is done) is not yet defined – commercial kit or opensource.

The Buildbotics CNC controller uses an RPi but offloads the step generate to get way better performance (250k steps/sec on 4 channels) than you will get with other solutions. See https://buildbotics.com/