Inverse Kinematics of a 2-Link Arm

~18 mincode completion

Implement ik_2link(l1, l2, x, y, elbow_up) returning the array [q1, q2] in radians.

Examples

Fully stretched along x: both joints zero

Input
ik_2link(1, 1, 2, 0, True)
Output
[0, 0]

Target (1, 1), elbow up: q1 = 0, q2 = pi/2

Input
ik_2link(1, 1, 1, 1, True)
Output
[0, 1.5708]

Same target, elbow down: q1 = pi/2, q2 = -pi/2

Input
ik_2link(1, 1, 1, 1, False)
Output
[1.5708, -1.5708]

Hints

Hint 1

bounds an array in one call.

Hint 2

Reach for atan2 rather than acos for q1.

Requirements

  • elbow_up: True for sin(q2) >= 0, False for sin(q2) <= 0

  • Return array [q1, q2] in radians

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~18 min

6 employers weight this skill

4 autonomy companies, 2 defense companies. Top match scores 92.

Python
import numpy as np


def ik_2link(l1, l2, x, y, elbow_up):
    """
    Closed-form inverse kinematics of a planar two-link arm.

    Args:
        l1, l2:   link lengths
        x, y:     target position (reachable)
        elbow_up: True for sin(q2) >= 0, False for sin(q2) <= 0

    Returns:
        array [q1, q2] in radians
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.