Algorithm Search⌘K

Algorithmadvanced

Super-twisting algorithm

Also called second-order sliding mode, super-twisting controller.

A sliding mode law whose switching acts on the derivative of the control, so the control is continuous and the sliding variable and its derivative reach zero in finite time.

Time
O(N) worst
Space
O(1) extra
Where
N = the number of sampling instants
Properties
deterministicnot exactonlineiterative
Origin
Arie Levant, Jaime A. Moreno, Marisol Osorio, 1993

The super-twisting algorithm is a continuous sliding mode law for a sliding variable ss whose derivative is s˙=u+d(t)\dot s = u + d(t), where dd is an unknown disturbance with a bounded derivative, ∣d˙∣≤L|\dot d| \le L. It uses the control

u=−k1∣s∣1/2sgn⁡(s)+w,w˙=−k2sgn⁡(s).u = -k_1 |s|^{1/2} \operatorname{sgn}(s) + w, \qquad \dot w = -k_2 \operatorname{sgn}(s).

The switching acts on w˙\dot w, not on uu, so uu is a continuous function of time. For gains that are large enough compared with LL, both ss and s˙\dot s reach 00 in finite time, and the integral term ww then equals −d-d exactly. Compared with the first order law of sliding mode control, it removes the discontinuity of the control and reduces chattering, at the price of a stronger assumption on dd.

In plain words

A plain sliding mode controller pushes with full force left or right, so its output jumps all the time. The super-twisting controller has two parts. One part pushes back with a force that grows with the square root of the error, so it is strong far away and soft near the target. The other part slowly builds up a correction, like the integral part of a PID controller, but it changes at a fixed rate in the direction of the error. This slow part learns the unknown disturbance and cancels it. Because the jumps happen only in the rate of the slow part, the output itself is smooth. The controller still reaches the target exactly in a finite time, as long as the disturbance does not change too fast.

The problem

Input. A measured sliding variable s(t)s(t) with s˙=u+d(t)\dot s = u + d(t), where dd is unknown, absolutely continuous, and ∣d˙(t)∣≤L|\dot d(t)| \le L for almost every tt. A sampling period h>0h > 0. Gains k1>0k_1 > 0 and k2>0k_2 > 0.

Output. At each sampling instant tj=jht_j = j h, a control value uju_j, held over [tj,tj+1)[t_j, t_{j+1}), such that in the continuous-time limit s=s˙=0s = \dot s = 0 after a finite time, and with sampling ∣s∣=O(h2)|s| = O(h^2) and ∣s˙∣=O(h)|\dot s| = O(h) after a finite time.

The sliding variable usually comes from a surface s=σ(x)s = \sigma(x) of relative degree one with respect to the input, as in sliding mode control. Then dd collects the known part of s˙\dot s that is not cancelled, and the uncertainty, and the condition ∣d˙∣≤L|\dot d| \le L must hold along the motion.

The algorithm

  1. SUPER-TWISTING-STEP(s, w, k_1, k_2, h)
  2. 1u ← -k_1 |s|^{1/2} sgn(s) + w
  3. 2w ← w - h k_2 sgn(s)
  4. 3return u

The state ww starts at w0w_0 (often 00) and is kept between calls. Line L1 computes the control from the current sample and the current integral state. Line L2 advances the integral state by one explicit Euler step of w˙=−k2sgn⁡(s)\dot w = -k_2 \operatorname{sgn}(s). Line L3 returns the control, which is held until the next sample.

In continuous time, put v=w+dv = w + d, so that

s˙=−k1∣s∣1/2sgn⁡(s)+v,v˙=−k2sgn⁡(s)+d˙(t).\dot s = -k_1 |s|^{1/2} \operatorname{sgn}(s) + v, \qquad \dot v = -k_2 \operatorname{sgn}(s) + \dot d(t).

At s=0s = 0 the right side is discontinuous, and solutions are taken in the sense of Filippov. The pair (s,v)(s, v) reaches (0,0)(0, 0) in finite time, and v=0v = 0 means w=−dw = -d.

Example

Let s˙=u+sin⁡t\dot s = u + \sin t, so L=1L = 1, with s(0)=1s(0) = 1, w0=0w_0 = 0, k1=k2=2k_1 = k_2 = 2 and h=0.01h = 0.01. The plant is integrated with 4th order Runge-Kutta with step hh, and the control is computed at the start of each step and held. The certificate below holds, because k2=2>L+2L2/k12=1.5k_2 = 2 > L + 2L^2 / k_1^2 = 1.5.

tt ss ww −d(t)-d(t)
0.5 0.151849 -1.00 -0.479426
1 0.002585 -0.76 -0.841471
1.5 -0.000131 -1.00 -0.997495
2 0.000023 -0.92 -0.909297
3 -0.000031 -0.16 -0.141120
4 -0.000063 0.76 0.756802

Measurements from this run and two more:

  • ∣s∣≤0.05|s| \le 0.05 for good from t=0.59t = 0.59.
  • For t≥6t \ge 6, the maximum of ∣s∣|s| is 4.35⋅10−44.35 \cdot 10^{-4} for h=0.01h = 0.01, 1.33⋅10−41.33 \cdot 10^{-4} for h=0.005h = 0.005 and 2.97⋅10−52.97 \cdot 10^{-5} for h=0.0025h = 0.0025, that is between 4.3h24.3 h^2 and 5.3h25.3 h^2. The accuracy is of order h2h^2, as the theory says.
  • ww follows −sin⁡t-\sin t within 0.0450.045 for t≥6t \ge 6. It moves by hk2=0.02h k_2 = 0.02 per step, so it can only follow in steps of that size.
  • For comparison, the first order law u=−1.5sgn⁡(s)u = -1.5 \operatorname{sgn}(s) with the same hh keeps ∣s∣≤0.0235|s| \le 0.0235, about 50 times larger, and its control jumps by 33 at almost every step.

The Lyapunov bound on the convergence time for these gains is 132.98132.98, far above the measured time below 11. The bound is a guarantee, not an estimate.

Correctness

The full proof is in proofs/correctness.md. Its main steps:

  • Coordinates. With ζ=(∣s∣1/2sgn⁡(s),v)\zeta = (|s|^{1/2} \operatorname{sgn}(s), v) and s≠0s \neq 0, the closed loop is ζ˙=∣ζ1∣−1A(θ)ζ\dot \zeta = |\zeta_1|^{-1} A(\theta) \zeta with A(θ)=[−k1/21/2−k2+θL0]A(\theta) = \begin{bmatrix} -k_1/2 & 1/2 \\ -k_2 + \theta L & 0 \end{bmatrix}, where θ=d˙sgn⁡(s)/L∈[−1,1]\theta = \dot d \operatorname{sgn}(s) / L \in [-1, 1].
  • Theorem (Lyapunov function). Let V=ζTPζV = \zeta^T P \zeta with P=[2k2+k12/2−k1/2−k1/21]P = \begin{bmatrix} 2k_2 + k_1^2/2 & -k_1/2 \\ -k_1/2 & 1 \end{bmatrix} (the matrix of Moreno and Osorio). Then V˙=−∣ζ1∣−1ζTQ(θ)ζ\dot V = -|\zeta_1|^{-1} \zeta^T Q(\theta) \zeta, with det⁡Q(θ)=k12k2/2−θLk12/2−θ2L2\det Q(\theta) = k_1^2 k_2 / 2 - \theta L k_1^2 / 2 - \theta^2 L^2. If k2>L+2L2/k12k_2 > L + 2L^2 / k_1^2, then Q(θ)Q(\theta) is positive definite for every θ∈[−1,1]\theta \in [-1, 1].
  • Theorem (finite time). Under that condition, V˙≤−qλmax⁡(P)−1/2V1/2\dot V \le -q \lambda_{\max}(P)^{-1/2} V^{1/2}, where qq is the smaller of the least eigenvalues of Q(1)Q(1) and Q(−1)Q(-1), so VV reaches 00 before T=2λmax⁡(P)1/2V(0)1/2/qT = 2 \lambda_{\max}(P)^{1/2} V(0)^{1/2} / q. The set s=0s = 0, v≠0v \neq 0 is crossed, not followed, so it does not break the argument.
  • Theorem (necessity). If k2<Lk_2 < L, no gain k1k_1 works: for d˙=L\dot d = L, staying on s=0s = 0 needs v=0v = 0, but v˙≥L−k2>0\dot v \ge L - k_2 > 0 there.
  • Theorem (accuracy with sampling). With sampling period hh, ∣s∣=O(h2)|s| = O(h^2) and ∣s˙∣=O(h)|\dot s| = O(h) after a finite time (Levant 1993). The tests measure the factor in front of h2h^2.

The tests check the identity Q(θ)=−(ATP+PA)Q(\theta) = -(A^T P + P A) and the formula for det⁡Q\det Q exactly in Fraction for 200 random gains, the identity V˙=−∣ζ1∣−1ζTQζ\dot V = -|\zeta_1|^{-1} \zeta^T Q \zeta exactly at 200 random points, the table, the h2h^2 accuracy for three periods, the comparison with the first order law, the rule of thumb gains, and the failure for k2<Lk_2 < L.

Proof: The super-twisting algorithm drives s and v to zero in finite time when k2 > L + 2 L^2 / k1^2, and k2 >= L is necessaryfull

The plant is s˙=u+d(t)\dot s = u + d(t), where dd is absolutely continuous and ∣d˙(t)∣≤L|\dot d(t)| \le L for almost every tt. The continuous-time form of lines L1 and L2 is u=−k1∣s∣1/2sgn⁡(s)+wu = -k_1 |s|^{1/2} \operatorname{sgn}(s) + w and w˙=−k2sgn⁡(s)\dot w = -k_2 \operatorname{sgn}(s), with k1,k2>0k_1, k_2 > 0. Put v=w+dv = w + d. Then

s˙=−k1∣s∣1/2sgn⁡(s)+v,v˙=−k2sgn⁡(s)+d˙(t).(1)\dot s = -k_1 |s|^{1/2} \operatorname{sgn}(s) + v, \qquad \dot v = -k_2 \operatorname{sgn}(s) + \dot d(t). \tag{1}

The first equation has a continuous right side. The second is discontinuous at s=0s = 0, and solutions are taken in the sense of Filippov: (s,v)(s, v) is absolutely continuous, the first equation holds for every tt, and v˙(t)∈−k2 Σ(s(t))+d˙(t)\dot v(t) \in -k_2\, \Sigma(s(t)) + \dot d(t) for almost every tt, where Σ(s)={sgn⁡s}\Sigma(s) = \{\operatorname{sgn} s\} for s≠0s \neq 0 and Σ(0)=[−1,1]\Sigma(0) = [-1, 1]. In this proof ∣⋅∣|\cdot| is the absolute value, ∥⋅∥\|\cdot\| is the Euclidean norm on R2\mathbb{R}^2, and λmin⁡\lambda_{\min}, λmax⁡\lambda_{\max} are the least and the largest eigenvalue of a symmetric matrix.

Lemma 1: the closed loop in the coordinates zeta

Let ζ1=∣s∣1/2sgn⁡(s)\zeta_1 = |s|^{1/2} \operatorname{sgn}(s) and ζ2=v\zeta_2 = v. The map s↦ζ1s \mapsto \zeta_1 is continuous and invertible (s=∣ζ1∣ζ1s = |\zeta_1| \zeta_1), so ζ(t)\zeta(t) is continuous in tt. Where s≠0s \neq 0, ζ1\zeta_1 is differentiable in ss with derivative 1/(2∣s∣1/2)=1/(2∣ζ1∣)1 / (2 |s|^{1/2}) = 1 / (2 |\zeta_1|). So, for almost every tt with s(t)≠0s(t) \neq 0,

ζ˙1=−k1ζ1+ζ22∣ζ1∣,ζ˙2=−k2sgn⁡(ζ1)+d˙=(−k2+θL) ζ1∣ζ1∣,\dot \zeta_1 = \frac{-k_1 \zeta_1 + \zeta_2}{2 |\zeta_1|}, \qquad \dot \zeta_2 = -k_2 \operatorname{sgn}(\zeta_1) + \dot d = \frac{(-k_2 + \theta L)\, \zeta_1}{|\zeta_1|},

with θ=d˙sgn⁡(ζ1)/L∈[−1,1]\theta = \dot d \operatorname{sgn}(\zeta_1) / L \in [-1, 1] (any θ\theta if L=0L = 0). Indeed ∣ζ1∣ d˙=d˙sgn⁡(ζ1) ζ1=θLζ1|\zeta_1|\, \dot d = \dot d \operatorname{sgn}(\zeta_1)\, \zeta_1 = \theta L \zeta_1. In matrix form,

ζ˙=1∣ζ1∣A(θ) ζ,A(θ)=[−k1/21/2−k2+θL0].□\dot \zeta = \frac{1}{|\zeta_1|} A(\theta)\, \zeta, \qquad A(\theta) = \begin{bmatrix} -k_1/2 & 1/2 \\ -k_2 + \theta L & 0 \end{bmatrix}. \qquad \square

Lemma 2: the Lyapunov matrix and its derivative

Let

P=[2k2+k12/2−k1/2−k1/21],Q(θ)=[k1k2+k13/2+θLk1−k12/2−θL−k12/2−θLk1/2].P = \begin{bmatrix} 2k_2 + k_1^2/2 & -k_1/2 \\ -k_1/2 & 1 \end{bmatrix}, \qquad Q(\theta) = \begin{bmatrix} k_1 k_2 + k_1^3/2 + \theta L k_1 & -k_1^2/2 - \theta L \\ -k_1^2/2 - \theta L & k_1/2 \end{bmatrix}.

(a) PP is positive definite: P11>0P_{11} > 0 and det⁡P=2k2+k12/4>0\det P = 2k_2 + k_1^2/4 > 0.

(b) A(θ)TP+PA(θ)=−Q(θ)A(\theta)^T P + P A(\theta) = -Q(\theta). With c=−k2+θLc = -k_2 + \theta L and p=2k2+k12/2p = 2k_2 + k_1^2/2, the product PAP A has the entries (PA)11=−k1p/2−k1c/2(PA)_{11} = -k_1 p / 2 - k_1 c / 2, (PA)12=p/2(PA)_{12} = p / 2, (PA)21=k12/4+c(PA)_{21} = k_1^2 / 4 + c and (PA)22=−k1/4(PA)_{22} = -k_1 / 4. Adding the transpose gives the diagonal entries −k1p−k1c=−k1k2−k13/2−θLk1-k_1 p - k_1 c = -k_1 k_2 - k_1^3/2 - \theta L k_1 and −k1/2-k_1 / 2, and the off-diagonal entry p/2+k12/4+c=k12/2+θLp/2 + k_1^2/4 + c = k_1^2/2 + \theta L. These are the entries of −Q(θ)-Q(\theta).

(c) det⁡Q(θ)=k12(k1k2+k132+θLk1)−(k122+θL)2=k12k22−θLk122−θ2L2.\det Q(\theta) = \frac{k_1}{2}\left(k_1 k_2 + \frac{k_1^3}{2} + \theta L k_1\right) - \left(\frac{k_1^2}{2} + \theta L\right)^2 = \frac{k_1^2 k_2}{2} - \frac{\theta L k_1^2}{2} - \theta^2 L^2.

(d) If k2>L+2L2/k12k_2 > L + 2L^2 / k_1^2, then Q(θ)Q(\theta) is positive definite for every θ∈[−1,1]\theta \in [-1, 1]. Its entry Q22=k1/2Q_{22} = k_1 / 2 is positive. The function θ↦det⁡Q(θ)\theta \mapsto \det Q(\theta) is a concave quadratic, so on [−1,1][-1, 1] it is smallest at an endpoint. det⁡Q(−1)−det⁡Q(1)=Lk12≥0\det Q(-1) - \det Q(1) = L k_1^2 \ge 0, and det⁡Q(1)=k12(k2−L)/2−L2>0\det Q(1) = k_1^2 (k_2 - L) / 2 - L^2 > 0 exactly when k2>L+2L2/k12k_2 > L + 2L^2/k_1^2. Conversely, if k2≤L+2L2/k12k_2 \le L + 2L^2/k_1^2, then Q(1)Q(1) is not positive definite.

(e) Let q=min⁡(λmin⁡Q(1),λmin⁡Q(−1))q = \min(\lambda_{\min} Q(1), \lambda_{\min} Q(-1)). For a fixed unit vector xx, θ↦xTQ(θ)x\theta \mapsto x^T Q(\theta) x is affine, so λmin⁡Q(θ)=min⁡∥x∥=1xTQ(θ)x\lambda_{\min} Q(\theta) = \min_{\|x\| = 1} x^T Q(\theta) x is concave in θ\theta and its minimum on [−1,1][-1, 1] is at an endpoint. So λmin⁡Q(θ)≥q\lambda_{\min} Q(\theta) \ge q for every θ∈[−1,1]\theta \in [-1, 1], and q>0q > 0 under the condition of (d). □\square

The tests check (b) and (c) exactly in Fraction for 200 random gains, and the identity of Lemma 3 at 200 random points.

Lemma 3: V decreases away from s = 0

Let V=ζTPζV = \zeta^T P \zeta and assume k2>L+2L2/k12k_2 > L + 2L^2/k_1^2. On every open time interval where s≠0s \neq 0, VV is absolutely continuous and, for almost every tt,

V˙=2ζTPζ˙=1∣ζ1∣ζT(ATP+PA)ζ=−1∣ζ1∣ζTQ(θ)ζ≤−q∥ζ∥2∣ζ1∣≤−q∥ζ∥≤−qλmax⁡(P)1/2V1/2,\dot V = 2 \zeta^T P \dot \zeta = \frac{1}{|\zeta_1|} \zeta^T (A^T P + P A) \zeta = -\frac{1}{|\zeta_1|} \zeta^T Q(\theta) \zeta \le -\frac{q \|\zeta\|^2}{|\zeta_1|} \le -q \|\zeta\| \le -\frac{q}{\lambda_{\max}(P)^{1/2}} V^{1/2},

using ∣ζ1∣≤∥ζ∥|\zeta_1| \le \|\zeta\| and V≤λmax⁡(P)∥ζ∥2V \le \lambda_{\max}(P) \|\zeta\|^2. So ddtV1/2≤−c\frac{d}{dt} V^{1/2} \le -c with c=q/(2λmax⁡(P)1/2)c = q / (2 \lambda_{\max}(P)^{1/2}) wherever V>0V > 0 and s≠0s \neq 0. □\square

Lemma 4: zeros of s with v nonzero are isolated

Let s(t∗)=0s(t^*) = 0 and v(t∗)≠0v(t^*) \neq 0. By the first equation of (1), which holds for every tt with a continuous right side, s˙(t∗)=v(t∗)≠0\dot s(t^*) = v(t^*) \neq 0, and s˙\dot s is continuous near t∗t^*. So ss is strictly monotone near t∗t^* and s(t)≠0s(t) \neq 0 for 0<∣t−t∗∣<ρ0 < |t - t^*| < \rho for some ρ>0\rho > 0. □\square

Theorem 1: finite-time convergence

Assume k2>L+2L2/k12k_2 > L + 2L^2/k_1^2. Then (s(t),v(t))=(0,0)(s(t), v(t)) = (0, 0) for all t≥Tt \ge T, where

T=2λmax⁡(P)1/2 V(0)1/2q.T = \frac{2 \lambda_{\max}(P)^{1/2}\, V(0)^{1/2}}{q}.

Proof. Fix ε>0\varepsilon > 0 and an interval [a,b][a, b] on which V≥εV \ge \varepsilon. The zeros of ss in [a,b][a, b] have v≠0v \neq 0, because s=v=0s = v = 0 gives V=0V = 0. By Lemma 4 each zero is isolated. If there were infinitely many, they would have an accumulation point tˉ∈[a,b]\bar t \in [a, b], with s(tˉ)=0s(\bar t) = 0 by continuity and v(tˉ)≠0v(\bar t) \neq 0 since V(tˉ)≥εV(\bar t) \ge \varepsilon; then tˉ\bar t would be isolated by Lemma 4, which is a contradiction. So ss has finitely many zeros in [a,b][a, b]. On each of the open intervals between them, Lemma 3 gives V1/2(t′′)−V1/2(t′)≤−c(t′′−t′)V^{1/2}(t'') - V^{1/2}(t') \le -c (t'' - t'), and since VV is continuous, the inequality holds across the zeros too. So V1/2(b)≤V1/2(a)−c(b−a)V^{1/2}(b) \le V^{1/2}(a) - c (b - a).

Now let tεt_\varepsilon be the first time with V(t)=εV(t) = \varepsilon (if V(0)≤εV(0) \le \varepsilon, put tε=0t_\varepsilon = 0). On [0,tε][0, t_\varepsilon], V≥εV \ge \varepsilon, so tε≤(V(0)1/2−ε1/2)/c≤Tt_\varepsilon \le (V(0)^{1/2} - \varepsilon^{1/2}) / c \le T. Hence VV takes values at most ε\varepsilon before TT for every ε>0\varepsilon > 0.

It remains to show that VV never increases. Let t0<t1t_0 < t_1 and suppose V(t1)>V(t0)V(t_1) > V(t_0). Let t2=sup⁡{t∈[t0,t1]:V(t)≤V(t0)}t_2 = \sup \{ t \in [t_0, t_1] : V(t) \le V(t_0) \}. By continuity V(t2)=V(t0)V(t_2) = V(t_0), and V>V(t0)≥0V > V(t_0) \ge 0 on (t2,t1](t_2, t_1]. For each δ∈(0,t1−t2)\delta \in (0, t_1 - t_2), VV is continuous and positive on [t2+δ,t1][t_2 + \delta, t_1], so it has a positive minimum there, and the first part, with ε\varepsilon equal to this minimum, gives V(t1)≤V(t2+δ)V(t_1) \le V(t_2 + \delta). Letting δ→0\delta \to 0 gives V(t1)≤V(t2)=V(t0)V(t_1) \le V(t_2) = V(t_0), a contradiction. So VV is non-increasing on [0,∞)[0, \infty). Combined with the first part, V(t)≤εV(t) \le \varepsilon for t≥Tt \ge T and every ε>0\varepsilon > 0, so V(t)=0V(t) = 0 for t≥Tt \ge T. Since PP is positive definite, ζ=0\zeta = 0, that is s=v=0s = v = 0. □\square

At s=v=0s = v = 0 the Filippov set for v˙\dot v is −k2[−1,1]+d˙-k_2 [-1, 1] + \dot d, which contains 00 because ∣d˙∣≤L<k2|\dot d| \le L < k_2. So staying at the origin is a valid solution, and by the theorem it is the only one. On the origin v=0v = 0, so w=−dw = -d: the integral state equals minus the disturbance, and the averaged value of k2Σ(s)k_2 \Sigma(s) is d˙\dot d.

Theorem 2: k_2 >= L is necessary

Let k2<Lk_2 < L and d(t)=Ltd(t) = L t, so d˙=L\dot d = L. Suppose s=0s = 0 on an interval II of positive length. By the first equation of (1), v=s˙=0v = \dot s = 0 on II, so v˙=0\dot v = 0 for almost every t∈It \in I. But v˙∈−k2[−1,1]+L⊂[L−k2,L+k2]\dot v \in -k_2 [-1, 1] + L \subset [L - k_2, L + k_2], and L−k2>0L - k_2 > 0. This is a contradiction. So no solution stays on s=0s = 0 for any k1k_1, and s=0s = 0 is not reached in the sense of Theorem 1. □\square

The test with k1=2k_1 = 2, k2=0.5k_2 = 0.5 and d=td = t shows s(10)>4s(10) > 4. The gap between k2≥Lk_2 \ge L and the sufficient condition k2>L+2L2/k12k_2 > L + 2L^2/k_1^2 comes from the choice of PP; other Lyapunov functions give other sufficient conditions.

Sampling

The implementation samples ss with period hh and integrates w˙\dot w by explicit Euler (line L2). Then exact convergence is replaced by a neighborhood: Levant (1993) shows that ∣s∣=O(h2)|s| = O(h^2) and ∣s˙∣=O(h)|\dot s| = O(h) after a finite time. This page does not reprove that result. The tests measure max⁡t≥6∣s∣\max_{t \ge 6} |s| between 4.3h24.3 h^2 and 5.3h25.3 h^2 for h=0.01,0.005,0.0025h = 0.01, 0.005, 0.0025 in the page example.

Complexity

Count real operations at unit cost, with the square root as one operation, as in the real RAM. Each sample costs O(1)O(1) time: one square root, one sign, and a few products and sums. The state is the single number ww, so the extra space is O(1)O(1). A run of NN samples costs O(N)O(N) time.

Properties

  • Continuous control. uu is a continuous function of ss and ww, and ww is continuous in time. The jump happens in w˙\dot w.
  • Finite-time convergence. Both ss and s˙\dot s reach 00 in finite time. The bound from the Lyapunov function is conservative.
  • Exact disturbance estimate. After convergence w=−dw = -d, so the algorithm also estimates the disturbance. This is why it serves as a differentiator and as an observer.
  • Homogeneity. With the weights 22 for ss and 11 for vv, the vector field is homogeneous of degree −1-1. This explains the finite-time convergence and the accuracy O(h2)O(h^2) and O(h)O(h) (Levant 1993).
  • Needs the relative degree one. ss must have relative degree one with respect to uu. The algorithm does not need s˙\dot s to be measured.
  • Stronger assumption than first order sliding. It needs a bound on d˙\dot d, not on dd. A disturbance that jumps is not covered.

Variants and improvements

  • Rule of thumb gains. The choice k1=1.5Lk_1 = 1.5 \sqrt{L}, k2=1.1Lk_2 = 1.1 L, which Levant (1998) uses for the differentiator, works well in practice. It is a rule of thumb, not a certificate: for L=1L = 1 it fails the condition k2>L+2L2/k12=1.89k_2 > L + 2L^2/k_1^2 = 1.89 of this page, but the example converges with it by t=0.92t = 0.92.
  • Exact differentiator. Run the algorithm on s=z−f(t)s = z - f(t) with z˙=u\dot z = u. Then uu converges to f˙\dot f in finite time when ∣f¨∣≤L|\ddot f| \le L (Levant 1998).
  • Observer. The same law drives a sliding mode observer for mechanical systems, where it estimates the velocity from the position.
  • Linear terms. Adding −k3s-k_3 s to uu and −k4s-k_4 s to w˙\dot w gives the generalized super-twisting algorithm, which handles disturbances that grow with ss and converges faster far from the surface. Moreno and Osorio's Lyapunov method extends to it.
  • Adaptive gains. Gains that increase while the sliding mode is lost and decrease while it holds avoid a conservative LL.
  • Higher order. Laws of order three and more drive s,s˙,s¨,…s, \dot s, \ddot s, \dots to 00 for a sliding variable of higher relative degree.

When to use it

Use it when a first order sliding mode controller would work but its switching control is not acceptable, and the disturbance is smooth enough that ∣d˙∣≤L|\dot d| \le L holds with a moderate LL. It is a common choice in electric drives, hydraulic and pneumatic actuators and aircraft control, and as a robust differentiator. If the disturbance can jump, use the first order law or a boundary layer. If dd is smooth and well modeled, a linear observer with a PID controller or active disturbance rejection control may be simpler to tune.

Pitfalls

  • k_2 too small at line L2. If k2≤Lk_2 \le L, the integral state cannot follow the disturbance, and ss drifts away (test case: d=td = t, k2=0.5k_2 = 0.5, and ss grows to 4.34.3 by t=10t = 10).
  • Large L from a crude bound. The required gains grow with LL, and with them the chattering. Use the smallest LL that the plant really has.
  • Relative degree two. If uu enters s¨\ddot s and not s˙\dot s, the algorithm does not apply. Use a different surface or an algorithm of higher order.
  • Integral windup at line L2. With input limits, ww keeps growing while the actuator is saturated. Limit ww in the same way as in anti-windup schemes.
  • Unmodeled actuator dynamics. A fast actuator still causes chattering, now in u˙\dot u, of smaller size. The algorithm reduces chattering, it does not remove it.
  • Explicit Euler at line L2. For large hk2h k_2 the integral state moves in large steps. Choose hh so that hk2h k_2 is small compared with the size of dd.

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.

ctlsim.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the control pages of https://algorithm.tamnd.com/. The authors are listed in prov/.

"""Fixed step simulation for the control pages.

Conventions:
- A continuous-time system is a function f(t, x, u) that returns x' as a
  flat list. x is a flat list. u is whatever the control returns (a flat
  list, a scalar, or None when there is no control).
- A discrete-time system is a function F(k, x, u) that returns x_(k+1).
- The integrator is the classical 4th order Runge-Kutta method with a fixed
  step dt. The input is held constant over each step.
- Time t_k = t0 + k dt is computed by multiplication, not by summation.
- The number of steps round((t1 - t0) / dt) and the hold ratio
  round(hold / dt) must match the real ratios within 1e-9 (relative), or
  ValueError is raised.

This file holds no controller or observer of a control page.
"""

__all__ = ["rk4_step", "simulate", "simulate_discrete", "lti"]


def _axpy(a, x, y):
    return [yi + a * xi for xi, yi in zip(x, y)]


def rk4_step(f, t, x, u, dt):
    """Return x(t + dt) after one RK4 step of x' = f(t, x, u), u held constant."""
    k1 = f(t, x, u)
    k2 = f(t + dt / 2, _axpy(dt / 2, k1, x), u)
    k3 = f(t + dt / 2, _axpy(dt / 2, k2, x), u)
    k4 = f(t + dt, _axpy(dt, k3, x), u)
    return [xi + dt / 6 * (a + 2 * b + 2 * c + d) for xi, a, b, c, d in zip(x, k1, k2, k3, k4)]


def _ratio(num, den, what):
    r = round(num / den)
    if abs(r * den - num) > 1e-9 * max(abs(num), abs(den)):
        raise ValueError(what + " is not an integer multiple of dt")
    return r


def simulate(f, x0, t0, t1, dt, control=None, hold=None):
    """Simulate x' = f(t, x, u) from x(t0) = x0 to t1 with RK4 and step dt.

    control(t, x) gives the input. It is called at each sampling instant
    t0 + j hold, and its value is held (zero-order hold) until the next
    instant. hold must be an integer multiple of dt; hold = None means
    hold = dt. With control = None the input is None.

    Returns (ts, xs, us): ts has N + 1 times, xs the N + 1 states, and us the
    N inputs, where us[k] acts on [ts[k], ts[k + 1]].
    """
    if dt <= 0:
        raise ValueError("dt must be positive")
    n_steps = _ratio(t1 - t0, dt, "t1 - t0")
    if n_steps < 0:
        raise ValueError("t1 is before t0")
    r = 1 if hold is None else _ratio(hold, dt, "hold")
    if r < 1:
        raise ValueError("hold must be at least dt")
    x = list(x0)
    ts = [t0]
    xs = [list(x)]
    us = []
    u = None
    for k in range(n_steps):
        t = t0 + k * dt
        if control is not None and k % r == 0:
            u = control(t, list(x))
        x = rk4_step(f, t, x, u, dt)
        ts.append(t0 + (k + 1) * dt)
        xs.append(x)
        us.append(u)
    return ts, xs, us


def simulate_discrete(F, x0, N, control=None):
    """Run x_(k+1) = F(k, x_k, u_k) for k = 0, ..., N - 1.

    u_k = control(k, x_k), or None if control is None. Returns (xs, us) with
    N + 1 states and N inputs. Exact if F and control are exact.
    """
    x = list(x0)
    xs = [list(x)]
    us = []
    for k in range(N):
        u = control(k, list(x)) if control is not None else None
        x = list(F(k, x, u))
        xs.append(x)
        us.append(u)
    return xs, us


def lti(A, B=None):
    """Return f(t, x, u) = A x + B u for the system x' = A x + B u.

    u may be a flat list, a scalar (one input) or None (zero input). B may
    be None for a system with no input.
    """
    def f(t, x, u):
        dx = [sum(a * xi for a, xi in zip(row, x)) for row in A]
        if B is None or u is None:
            return dx
        uu = u if isinstance(u, (list, tuple)) else [u]
        return [d + sum(b * ui for b, ui in zip(row, uu)) for d, row in zip(dx, B)]
    return f


def _self_test():
    import math
    f = lti([[-1.0]])
    _, xs, _ = simulate(f, [1.0], 0.0, 1.0, 0.01)
    assert abs(xs[-1][0] - math.exp(-1.0)) < 1e-9
    return True


if __name__ == "__main__":
    _self_test()
super_twisting_algorithm.py 94 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/super-twisting-algorithm/. The authors are listed in prov/QDH2NE.jsonl.

"""The super-twisting algorithm for a sliding variable with

    s' = u + d(t),   |d'(t)| <= L,

in its sampled form with sampling period h:

    u_j     = -k1 |s_j|^(1/2) sgn(s_j) + w_j,
    w_(j+1) = w_j - h k2 sgn(s_j).

In continuous time the closed loop is s' = -k1 |s|^(1/2) sgn(s) + v,
v' = -k2 sgn(s) + d' with v = w + d. With z = (|s|^(1/2) sgn(s), v) the
quadratic form V = z^T P z,

    P = [[2 k2 + k1^2 / 2, -k1 / 2], [-k1 / 2, 1]],

decreases along every solution when k1 > 0 and k2 > L + 2 L^2 / k1^2, and
s = v = 0 is reached in finite time T <= 2 sqrt(lambda_max(P)) sqrt(V(0)) / q,
where q is the smaller of the least eigenvalues of Q(1) and Q(-1) below.

lyapunov_matrix, q_matrix and certified are exact for Fraction input.
"""

import math

__all__ = ["sgn", "SuperTwisting", "lyapunov_matrix", "q_matrix", "certified", "time_bound"]


def sgn(s):
    """Return -1, 0 or 1."""
    return (s > 0) - (s < 0)


class SuperTwisting:
    """Sampled super-twisting controller with gains k1, k2 and period h."""

    def __init__(self, k1, k2, h, w0=0):
        if k1 <= 0 or k2 <= 0:
            raise ValueError("k1 and k2 must be positive")
        if h <= 0:
            raise ValueError("h must be positive")
        self.k1 = k1
        self.k2 = k2
        self.h = h
        self.w = w0

    def step(self, s):
        """Return u_j for the measured s_j and advance the integral state."""
        sg = sgn(s)
        u = -self.k1 * math.sqrt(abs(s)) * sg + self.w  # @line L1
        self.w = self.w - self.h * self.k2 * sg  # @line L2
        return u  # @line L3


def lyapunov_matrix(k1, k2):
    """P of the quadratic Lyapunov function V = z^T P z."""
    return [[2 * k2 + k1 * k1 / 2, -k1 / 2], [-k1 / 2, 1]]


def q_matrix(k1, k2, L, theta):
    """Q(theta) = -(A^T P + P A) for A = [[-k1/2, 1/2], [-k2 + theta L, 0]]."""
    a = k1 * k2 + k1 ** 3 / 2 + theta * L * k1
    b = -k1 * k1 / 2 - theta * L
    return [[a, b], [b, k1 / 2]]


def certified(k1, k2, L):
    """True if Q(1) and Q(-1) are positive definite, that is k2 > L + 2 L^2 / k1^2."""
    for theta in (1, -1):
        q = q_matrix(k1, k2, L, theta)
        if not (q[1][1] > 0 and q[0][0] * q[1][1] - q[0][1] * q[1][0] > 0):
            return False
    return True


def _eig2(m):
    a, b, d = float(m[0][0]), float(m[0][1]), float(m[1][1])
    r = math.hypot((a - d) / 2, b)
    return (a + d) / 2 - r, (a + d) / 2 + r


def time_bound(k1, k2, L, s0, v0):
    """Upper bound on the time to reach s = v = 0 from s(0) = s0, v(0) = v0."""
    if not certified(k1, k2, L):
        raise ValueError("the gains are not certified for this L")
    p = lyapunov_matrix(k1, k2)
    z1 = math.sqrt(abs(s0)) * sgn(s0)
    v0 = float(v0)
    V0 = float(p[0][0]) * z1 * z1 + 2 * float(p[0][1]) * z1 * v0 + v0 * v0
    lmax = _eig2(p)[1]
    q = min(_eig2(q_matrix(k1, k2, L, 1))[0], _eig2(q_matrix(k1, k2, L, -1))[0])
    return 2 * math.sqrt(lmax) * math.sqrt(V0) / q
test_super_twisting_algorithm.py 144 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/super-twisting-algorithm/. The authors are listed in prov/QDH2NE.jsonl.

import math
import random
from fractions import Fraction

import ctlsim
from super_twisting_algorithm import SuperTwisting, certified, lyapunov_matrix, q_matrix, sgn, time_bound

# Simulations: the plant s' = u + d(t) is integrated with classical RK4
# (ctlsim) with step dt equal to the sampling period h of the controller, so
# u is held over each step. Tolerances are stated at each check.


def steps(T, h):
    n = round(T / h)
    assert abs(n * h - T) < 1e-12
    return n


def run(k1, k2, h, T, d=math.sin, s0=1.0):
    st = SuperTwisting(k1, k2, h)
    ws = []

    def control(t, x):
        ws.append(st.w)
        return st.step(x[0])

    ts, xs, us = ctlsim.simulate(lambda t, x, u: [u + d(t)], [s0], 0.0, T, h, control=control)
    ws.append(st.w)
    assert len(us) == steps(T, h)
    return ts, [x[0] for x in xs], ws


def rand_frac(rng, lo, hi, den=8):
    return Fraction(rng.randint(lo * den, hi * den), den)


def test_step_by_hand():
    st = SuperTwisting(2, 2, Fraction(1, 100))
    assert st.step(4.0) == -4.0
    assert st.w == Fraction(-2, 100)
    assert st.step(0.0) == -0.02
    assert st.w == Fraction(-2, 100)
    assert abs(st.step(-0.25) - (1.0 - 0.02)) < 1e-15


def test_q_matrix_is_minus_lyapunov_derivative():
    # Q(theta) = -(A^T P + P A) and det Q = k1^2 k2 / 2 - theta L k1^2 / 2 - theta^2 L^2, exactly.
    rng = random.Random(3)
    for _ in range(200):
        k1, k2, L = rand_frac(rng, 1, 5), rand_frac(rng, 1, 9), rand_frac(rng, 0, 3)
        theta = rand_frac(rng, -1, 1)
        P = lyapunov_matrix(k1, k2)
        A = [[-k1 / 2, Fraction(1, 2)], [-k2 + theta * L, 0]]
        M = [[sum(A[k][i] * P[k][j] + P[i][k] * A[k][j] for k in range(2)) for j in range(2)] for i in range(2)]
        Q = q_matrix(k1, k2, L, theta)
        assert all(M[i][j] == -Q[i][j] for i in range(2) for j in range(2))
        det = Q[0][0] * Q[1][1] - Q[0][1] * Q[1][0]
        assert det == k1 * k1 * k2 / 2 - theta * L * k1 * k1 / 2 - theta * theta * L * L
        assert certified(k1, k2, L) == (k2 > L + 2 * L * L / (k1 * k1))


def test_lyapunov_rate_exact():
    # Along s' = -k1 |s|^(1/2) sgn s + v, v' = -k2 sgn s + rho, |rho| <= L, in
    # z = (|s|^(1/2) sgn s, v): V' = -z^T Q(theta) z / |z1| with theta = rho sgn(z1) / L.
    rng = random.Random(5)
    for _ in range(200):
        k1, k2, L = rand_frac(rng, 1, 4), rand_frac(rng, 1, 6), rand_frac(rng, 1, 2)
        z1 = rand_frac(rng, -3, 3)
        if z1 == 0:
            continue
        z2 = rand_frac(rng, -3, 3)
        rho = rand_frac(rng, -1, 1) * L
        dz1 = (-k1 * z1 + z2) / (2 * abs(z1))
        dz2 = -k2 * sgn(z1) + rho
        P = lyapunov_matrix(k1, k2)
        z, dz = [z1, z2], [dz1, dz2]
        vdot = 2 * sum(z[i] * P[i][j] * dz[j] for i in range(2) for j in range(2))
        theta = rho * sgn(z1) / L
        Q = q_matrix(k1, k2, L, theta)
        assert vdot == -sum(z[i] * Q[i][j] * z[j] for i in range(2) for j in range(2)) / abs(z1)
        if certified(k1, k2, L):
            assert vdot < 0


def test_page_example():
    # s' = u + sin t, L = 1, gains (2, 2): certified since 2 > 1 + 2 / 4.
    assert certified(2, 2, 1)
    assert abs(time_bound(2.0, 2.0, 1.0, 1.0, 0.0) - 132.984) < 1e-3
    h = 0.01
    ts, ss, ws = run(2.0, 2.0, h, 10.0)
    table = {0.5: (0.151849, -1.0), 1.0: (0.002585, -0.76), 2.0: (0.000023, -0.92), 4.0: (-0.000063, 0.76)}
    for t, (s, w) in table.items():
        i = steps(t, h)
        assert abs(ss[i] - s) < 5e-7 and abs(ws[i] - w) < 1e-9
    # |s| <= 0.05 for good from t = 0.59 (measured)
    i = steps(0.59, h)
    assert all(abs(s) <= 0.05 for s in ss[i:]) and abs(ss[i - 1]) > 0.05
    # w tracks -d: max |w + sin t| for t >= 6 is 0.045 (measured), about 2 h k2
    assert max(abs(w + math.sin(t)) for t, w in zip(ts, ws) if t >= 6) < 0.05


def test_accuracy_is_order_h_squared():
    # max |s| for t >= 6 is 4.35 h^2, 5.31 h^2, 4.75 h^2 for h = 0.01, 0.005, 0.0025 (measured).
    for h in (0.01, 0.005, 0.0025):
        ts, ss, ws = run(2.0, 2.0, h, 10.0)
        late = max(abs(s) for t, s in zip(ts, ss) if t >= 6)
        assert 3 * h * h < late < 6 * h * h


def test_first_order_comparison():
    # u = -1.5 sgn s with the same h: band 0.0235, about 50 times the super-twisting band.
    h = 0.01
    ts, xs, us = ctlsim.simulate(lambda t, x, u: [u + math.sin(t)], [1.0], 0.0, 10.0, h, control=lambda t, x: -1.5 * sgn(x[0]))
    assert len(us) == steps(10.0, h)
    band = max(abs(x[0]) for t, x in zip(ts, xs) if t >= 6)
    assert 0.02 < band <= 2.5 * h


def test_rule_of_thumb_gains():
    # k1 = 1.5 sqrt(L), k2 = 1.1 L is not certified by this P, but converges here.
    assert not certified(1.5, 1.1, 1.0)
    ts, ss, ws = run(1.5, 1.1, 0.01, 10.0)
    assert max(abs(s) for t, s in zip(ts, ss) if t >= 1) <= 0.05
    assert max(abs(s) for t, s in zip(ts, ss) if t >= 6) < 5e-4


def test_k2_below_L_fails():
    # d = t has d' = 1 = L. With k2 = 0.5 < L, v' >= 0.5 and s drifts away.
    ts, ss, ws = run(2.0, 0.5, 0.01, 10.0, d=lambda t: t)
    assert ss[-1] > 4.0
    ts, ss, ws = run(2.0, 1.5, 0.01, 10.0, d=lambda t: t)
    assert max(abs(s) for t, s in zip(ts, ss) if t >= 6) < 2e-4


def test_errors():
    for bad in [lambda: SuperTwisting(0, 1, 0.1), lambda: SuperTwisting(1, 1, 0), lambda: time_bound(1.0, 1.0, 1.0, 1.0, 0.0)]:
        try:
            bad()
        except ValueError:
            continue
        raise AssertionError("no error")

History

Arie Levant (who published earlier as Levantovsky) introduced the super-twisting algorithm in his 1993 paper on sliding order and sliding accuracy, among other laws that keep ss and s˙\dot s at zero. His proofs used the geometry of the phase plane, and he showed that sampling with period hh gives an accuracy of order h2h^2. In 1998 he used the same law as a robust exact differentiator, which made it popular in observers and output feedback. A Lyapunov function for the algorithm was missing until Moreno and Osorio presented a quadratic form in the coordinates (∣s∣1/2sgn⁡s,v)(|s|^{1/2} \operatorname{sgn} s, v) in 2008 and extended it to strict Lyapunov functions in 2012. This made the usual tools of Lyapunov analysis available, such as convergence time estimates and robustness margins.

References

  1. Sliding order and sliding accuracy in sliding mode control, the definition of the super-twisting algorithm, its finite-time convergence, and the accuracy of order h^2 with sampling period h
  2. A. Levant, Robust exact differentiation via sliding mode technique, Automatica 34(3), 379-384, 1998, the use of the super-twisting algorithm as an exact differentiator with a bound on the second derivative of the signal
  3. J. A. Moreno and M. Osorio, Strict Lyapunov functions for the super-twisting algorithm, IEEE Transactions on Automatic Control 57(4), 1035-1040, 2012, the quadratic form in the coordinates (|s|^(1/2) sgn s, v) and the finite-time estimate
  4. Christopher Edwards and Sarah K. Spurgeon, Sliding Mode Control, Theory and Applications, Taylor and Francis, 1998, the background on sliding modes, the reaching condition and matched uncertainty

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.worst
  • 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