Skip to content
Tactiverge

School/○ Innovator/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?

About 40 min · 3 goals · Checked by automated tests

1.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.

2.Predict

L1 = L2 = 0.5 m. The target is (1, 0) m. What must the elbow angle θ2 be?

Guess first. Being wrong here is how the surprise works.

3.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?

Loading the interactive…

Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

L1 = 0.5 m, L2 = 0.5 m, target (0.5, 0.5) m. What is cos θ2? (2 decimal places)

5.Watch

→ 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.

4 min 3 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

6.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

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.

Derivation: Two-link IK with the law of cosines
  1. 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.

  2. The interior angle at the elbow is 180° − |θ2| (θ2 measures how far link 2 turns away from straight).

  3. Law of cosines: d² = L1² + L2² − 2·L1·L2·cos(180° − θ2) = L1² + L2² + 2·L1·L2·cos θ2.

  4. 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).

  5. If the right-hand side is above 1 or below −1, no angle exists: the target is outside the ring.

  6. 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.

  7. 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.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A two-link arm has L1 = 0.4 m and L2 = 0.4 m. Its hand must reach (0.697, 0.275) m. How much must the elbow bend, |θ2|, in degrees? (The elbow-up and elbow-down solutions share this size.)

°

Practice 3 · a fresh version every time

A two-link arm (L1 = 0.5 m, L2 = 0.35 m) must reach (0.097, 0.674) m with the elbow DOWN (θ2 positive). Its elbow angle is θ2 = 74.9°. What is the shoulder angle θ1, in degrees? (between −180 and 180)

°

Practice 4

You compute cos θ2 = 1.18. What does it mean?

Practice 5

What is the most reliable quick check of an IK answer?

8.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.

Open BUILD 004: Design a Robotic Gripper →

9.Reflect

10.Go further

Concepts: Inverse kinematics and the workspace · Forward kinematics