TactivergeLite

Lite / Textbooks / Robotics From First Principles / Chapter 6

6. Inverse kinematics

Version 1 · Technically checked (automated checks only) · 3 source lessons · Full interactive version

Introduction

Usually you know where the hand should go and need the joint angles. That is inverse kinematics, and it is harder: the law of cosines gives the elbow angle, but there are often two answers, elbow up and elbow down, and sometimes none at all. Near the edge of the workspace and when the arm is fully stretched, tiny moves of the hand demand huge moves of the joints. Those are singularities.

Figure 6.1. Two ways to reach one point (described in words)

A two-link arm with 1 m links reaching the target (1.2, 0.6). The law of cosines gives cos θ2 = -0.1, so θ2 = ±95.7°. Elbow down: θ1 = -21.3°, θ2 = 95.7°. Elbow up (dashed): θ1 = 74.4°, θ2 = -95.7°.

Explanation

Built from 3 School lessons. Each part is the lesson's own explanation.

6.1 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?

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.

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

Common mix-ups

Open the Lite lesson: Inverse kinematics: from a target to joint angles

6.2 Elbow up or elbow down? Choosing between solutions

Two sets of joint angles put the hand on the same bolt. One of them smashes the elbow into a shelf. How does a robot choose?

Mirror images

The two solutions share |θ2| and are reflections of each other across the line from shoulder to target. With this course's convention elbow up has θ2 < 0, elbow down θ2 > 0. At the edges of the ring (θ2 = 0 or ±180°) they coincide; in the middle of the ring they differ the most.

How to choose

Common rules: (1) reject any solution outside joint limits or that collides with something; (2) among the rest, pick the one closest to the current joint angles, so the move is short and predictable; (3) keep the same branch throughout a task. Many industrial arms fix the branch in the program ('elbow up' configuration) for exactly this reason.

Don't flip mid-move

If a controller re-solves IK at every step of a straight-line hand move and naively picks 'the first solution', it can jump from one branch to the other. The hand target barely changes, but the elbow swings through a huge arc in one step. That's a sudden, fast, dangerous motion, and a classic bug to test for.

Common mix-ups

Open the Lite lesson: Elbow up or elbow down? Choosing between solutions

6.3 Unreachable targets, joint limits and singularities

Near full stretch, a tiny hand movement can need a huge, fast joint movement. Why do robots go 'weak' at the edge of their reach?

Outside the ring

If d > L1 + L2 (beyond reach) or d < |L1 − L2| (inside the hole), IK has no solution. A good solver says so and reports the gap: d − (L1 + L2) or |L1 − L2| − d. A common safe fallback is to reach the nearest reachable point along the same direction, and flag the shortfall rather than hide it.

Singularities

When θ2 = 0° (straight) or ±180° (folded), the two links are in line. Then both joints can only swing the hand sideways around the shoulder; moving along the arm's line needs no joint motion at first order. The arm's Jacobian determinant, L1·L2·sin θ2, is zero there. Near a singularity, small hand moves along that line demand very large, fast joint moves.

From joint error to hand error

Encoders are never perfect. A shoulder error Δθ1 swings the whole arm, moving the hand by about d·Δθ1 (radians), where d is the shoulder-to-hand distance. A stretched-out arm amplifies errors most. A 0.5° error at d = 0.8 m is 0.8 × 0.00873 ≈ 7 mm: enough to miss a small hole.

Common mix-ups

Open the Lite lesson: Unreachable targets, joint limits and singularities

Worked examples

Inverse kinematics: from a target to joint angles: example 1

L1 = 5 cm, L2 = 3 cm, target (6, 2) cm. Find both solutions.

Solution

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.

Elbow up or elbow down? Choosing between solutions: example 1

The arm is at (θ1, θ2) = (40°, −70°). The two IK solutions for the next target are (45°, −75°) and (−5°, 75°). Which should it use?

Solution

The first: it's 5° away on each joint, on the same branch (θ2 negative). The second needs a 45° shoulder move and a 150° elbow swing.

Unreachable targets, joint limits and singularities: example 1

L1 = 40 cm, L2 = 25 cm. A target is at (50, 45) cm. Reachable? If not, by how much is it out?

Solution

d = √(2500 + 2025) = √4525 ≈ 67.3 cm. Outer radius 65 cm. Unreachable, 2.3 cm beyond full stretch.

Interactive figures, in words

Figure 6.2 from Inverse kinematics: from a target to joint angles

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?

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.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. L1 = 0.5 m, L2 = 0.5 m, target (0.5, 0.5) m. What is cos θ2? (2 decimal places)
    Answer it in “Inverse kinematics: from a target to joint angles”
  2. You compute cos θ2 = 1.18. What does it mean?
    Answer it in “Inverse kinematics: from a target to joint angles”
  3. What is the most reliable quick check of an IK answer?
    Answer it in “Inverse kinematics: from a target to joint angles”
  4. What do the elbow-up and elbow-down solutions for the same target always share?
    Answer it in “Elbow up or elbow down? Choosing between solutions”
  5. Which rule best avoids sudden elbow swings during a long move?
    Answer it in “Elbow up or elbow down? Choosing between solutions”
  6. For a two-link arm, at which elbow angles is it singular?
    Answer it in “Unreachable targets, joint limits and singularities”
  7. A target is 3 cm beyond reach. What should a well-designed IK function do?
    Answer it in “Unreachable targets, joint limits and singularities”

Engineering applications

Avoiding the table

When an arm works just above a table, the elbow-down solution may drive the elbow through the surface. Controllers pick the solution that keeps clear of obstacles and stays far from joint limits.

Graceful failure

If a target is out of reach, the cosine of the elbow angle comes out bigger than 1 and there is no solution. Good code detects that and moves to the nearest reachable point instead of crashing.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Inverse kinematics
Inverse kinematics finds joint angles that place the end-effector at a desired position. There may be several solutions, one, or none. Field Guide
Forward kinematics (two-link arm)
Forward kinematics computes where the end-effector is from the joint angles and link lengths. Field Guide
Workspace and reach
A robot's workspace is the set of all positions its end-effector can reach. For a two-link planar arm it is a ring between radii |L1 − L2| and L1 + L2. Field Guide

Project: A safe IK function

Write a function that returns both inverse-kinematics solutions for a two-link arm, reports clearly when a target is unreachable, and checks every answer with forward kinematics. Test it on targets inside, on the edge of and outside the workspace.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

Further reading from the lessons

← 5. Forward kinematics7. Motion and trajectories →