GRBL's cornering algorithm
Updated on .
Cornering is how a machine tool head follows a path through a vertex at speed given a machine’s physical constraints. Sonny Jeon’s article on improving GRBL’s cornering algorithm describes a heuristic that keeps a machine’s centripetal acceleration constant by approximating the corner with a circle. It’s implemented in GRBL’s planner C code and widely copied by other motion planning systems.
GRBL is a G-code interpreter and receives high-level instructions for where to move the tool and converts them to low-level stepper motor movements. In contrast, EiBotBoard’s “low-level move” commands and Klipper leave motion planning up to control software running on more powerful devices and communicate detailed moves to simple firmware. Regardless, any open source motion planning system I could find use Jeon’s algorithm to lower speed going into corners.
This algorithm finds a cornering velocity based on the maximum acceleration the machine is capable of and a user-defined constant factor. In mathematical terms, cornering velocity with constant centripetal acceleration around a circle of radius is given by . This is derived from analyzing the change in velocity for circular motion.
The algorithm uses a circle that touches the edges of the corner and a deviation factor to control the minimum distance between the corner and the circle. A smaller translates to a smaller and a proportionally smaller . Bisecting the angle of the two edges creates a right triangle with the opposite side having length and the hypotenuse length . From trigonometry:
And rearranging the terms to solve for :
The angle is given by the dot product of the incoming edge and the outgoing edge :
To avoid the expensive and operations, Sonny used the half-angle identity for :
Where the sign of the expression is the sign of:
For an angle in , the expression is always positive. Keeping the expression in terms of cosine allows substitution of the dot product, which is just a sum of products, which in 2-dimensions is:
To compute the cosine:
Combining the equations above to find :
This approach requires just two square root operations for each vertex. The division by the two vector’s magnitudes are usually avoided by normalzing the vectors to the unit vectors.
The cornering velocity is used as the ending and starting velocities for the motion pieces leading to and from the vertex in Constant acceleration motion planning.
GRBL’s code is particularly terse, but benefits from thorough commenting. Instead of discrete operators for the vector components of motion, the operations are done incrementally, without any wasted steps.
Saxi overall has a more understandable structure to the code, with ASCII art comments to describe trapezoidal motion planning.
Klipper’s code is accompanied by a Kinematics document.