Lite / Intelligent Robotic Systems / Week 16
Inverse kinematics: from a target to joint angles
Forward kinematics always has one answer. Ask the question backwards, 'which angles put the hand HERE?', and you may get two answers, or none. Why?
Why should I care?
Tasks are given as positions ('pick the bolt at (0.5, 0.2) m'), but motors take angles. Inverse kinematics (IK) closes that gap. For a two-link arm it has a neat closed-form answer built from the law of cosines and atan2, the same tools you've practised all term.
- Derive the elbow angle of a two-link arm with the law of cosines.
- Find the shoulder angle with atan2 for a chosen elbow solution.
- Check an inverse-kinematics answer by running forward kinematics on it.
Predict
L1 = L2 = 0.5 m. The target is (1, 0) m. What must the elbow angle θ2 be?
- 0°
- 90°
- 180°
- There is no solution
Guess first, then reveal
0°: the target is exactly L1 + L2 away, so the arm must be straight, pointing along +x. That's the edge of the workspace, where the two elbow solutions merge into one.
Discover
In 'Inverse' mode, drag the target around with the sliders. Watch the readout's cos θ2 value. What happens to it as the target approaches the outer edge? The inner edge? Just outside? Note the grey dashed arm: when do the two solutions look most different?
What the interactive shows (text version)
In Inverse mode a crosshair marks the target; the arm is drawn reaching it with the chosen elbow, and the other solution is shown as a grey dashed arm. The readout gives d, cos θ2, both joint-angle pairs and an FK check, or says the target is unreachable and by how much.
Watch (optional)
Watch for: Watch for the two-solution picture: the same hand position reached with the elbow on either side.
Shows the reverse problem of choosing joint angles to reach a target, and why it can have several answers.
“Modern Robotics, Chapter 6: Inverse Kinematics of Open Chains” by Northwestern Robotics (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
The elbow first
Only the distance to the target decides how much the elbow must bend; direction doesn't matter. Compute d² = x² + y², then cos θ2 = (d² − L1² − L2²)/(2L1L2). If |cos θ2| > 1 the target is unreachable. Otherwise there are two elbow angles, +θ2 and −θ2 (one if θ2 = 0 or 180°).
Then the shoulder
With θ2 chosen, θ1 = atan2(y, x) − atan2(L2 sin θ2, L1 + L2 cos θ2). The first term points the shoulder straight at the target; the second rotates it back by the angle the bent elbow adds. Use atan2 for both: targets behind or below the base are common.
Trust, but verify
IK formulas are easy to get subtly wrong (a sign, the order of atan2's arguments, degrees vs radians). The fix is cheap: run FK on your answer. If the hand lands on the target, the IK is right. Every robot library's tests do exactly this, as do this lesson's practice generators.
Explain it another way
Intuitive
To touch something, you first decide how much to bend your elbow (by how far away it is), then swing your whole arm to aim. Close things need a big bend; far things need a straight arm.
Technical
Two-link IK is a closed-form solution of two nonlinear equations in two unknowns. The two branches are related by reflecting the elbow across the shoulder–target line. Numerical IK (iterating with the Jacobian) is used when no closed form exists, as for many 6-joint arms with offsets.
Mathematical
L1 = 0.6, L2 = 0.25, target (0.57, 0.6): d² = 0.6849. cos θ2 = (0.6849 − 0.36 − 0.0625)/0.3 = 0.8747, |θ2| ≈ 29.0°. Elbow down θ2 = +29.0°: θ1 = atan2(0.6, 0.57) − atan2(0.25 sin 29°, 0.6 + 0.25 cos 29°) = 46.47° − 8.42° ≈ 38.0°.
Applied
When an operator types a target into a robot pendant, the controller solves IK, checks that the result respects joint limits, and only then moves. A target that fails IK is rejected before anything moves.
Derivation: Two-link IK with the law of cosines
Shoulder at the origin, target at (x, y), distance d = √(x² + y²). The shoulder, elbow and hand form a triangle with sides L1, L2 and d.
The interior angle at the elbow is 180° − |θ2| (θ2 measures how far link 2 turns away from straight).
Law of cosines: d² = L1² + L2² − 2·L1·L2·cos(180° − θ2) = L1² + L2² + 2·L1·L2·cos θ2.
So cos θ2 = (x² + y² − L1² − L2²) / (2·L1·L2), and θ2 = ±cos⁻¹(that). Plus is elbow down, minus is elbow up (this course's convention).
If the right-hand side is above 1 or below −1, no angle exists: the target is outside the ring.
Shoulder: aim at the target, then correct for the bend: θ1 = atan2(y, x) − atan2(L2 sin θ2, L1 + L2 cos θ2). The correction flips sign with θ2, giving the two matching shoulder angles.
Always check: put (θ1, θ2) back through FK. You should land on (x, y).
Worked example 1
L1 = 5 cm, L2 = 3 cm, target (6, 2) cm. Find both solutions.
d² = 40. cos θ2 = (40 − 25 − 9)/30 = 0.2, |θ2| = 78.5°. Elbow up (θ2 = −78.5°): θ1 = atan2(2, 6) − atan2(3 sin(−78.5°), 5 + 3 cos 78.5°) = 18.4° − (−27.7°) = 46.1°. Elbow down: θ1 = 18.4° − 27.7° = −9.3°. These match the Gripper Build's Level 4 answer key.
Common mix-ups
IK always has exactly one answer, like FK.
✓ A two-link arm usually has two (elbow up and down), one at the ring's edges, and none outside it.θ1 is just atan2(y, x).
✓ That points the shoulder at the target; you must subtract the extra angle the bent elbow adds.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write ik(L1, L2, x, y, elbow) in Python. Test it by checking that fk(ik(...)) returns the target for 20 random reachable targets, and that it reports unreachable targets cleanly instead of crashing.
Reflect
The derivation started from a triangle. Where exactly would it break if the target were outside the workspace, and how does the formula tell you?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- Law of cosines (Wikipedia)
Proofs and uses of the formula at the heart of two-link IK.