Skip to main content

Inverse Kinematics

Mediumdifficulty

Math used

Right-triangle trigonometry, sine and cosine, atan2, and square roots. Jacobians and numerical solvers are an optional next step for mechanisms without a tidy geometric solution.

Drivers and autonomous routines think in goals: put the claw here, point the wrist there, slide the robot sideways. Motors think in angles and speeds. Inverse kinematics translates from the first to the second. For a drivetrain, the goal is a robot motion (vx,vy,ω)(v_x, v_y, \omega). For an arm, it's a claw position (x,y)(x, y) measured from the shoulder pivot. Either way the question is the same: what should the motors do?

First ask: how many freedoms does the mechanism have?​

Team 21229 Quality Control's workshop makes a good point before any equations: start by counting degrees of freedom, the independent ways something can move. A slide has one. A single arm joint has one. A turret with an arm has at least two. A claw that has to stay level adds a constraint.

Compare how many the target needs with how many the mechanism has:

CaseWhat it means on a robotWhat the solver must do
Too few mechanism freedomsA one-joint arm cannot place a claw at every (x,y)(x, y) point.Reject unreachable targets or redesign the mechanism.
Same number of freedomsA two-link arm can often solve (x,y)(x, y) with geometry.Use a closed-form trig/vector solution when possible.
Extra mechanism freedomsA turret + arm + slide may have many ways to reach the same point.Choose a configuration using constraints, preferences, or numerical IK.

So there isn't always one answer. Sometimes there are two (elbow up and elbow down), and sometimes infinitely many, and the code has to pick one that avoids collisions, stays within joint limits, keeps the claw level, and doesn't jump between two very different poses.

Try it​

Drag the orange target. The demo solves for two angles, shoulder and elbow. Switch between elbow up and elbow down: both reach the same point. On a robot, pick the one that's closest to where the arm already is, not just any valid one.

Live demoInverse kinematics - drag the claw target
+x+ytarget
x: 14.0 uy: 11.5 ur: 18.1 utheta1: 71 degtheta2: -73 degreachable: yes
shoulder linkelbow linkdesired claw positionreachable workspace
The Basics of Inverse Kinematics in Robotics -

21229 Quality Control explains the same position-to-angles idea visually.

Open video

Forward kinematics: angles to position​

It's easier to start with the opposite question, forward kinematics: if you know the joint angles, where is the claw?

Take a two-link arm with a first link of length L1L_1 (shoulder to elbow) and a second of length L2L_2 (elbow to claw). The shoulder angle θ1\theta_1 is measured from horizontal. The elbow angle θ2\theta_2 is measured from the first link, so θ2=0\theta_2 = 0 means the arm is straight.

The first link is the hypotenuse of a right triangle, so it reaches:

x1=L1cos⁡(θ1)y1=L1sin⁡(θ1)\begin{aligned} x_1 &= L_1\cos(\theta_1) \\ y_1 &= L_1\sin(\theta_1) \end{aligned}

The second link points at θ1+θ2\theta_1 + \theta_2, because it's mounted on the end of the first:

x2=L2cos⁡(θ1+θ2)y2=L2sin⁡(θ1+θ2)\begin{aligned} x_2 &= L_2\cos(\theta_1 + \theta_2) \\ y_2 &= L_2\sin(\theta_1 + \theta_2) \end{aligned}

Add the two together for the claw position:

x=L1cos⁡(θ1)+L2cos⁡(θ1+θ2)y=L1sin⁡(θ1)+L2sin⁡(θ1+θ2)\boxed{ \begin{aligned} x &= L_1\cos(\theta_1) + L_2\cos(\theta_1 + \theta_2) \\ y &= L_1\sin(\theta_1) + L_2\sin(\theta_1 + \theta_2) \end{aligned} }

Check it: with both links 10 in, θ1=90°\theta_1 = 90° (straight up) and θ2=−90°\theta_2 = -90° (elbow bent back down to horizontal), the claw is at x=0+10cos⁡0°=10x = 0 + 10\cos 0° = 10 and y=10+10sin⁡0°=10y = 10 + 10\sin 0° = 10.

Inverse kinematics: position to angles​

Robot code usually needs the reverse: the routine says "put the claw at (x,y)(x, y)," and the motors need angles.

Draw a triangle from shoulder to elbow to claw. The side from the shoulder to the claw has length

r=x2+y2r = \sqrt{x^2 + y^2}

by Pythagoras. If rr is more than L1+L2L_1 + L_2, the target is out of reach. If it's less than ∣L1−L2∣|L_1 - L_2|, it's too close, because one link can't fold through the other.

The triangle's three sides are L1L_1, L2L_2, and rr, so the law of cosines gives the angle at the elbow:

cos⁡(θ2)=r2−L12−L222L1L2\boxed{ \cos(\theta_2) = \frac{r^2 - L_1^2 - L_2^2}{2L_1L_2} }

(The sign is flipped from the textbook form because θ2\theta_2 is measured from straight, not the inside angle of the triangle.) Then:

θ2=±cos⁡−1 ⁣(r2−L12−L222L1L2)\theta_2 = \pm \cos^{-1}\!\left( \frac{r^2 - L_1^2 - L_2^2}{2L_1L_2} \right)

The ±\pm is the choice between elbow up and elbow down. Both put the claw in the same place, but one might hit the robot frame and the other might not.

For the shoulder angle, aim straight at the target, then back off by the angle inside the triangle between "straight at the target" and "along the first link":

θ1=atan2⁡(y,x)−atan2⁡ ⁣(L2sin⁡(θ2),L1+L2cos⁡(θ2))\boxed{ \theta_1 = \operatorname{atan2}(y, x) - \operatorname{atan2}\!\left( L_2\sin(\theta_2), L_1 + L_2\cos(\theta_2) \right) }

atan2(y, x) is like tan⁡−1(y/x)\tan^{-1}(y/x), but it looks at the signs of both inputs, so it gets the angle right even when the target is behind the shoulder. The second atan2 is that inside angle: measured along the first link, the claw is L1+L2cos⁡θ2L_1 + L_2\cos\theta_2 out and L2sin⁡θ2L_2\sin\theta_2 to the side.

The drivetrain version​

The mecanum mixer from Mecanum Kinematics is inverse kinematics too. The goal is a robot motion (vx,vy,ω)(v_x, v_y, \omega), and the wheel powers are:

[wFLwFRwBLwBR]=[1−1−11−1−11−1−11−1−1][vxvyω]\begin{bmatrix} w_{FL} \\ w_{FR} \\ w_{BL} \\ w_{BR} \end{bmatrix} = \begin{bmatrix} 1 & -1 & -1 \\ 1 & \phantom{-}1 & \phantom{-}1 \\ 1 & \phantom{-}1 & -1 \\ 1 & -1 & \phantom{-}1 \end{bmatrix} \begin{bmatrix} v_x \\ v_y \\ \omega \end{bmatrix}

A desired motion goes in and motor commands come out. The difference is that they're wheel speeds instead of joint angles.

A clean FTC implementation​

Keep inverse kinematics out of the OpMode. The OpMode asks for a target, and a separate class returns the angles. That way you can test the math at a desk without the robot.

A check you can do in your head: both links 10 in, target (10,10)(10, 10). Then cos⁡θ2=0\cos\theta_2 = 0. Elbow up uses sin⁡θ2=−1\sin\theta_2 = -1 and gives shoulder 90°90°, elbow −90°-90°, with the elbow at (0,10)(0, 10). That's the pose from the forward-kinematics check above. Elbow down gives shoulder 0°0°, elbow +90°+90°, with the elbow at (10,0)(10, 0). Both put the claw at (10,10)(10, 10).

Two-link arm inverse kinematics
// x, y is the wrist target measured FROM THE SHOULDER PIVOT. Units are
// arbitrary but must match link1 and link2; angles come back in radians.
ArmSolution solve(double x, double y, boolean elbowUp) {
if (!(link1 > 0 && link2 > 0)) throw new IllegalStateException("links");

double r = Math.hypot(x, y);

double minReach = Math.abs(link1 - link2); // cannot fold tighter
double maxReach = link1 + link2; // cannot stretch past
boolean reachable = minReach <= r && r <= maxReach;

double cosElbow = (r * r - link1 * link1 - link2 * link2)
/ (2 * link1 * link2);
cosElbow = clamp(cosElbow, -1, 1); // nearest pose, not a NaN

double sinMagnitude = Math.sqrt(Math.max(0, 1 - cosElbow * cosElbow));
double sinElbow = elbowUp ? -sinMagnitude : sinMagnitude;
double elbow = Math.atan2(sinElbow, cosElbow);

double shoulder = Math.atan2(y, x)
- Math.atan2(link2 * sinElbow, link1 + link2 * cosElbow);

return new ArmSolution(shoulder, elbow, reachable);
}

The sign of sinElbow is the only difference between the two answers. With +x+x to the right and +y+y up, a negative sine puts the elbow above the line from shoulder to target.

ExerciseThe workspace is a ring

minReach and maxReach describe a ring, not a filled circle. When does the hole in the middle disappear, and what does that suggest about choosing link lengths?

Show solution

When link1 == link2, |link1 - link2| is zero, and the arm can fold back to reach the pivot. Equal links give the most usable area for a given total length, which is why most two-link arms are close to equal. Unequal links leave a dead zone right in front of the shoulder, which is often where you wanted to pick something up.

ExerciseDerive both angle lines

The triangle has sides link1, link2, and r. Write c2=a2+b2−2abcos⁡Cc^2 = a^2 + b^2 - 2ab\cos C with CC the inside angle at the elbow, and solve for cos⁡C\cos C. The code's elbow angle is measured from straight, not the inside angle. How are they related?

Then the shoulder: draw the triangle, mark the angle between "straight at the target" and "along link 1," and check that its two sides are link2*sin(elbow) and link1 + link2*cos(elbow).

Show solution

The law of cosines gives cos⁡C=(l12+l22−r2)/(2l1l2)\cos C = (l_1^2 + l_2^2 - r^2)/(2l_1l_2). That's the negative of the code's formula, because the code's angle is π−C\pi - C: zero when the arm is straight, not when it's folded shut, and cos⁡(π−C)=−cos⁡C\cos(\pi - C) = -\cos C.

For the shoulder, drop a perpendicular from the wrist onto the line of link 1. Along link 1 the wrist is l1+l2cos⁡(elbow)l_1 + l_2\cos(\text{elbow}) out, and across it l2sin⁡(elbow)l_2\sin(\text{elbow}), so their atan2 is the angle between aiming at the target and aiming along link 1.

It has to be atan2, not atan, because atan gives the same answer for a target in front of the shoulder and one directly behind it.

Then turn angles into commands for your mechanism. That part is specific to your robot: encoder offsets, gear ratios, feedforward, joint limits, and homing all go there, not in the solver.

FTCUsing the solver inside a subsystem
void moveWristTo(double xInches, double yInches) {
ArmSolution target = solve(xInches, yInches, true);

if (!target.reachable()) {
report("arm target outside reachable workspace");
return; // do not command a clamped pose
}

shoulderController.setTarget(toShoulderTicks(target.shoulder()));
elbowController.setTarget(toElbowTicks(target.elbow()));

// Radians are unreadable while tuning.
log(Math.toDegrees(target.shoulder()), Math.toDegrees(target.elbow()));
}
ExerciseWhere the offsets live

The solver returns angles and knows nothing about encoders, gear ratios, zero offsets, or motor limits. What does toShoulderTicks need to know, and where does a mechanical zero offset belong?

Show solution

Ticks per revolution, the gear ratio between motor and joint, which direction is positive, and the encoder reading at the geometric zero. Those are all facts about this robot, and none of them are in solve, which is why solve can be checked with a calculator.

The zero offset goes in toShoulderTicks. If you put it in the solver, the math stops matching the diagram it came from, and every check by hand disagrees with the code.

Reachable doesn't mean safe

The math only knows the link lengths. It doesn't know your claw hits the slides or that the intake cable gets pulled tight. After the solver gives an answer, still apply joint limits, collision rules, and current limits before moving anything.

Nearly straight or nearly folded is touchy

When the arm is almost straight or almost folded, a small change in the target can need a big change in the joint angles. These poses are called singularities. Avoid paths that pass through them unless the controller is slow, current-limited, and well tested.

Check angles before tuning

Most IK bugs are angle-convention bugs: the zero is off, an encoder sign is flipped, or inches and ticks got mixed up. Before tuning anything, print the measured shoulder and elbow angles, move the arm by hand, and check that positive angles go the way the equations say.

Sources & references
  • Kevin Lynch and Frank Park, Modern Robotics - open robotics text and video series covering forward/inverse kinematics and robot motion.
  • 21229 Quality Control, The Basics of Inverse Kinematics in Robotics - visual robotics introduction to degree-of-freedom analysis, multiple IK configurations, constraints, and common IK algorithms.
  • WPILib, Kinematics and Odometry
    • drivetrain kinematics in the same "desired chassis motion to actuator motion" framing used here.
  • Tyler Veness, Controls Engineering in FRC
    • practical controls framing for robotics mechanisms and drivetrains.