← all notes

Unit 4 · Inverse Kinematics

Inverse kinematics toolkit - arctan2, the 2-link arm and the guessing loop

Forward kinematics: “I set the joints like this, where is the hand?” Inverse kinematics is the other way round: “I want the hand here, how do I set the joints?” It is harder because the answer can be two poses, one pose, infinitely many, or none at all.

The course splits it in two:

Most of 4.1 turns out to be much easier than it looks. Most of the questions are “draw the arm and think” questions, not algebra marathons.

One joint at the origin, one link of length aa. FK is x=acos⁡qx = a\cos q, y=asin⁡qy = a\sin q. Square both, add, use cos⁡2+sin⁡2=1\cos^2 + \sin^2 = 1:

x2+y2=a2x^2 + y^2 = a^2

So the workspace is a circle, not a disc. A target inside the circle is as unreachable as one outside: the rigid link would overshoot it.

Recipe: compute x2+y2x^2 + y^2. Not a2a^2? No configuration exists. Equal? Then q=arctan2⁡(y,x)q = \operatorname{arctan2}(y, x).

With a=1a = 1: (22,22)(\tfrac{\sqrt2}{2}, \tfrac{\sqrt2}{2}) gives 12+12=1\tfrac12 + \tfrac12 = 1, reachable at q=π4q = \tfrac{\pi}{4}. (24,22)(\tfrac{\sqrt2}{4}, \tfrac{\sqrt2}{2}) gives 0.125+0.5=0.6250.125 + 0.5 = 0.625, unreachable. One joint means one number, so write q=π4q = \tfrac{\pi}{4}, not a vector.

Linear FK: IK is just Gauss

Sometimes FK is handed to you as a matrix: XE=A qX_E = A\,q. Then IK is solving Aq=XEAq = X_E. No trig at all.

  1. Write the augmented matrix [A∣XE][A \mid X_E]. The target goes right of the bar. (At 2 am I put the qqs there. Don’t.)
  2. Clear everything below the diagonal with row operations.
  3. Back-substitute from the bottom row up.
  4. Plug qq back into AqAq and check you get XEX_E.

For a 2×22\times2 the inverse formula is faster: A−1=1ad−bc[d−b−ca]A^{-1} = \tfrac{1}{ad-bc}\begin{bmatrix} d & -b \\ -c & a\end{bmatrix}. The calculator solves these too, but write the steps if the question asks for them.

arctan2 and the Casio trick

Using arctan2 is important for the exam, so it is worth understanding why plain arctan is not enough.

Plain arctan⁡(y/x)\arctan(y/x) only sees the ratio. −11\tfrac{-1}{1} and 1−1\tfrac{1}{-1} are the same number, so it cannot tell the bottom-right quadrant from the top-left. The classic example:

arctan⁡ ⁣(0−1)=arctan⁡ ⁣(01)=0,butarctan2⁡(0,−1)=π\arctan\!\left(\tfrac{0}{-1}\right) = \arctan\!\left(\tfrac{0}{1}\right) = 0, \qquad\text{but}\qquad \operatorname{arctan2}(0, -1) = \pi

arctan2⁡(y,x)\operatorname{arctan2}(y, x) takes both numbers separately, checks their signs, and returns the real angle of the point (x,y)(x, y), anywhere in the circle.

The Casio fx-991ES PLUS has no arctan2 button, but it has Pol, polar conversion, which does exactly the same thing:

  1. SHIFT + [+] opens Pol(.
  2. Type the x value. SHIFT + [)] gives the comma. Type the y value. Close the bracket.
  3. Press =. It shows rr and θ\theta. θ\theta is your arctan2.

Order flip. The maths writes arctan2⁡(y,x)\operatorname{arctan2}(y, x), y first. Pol wants Pol(x,y)\text{Pol}(x, y), x first. And as a bonus, r=x2+y2r = \sqrt{x^2 + y^2} is the distance to the target, which is exactly what the reachability check needs.

Two links a1a_1, a2a_2, shoulder angle q1q_1, elbow angle q2q_2. Elbow first, from the cosine rule:

c2=x2+y2−a12−a222a1a2,s2=±1−c22,q2=arctan2⁡(s2,c2)c_2 = \frac{x^2 + y^2 - a_1^2 - a_2^2}{2a_1 a_2}, \qquad s_2 = \pm\sqrt{1 - c_2^2}, \qquad q_2 = \operatorname{arctan2}(s_2, c_2)

If c2c_2 comes out bigger than 1 or smaller than −1-1, the target is out of reach and you are done. The ±\pm is the elbow-up / elbow-down choice.

Then the shoulder, as a difference of two angles:

q1=arctan2⁡(y,x)⏟γ−arctan2⁡(a2s2, a1+a2c2)⏟αq_1 = \underbrace{\operatorname{arctan2}(y, x)}_{\gamma} - \underbrace{\operatorname{arctan2}(a_2 s_2,\ a_1 + a_2 c_2)}_{\alpha}

Worked example: a1=a2=1a_1 = a_2 = 1, target (1,−0.5)(1, -0.5), and q2=112.02∘q_2 = 112.02^\circ given.

The target is below the xx-axis and the elbow bends up, so the shoulder has to rotate down to compensate. The negative answer makes sense, and that kind of quick “does the sign make sense?” check is worth doing every time.

How many solutions? Picture the arm

This one throws people because there is nothing to calculate. Take a1=a2=1a_1 = a_2 = 1, so the maximum reach is 2 and folding the arm back gets you to 0.

Distance to targetSolutionsWhy
∥XE∥>2\lVert X_E\rVert > 2noneeven fully stretched the arm is too short
∥XE∥=2\lVert X_E\rVert = 2exactly oneonly the straight arm (q2=0q_2 = 0) gets there
0<∥XE∥<20 < \lVert X_E\rVert < 2twoelbow up or elbow down, both hit it
∥XE∥=0\lVert X_E\rVert = 0infinitely manyelbow folded back (q2=πq_2 = \pi), shoulder spins freely

The last row is my favourite: with the arm folded flat on itself the tip sits on the base, and you can spin q1q_1 like a ceiling fan without the tip moving at all.

Numerical IK: walking to a door in the dark

qk+1=qk+J−1(qk) (XE−FK(qk))q_{k+1} = q_k + J^{-1}(q_k)\,\bigl(X_E - FK(q_k)\bigr)

It looks like wizardry, but it is how you would walk to a door in a dark room:

  1. Guess. Pick some joint angles qkq_k, even random ones.
  2. Check. Run FK to see where the hand actually is. Compare with the target: the error is XE−FK(qk)X_E - FK(q_k). Zero error? Done.
  3. Correct. The Jacobian JJ says “if I tweak the joints a little, the hand moves like this.” Its inverse turns that around: “the hand is off by this much, so tweak the joints by this much.”
  4. Repeat. Update qq and go again, many times a second, until the error is basically zero.

It is an approximation (Taylor expansion of FK, truncated after the linear term), which is why it has to loop.

It breaks near singularities: when det⁡J≈0\det J \approx 0, J−1J^{-1} blows up and the correction step goes crazy. And when the robot is redundant (more joints than workspace coordinates, like 3 joints on a 2-D target), JJ is not square and has no inverse. Then you use the pseudo-inverse J#=JT(JJT)−1J^{\#} = J^T(JJ^T)^{-1}, which picks the “cheapest” of the infinitely many solutions.