# Module 1.5: Inverse Kinematics

- Course: [Course 1: Frames, Transform Networks & Intro to Kinematics](https://roboticsconcepts.dev/courses/frames-transform-networks-kinematics.md)
- HTML Page: https://roboticsconcepts.dev/courses/frames-transform-networks-kinematics/inverse-kinematics/
- Key Concepts: Elbow-up vs. elbow-down ambiguity, Workspace boundaries & singularities, Geometric vs. iterative IK (CCD)

Forward kinematics goes from joint angles to hand position, which is easy and has one clean answer. Real tasks work in reverse: you see a cup at (x, y) and need to figure out what joint angles put your hand there. That's Inverse Kinematics (IK) — and unlike FK, it gets weird fast.

## Ambiguity, unreachable targets, and stretched-out singularities

Try touching a spot on your desk in front of you without moving your hand. You can probably drop your elbow down low or hike your elbow up high while keeping your fingertips in the exact same spot. A 2-link arm has that same two-branch choice: elbow-up vs. elbow-down.

Now try reaching a mug across the room without leaning forward: the target is outside your reachable workspace, so no exact solution exists. And right at the edge where your arm stretches completely straight, you hit a singularity — moving the hand radially outward becomes impossible, and tiny changes in target direction force large joint swings.

## Triangle geometry vs. iterative solvers

For a 2-link planar arm, we can solve IK in one shot using the law of cosines on the triangle formed by the shoulder, elbow, and target. For a 7-DOF arm or a humanoid reaching around a shelf, closed-form formulas are messy or nonexistent, so we use iterative solvers like Cyclic Coordinate Descent (CCD) or Jacobian methods: start from the current pose and nudge one joint at a time to reduce tip error step by step.

## Interactive Sandbox: Iterative vs. Geometric Inverse Kinematics Playground

Drag the orange target crosshair around the workspace. In Geometric mode, flip between Elbow-Down and Elbow-Up (notice the dashed ghost arm) and drag outside the circle to trigger Unreachable. Then switch to Iterative (CCD) and step through convergence.

## Watching CCD converge (and why it slows down)

In Iterative (CCD) mode, click +1 Step a few times. Each iteration aligns the elbow toward the target, then aligns the shoulder toward the target. The first two or three steps slash most of the distance error; later steps polish the final millimeters.

Because iterative solvers start from the arm's current joint angles, they naturally stay on whichever elbow branch is closest instead of violently flipping configurations mid-motion.

## Law of cosines for 2-link elbow angle

```text
cos(θ₂) = (x² + y² - L₁² - L₂²) / (2 · L₁ · L₂)   →   θ₂ = ± arccos(...)
```

If the right-hand side falls outside [-1, 1], the target is unreachable. The ± sign gives the elbow-down and elbow-up solutions.

## Check Your Intuition

**Question:** Why does a 2-link arm suddenly show two valid IK solutions for most points inside its workspace, and only one right on the outer boundary circle?

- [✓] At the outer boundary the arm is fully stretched straight (θ₂ = 0°), so elbow-up and elbow-down merge into one line
- [ ] The solver switches from radians to degrees at the boundary
- [ ] Gravity pulls the elbow down when the arm reaches far

**Explanation:** When r = L₁ + L₂, arccos(1) = 0°, so +0° and -0° are the exact same straight-arm configuration — a kinematic singularity.

## Where You'll See This in Modern Robotics

Many teleoperation rigs and learned policies output target end-effector poses at 20–50 Hz and rely on a damped iterative IK solver underneath to convert those poses into smooth, singularity-safe joint commands.

## Companion Python Notebook: 05 · Inverse Kinematics: Geometric vs. Iterative CCD

Implement closed-form law-of-cosines IK alongside iterative Cyclic Coordinate Descent (CCD) and plot convergence error across iterations.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-1/05_inverse_kinematics.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-1/05_inverse_kinematics.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-1/05_inverse_kinematics.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-1/05_inverse_kinematics.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-1/05_inverse_kinematics.py`
