Gravity Torque of a 2-Link Arm

~14 mincode completion

Implement gravity_torque(l1, l2, m1, m2, q1, q2, g) returning the array [tau1, tau2].

Examples

Stretched horizontally: the worst case for both motors

Input
gravity_torque(1, 1, 1, 1, 0, 0, 10)
Output
[20, 5]

Straight up: the load is balanced and both torques vanish

Input
gravity_torque(1, 1, 1, 1, 1.5707963267948966, 0, 10)
Output
[0, 0]

Link 2 folded vertical: joint 2 is free, joint 1 still carries link 1

Input
gravity_torque(1, 1, 1, 1, 0, 1.5707963267948966, 10)
Output
[15, 0]

Hints

Hint 1

Convert the input with before doing elementwise work.

Hint 2

Reach for the half length to the centre of mass rather than the full link length.

Requirements

  • g: gravitational acceleration

  • Return (2,) array [tau1, tau2]

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~14 min

6 employers weight this skill

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

Python
import numpy as np


def gravity_torque(l1, l2, m1, m2, q1, q2, g):
    """
    Static torques needed to hold a 2-link arm against gravity.

    Masses are point masses at the midpoint of each link, gravity acts
    along -y.

    Args:
        l1, l2: link lengths
        m1, m2: link masses
        q1, q2: joint angles in radians
        g:      gravitational acceleration

    Returns:
        (2,) array [tau1, tau2]
    """
    # 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.