Project 5: inverse kinematics under test

Level 6 (after Lessons 6.1 to 6.3) · Time 3 to 4 hours · Difficulty 3 of 5

Objective

Six-dimensional inverse kinematics for the ee site of models/arm7.xml: position and orientation, by damped least squares, inside the joint limits, with a null-space term that picks the solution closest to a preferred posture. The solver must also say when it has failed. An IK routine that returns a wrong answer with a success flag is worse than one that returns nothing.

Prerequisites

Starter code

Expected behaviour

“Reachable” is defined without the solver: the tests generate targets by forward kinematics of random joint vectors inside the limits, so every target has at least one exact solution. The tests use seed 2026. The numbers below are from seed 123, which the reference was developed on.

Tests

MJC_IMPL=starter pytest projects/p05_ik
pytest projects/p05_ik

Nine tests:

Each common failure below is a mutant in mutants.json; python tools/mutate_project.py p05_ik checks that each one fails at least one test.

Common failures

Extension challenges

  1. Analytic IK for the 2-link arm (arm2.xml) and a comparison with your solver: accuracy, iterations, and what happens at the elbow singularity.
  2. Iterations to converge against the damping $\lambda$, from $10^{-3}$ to 0.5; explain the curve.
  3. Collision-aware IK: reject solutions where mj_collision reports arm self-contact, and measure how many targets that removes.

Solution

solution.py, about 60 lines.