Inverse kinematics — reference

Target point in, joint angles out. One page. L₁ = 18 cm · L₂ = 22 cm · reach 8–40 cm · shoulder 20°–150°

Which direction am I going?

Forward — angles in, position out. You pick θ₁ and θ₂, the math says where the tip lands.

Inverse — position in, angles out. You pick the target, the math says which angles reach it. Harder, and the priority skill of the unit.

This unit is always elbow-up

Every target has two solutions. This unit uses elbow-up at both endpoints, so the motion keeps one shape, and it is a must-have on the Day 16 gate.

elbow-up   θ₁ = α + β
elbow-down   θ₁ = α − β  (not used here)

The pipeline, in order

L₁ L₂ r = 28.43 α β θ₂′ (18, 22)
θ₁ = α + β. θ₂′ is the turn away from straight.
1
r = √(x² + y²)   the straight-line distance from the base to the target.
Gate 1 · Step 0 reach check. Is 8 ≤ r ≤ 40? If not, stop and move the point. 40 is the arm straight out (18 + 22). 8 is the elbow stop, not the geometric 4.
2
cos θ₂′ = (r² − L₁² − L₂²) ÷ (2 · L₁ · L₂)
Gate 2 · is cos θ₂′ between −1 and 1? Outside that, no angle has it and arccos is undefined. A negative cosine inside the range is perfectly fine — it just means an obtuse elbow.
3
θ₂′ = arccos(cos θ₂′)
4
α = arctan(y ÷ x)
5
cos β = (L₁² + r² − L₂²) ÷ (2 · L₁ · r)  →  β = arccos(cos β)
6
θ₁ = α + β
Gate 3 · is 20° ≤ θ₁ ≤ 150°? Outside that it is correct mathematics the shoulder cannot hold.
7
Driving the arm. Easiest is GO: type the target (x, y) and the app solves it and handles the offset. Typing angles by hand? The elbow field is offset: field = −θ₂′ + 90.

Worked — the unit's own motion

start (18, 22)end (10, 8)
r28.4312.81
cos θ₂′0.000-0.813
θ₂′90.0°144.4°
α50.7°38.7°
β50.7°89.5°
θ₁101.4°128.2°
elbow field0.0°-54.4°

The start is convenient on purpose: r² = 808 = L₁² + L₂², so cos θ₂′ = 0 and the links meet at a right angle.

Three ways a target fails

startend too closetoo farshoulder r = 40 r = 8
Too far. (30, 30), r = 42.43. cos θ₂′ = 1.253, outside −1…1. The calculator error is the answer.
Too close. (5, 4), r = 6.40. The algebra answers, because 6.40 clears the geometric 4 cm. The arm still cannot fold that tight. This is why the floor is 8.
Shoulder cannot hold it. (3, 14) is in reach, but θ₁ = 162.9° is past 150°. Nothing on paper looks wrong.

Before you call it solved