Algorithm Search⌘K

Algorithmcore

Computed-torque control

Also called inverse dynamics control.

Cancels the arm dynamics with an inverse dynamics model and adds PD feedback, so that with an exact model the joint error obeys a linear, decoupled equation.

Time
O(n) per_step
Space
O(n) extra
Where
n = the number of joints of a serial chain; the bounds are for one control step
Properties
deterministicnot exactonline
Origin
J. Y. S. Luh, Michael W. Walker, Richard P. Paul, 1980

Computed-torque control is a feedback law for a robot arm that uses a model of the arm's dynamics to cancel its nonlinear inertia, Coriolis and gravity terms, and then adds a PD feedback on the joint error. If the model is exact, the joint error of every joint obeys the same linear, decoupled second-order equation, so the designer picks its decay rate directly. It is the robot case of feedback linearization, and it needs the inverse dynamics of the arm at each control step. It differs from PD control with gravity compensation, which cancels only gravity, and from passivity-based control, which keeps the inertia of the arm instead of cancelling it.

In plain words

A robot arm is hard to steer because its joints push on each other. When the elbow moves, the shoulder feels a reaction, and gravity pulls with a force that changes with the pose. A simple spring-like feedback on each joint fights these effects without knowing them, so the arm lags behind the path.

Computed-torque control asks the model of the arm one question at each step: which joint torques give the arm the acceleration that I want now? It then sends those torques. The wanted acceleration is the acceleration of the path plus a correction that pulls the arm back toward the path. If the model is right, the arm behaves like a set of independent, simple systems, one for each joint, and the error dies away at a rate that the designer chooses.

The problem

The arm has nn joints with positions q∈Rnq \in \mathbb{R}^n. It obeys the manipulator equation

M(q)q¨+C(q,q˙)q˙+g(q)=τ,M(q)\ddot q + C(q, \dot q)\dot q + g(q) = \tau,

where M(q)M(q) is the symmetric positive definite joint-space inertia matrix, C(q,q˙)q˙C(q, \dot q)\dot q holds the Coriolis and centripetal torques, g(q)g(q) is the gravity torque, and τ\tau is the vector of joint torques (in N m for revolute joints, N for prismatic joints). There is no contact, so the term JTFextJ^T F_{\text{ext}} is zero. A desired trajectory qd(t)q_d(t) is given, twice differentiable, with q˙d\dot q_d and q¨d\ddot q_d known. The task is to choose τ\tau from the measured qq and q˙\dot q so that the error e=qd−qe = q_d - q goes to zero.

The algorithm

The controller has a model M^\hat M, C^\hat C, g^\hat g of the arm, and gain matrices KpK_p and KdK_d, both symmetric positive definite. In practice they are diagonal.

  1. COMPUTED-TORQUE(q, q', q_d, q_d', q_d'', K_p, K_d)
  2. 1e ← q_d - q
  3. 2e' ← q_d' - q'
  4. 3a ← q_d'' + K_d e' + K_p e ▷ the wanted joint acceleration
  5. 4τ ← M̂(q) a + Ĉ(q, q') q' + ĝ(q) ▷ inverse dynamics of the model
  6. 5return τ

Line L4 is the inverse dynamics of the model at the state (q,q˙)(q, \dot q) with the acceleration aa. A program does not need to form M^\hat M: one call of the recursive Newton-Euler algorithm with the input (q,q˙,a)(q, \dot q, a) returns the same torque. With the model equal to the arm, the closed loop is M(q)q¨=M(q)aM(q)\ddot q = M(q) a, so q¨=a\ddot q = a, and line L3 gives

e¨+Kde˙+Kpe=0.\ddot e + K_d \dot e + K_p e = 0.

With diagonal gains Kp=diag⁡(ωi2)K_p = \operatorname{diag}(\omega_i^2) and Kd=diag⁡(2ζiωi)K_d = \operatorname{diag}(2 \zeta_i \omega_i), joint ii is a second-order system with natural frequency ωi\omega_i in rad/s and damping ratio ζi\zeta_i. The choice ζi=1\zeta_i = 1 (critical damping) gives the fastest decay without overshoot for a given ωi\omega_i.

Example

Take the planar 2R arm with L1=L2=1L_1 = L_2 = 1 m and point masses of 1 kg at the link ends, in a vertical plane, with q1q_1 measured from the horizontal and g=9.81g = 9.81 m/s2^2. Its inertia matrix is

M(q)=[3+2cos⁡q21+cos⁡q21+cos⁡q21],M(q) = \begin{bmatrix} 3 + 2\cos q_2 & 1 + \cos q_2 \\ 1 + \cos q_2 & 1 \end{bmatrix},

and its gravity torque is g(q)=(2gcos⁡q1+gcos⁡(q1+q2), gcos⁡(q1+q2))g(q) = (2 g \cos q_1 + g \cos(q_1 + q_2),\ g \cos(q_1 + q_2)) in N m.

The arm is at rest at q=(0,π/2)q = (0, \pi/2), and the set point is qd=(0.1,π/2)q_d = (0.1, \pi/2) rad with q˙d=q¨d=0\dot q_d = \ddot q_d = 0. The gains are Kp=25IK_p = 25 I and Kd=10IK_d = 10 I, so ω=5\omega = 5 rad/s and ζ=1\zeta = 1.

step value
L1, L2 e=(0.1,0)e = (0.1, 0), e˙=(0,0)\dot e = (0, 0)
L3 a=(2.5,0)a = (2.5, 0) rad/s2^2
L4, inertia M(q)=[[3,1],[1,1]]M(q) = [[3, 1], [1, 1]], so Ma=(7.5,2.5)M a = (7.5, 2.5)
L4, gravity g(q)=(19.62,0)g(q) = (19.62, 0), and Cq˙=0C \dot q = 0 at rest
L5 τ=(27.12,2.5)\tau = (27.12, 2.5) N m

The second joint needs a torque of 2.5 N m although its own error is zero: it must hold back the reaction of the first joint. With an exact model, the error of joint 1 is e1(t)=0.1(1+5t)e−5te_1(t) = 0.1 (1 + 5 t) e^{-5 t} rad. This is 0.0736 at t=0.2t = 0.2 s, 0.0287 at t=0.5t = 0.5 s and 0.0040 at t=1t = 1 s, and the error of joint 2 stays zero.

Now let the second mass be 1.5 kg, while the model keeps 1 kg, and let the set point be qd=(0.3,0.4)q_d = (0.3, 0.4) with Kp=100IK_p = 100 I and Kd=20IK_d = 20 I. A simulation with RK4 and a step of 2 ms comes to rest at q=(0.29004,0.38078)q = (0.29004, 0.38078). The steady error (0.00996,0.01922)(0.00996, 0.01922) rad is the solution of Kpe=M^(q)−1(g(q)−g^(q))K_p e = \hat M(q)^{-1}(g(q) - \hat g(q)), which follows from the closed loop at rest.

Correctness

The claim is: if M^=M\hat M = M, C^q˙=Cq˙\hat C \dot q = C \dot q and g^=g\hat g = g, and KpK_p, KdK_d are symmetric positive definite, then e=0e = 0, e˙=0\dot e = 0 is a globally exponentially stable equilibrium of the error dynamics. The proof substitutes line L4 into the manipulator equation, uses that M(q)M(q) is invertible to get q¨=a\ddot q = a, and then shows that the linear system e¨+Kde˙+Kpe=0\ddot e + K_d \dot e + K_p e = 0 has a Hurwitz state matrix. The full proof, and the formula for the steady error under a constant model error, are in the proof file.

Proof: Computed-torque control makes the joint error exponentially stablefull

The arm obeys M(q)q¨+C(q,q˙)q˙+g(q)=τM(q)\ddot q + C(q, \dot q)\dot q + g(q) = \tau, where M(q)M(q) is symmetric positive definite for each qq. The desired trajectory qdq_d is twice differentiable. We write e=qd−qe = q_d - q.

Lemma 1 (closed loop). Assume that the model is exact: M^(q)=M(q)\hat M(q) = M(q), C^(q,q˙)q˙=C(q,q˙)q˙\hat C(q, \dot q)\dot q = C(q, \dot q)\dot q and g^(q)=g(q)\hat g(q) = g(q) for all qq, q˙\dot q. Then under COMPUTED-TORQUE, e¨+Kde˙+Kpe=0\ddot e + K_d \dot e + K_p e = 0.

Proof. Lines L1 and L2 compute ee and e˙\dot e, and line L3 computes a=q¨d+Kde˙+Kpea = \ddot q_d + K_d \dot e + K_p e. Line L4 gives τ=M(q)a+C(q,q˙)q˙+g(q)\tau = M(q) a + C(q, \dot q)\dot q + g(q). We put this τ\tau into the manipulator equation. The terms Cq˙C\dot q and gg are on both sides and cancel, so M(q)q¨=M(q)aM(q)\ddot q = M(q) a. Because M(q)M(q) is invertible, q¨=a\ddot q = a. Then e¨=q¨d−q¨=q¨d−a=−Kde˙−Kpe\ddot e = \ddot q_d - \ddot q = \ddot q_d - a = -K_d \dot e - K_p e. □\square

Theorem. If also KpK_p and KdK_d are symmetric positive definite, then the state (e,e˙)=(0,0)(e, \dot e) = (0, 0) of the closed loop is globally exponentially stable.

Proof. Write z=(e,e˙)∈R2nz = (e, \dot e) \in \mathbb{R}^{2n}. By Lemma 1, z˙=Az\dot z = A z with

A=[0I−Kp−Kd].A = \begin{bmatrix} 0 & I \\ -K_p & -K_d \end{bmatrix}.

This is a linear time-invariant system, so it is enough to show that each eigenvalue of AA has a negative real part. Let Az=λzA z = \lambda z with z=(x,y)≠0z = (x, y) \ne 0 and λ∈C\lambda \in \mathbb{C}. The first block row gives y=λxy = \lambda x, so x≠0x \ne 0. The second block row gives −Kpx−λKdx=λ2x-K_p x - \lambda K_d x = \lambda^2 x. We multiply on the left by x∗x^*, the conjugate transpose of xx, and set p=x∗Kpxp = x^* K_p x, d=x∗Kdxd = x^* K_d x and s=x∗xs = x^* x. Because KpK_p and KdK_d are symmetric positive definite and x≠0x \ne 0, the numbers pp, dd and ss are real and positive. Then

sλ2+dλ+p=0.s \lambda^2 + d \lambda + p = 0.

The roots of a quadratic with positive real coefficients have negative real parts. If the discriminant d2−4spd^2 - 4 s p is negative, both roots have the real part −d/(2s)<0-d/(2s) < 0. If it is not negative, both roots are real, their product p/sp/s is positive and their sum −d/s-d/s is negative, so both are negative. Thus AA is Hurwitz, and there are constants k≥1k \ge 1 and σ>0\sigma > 0 with ∥z(t)∥≤ke−σt∥z(0)∥\|z(t)\| \le k e^{-\sigma t}\|z(0)\| for every initial state. ■\blacksquare

Diagonal gains. If Kp=diag⁡(ωi2)K_p = \operatorname{diag}(\omega_i^2) and Kd=diag⁡(2ωi)K_d = \operatorname{diag}(2 \omega_i) with ωi>0\omega_i > 0, the equations separate into e¨i+2ωie˙i+ωi2ei=0\ddot e_i + 2 \omega_i \dot e_i + \omega_i^2 e_i = 0. The characteristic polynomial is (λ+ωi)2(\lambda + \omega_i)^2, and the solution is ei(t)=(ei(0)+(e˙i(0)+ωiei(0))t)e−ωite_i(t) = (e_i(0) + (\dot e_i(0) + \omega_i e_i(0)) t) e^{-\omega_i t}. One checks this by differentiation: with c=e˙i(0)+ωiei(0)c = \dot e_i(0) + \omega_i e_i(0), e˙i=(c−ωiei(0)−ωict)e−ωit\dot e_i = (c - \omega_i e_i(0) - \omega_i c t) e^{-\omega_i t}, which equals e˙i(0)\dot e_i(0) at t=0t = 0, and e¨i+2ωie˙i+ωi2ei=0\ddot e_i + 2\omega_i \dot e_i + \omega_i^2 e_i = 0.

Steady offset under a model error. Let the set point be constant (q˙d=q¨d=0\dot q_d = \ddot q_d = 0), and let the model be wrong. Suppose that the closed loop comes to rest at some qq. Then q˙=0\dot q = 0 and q¨=0\ddot q = 0, so the manipulator equation gives τ=g(q)\tau = g(q). Line L4 at rest gives τ=M^(q)Kpe+g^(q)\tau = \hat M(q) K_p e + \hat g(q), because a=Kpea = K_p e and C^q˙=0\hat C \dot q = 0. Hence Kpe=M^(q)−1(g(q)−g^(q))K_p e = \hat M(q)^{-1}(g(q) - \hat g(q)), provided M^(q)\hat M(q) is invertible. The offset is zero when the gravity model is exact at the rest pose, and for Kp=kIK_p = k I its size is of order 1/k1/k, because gg, g^\hat g and M^−1\hat M^{-1} are bounded on a bounded set of poses. This statement assumes a rest point; it does not prove that one is reached.

Complexity

In the real-RAM model, with nn joints of a serial chain:

  • Lines L1 to L3 take O(n)O(n) operations with diagonal gains, and O(n2)O(n^2) with full gain matrices.
  • Line L4 is one inverse dynamics call. The recursive Newton-Euler algorithm does it in O(n)O(n) operations for a serial chain or a tree, without forming MM.
  • If a program forms M(q)M(q) by the composite rigid body algorithm and multiplies it by aa, the step costs O(n2)O(n^2) instead. No linear system is solved, because the law needs MaM a, not M−1M^{-1}.

The extra memory is O(n)O(n). The law runs once per control period, so the O(n)O(n) bound is what makes it practical for arms with many joints.

Properties

  • Decoupled linear error dynamics. With an exact model, each joint error is a separate linear second-order system, independent of the pose and of the speed.
  • Model based. The quality depends on the model. A constant error in the gravity model gives a constant offset; an error in the inertia model gives errors that grow with the acceleration of the trajectory.
  • Not robust by design. The law has no term that rejects a constant disturbance. An integral term or a robust or adaptive outer law is needed for that.
  • Nominal stability is exponential. This is stronger than the asymptotic stability of PD control with gravity compensation, but it holds only for the exact model.

Variants and improvements

  • Task-space form. Replace the joint error by the end-effector error and set a=J−1(x¨d+Kdx~˙+Kpx~−J˙q˙)a = J^{-1}(\ddot x_d + K_d \dot{\tilde x} + K_p \tilde x - \dot J \dot q) for a square, nonsingular Jacobian. This is resolved-acceleration control (Luh, Walker and Paul 1980). The operational space formulation is the version for redundant arms.
  • Integral action. Lynch and Park add Ki∫e dtK_i \int e\, dt to line L3, which removes the offset from a constant model error when the gains make the third-order error dynamics stable.
  • Feedforward with PD. Compute M^(qd)q¨d+C^(qd,q˙d)q˙d+g^(qd)\hat M(q_d)\ddot q_d + \hat C(q_d, \dot q_d)\dot q_d + \hat g(q_d) on the desired trajectory, offline if needed, and add a PD term on the error. This needs less computation online, and its stability holds only locally or with large enough gains.
  • Robust and adaptive versions. A sliding mode term handles bounded model errors. The Slotine-Li adaptive controller estimates the dynamic parameters online and has a proof that does not need the inverse of the estimated inertia.

When to use it

Use it when a good dynamic model is known and the arm must follow fast trajectories with small error, for example on a direct drive arm or an arm with a calibrated model after dynamic parameter identification. For slow motions, or set points only, PD control with gravity compensation is simpler and needs only the gravity model. If the payload is unknown, use an adaptive law. For an arm that touches its environment, the motion goal alone is not enough; see impedance control.

Pitfalls

  • Joint friction and motor dynamics. The manipulator equation above has no friction, no gear backlash and no motor dynamics. On a geared arm these effects are often larger than the rigid body terms, and they are not cancelled.
  • High gains and flexibility. The linear error equation tempts the designer to choose a large ω\omega. Joint flexibility, sensor noise and the sampling period limit the gains on a real arm; the limit is found by tests, not derived.
  • A wrong CC term. Only the product C(q,q˙)q˙C(q, \dot q)\dot q matters here, so any valid factorization works. Do not mix this with the Slotine-Li law, which needs CC from the Christoffel symbols.
  • Torque limits. The law assumes that each computed torque can be applied. When a torque saturates, the cancellation fails and the error dynamics are no longer linear.
  • Singular task-space form. The task-space variant needs J−1J^{-1}, which does not exist at a kinematic singularity.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

computed_torque_control.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/computed-torque-control/. The authors are listed in prov/YZJ7AN.jsonl.

"""Computed-torque control of a serial arm, with a planar 2R arm to test it.

The arm obeys the manipulator equation M(q) q'' + C(q, q') q' + g(q) = tau.
The control law is tau = M(q) a + C(q, q') q' + g(q) with
a = qd'' + Kd (qd' - q') + Kp (qd - q). With an exact model the joint error
e = qd - q obeys e'' + Kd e' + Kp e = 0.

Matrices are lists of rows and vectors are lists. Only the standard library.
"""

import math

GRAVITY = 9.81


class TwoLinkArm:
    """Planar 2R arm in a vertical plane, with point masses at the link ends.

    q1 is measured from the horizontal x axis, q2 from link 1. The y axis
    points up, and gravity acts along -y. C is built from the Christoffel
    symbols of the first kind, so M' - 2 C is skew-symmetric.
    """

    def __init__(self, m1=1.0, m2=1.0, l1=1.0, l2=1.0, grav=GRAVITY):
        self.m1, self.m2, self.l1, self.l2, self.grav = m1, m2, l1, l2, grav

    def mass(self, q):
        m1, m2, l1, l2 = self.m1, self.m2, self.l1, self.l2
        c2 = math.cos(q[1])
        a = (m1 + m2) * l1 * l1 + m2 * l2 * l2 + 2.0 * m2 * l1 * l2 * c2
        b = m2 * l2 * l2 + m2 * l1 * l2 * c2
        return [[a, b], [b, m2 * l2 * l2]]

    def coriolis(self, q, qd):
        h = self.m2 * self.l1 * self.l2 * math.sin(q[1])
        return [[-h * qd[1], -h * (qd[0] + qd[1])], [h * qd[0], 0.0]]

    def gravity(self, q):
        m1, m2, l1, l2, gr = self.m1, self.m2, self.l1, self.l2, self.grav
        c1, c12 = math.cos(q[0]), math.cos(q[0] + q[1])
        return [(m1 + m2) * gr * l1 * c1 + m2 * gr * l2 * c12, m2 * gr * l2 * c12]

    def energy(self, q, qd):
        """Kinetic plus potential energy, with zero potential at height 0."""
        m = self.mass(q)
        kin = 0.5 * sum(qd[i] * m[i][j] * qd[j] for i in range(2) for j in range(2))
        y1 = self.l1 * math.sin(q[0])
        y2 = y1 + self.l2 * math.sin(q[0] + q[1])
        return kin + self.grav * (self.m1 * y1 + self.m2 * y2)

    def accel(self, q, qd, tau):
        """Solve M q'' = tau - C q' - g for q''."""
        m = self.mass(q)
        c = self.coriolis(q, qd)
        g = self.gravity(q)
        r = [tau[i] - c[i][0] * qd[0] - c[i][1] * qd[1] - g[i] for i in range(2)]
        return solve2(m, r)


def solve2(a, b):
    """Solve the 2 by 2 system a x = b by Cramer's rule."""
    det = a[0][0] * a[1][1] - a[0][1] * a[1][0]
    if det == 0.0:
        raise ValueError("singular 2 by 2 matrix")
    return [(b[0] * a[1][1] - a[0][1] * b[1]) / det, (a[0][0] * b[1] - b[0] * a[1][0]) / det]


def computed_torque(model, q, qd, q_des, qd_des, qdd_des, kp, kd):
    """Return the joint torques of computed-torque control.

    model gives mass(q), coriolis(q, qd) and gravity(q); it is the model
    that the controller believes, which can differ from the true arm.
    kp and kd are lists of diagonal gains.
    """
    n = len(q)
    e = [q_des[i] - q[i] for i in range(n)]  # @line L1
    ed = [qd_des[i] - qd[i] for i in range(n)]  # @line L2
    a = [qdd_des[i] + kd[i] * ed[i] + kp[i] * e[i] for i in range(n)]  # @line L3
    m = model.mass(q)  # @line L4
    c = model.coriolis(q, qd)
    g = model.gravity(q)
    tau = [sum(m[i][j] * a[j] + c[i][j] * qd[j] for j in range(n)) + g[i] for i in range(n)]
    return tau  # @line L5


def rk4_step(f, t, x, dt):
    """One classical Runge-Kutta step for x' = f(t, x)."""
    k1 = f(t, x)
    k2 = f(t + dt / 2, [x[i] + dt / 2 * k1[i] for i in range(len(x))])
    k3 = f(t + dt / 2, [x[i] + dt / 2 * k2[i] for i in range(len(x))])
    k4 = f(t + dt, [x[i] + dt * k3[i] for i in range(len(x))])
    return [x[i] + dt / 6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) for i in range(len(x))]


def simulate(arm, model, traj, q0, qd0, kp, kd, dt, steps):
    """Simulate the closed loop with RK4 and the control law inside each stage.

    traj(t) returns (q_des, qd_des, qdd_des). Return the list of
    (t, q, qd) at each step, including the start.
    """

    def f(t, x):
        q, qd = x[:2], x[2:]
        qs, qds, qdds = traj(t)
        tau = computed_torque(model, q, qd, qs, qds, qdds, kp, kd)
        return qd + arm.accel(q, qd, tau)

    x = list(q0) + list(qd0)
    out = [(0.0, x[:2], x[2:])]
    for k in range(steps):
        x = rk4_step(f, k * dt, x, dt)
        out.append(((k + 1) * dt, x[:2], x[2:]))
    return out
test_computed_torque_control.py 98 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/computed-torque-control/. The authors are listed in prov/YZJ7AN.jsonl.

import math
import random

from computed_torque_control import TwoLinkArm, computed_torque, rk4_step, simulate, solve2


def traj(t):
    q = [0.5 * math.sin(t), 0.3 * math.cos(2 * t)]
    qd = [0.5 * math.cos(t), -0.6 * math.sin(2 * t)]
    qdd = [-0.5 * math.sin(t), -1.2 * math.cos(2 * t)]
    return q, qd, qdd


def test_mass_matrix_check_values():
    arm = TwoLinkArm()
    for q2, want in ((0.0, [[5, 2], [2, 1]]), (math.pi / 2, [[3, 1], [1, 1]])):
        m = arm.mass([0.3, q2])
        for i in range(2):
            for j in range(2):
                assert abs(m[i][j] - want[i][j]) < 1e-12


def test_model_conserves_energy_without_torque():
    # Independent check of M, C and g: with tau = 0 the energy is constant.
    arm = TwoLinkArm()
    x = [0.4, -0.7, 0.5, -0.2]
    e0 = arm.energy(x[:2], x[2:])
    for k in range(2000):
        x = rk4_step(lambda t, s: s[2:] + arm.accel(s[:2], s[2:], [0.0, 0.0]), k * 1e-3, x, 1e-3)
    assert abs(arm.energy(x[:2], x[2:]) - e0) < 1e-7


def test_skew_symmetry_of_model():
    arm = TwoLinkArm(m1=1.3, m2=0.7, l1=0.9, l2=0.6)
    rng = random.Random(7)
    for _ in range(20):
        q = [rng.uniform(-3, 3) for _ in range(2)]
        qd = [rng.uniform(-2, 2) for _ in range(2)]
        h = 1e-6
        mp = arm.mass([q[i] + h * qd[i] for i in range(2)])
        mm = arm.mass([q[i] - h * qd[i] for i in range(2)])
        mdot = [[(mp[i][j] - mm[i][j]) / (2 * h) for j in range(2)] for i in range(2)]
        c = arm.coriolis(q, qd)
        s = [[mdot[i][j] - 2 * c[i][j] for j in range(2)] for i in range(2)]
        for i in range(2):
            for j in range(2):
                assert abs(s[i][j] + s[j][i]) < 1e-6


def test_exact_model_gives_linear_error_dynamics():
    arm = TwoLinkArm()
    w = 5.0
    kp, kd = [w * w, w * w], [2 * w, 2 * w]
    qs, qds, _ = traj(0.0)
    e0 = [0.2, -0.1]
    ed0 = [-0.1, -0.3]
    q0 = [qs[i] - e0[i] for i in range(2)]
    qd0 = [qds[i] - ed0[i] for i in range(2)]
    out = simulate(arm, arm, traj, q0, qd0, kp, kd, 1e-3, 2000)
    for t, q, _ in out[::100]:
        want_q = traj(t)[0]
        for i in range(2):
            e_closed = (e0[i] + (ed0[i] + w * e0[i]) * t) * math.exp(-w * t)
            assert abs((want_q[i] - q[i]) - e_closed) < 1e-8


def test_wrong_model_leaves_the_predicted_offset():
    arm = TwoLinkArm(m2=1.5)
    model = TwoLinkArm(m2=1.0)
    kp, kd = [100.0, 100.0], [20.0, 20.0]
    goal = [0.3, 0.4]

    def hold(t):
        return goal, [0.0, 0.0], [0.0, 0.0]

    out = simulate(arm, model, hold, [0.0, 0.0], [0.0, 0.0], kp, kd, 2e-3, 3000)
    _, q, qd = out[-1]
    e = [goal[i] - q[i] for i in range(2)]
    assert max(abs(v) for v in qd) < 1e-6
    # At rest: g(q) = Mhat(q) Kp e + ghat(q), so Kp e = Mhat^(-1) (g - ghat).
    dg = [arm.gravity(q)[i] - model.gravity(q)[i] for i in range(2)]
    want = solve2(model.mass(q), dg)
    for i in range(2):
        assert abs(kp[i] * e[i] - want[i]) < 1e-6
    assert max(abs(v) for v in e) > 1e-2


def test_torque_matches_inverse_dynamics_when_on_track():
    # On the desired trajectory the law gives tau = M qd'' + C qd' + g.
    arm = TwoLinkArm()
    qs, qds, qdds = traj(0.7)
    tau = computed_torque(arm, qs, qds, qs, qds, qdds, [9.0, 9.0], [6.0, 6.0])
    acc = arm.accel(qs, qds, tau)
    for i in range(2):
        assert abs(acc[i] - qdds[i]) < 1e-12

History

Model-based torque computation for arms was studied in the 1970s. Luh, Walker and Paul published in 1980 the recursive Newton-Euler scheme that computes the torques online, and the resolved-acceleration control law, which is computed-torque control in task coordinates. The textbooks of Spong, Hutchinson and Vidyasagar, Siciliano et al. and Lynch and Park present the law as inverse dynamics control or computed torque control, as the robot case of feedback linearization.

References

  1. Kevin M. Lynch and Frank C. Park, Modern Robotics, Mechanics, Planning, and Control, Cambridge University Press, 2017, Chapter 11, Section 11.4.2 (motion control of a multi-joint robot: computed torque control) and Section 11.2.2 (second-order linear error dynamics)
  2. Mark W. Spong, Seth Hutchinson and M. Vidyasagar, Robot Modeling and Control, second edition, Wiley, 2020, Chapter 9 (nonlinear and multivariable control, inverse dynamics control)
  3. Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics, Modelling, Planning and Control, Springer, 2009, Chapter 8 (motion control, centralized control, inverse dynamics control)
  4. J. Y. S. Luh, M. W. Walker and R. P. C. Paul, Resolved-acceleration control of mechanical manipulators, IEEE Transactions on Automatic Control 25(3), 1980, pages 468 to 474
  5. J. Y. S. Luh, M. W. Walker and R. P. C. Paul, On-line computational scheme for mechanical manipulators, Journal of Dynamic Systems, Measurement, and Control 102(2), 69-76, 1980, the whole paper (the recursive Newton-Euler computation of the joint torques)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.per_step
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example