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
- Model
- Real RAM
- Properties
- deterministicnot exactonline
- Evidence
- Proof sketchCode in 1 languages
- 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 joints with positions . It obeys the manipulator equation
where is the symmetric positive definite joint-space inertia matrix, holds the Coriolis and centripetal torques, is the gravity torque, and is the vector of joint torques (in N m for revolute joints, N for prismatic joints). There is no contact, so the term is zero. A desired trajectory is given, twice differentiable, with and known. The task is to choose from the measured and so that the error goes to zero.
The algorithm
The controller has a model , , of the arm, and gain matrices and , both symmetric positive definite. In practice they are diagonal.
COMPUTED-TORQUE(q, q', q_d, q_d', q_d'', K_p, K_d)- 1
e ← q_d - q - 2
e' ← q_d' - q' - 3
a ← q_d'' + K_d e' + K_p e ▷ the wanted joint acceleration - 4
τ ← M̂(q) a + Ĉ(q, q') q' + ĝ(q) ▷ inverse dynamics of the model - 5
return τ
Line L4 is the inverse dynamics of the model at the state with the acceleration . A program does not need to form : one call of the recursive Newton-Euler algorithm with the input returns the same torque. With the model equal to the arm, the closed loop is , so , and line L3 gives
With diagonal gains and , joint is a second-order system with natural frequency in rad/s and damping ratio . The choice (critical damping) gives the fastest decay without overshoot for a given .
Example
Take the planar 2R arm with m and point masses of 1 kg at the link ends, in a vertical plane, with measured from the horizontal and m/s. Its inertia matrix is
and its gravity torque is in N m.
The arm is at rest at , and the set point is rad with . The gains are and , so rad/s and .
| step | value |
|---|---|
| L1, L2 | , |
| L3 | rad/s |
| L4, inertia | , so |
| L4, gravity | , and at rest |
| L5 | 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 rad. This is 0.0736 at s, 0.0287 at s and 0.0040 at 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 with and . A simulation with RK4 and a step of 2 ms comes to rest at . The steady error rad is the solution of , which follows from the closed loop at rest.
Correctness
The claim is: if , and , and , are symmetric positive definite, then , is a globally exponentially stable equilibrium of the error dynamics. The proof substitutes line L4 into the manipulator equation, uses that is invertible to get , and then shows that the linear system 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 , where is symmetric positive definite for each . The desired trajectory is twice differentiable. We write .
Lemma 1 (closed loop). Assume that the model is exact: , and for all , . Then under COMPUTED-TORQUE, .
Proof. Lines L1 and L2 compute and , and line L3 computes . Line L4 gives . We put this into the manipulator equation. The terms and are on both sides and cancel, so . Because is invertible, . Then .
Theorem. If also and are symmetric positive definite, then the state of the closed loop is globally exponentially stable.
Proof. Write . By Lemma 1, with
This is a linear time-invariant system, so it is enough to show that each eigenvalue of has a negative real part. Let with and . The first block row gives , so . The second block row gives . We multiply on the left by , the conjugate transpose of , and set , and . Because and are symmetric positive definite and , the numbers , and are real and positive. Then
The roots of a quadratic with positive real coefficients have negative real parts. If the discriminant is negative, both roots have the real part . If it is not negative, both roots are real, their product is positive and their sum is negative, so both are negative. Thus is Hurwitz, and there are constants and with for every initial state.
Diagonal gains. If and with , the equations separate into . The characteristic polynomial is , and the solution is . One checks this by differentiation: with , , which equals at , and .
Steady offset under a model error. Let the set point be constant (), and let the model be wrong. Suppose that the closed loop comes to rest at some . Then and , so the manipulator equation gives . Line L4 at rest gives , because and . Hence , provided is invertible. The offset is zero when the gravity model is exact at the rest pose, and for its size is of order , because , and 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 joints of a serial chain:
- Lines L1 to L3 take operations with diagonal gains, and with full gain matrices.
- Line L4 is one inverse dynamics call. The recursive Newton-Euler algorithm does it in operations for a serial chain or a tree, without forming .
- If a program forms by the composite rigid body algorithm and multiplies it by , the step costs instead. No linear system is solved, because the law needs , not .
The extra memory is . The law runs once per control period, so the 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 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 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 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 . 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 term. Only the product matters here, so any valid factorization works. Do not mix this with the Slotine-Li law, which needs 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 , 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.
# 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
- 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)
- 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)
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag YZJ7AN Edit this page History Report an error Markdown