Joint space or Cartesian space: why a straight line is hard
On a 3D printer, G1 X100 Y50 moves the nozzle in a straight line to that point.
Code: marlin-firmware on GitHub · Watch: 6DOF arm first test · Smooth arm motion
On a 3D printer, G1 X100 Y50 moves the nozzle in a straight line to that point. On my arm, the same kind of command moves the joints, and the gripper takes whatever path falls out of that. It’s almost never a straight line.
This is the most important idea in arm control, and it’s easier to see in a few lines of JavaScript than in a textbook.
Two ways to describe a position
- Joint space: the angle of every joint. “Shoulder at 60°, elbow at -100°.” This is what the motors understand.
- Cartesian space: where the gripper is, in millimetres. “220 mm out, 40 mm up.” This is what you care about.
With ARTICULATED_ROBOT_ARM, Marlin works only in joint space. A G1 moves every joint from its current angle to its target angle, all at proportional speeds so they finish together. That’s a straight line in joint space. In Cartesian space, it’s a curve.
Forward kinematics
Going from joint angles to the gripper’s position is called forward kinematics. For the first three joints of a simple arm (a base that turns, then a shoulder and an elbow that bend in the same plane), it’s a bit of trigonometry:
const deg = (d) => (d * Math.PI) / 180;
// Link lengths in mm (an example arm, not exact measurements)
const L1 = 120; // shoulder to elbow
const L2 = 100; // elbow to wrist
// shoulder and elbow: 0 = pointing straight out, positive = up
function forwardKinematics({ base, shoulder, elbow }) {
const reach =
L1 * Math.cos(deg(shoulder)) + L2 * Math.cos(deg(shoulder + elbow));
const height =
L1 * Math.sin(deg(shoulder)) + L2 * Math.sin(deg(shoulder + elbow));
return {
x: reach * Math.cos(deg(base)),
y: reach * Math.sin(deg(base)),
z: height,
};
}
forwardKinematics({ base: 0, shoulder: 0, elbow: 0 });
// { x: 220, y: 0, z: 0 } (fully stretched out)
Each link adds its length in the direction it points, and the elbow’s direction is the shoulder angle plus its own.
How far off a joint move is
Now move the shoulder from 60° to 10° and the elbow from -100° to -20°, the way Marlin would, and record where the gripper goes:
const A = { base: 0, shoulder: 60, elbow: -100 };
const B = { base: 0, shoulder: 10, elbow: -20 };
for (let i = 0; i <= 20; i++) {
const t = i / 20;
const p = forwardKinematics({
base: A.base + (B.base - A.base) * t,
shoulder: A.shoulder + (B.shoulder - A.shoulder) * t,
elbow: A.elbow + (B.elbow - A.elbow) * t,
});
// compare p with the straight line from start to end
}
The gripper starts at about (137, 40) mm and ends at (217, 4) mm. On the way, it bows up to 9.6 mm away from the straight line between those two points. For moving through empty space, nobody cares. For sliding a part into a slot, or drawing a line with a pen, 9.6 mm is a miss.
See it for yourself
This small page draws the arm, the curved path a joint move takes (red), and the straight line you might have expected (grey dashes). Drag the sliders to change the starting pose. It’s plain HTML, so you can paste it into a file and open it in a browser.
<canvas id="c" width="400" height="300"></canvas>
<label>Shoulder <input id="s" type="range" min="0" max="120" value="60"></label>
<label>Elbow <input id="e" type="range" min="-150" max="0" value="-100"></label>
<script>
const L1 = 120, L2 = 100;
const rad = (d) => (d * Math.PI) / 180;
const ctx = document.getElementById('c').getContext('2d');
const s = document.getElementById('s');
const e = document.getElementById('e');
const target = { shoulder: 10, elbow: -20 };
function fk(shoulder, elbow) {
const elbowPt = [L1 * Math.cos(rad(shoulder)), L1 * Math.sin(rad(shoulder))];
const tip = [
elbowPt[0] + L2 * Math.cos(rad(shoulder + elbow)),
elbowPt[1] + L2 * Math.sin(rad(shoulder + elbow)),
];
return { elbowPt, tip };
}
// Canvas y points down, so flip it; the shoulder sits near the bottom left
const toCanvas = ([x, y]) => [60 + x, 260 - y];
function draw() {
const start = { shoulder: +s.value, elbow: +e.value };
ctx.clearRect(0, 0, 400, 300);
// Path the tip takes when both joints move evenly (what Marlin does)
ctx.strokeStyle = '#d33';
ctx.beginPath();
for (let i = 0; i <= 50; i++) {
const t = i / 50;
const { tip } = fk(
start.shoulder + (target.shoulder - start.shoulder) * t,
start.elbow + (target.elbow - start.elbow) * t,
);
const [x, y] = toCanvas(tip);
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
}
ctx.stroke();
// The straight line you probably expected
ctx.strokeStyle = '#999';
ctx.setLineDash([4, 4]);
ctx.beginPath();
ctx.moveTo(...toCanvas(fk(start.shoulder, start.elbow).tip));
ctx.lineTo(...toCanvas(fk(target.shoulder, target.elbow).tip));
ctx.stroke();
ctx.setLineDash([]);
// The arm itself
const { elbowPt, tip } = fk(start.shoulder, start.elbow);
ctx.strokeStyle = '#222';
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(...toCanvas([0, 0]));
ctx.lineTo(...toCanvas(elbowPt));
ctx.lineTo(...toCanvas(tip));
ctx.stroke();
ctx.lineWidth = 1;
}
s.addEventListener('input', draw);
e.addEventListener('input', draw);
draw();
</script>
Play with it for a minute and a pattern shows up: small moves are nearly straight, and big moves that swing the elbow through a large angle curve the most.
Getting a straight line
The fix is to go the other way: pick points along the straight line, and work out the joint angles for each one. That’s inverse kinematics. For two links in a plane it has a closed-form answer:
const toDeg = (r) => (r * 180) / Math.PI;
// Returns the "elbow up" solution, or null if the point is out of reach
function inverseKinematics({ x, z }) {
const cosElbow = (x * x + z * z - L1 * L1 - L2 * L2) / (2 * L1 * L2);
if (cosElbow < -1 || cosElbow > 1) return null; // too far, or too close
const elbow = -Math.acos(cosElbow);
const shoulder =
Math.atan2(z, x) - Math.atan2(L2 * Math.sin(elbow), L1 + L2 * Math.cos(elbow));
return { shoulder: toDeg(shoulder), elbow: toDeg(elbow) };
}
function straightLine(from, to, pieces = 20) {
const moves = [];
for (let i = 0; i <= pieces; i++) {
const t = i / pieces;
const joints = inverseKinematics({
x: from.x + (to.x - from.x) * t,
z: from.z + (to.z - from.z) * t,
});
if (!joints) throw new Error(`point ${i} is out of reach`);
moves.push(joints);
}
return moves; // send each one as its own G1
}
Each piece is still a joint move, so it still curves a little, but a short piece barely has room to. For the same move as before:
| Pieces | Furthest from the straight line |
|---|---|
| 1 (a single G1) | 9.6 mm |
| 5 | 0.76 mm |
| 10 | 0.26 mm |
| 20 | 0.08 mm |
The error drops fast. A handful of pieces is usually enough.
Things the maths hides
- Two answers. Most points can be reached with the elbow up or the elbow down. The function above always picks up. A real arm has to pick one and stick with it, or it will flip halfway through a move.
- No answer. Points too far away, or too close to the base, return null. A planner has to handle that before it sends anything.
- Zero isn’t zero. In this code, 0° means “pointing straight out”. In Marlin, 0 is wherever the joint homes. Every joint needs an offset, and a sign that matches its INVERT_*_DIR setting, before these numbers mean anything on the real arm.
- More pieces, more traffic. Every piece is a line of G-code over USB, and Marlin’s planner holds 16 moves. Short pieces at high speed can drain it faster than the host refills it, and the arm stutters.
That last point is where hand-written IK stops being fun for six joints, and where MoveIt 2 comes in.