Algorithm Search⌘K

Algorithmadvanced

Slotine-Li adaptive controller

Also called adaptive computed torque.

Tracks a joint trajectory while it adapts the dynamic parameters along the regressor. Its stability proof uses the skew symmetry of the arm dynamics.

Time
O(n^2) per_step
Space
O(n^2) extra
Where
n = the number of joints; the arm has 10 n inertial parameters, and the bounds are for one control step with the regressor formed explicitly
Properties
deterministicnot exactonline
Origin
Jean Jacques E. Slotine, Weiping Li, 1987

The Slotine-Li adaptive controller makes a robot arm follow a joint trajectory when the masses, centers of mass and inertias of its links are not known. It uses the fact that the arm dynamics are linear in these parameters, M(q)q¨+C(q,q˙)q˙+g(q)=Y(q,q˙,q¨)πM(q)\ddot q + C(q, \dot q)\dot q + g(q) = Y(q, \dot q, \ddot q)\pi, and updates an estimate π^\hat\pi online from the tracking error. Unlike adaptive versions of computed-torque control, it needs neither the measured joint accelerations nor the inverse of the estimated inertia matrix. Its Lyapunov proof uses the skew symmetry of M˙−2C\dot M - 2C, which places it among the passivity-based controllers. The tracking error goes to zero; the parameter estimate goes to the true value only if the trajectory is rich enough.

In plain words

A controller with a model of the arm can follow a path closely, but only if the model is right. A new payload in the gripper changes the model. This controller starts with a guess of the masses and inertias and keeps correcting the guess while it moves.

It combines the position error and the speed error into one number per joint, called ss. When ss is zero, the position error dies out by itself at a fixed rate. The controller pushes ss toward zero with a model-based torque and a damping term. Each time ss is not zero, it shifts the guessed parameters in the direction that would have reduced ss. A sum of a kinetic energy in ss and the squared parameter error never grows, and this gives the proof.

The problem

The arm has nn joints and 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 with no contact. The matrix CC is built from the Christoffel symbols, so M˙−2C\dot M - 2C is skew-symmetric. By linearity in the dynamic parameters, there is a known regressor matrix YY with

M(q)a+C(q,q˙)v+g(q)=Y(q,q˙,v,a) πM(q) a + C(q, \dot q) v + g(q) = Y(q, \dot q, v, a)\,\pi

for all qq, q˙\dot q, vv, aa, where π∈Rp\pi \in \mathbb{R}^p is a constant but unknown parameter vector. A desired trajectory qd(t)q_d(t) is given, with qdq_d, q˙d\dot q_d and q¨d\ddot q_d bounded. The task is to choose τ\tau from qq, q˙\dot q and the trajectory, without π\pi, so that q−qd→0q - q_d \to 0.

The algorithm

The gains are symmetric positive definite matrices Λ\Lambda and KDK_D of size nn and Γ\Gamma of size pp. In practice they are diagonal. The state of the controller is the estimate π^\hat\pi.

  1. SLOTINE-LI(q, q', q_d, q_d', q_d'', π̂, Λ, K_D, Γ)
  2. 1q̃ ← q - q_d
  3. 2q_r' ← q_d' - Λ q̃ ▷ reference velocity
  4. 3q_r'' ← q_d'' - Λ (q' - q_d') ▷ its derivative, without q''
  5. 4s ← q' - q_r' ▷ s = q̃' + Λ q̃
  6. 5τ ← Y(q, q', q_r', q_r'') π̂ - K_D s
  7. 6π̂' ← -Γ Y(q, q', q_r', q_r'')ᵀ s ▷ adaptation law
  8. 7return τ, π̂'

The program integrates π^\hat\pi with the rate of line L6 between control steps. Line L3 uses q~˙=q˙−q˙d\dot{\tilde q} = \dot q - \dot q_d, so the law needs no acceleration measurement. The velocity slot of YY gets q˙r\dot q_r, not q˙\dot q: the model torque in line L5 is M^q¨r+C^(q,q˙)q˙r+g^\hat M \ddot q_r + \hat C(q, \dot q)\dot q_r + \hat g. The name "adaptive computed torque" is also used for the earlier scheme of Craig, Hsu and Sastry, which needs the acceleration and the inverse of M^\hat M.

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 g=9.81g = 9.81 m/s2^2. Its dynamics are linear in π=(a1,a2,a3,b1,b2)\pi = (a_1, a_2, a_3, b_1, b_2), where a1=(m1+m2)l12+m2l22a_1 = (m_1 + m_2) l_1^2 + m_2 l_2^2, a2=m2l1l2a_2 = m_2 l_1 l_2, a3=m2l22a_3 = m_2 l_2^2, b1=(m1+m2)l1gb_1 = (m_1 + m_2) l_1 g and b2=m2l2gb_2 = m_2 l_2 g. The true value is π=(3,1,1,19.62,9.81)\pi = (3, 1, 1, 19.62, 9.81), and the controller starts with half of it, π^=(1.5,0.5,0.5,9.81,4.905)\hat\pi = (1.5, 0.5, 0.5, 9.81, 4.905). The desired trajectory is qd(t)=(0.5sin⁡t, 0.3cos⁡2t)q_d(t) = (0.5 \sin t,\ 0.3 \cos 2t) rad. The gains are Λ=5I\Lambda = 5 I, KD=20IK_D = 20 I and Γ=diag⁡(1,1,1,20,20)\Gamma = \operatorname{diag}(1, 1, 1, 20, 20).

At t=0t = 0 the arm is at rest at q=(0.2,0.1)q = (0.2, 0.1), and qd=(0,0.3)q_d = (0, 0.3), q˙d=(0.5,0)\dot q_d = (0.5, 0), q¨d=(0,−1.2)\ddot q_d = (0, -1.2).

line value
L1 q~=(0.2,−0.2)\tilde q = (0.2, -0.2)
L2 q˙r=(0.5−1, 0+1)=(−0.5,1)\dot q_r = (0.5 - 1,\ 0 + 1) = (-0.5, 1)
L3 q¨r=(0+2.5, −1.2−0)=(2.5,−1.2)\ddot q_r = (0 + 2.5,\ -1.2 - 0) = (2.5, -1.2)
L4 s=(0.5,−1)s = (0.5, -1)
L5 τ=(9.34,26.58)\tau = (9.34, 26.58) N m
L6 π^˙=(−1.25,0.60,1.90,−9.80,9.55)\dot{\hat\pi} = (-1.25, 0.60, 1.90, -9.80, 9.55)

A simulation of the closed loop with RK4 and a step of 4 ms gives these values, where VV is the Lyapunov function of the next section.

time (s) largest joint error (rad) VV π^\hat\pi
0 0.200 4.509 (1.50,0.50,0.50,9.81,4.91)(1.50, 0.50, 0.50, 9.81, 4.91)
1 0.046 1.543 (1.85,0.80,0.01,16.44,11.86)(1.85, 0.80, 0.01, 16.44, 11.86)
2 0.012 1.372 (1.82,0.83,0.04,17.37,11.44)(1.82, 0.83, 0.04, 17.37, 11.44)
5 0.011 1.048 (1.85,0.98,0.22,19.54,11.58)(1.85, 0.98, 0.22, 19.54, 11.58)
10 0.003 0.883 (1.84,1.01,0.37,19.21,9.42)(1.84, 1.01, 0.37, 19.21, 9.42)

The error falls, and VV never grows. The estimate moves toward the truth, but after 10 s it is still far from it in a1a_1 and a3a_3: the tracking does not need the true parameters.

Correctness

Write π~=π^−π\tilde\pi = \hat\pi - \pi. Putting line L5 into the arm equation and subtracting Yπ=Mq¨r+Cq˙r+gY\pi = M\ddot q_r + C\dot q_r + g gives the closed loop

M(q)s˙+C(q,q˙)s+KDs=Yπ~.M(q)\dot s + C(q, \dot q)s + K_D s = Y\tilde\pi.

The function V=12sTM(q)s+12π~TΓ−1π~V = \tfrac12 s^T M(q) s + \tfrac12 \tilde\pi^T \Gamma^{-1}\tilde\pi has the derivative V˙=−sTKDs+12sT(M˙−2C)s=−sTKDs\dot V = -s^T K_D s + \tfrac12 s^T(\dot M - 2C)s = -s^T K_D s, because the adaptation law of line L6 cancels the cross term sTYπ~s^T Y \tilde\pi and M˙−2C\dot M - 2C is skew-symmetric. So VV never grows, and ss and π~\tilde\pi stay bounded. The integral of sTKDss^T K_D s is at most V(0)V(0), and Barbalat's lemma then gives s→0s \to 0. Since q~˙=−Λq~+s\dot{\tilde q} = -\Lambda\tilde q + s, also q~→0\tilde q \to 0 and q~˙→0\dot{\tilde q} \to 0. The full proof is in the proof file.

The skew symmetry is used with the vector ss, not with q˙\dot q. This needs the matrix CC from the Christoffel symbols; another factorization with the same product Cq˙C\dot q does not work.

Proof: The Slotine-Li controller drives the tracking error to zerofull

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 with q∈Rnq \in \mathbb{R}^n. We assume:

  1. M(q)M(q) is symmetric, and there are constants 0<μ≤μˉ0 < \mu \le \bar\mu with μ xTx≤xTM(q)x≤μˉ xTx\mu\, x^T x \le x^T M(q) x \le \bar\mu\, x^T x for all qq, xx.
  2. CC is the Christoffel matrix, so M˙−2C\dot M - 2C is skew-symmetric: xT(M˙−2C)x=0x^T(\dot M - 2C)x = 0 for every vector xx, where M˙=ddtM(q(t))\dot M = \frac{d}{dt}M(q(t)).
  3. MM, CC, gg and YY are continuous, and Y(q,q˙,v,a)π=M(q)a+C(q,q˙)v+g(q)Y(q, \dot q, v, a)\pi = M(q)a + C(q, \dot q)v + g(q) for all arguments, with a constant π\pi. On bounded sets of arguments they are bounded.
  4. qdq_d, q˙d\dot q_d and q¨d\ddot q_d are bounded, and Λ\Lambda, KDK_D, Γ\Gamma are symmetric positive definite.

We write q~=q−qd\tilde q = q - q_d, π~=π^−π\tilde\pi = \hat\pi - \pi, and YY for Y(q,q˙,q˙r,q¨r)Y(q, \dot q, \dot q_r, \ddot q_r).

Lemma 1 (closed loop). Under SLOTINE-LI, q~˙=−Λq~+s\dot{\tilde q} = -\Lambda\tilde q + s and M(q)s˙+C(q,q˙)s+KDs=Yπ~M(q)\dot s + C(q, \dot q)s + K_D s = Y\tilde\pi.

Proof. Line L4 gives s=q˙−q˙r=q˙−q˙d+Λq~=q~˙+Λq~s = \dot q - \dot q_r = \dot q - \dot q_d + \Lambda\tilde q = \dot{\tilde q} + \Lambda\tilde q, which is the first equation. The derivative of q˙r=q˙d−Λq~\dot q_r = \dot q_d - \Lambda\tilde q is q¨d−Λ(q˙−q˙d)\ddot q_d - \Lambda(\dot q - \dot q_d), which is the value q¨r\ddot q_r of line L3. So s˙=q¨−q¨r\dot s = \ddot q - \ddot q_r. By assumption 3, Yπ=Mq¨r+Cq˙r+gY\pi = M\ddot q_r + C\dot q_r + g, and line L5 gives τ=Yπ+Yπ~−KDs\tau = Y\pi + Y\tilde\pi - K_D s. The arm equation becomes Mq¨+Cq˙+g=Mq¨r+Cq˙r+g+Yπ~−KDsM\ddot q + C\dot q + g = M\ddot q_r + C\dot q_r + g + Y\tilde\pi - K_D s. We subtract and use s˙=q¨−q¨r\dot s = \ddot q - \ddot q_r and s=q˙−q˙rs = \dot q - \dot q_r to get Ms˙+Cs=Yπ~−KDsM\dot s + Cs = Y\tilde\pi - K_D s. □\square

Lemma 2. The function V=12sTM(q)s+12π~TΓ−1π~V = \tfrac12 s^T M(q)s + \tfrac12\tilde\pi^T\Gamma^{-1}\tilde\pi satisfies V˙=−sTKDs\dot V = -s^T K_D s.

Proof. π\pi is constant, so π~˙=π^˙=−ΓYTs\dot{\tilde\pi} = \dot{\hat\pi} = -\Gamma Y^T s by line L6. MM and Γ−1\Gamma^{-1} are symmetric, so

V˙=sTMs˙+12sTM˙s+π~TΓ−1π~˙=sT(Yπ~−Cs−KDs)+12sTM˙s−π~TYTs.\dot V = s^T M\dot s + \tfrac12 s^T\dot M s + \tilde\pi^T\Gamma^{-1}\dot{\tilde\pi} = s^T(Y\tilde\pi - Cs - K_D s) + \tfrac12 s^T\dot M s - \tilde\pi^T Y^T s.

The terms sTYπ~s^T Y\tilde\pi and π~TYTs\tilde\pi^T Y^T s are the same number and cancel. What is left is −sTKDs+12sT(M˙−2C)s-s^T K_D s + \tfrac12 s^T(\dot M - 2C)s, and the last term is zero by assumption 2. □\square

Theorem. Under assumptions 1 to 4, every solution of the closed loop is bounded, π^\hat\pi is bounded, and q~(t)→0\tilde q(t) \to 0 and q~˙(t)→0\dot{\tilde q}(t) \to 0 as t→∞t \to \infty.

Proof. Step 1 (boundedness). By Lemma 2, V(t)≤V(0)V(t) \le V(0). By assumption 1, 12μ∥s∥2≤V\tfrac12\mu\|s\|^2 \le V, so ss is bounded, and π~\tilde\pi is bounded because Γ−1\Gamma^{-1} is positive definite. The equation q~˙=−Λq~+s\dot{\tilde q} = -\Lambda\tilde q + s is a linear system with the Hurwitz matrix −Λ-\Lambda and a bounded input, so q~\tilde q is bounded, and then q~˙\dot{\tilde q} is bounded. With assumption 4, qq, q˙\dot q, q˙r\dot q_r and q¨r\ddot q_r are bounded. So YY, CC and gg are bounded by assumption 3. By Lemma 1 and assumption 1, s˙=M−1(Yπ~−Cs−KDs)\dot s = M^{-1}(Y\tilde\pi - Cs - K_D s) is bounded, since ∥M−1∥≤1/μ\|M^{-1}\| \le 1/\mu.

Step 2 (s→0s \to 0). Integrating Lemma 2 gives ∫0tsTKDs dτ=V(0)−V(t)≤V(0)\int_0^t s^T K_D s\, d\tau = V(0) - V(t) \le V(0). So the function w(t)=s(t)TKDs(t)≥0w(t) = s(t)^T K_D s(t) \ge 0 has a finite integral over [0,∞)[0, \infty). Its derivative 2sTKDs˙2 s^T K_D\dot s is bounded by step 1, so ww is uniformly continuous. By Barbalat's lemma, w(t)→0w(t) \to 0, and since KDK_D is positive definite, s(t)→0s(t) \to 0.

Step 3 (q~→0\tilde q \to 0). Let κ>0\kappa > 0 be the smallest eigenvalue of Λ\Lambda. Then ∥e−Λt∥=e−κt\|e^{-\Lambda t}\| = e^{-\kappa t} because Λ\Lambda is symmetric. For t≥T≥0t \ge T \ge 0,

q~(t)=e−Λ(t−T)q~(T)+∫Tte−Λ(t−τ)s(τ) dτ.\tilde q(t) = e^{-\Lambda(t - T)}\tilde q(T) + \int_T^t e^{-\Lambda(t - \tau)}s(\tau)\, d\tau.

Given ε>0\varepsilon > 0, choose TT with ∥s(τ)∥≤ε\|s(\tau)\| \le \varepsilon for τ≥T\tau \ge T. Then ∥q~(t)∥≤e−κ(t−T)∥q~(T)∥+ε/κ\|\tilde q(t)\| \le e^{-\kappa(t - T)}\|\tilde q(T)\| + \varepsilon/\kappa, which is less than 2ε/κ2\varepsilon/\kappa for tt large. So q~→0\tilde q \to 0, and q~˙=−Λq~+s→0\dot{\tilde q} = -\Lambda\tilde q + s \to 0. ■\blacksquare

Remark on the parameters. The proof shows that π^\hat\pi is bounded, not that it converges to π\pi. When q~→0\tilde q \to 0 and s→0s \to 0, Lemma 1 gives Yπ~→0Y\tilde\pi \to 0 along the trajectory. If YY along the desired trajectory is persistently exciting, this forces π~→0\tilde\pi \to 0; otherwise π~\tilde\pi can stay away from zero.

Complexity

In the real-RAM model, a serial arm with nn joints has p=10np = 10 n inertial parameters (fewer after reduction to the base inertial parameters). The regressor YY is an n×pn \times p matrix, but the torque of joint ii depends only on the parameters of links ii to nn, so it has O(n2)O(n^2) nonzero entries. Forming YY, the product Yπ^Y\hat\pi and the product YTsY^T s take O(n2)O(n^2) operations per control step with recursive kinematics, and the memory is O(n2)O(n^2). The 2R example has p=5p = 5 and constant cost.

Properties

  • Tracking without parameters. The tracking error goes to zero for any constant parameter error, from any initial state, with no bound on the gains other than positive definiteness.
  • No acceleration, no inverse. The law uses only qq, q˙\dot q and the trajectory, and it never inverts the estimated inertia matrix, which could become singular during adaptation.
  • Asymptotic, not exponential. Without more conditions, the proof gives only q~→0\tilde q \to 0. With persistent excitation of YY along the desired trajectory, the estimate converges to π\pi and the convergence becomes exponential.
  • Bounded parameter estimate. The estimate stays bounded but need not converge. For a set point, it usually stops at a wrong value that matches the gravity torque at the goal.

Variants and improvements

  • Passivity-based form. With π^\hat\pi fixed to a known model, the law τ=M^q¨r+C^q˙r+g^−KDs\tau = \hat M\ddot q_r + \hat C\dot q_r + \hat g - K_D s is the passivity-based tracking controller (Spong, Hutchinson and Vidyasagar call the adaptive case passivity-based adaptive control).
  • Robust modifications. With noise or unmodeled dynamics, the estimate can drift. A dead zone on ss, projection of π^\hat\pi onto a known box, or sigma modification keeps the estimate bounded.
  • Composite adaptation. Slotine and Li also combined the tracking error with a prediction error of the torque in the update, which uses the data better.
  • Physically consistent estimates. Projection onto parameters with positive masses and valid inertia tensors keeps M^\hat M positive definite, which some variants need.
  • Other adaptive schemes. Craig, Hsu and Sastry adapt computed-torque control; their proof needs the joint acceleration and a bounded M^−1\hat M^{-1}. For general systems, see adaptive control and model-reference adaptive control.

When to use it

Use it when the arm follows trajectories with an unknown or changing payload, and the structure of the dynamics is known exactly. Typical cases are direct drive and low-friction arms where the rigid body model dominates. If the parameters are known well from dynamic parameter identification, a fixed-model law is simpler. If the model structure is wrong, for example with large friction, add friction terms to YY or use a robust law such as sliding mode control.

Pitfalls

  • Wrong CC. Using a factorization of the Coriolis terms that is not the Christoffel one breaks the proof, because the proof needs sT(M˙−2C)s=0s^T(\dot M - 2C)s = 0 for s≠q˙s \ne \dot q.
  • Drift of the estimate. Bounded noise or a small model error can make π^\hat\pi drift slowly without bound in the pure law. Use one of the robust modifications on a real arm.
  • Large adaptation gains. A large Γ\Gamma makes the estimate react to noise and can excite unmodeled dynamics. Choose Γ\Gamma by scale: parameters in kg m2^2 and in N m need different gains.
  • Expecting identification. A converged tracking error does not mean a converged parameter estimate. Do not use π^\hat\pi as an identified model unless the trajectory was persistently exciting.
  • Discrete time. The proof is for continuous time. A slow control rate or a crude integration of line L6 adds errors that the proof does not cover.

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.

slotine_li_adaptive_control.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/slotine-li-adaptive-control/. The authors are listed in prov/RN7MPK.jsonl.

"""The Slotine-Li adaptive controller, with a planar 2R arm to test it.

The arm obeys M(q) q'' + C(q, q') q' + g(q) = tau, and the dynamics are
linear in a parameter vector: M(q) a + C(q, q') v + g(q) = Y(q, q', v, a) pi.
The law uses the reference velocity qr' = qd' - Lambda (q - qd) and the
sliding variable s = q' - qr':

    tau = Y(q, q', qr', qr'') pi_hat - Kd s,    pi_hat' = -Gamma Y^T s.

C must be the Christoffel matrix, so that M' - 2 C is skew-symmetric.
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.

    The parameter vector is pi = (a1, a2, a3, b1, b2) with
    a1 = (m1 + m2) l1^2 + m2 l2^2, a2 = m2 l1 l2, a3 = m2 l2^2,
    b1 = (m1 + m2) l1 g and b2 = m2 l2 g.
    """

    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 params(self):
        m1, m2, l1, l2, gr = self.m1, self.m2, self.l1, self.l2, self.grav
        return [
            (m1 + m2) * l1 * l1 + m2 * l2 * l2,
            m2 * l1 * l2,
            m2 * l2 * l2,
            (m1 + m2) * l1 * gr,
            m2 * l2 * gr,
        ]

    def mass(self, q):
        a1, a2, a3, _, _ = self.params()
        c2 = math.cos(q[1])
        return [[a1 + 2 * a2 * c2, a3 + a2 * c2], [a3 + a2 * c2, a3]]

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

    def gravity(self, q):
        _, _, _, b1, b2 = self.params()
        c1, c12 = math.cos(q[0]), math.cos(q[0] + q[1])
        return [b1 * c1 + b2 * c12, b2 * c12]

    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 regressor(q, qd, v, a):
    """Y(q, q', v, a) with Y pi = M(q) a + C(q, q') v + g(q) for the 2R arm."""
    c1, c2 = math.cos(q[0]), math.cos(q[1])
    s2 = math.sin(q[1])
    c12 = math.cos(q[0] + q[1])
    row1 = [
        a[0],
        2 * c2 * a[0] + c2 * a[1] - s2 * qd[1] * v[0] - s2 * (qd[0] + qd[1]) * v[1],
        a[1],
        c1,
        c12,
    ]
    row2 = [0.0, c2 * a[0] + s2 * qd[0] * v[0], a[0] + a[1], 0.0, c12]
    return [row1, row2]


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 slotine_li(q, qd, q_des, qd_des, qdd_des, pi_hat, lam, kd, gamma):
    """Return (tau, pi_hat_dot) of the Slotine-Li law.

    lam, kd and gamma are lists of diagonal gains. pi_hat is the current
    estimate of the parameter vector.
    """
    n = len(q)
    qt = [q[i] - q_des[i] for i in range(n)]  # @line L1
    qr_d = [qd_des[i] - lam[i] * qt[i] for i in range(n)]  # @line L2
    qr_dd = [qdd_des[i] - lam[i] * (qd[i] - qd_des[i]) for i in range(n)]  # @line L3
    s = [qd[i] - qr_d[i] for i in range(n)]  # @line L4
    y = regressor(q, qd, qr_d, qr_dd)  # @line L5
    p = len(pi_hat)
    tau = [sum(y[i][k] * pi_hat[k] for k in range(p)) - kd[i] * s[i] for i in range(n)]
    pi_dot = [-gamma[k] * sum(y[i][k] * s[i] for i in range(n)) for k in range(p)]  # @line L6
    return tau, pi_dot  # @line L7


def lyapunov(arm, q, qd, q_des, qd_des, pi_hat, lam, gamma):
    """V = 1/2 s^T M s + 1/2 (pi_hat - pi)^T Gamma^(-1) (pi_hat - pi)."""
    s = [qd[i] - qd_des[i] + lam[i] * (q[i] - q_des[i]) for i in range(2)]
    m = arm.mass(q)
    pi = arm.params()
    kin = 0.5 * sum(s[i] * m[i][j] * s[j] for i in range(2) for j in range(2))
    est = 0.5 * sum((pi_hat[k] - pi[k]) ** 2 / gamma[k] for k in range(len(pi)))
    return kin + est


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 closed_loop(arm, traj, lam, kd, gamma):
    """Right side of the closed loop on the state (q, q', pi_hat)."""

    def f(t, x):
        q, qd, pi_hat = x[:2], x[2:4], x[4:]
        qs, qds, qdds = traj(t)
        tau, pi_dot = slotine_li(q, qd, qs, qds, qdds, pi_hat, lam, kd, gamma)
        return qd + arm.accel(q, qd, tau) + pi_dot

    return f


def simulate(arm, traj, q0, qd0, pi0, lam, kd, gamma, dt, steps):
    """Simulate with RK4. Return the list of (t, q, qd, pi_hat)."""
    f = closed_loop(arm, traj, lam, kd, gamma)
    x = list(q0) + list(qd0) + list(pi0)
    out = [(0.0, x[:2], x[2:4], x[4:])]
    for k in range(steps):
        x = rk4_step(f, k * dt, x, dt)
        out.append(((k + 1) * dt, x[:2], x[2:4], x[4:]))
    return out
test_slotine_li_adaptive_control.py 116 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/slotine-li-adaptive-control/. The authors are listed in prov/RN7MPK.jsonl.

import math
import random

from slotine_li_adaptive_control import (
    TwoLinkArm,
    closed_loop,
    lyapunov,
    regressor,
    rk4_step,
    simulate,
    slotine_li,
)

LAM = [5.0, 5.0]
KD = [20.0, 20.0]
GAMMA = [1.0, 1.0, 1.0, 20.0, 20.0]


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_regressor_matches_model():
    rng = random.Random(3)
    for _ in range(20):
        arm = TwoLinkArm(*[rng.uniform(0.5, 2.0) for _ in range(4)])
        q, qd, v, a = ([rng.uniform(-3, 3) for _ in range(2)] for _ in range(4))
        y = regressor(q, qd, v, a)
        pi = arm.params()
        m, c, g = arm.mass(q), arm.coriolis(q, qd), arm.gravity(q)
        for i in range(2):
            want = sum(m[i][j] * a[j] + c[i][j] * v[j] for j in range(2)) + g[i]
            assert abs(sum(y[i][k] * pi[k] for k in range(5)) - want) < 1e-12


def test_tracking_with_wrong_parameters_and_energy_balance():
    arm = TwoLinkArm()
    pi0 = [0.5 * p for p in arm.params()]
    dt = 4e-3
    out = simulate(arm, traj, [0.2, 0.1], [0.0, 0.0], pi0, LAM, KD, GAMMA, dt, 2500)
    vs, ws = [], []
    for t, q, qd, ph in out:
        qs, qds, _ = traj(t)
        vs.append(lyapunov(arm, q, qd, qs, qds, ph, LAM, GAMMA))
        s = [qd[i] - qds[i] + LAM[i] * (q[i] - qs[i]) for i in range(2)]
        ws.append(sum(KD[i] * s[i] ** 2 for i in range(2)))
    for k in range(len(vs) - 1):
        assert vs[k + 1] <= vs[k] + 1e-9
    # V(0) - V(T) equals the integral of s^T Kd s (Simpson's rule).
    inner = sum((4 if k % 2 else 2) * ws[k] for k in range(1, len(ws) - 1))
    dissipated = dt / 3 * (ws[0] + inner + ws[-1])
    assert abs((vs[0] - vs[-1]) - dissipated) < 1e-3
    t, q, _, _ = out[-1]
    qs = traj(t)[0]
    assert max(abs(q[i] - qs[i]) for i in range(2)) < 0.02


def test_lyapunov_derivative_formula():
    arm = TwoLinkArm(m1=0.8, m2=1.3, l1=1.1, l2=0.7)
    f = closed_loop(arm, traj, LAM, KD, GAMMA)
    rng = random.Random(9)
    for _ in range(10):
        t = rng.uniform(0, 5)
        x = [rng.uniform(-2, 2) for _ in range(4)] + [p * rng.uniform(0.3, 1.7) for p in arm.params()]
        h = 1e-5
        xp, xm = rk4_step(f, t, x, h), rk4_step(f, t, x, -h)

        def v(y, tt):
            qs, qds, _ = traj(tt)
            return lyapunov(arm, y[:2], y[2:4], qs, qds, y[4:], LAM, GAMMA)

        dv = (v(xp, t + h) - v(xm, t - h)) / (2 * h)
        qs, qds, _ = traj(t)
        s = [x[2 + i] - qds[i] + LAM[i] * (x[i] - qs[i]) for i in range(2)]
        want = -sum(KD[i] * s[i] ** 2 for i in range(2))
        assert abs(dv - want) < 1e-4 * (1 + abs(want))


def test_exact_parameters_on_the_sliding_surface():
    # With pi_hat = pi, Gamma = 0 and s(0) = 0, s stays 0 and q~ = q~(0) e^(-lambda t).
    arm = TwoLinkArm()
    qs, qds, _ = traj(0.0)
    qt0 = [0.1, -0.2]
    q0 = [qs[i] + qt0[i] for i in range(2)]
    qd0 = [qds[i] - LAM[i] * qt0[i] for i in range(2)]
    out = simulate(arm, traj, q0, qd0, arm.params(), LAM, KD, [0.0] * 5, 2e-3, 1000)
    for t, q, _, _ in out[::100]:
        qs = traj(t)[0]
        for i in range(2):
            assert abs((q[i] - qs[i]) - qt0[i] * math.exp(-LAM[i] * t)) < 1e-9


def test_set_point_does_not_identify_the_parameters():
    arm = TwoLinkArm()
    goal = [0.4, 0.6]

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

    pi0 = [0.5 * p for p in arm.params()]
    out = simulate(arm, hold, [0.0, 0.0], [0.0, 0.0], pi0, LAM, KD, GAMMA, 6e-3, 2500)
    _, q, qd, ph = out[-1]
    assert max(abs(goal[i] - q[i]) for i in range(2)) < 1e-3
    # The inertial parameters a1, a2, a3 stop changing far from the truth.
    pi = arm.params()
    assert abs(ph[1] - pi[1]) > 1.0
    _, _, _, ph_mid = out[1250]
    assert max(abs(ph[k] - ph_mid[k]) for k in range(3)) < 1e-2
    tau, _ = slotine_li(q, qd, goal, [0.0, 0.0], [0.0, 0.0], ph, LAM, KD, GAMMA)
    assert all(math.isfinite(v) for v in tau)

History

Adaptive control of manipulators was an active topic in the mid-1980s. Craig, Hsu and Sastry published an adaptive computed-torque law in the International Journal of Robotics Research in 1987, after a conference version in 1986. In the same year and journal, Slotine and Li published their law, which uses the reference velocity q˙r\dot q_r and the skew symmetry of the dynamics, and needs no acceleration and no inverse of M^\hat M. Textbooks now present it as the main example of adaptive robot control and of passivity-based design.

References

  1. Jean-Jacques E. Slotine and Weiping Li, On the adaptive control of robot manipulators, The International Journal of Robotics Research 6(3), 1987, pages 49 to 59
  2. John J. Craig, Ping Hsu and S. Shankar Sastry, Adaptive control of mechanical manipulators, The International Journal of Robotics Research 6(2), 1987, pages 16 to 28
  3. Mark W. Spong, Seth Hutchinson and M. Vidyasagar, Robot Modeling and Control, second edition, Wiley, 2020, Chapter 9, Section 9.4 (passivity-based control) and Section 9.4.2 (passivity-based adaptive control)
  4. Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics, Modelling, Planning and Control, Springer, 2009, Chapter 8 (motion control, adaptive control)

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