Inverse Kinematics
Mediumdifficulty
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 . For an arm, it's a claw position 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:
| Case | What it means on a robot | What the solver must do |
|---|---|---|
| Too few mechanism freedoms | A one-joint arm cannot place a claw at every point. | Reject unreachable targets or redesign the mechanism. |
| Same number of freedoms | A two-link arm can often solve with geometry. | Use a closed-form trig/vector solution when possible. |
| Extra mechanism freedoms | A 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.
21229 Quality Control explains the same position-to-angles idea visually.
Open videoForward 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 (shoulder to elbow) and a second of length (elbow to claw). The shoulder angle is measured from horizontal. The elbow angle is measured from the first link, so means the arm is straight.
The first link is the hypotenuse of a right triangle, so it reaches:
The second link points at , because it's mounted on the end of the first:
Add the two together for the claw position:
Check it: with both links 10 in, (straight up) and (elbow bent back down to horizontal), the claw is at and .
Inverse kinematics: position to angles
Robot code usually needs the reverse: the routine says "put the claw at ," and the motors need angles.
Draw a triangle from shoulder to elbow to claw. The side from the shoulder to the claw has length
by Pythagoras. If is more than , the target is out of reach. If it's less than , it's too close, because one link can't fold through the other.
The triangle's three sides are , , and , so the law of cosines gives the angle at the elbow:
(The sign is flipped from the textbook form because is measured from straight, not the inside angle of the triangle.) Then:
The 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":
atan2(y, x) is like , 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 out and to the side.
The drivetrain version
The mecanum mixer from Mecanum Kinematics is inverse kinematics too. The goal is a robot motion , and the wheel powers are:
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 . Then . Elbow up uses and gives shoulder , elbow , with the elbow at . That's the pose from the forward-kinematics check above. Elbow down gives shoulder , elbow , with the elbow at . Both put the claw at .
// 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 to the right and
up, a negative sine puts the elbow above the line from shoulder to target.
When 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
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.
The triangle has sides link1, link2, and r. Write with the inside
angle at the elbow, and solve for . 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 The law of cosines gives . That's the negative of the code's
formula, because the code's angle is : zero when the arm is straight, not when it's folded
shut, and . For the shoulder, drop a perpendicular from the wrist onto the line of link 1. Along link 1 the wrist is
out, and across it , so their It has to be link2*sin(elbow) and link1 + link2*cos(elbow).Show solution
atan2 is the angle
between aiming at the target and aiming along link 1.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.
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()));
}
The solver returns angles and knows nothing about encoders, gear ratios, zero offsets, or motor limits.
What does 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
The zero offset goes in toShoulderTicks need to know, and where does a mechanical zero offset belong?Show solution
solve, which is why solve can be checked with a calculator.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.
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.
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.
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.
- 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.