Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Kinematics

Inverse Kinematics

Solve inverse kinematics for robot arms: the closed-form two-link solution with elbow up and down, reachability checks, and numerical IK with the Jacobian transpose and damped least squares.

Inverse kinematics (IK) is the problem every pick-and-place program needs solved: the camera says the cup is at (0.9, 0.4), so what joint angles put the gripper there? It is the reverse of forward kinematics, and much harder, because the forward map is non-linear and not one-to-one. A target may be reached by two configurations, by infinitely many, or by none.

Two families of methods exist. Analytic (closed-form) methods give exact answers quickly for robots with a suitable geometry. Numerical methods iterate toward an answer and work for any robot. We build both.

Given a target (x, y) and link lengths l1, l2. Let rยฒ = xยฒ + yยฒ. The distance from base to target is one side of a triangle whose other two sides are the links. The law of cosines gives the elbow angle:

cos ฮธ2 = (rยฒ - l1ยฒ - l2ยฒ) / (2 l1 l2)

If the right side lies outside [-1, 1], the target is unreachable (too far, or too close to the base). Otherwise ฮธ2 = ยฑ acos(...). The two signs are the elbow-down and elbow-up solutions. The shoulder angle then follows from the geometry:

ฮธ1 = atan2(y, x) - atan2(l2 sin ฮธ2, l1 + l2 cos ฮธ2)

Use atan2 rather than atan so that the quadrant is correct.

Python Closed-form IK with both elbow solutions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both configurations reach the same point, which is why robot programs need a rule to pick one: nearest to the current pose, avoiding joint limits, or avoiding obstacles. Smooth motion usually means choosing the solution closest to the present joint values so the arm does not suddenly flip.

Geometric reasoning for 3D arms

For six-axis arms with a spherical wrist (the last three joint axes intersect at one point), IK splits into two parts: the first three joints position the wrist center, then the last three joints orient the tool. Industrial arms are designed this way so that closed-form solutions exist, with up to eight solutions for one pose (shoulder left or right, elbow up or down, wrist flipped or not). Arms without that property need numerical methods.

Numerical IK

When no closed form exists, treat IK as an optimization problem: find joint angles q that make fk(q) equal the target. Let the error be e = target - fk(q). The Jacobian J (see Jacobian and Velocity) links small joint changes to small tip changes: ฮ”x โ‰ˆ J ฮ”q. Then iterate:

  1. compute the error e,
  2. compute ฮ”q from e and J,
  3. update q โ† q + ฮ”q and repeat until |e| is small.

Two common update rules:

  • Jacobian transpose: ฮ”q = ฮฑ Jแต€ e. Simple and safe, but slow.
  • Damped least squares (Levenberg-Marquardt): ฮ”q = Jแต€ (J Jแต€ + ฮปยฒ I)โปยน e. Robust near singularities because ฮป stops the step from exploding.
Python Numerical IK for a 3-link planar arm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three-link arm is redundant for a 2-D target, so the result depends on the starting guess. Library solvers add extra goals (stay near a preferred pose, avoid joint limits) in this null space.

Practical issues

  • Reachability: check the workspace first; report failure instead of returning garbage. Unreachable targets make numerical solvers wander forever, so always cap iterations.
  • Joint limits: clip or reject solutions beyond the limits. Real robots cannot rotate a cable-fed wrist forever.
  • Singularities: near a stretched-out arm, small tip motions need huge joint changes; damping helps.
  • Continuity: when tracking a path, seed each IK call with the previous solution so the arm moves smoothly.
  • Orientation: a full 6-D pose has position and orientation errors; they must be combined with sensible weights.
Python Following a line with IK, seeded by the previous solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The joint curves are smooth because each step picked the solution nearest the previous one. Without that rule a solver could jump between the elbow-up and elbow-down branches.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a target have two IK solutions for a planar two-link arm?

    The law of cosines gives cos(theta2), and theta2 can be positive or negative.
  2. 2. How do you detect an unreachable target in the analytic solution?

    If the triangle inequality fails, the computed cosine is outside [-1, 1].
  3. 3. What is the advantage of damped least squares over the plain Jacobian inverse?

    The damping term stops steps from blowing up when the Jacobian is nearly singular.
  4. 4. Why seed IK with the previous solution when following a path?

    Choosing the nearest solution avoids sudden flips of the arm.

Summary

  • IK finds joint values for a desired pose; there may be zero, several or infinitely many answers.
  • A planar two-link arm has a closed form with elbow-up and elbow-down branches.
  • Numerical IK iterates with the Jacobian; damped least squares is the robust default.
  • Check reachability, joint limits and singularities, and seed each solve with the last one for smooth motion.

Next: Jacobian and Velocity