Inverse Kinematics Calculator (2-Link Planar Arm)
Compute joint angles θ₁ and θ₂ to reach a target (x,y) with a 2-link planar arm.
Returns both elbow-up and elbow-down solutions or reports unreachable.
The inverse problem
Inverse kinematics asks: given a target Cartesian position for the end-effector, what joint angles will put it there? This is the harder of the two kinematics directions because most reachable positions have multiple valid joint configurations.
For a 2-link planar arm with link lengths L₁ and L₂, given a target (x, y), the closed-form solution is:
θ₂ = ±arccos((x² + y² − L₁² − L₂²) / (2 L₁ L₂)) θ₁ = atan2(y, x) − atan2(L₂ sin θ₂, L₁ + L₂ cos θ₂)
The ± in θ₂ gives the two solutions: positive sign is “elbow down” (elbow bent away from the direct line to target), negative sign is “elbow up.” Both place the gripper at the same (x, y); the elbow is on opposite sides of the shoulder-target line.
Why two solutions exist
Visualize: shoulder at origin, target at (x, y). Draw the chord from shoulder to target. There are two triangles you can build with sides L₁ (upper arm) and L₂ (forearm) sharing this chord: one with the elbow above the chord, one below. Both are geometrically valid, both give the same gripper position, but the joint angles differ.
This is not unique to 2-link arms. Industrial 6-DOF arms typically have 8 to 16 valid joint configurations for a given gripper pose, because each joint has its own ambiguities. Picking the right configuration (avoiding collisions, minimizing motor strain, preferring elbow-down for stability) is a major part of motion planning.
The reachable workspace
The target must lie within the annulus from |L₁ − L₂| to L₁ + L₂ from the base. The cosine in the θ₂ formula is undefined (or, strictly, outside [-1, 1]) outside this range:
- If x² + y² > (L₁ + L₂)², the target is beyond maximum reach. No solution.
- If x² + y² < (L₁ − L₂)², the target is too close to the base. No solution.
- If x² + y² = (L₁ + L₂)², the arm is fully extended: θ₂ = 0 exactly, unique solution.
- If x² + y² = (L₁ − L₂)², the arm is fully folded: θ₂ = ±180°, unique solution.
This calculator reports “unreachable” with the specific reason when the target falls outside the annulus.
Atan2 and quadrant handling
The formula uses atan2(y, x) rather than arctan(y/x) because atan2 correctly handles all four quadrants. Plain arctan returns angles only in (−90°, 90°), losing the quadrant information when x is negative or y crosses through zero. Atan2 returns the full (−180°, 180°] range.
In code: Math.atan2(y, x) is the JavaScript standard library function. The first argument is the y-component, the second is x. The result is the angle in radians from positive x-axis to the point (x, y), measured counterclockwise.
A worked example: reach a point at (0.6, 0.4)
Setup: L₁ = 0.5 m, L₂ = 0.3 m. Target: (0.6, 0.4).
Check reachability: distance = √(0.36 + 0.16) = √0.52 ≈ 0.721 m. Max reach = 0.8, min reach = 0.2. Within range.
cos θ₂ = (0.36 + 0.16 − 0.25 − 0.09) / (2 × 0.5 × 0.3) = 0.18 / 0.30 = 0.600 θ₂ = ±arccos(0.6) = ±53.13°
Solution 1 (elbow down, θ₂ = +53.13°): θ₁ = atan2(0.4, 0.6) − atan2(0.3 × sin(53.13°), 0.5 + 0.3 × cos(53.13°)) = atan2(0.4, 0.6) − atan2(0.24, 0.68) = 33.69° − 19.44° = 14.25°
Solution 2 (elbow up, θ₂ = −53.13°): θ₁ = atan2(0.4, 0.6) − atan2(0.3 × sin(−53.13°), 0.5 + 0.3 × cos(−53.13°)) = 33.69° − atan2(−0.24, 0.68) = 33.69° − (−19.44°) = 33.69° + 19.44° = 53.13°
Both put the gripper exactly at (0.6, 0.4). Elbow positions differ:
- Solution 1: elbow at (0.485, 0.123)
- Solution 2: elbow at (0.3, 0.4)
Choose the configuration that fits your collision constraints, joint-limit constraints, and motion path.
Choosing between elbow up and elbow down
For pick-and-place with a fixed table, elbow-up is often preferred (elbow points away from the table, less collision risk). For ceiling-mounted arms, elbow-down is the safer default. The choice also depends on:
- Joint range limits: real joints cannot rotate freely past ±180° in most cases. Some configurations may not be physically reachable due to mechanical stops.
- Singularity avoidance: near θ₂ = 0 (fully extended) and θ₂ = ±180° (fully folded), small changes in target position cause large joint-angle changes. These are singular configurations and dangerous for motion control.
- Energy efficiency: arms holding a load against gravity may prefer one configuration that puts the load over a stronger axis.
Why IK is hard in higher dimensions
For 2 DOF planar arms, the closed-form solution is straightforward (just shown). For 3 DOF planar (an arm with a wrist), there is a 1-parameter family of configurations for each end-effector pose, requiring an extra constraint to pick a unique solution. For 6 DOF spatial arms with full 3D position and orientation, closed-form solutions exist for some kinematic structures (like the classic PUMA arm) but require careful derivation, and “redundant” arms (7+ DOF) have infinitely many solutions.
For arbitrary kinematic structures, iterative methods are used: Jacobian inverse, Jacobian transpose, damped least squares, or cyclic coordinate descent. These solve IK numerically by repeatedly applying forward kinematics and correcting toward the target. The math gets more involved but the principle is the same as the 2-link case: find joint angles that place the end-effector where you want.
Companion calculator
For the forward problem (given joint angles, find end-effector position), use the Forward Kinematics Calculator. The two pair naturally: forward gives the workspace; inverse plans motion to specific targets.