Moving your CNC with Bresenham’s algorithm
Programming stepper motors to move together correctly for CNC machining can be a challenge and I’m here to help. To move one motor on it’s own is easy enough. Moving several is not too hard, either. What if all those motors are supposed to move in concert and finish their moves at the same time, perfectly, every time? The solution is today’s topic.
A single stepper motor isn’t too hard. Most stepper motors are driven through a dual H-bridge stepper adapter like the A4988, the TMC2130, or the classic L293D. You, the programmer, set the enable pin (turn on the motor), the direction pin, and then flip the step pin high and low for the desired number of steps. Easy enough…
When it’s time to move two motors, I can (of course) move each the full distance.
I could use an Adafruit Stepper Motor Shield, set the target steps and position, and then say “go”. It will move them both one step at a time. One of the motors will be done first. This is sometimes called the hockey stick.
But what we want is both to arrive at the same time.
Notice that I’m using line drawings to describe these movements. The solution to moving these motors is the same one used to draw lines on a screen – Brenseham’s line algorithm.
Now that I’ve described what the issue is and the name of the solution, Here’s the wikipedia entry on Brenseham’s line algorithm. It has pseudo-code and lots more examples to help you out. If that’s not enough, post to our forums with your questions!
One final note: an especially nice feature of Brenseham’s line algorithm is that it does all the math with whole numbers only, which are much faster to calculate in a microchip AND won’t have any rounding errors that can happen in said microchips. So perfect results every time.
Brenseham’s line algorithm is used in Makelangelo-firmware, Marlin, and probably several other CNC firmwares.