Algorithm Search⌘K

Algorithmcore

Leader-follower formation control

Also called l-phi formation control.

Each follower keeps a set distance and bearing to its leader by feedback linearization of a point ahead of its axle, so the formation is a tree of such pairs.

Time
O(k r) totalO(r) per_step
Space
O(r) extra
Counts
messages O(r) per_step
Where
k = the number of integration steps; r = the number of robots
Properties
deterministiconlinenot randomized
Origin
Jaydev P. Desai, James P. Ostrowski, Vijay Kumar, 2001

Leader-follower formation control builds a formation of wheeled robots from pairs. One robot, the root, drives freely. Each other robot, a follower, picks one leader and keeps a target separation ldl_d and bearing ψd\psi_d to it, measured in the frame of the leader. The followers are unicycles, which cannot move sideways, so the law controls a point at a distance d>0d > 0 ahead of the wheel axle. For that point, the map from the inputs (v,ω)(v, \omega) to the rates (l˙,ψ˙)(\dot l, \dot\psi) is invertible, and feedback linearization makes both errors decay exponentially for any motion of the leader. The heading of the follower is left as an internal state, and behind a leader that drives forward it converges to the heading of the leader. The robots form a tree, so the formation is set by r−1r - 1 pairs of targets.

In plain words

Think of ducklings following their mother. Each duckling does not look at the whole family. It picks one duck ahead of it and tries to stay at a fixed distance and at a fixed angle behind it, for example one step back and a little to the left. The first duck goes where it wants, and the rest of the line follows.

A wheeled robot cannot slide sideways, so it cannot fix both its distance and its angle directly with its own center. Instead it watches a point just in front of its wheels, which it can push in any direction by driving and turning. It steers that point to the right spot next to its leader. The body of the robot then lines up behind, as long as the leader keeps moving forward.

The problem

The model. There are rr unicycle robots with states (xi,yi,θi)(x_i, y_i, \theta_i) in meters and radians, and inputs (vi,ωi)(v_i, \omega_i): x˙=vcos⁡θ\dot x = v \cos\theta, y˙=vsin⁡θ\dot y = v \sin\theta, θ˙=ω\dot\theta = \omega. The robots form a directed tree: robot 1 is the root, and every other robot FF has one leader LL. A follower controls its point PF=(xF+dcos⁡θF,yF+dsin⁡θF)P_F = (x_F + d \cos\theta_F, y_F + d \sin\theta_F), with d>0d > 0. It measures the separation ll and the bearing ψ\psi, defined by

PF−(xL,yL)=l (cos⁡(θL+ψ),sin⁡(θL+ψ)),P_F - (x_L, y_L) = l\,(\cos(\theta_L + \psi), \sin(\theta_L + \psi)),

so ψ\psi is measured from the heading of the leader, and ψ=π\psi = \pi means straight behind. It also needs the relative heading θL−θF\theta_L - \theta_F, and it receives the inputs vLv_L and ωL\omega_L of its leader, one message per follower and per step. Time is continuous. The sign conventions of Desai, Ostrowski and Kumar differ in details; the page uses the conventions above, and the proof derives every formula from them.

Input. The tree, the targets (ld,ψd)(l_d, \psi_d) of every follower, the gains α1,α2>0\alpha_1, \alpha_2 > 0, the offset dd, the initial states, and the inputs of the root as a function of time.

Output. The trajectories, with l→ldl \to l_d and ψ→ψd\psi \to \psi_d for every follower.

How it differs from close pages. In distance-based formation control the graph is rigid and undirected, each robot reacts to several neighbors, and convergence is local. Here the graph is a tree, the root is not influenced by anyone, and the errors decay exponentially from any start with l>0l > 0, but an error of a leader passes on to all robots below it. The name "l-phi" in the alias refers to this separation-bearing pair; Desai, Ostrowski and Kumar write it with ψ\psi.

The algorithm

  1. LEADER-FOLLOWER(tree, targets, α1, α2, d, states, root inputs, T, h)
  2. 1for each step of length h until time T do
  3. 2for each follower F in tree order, with leader L, do
  4. 3measure l, ψ and γ = θ_L + ψ - θ_F; receive v_L and ω_L
  5. 4a ← α1 (l_d - l) + v_L cos ψ ▷ wanted rate of l, plus the motion of the leader
  6. 5b ← l (α2 (ψ_d - ψ) + ω_L) - v_L sin ψ ▷ the same for ψ, times l
  7. 6v_F ← a cos γ - b sin γ
  8. 7ω_F ← (a sin γ + b cos γ) / d
  9. 8move every robot with its inputs for time h
  10. 9return the states

Lines L4 to L7 invert the kinematics of the pair. The rates of ll and ψ\psi depend on (vF,ωF)(v_F, \omega_F) through a matrix with determinant d/ld / l, and lines L6 and L7 apply its inverse to the wanted rates. "Tree order" in line L2 means that a leader gets its inputs before its followers, so each follower can use the current vLv_L and ωL\omega_L. The angle difference ψd−ψ\psi_d - \psi in line L5 is wrapped to [−π,π)[-\pi, \pi). The implementation integrates the closed loop with the classic fourth order Runge-Kutta method and a fixed step.

Example

One evaluation by hand. The leader stands at the origin with θL=0\theta_L = 0 and drives straight with vL=0.5v_L = 0.5 m/s. The follower stands at (−2,0.5)(-2, 0.5) with θF=0\theta_F = 0, and d=0.1d = 0.1 m, so PF=(−1.9,0.5)P_F = (-1.9, 0.5). The target is ld=1l_d = 1 m and ψd=3π/4=2.3562\psi_d = 3\pi/4 = 2.3562, which is behind the leader and to its left, and α1=α2=1\alpha_1 = \alpha_2 = 1 per second. Then l=3.86=1.9647l = \sqrt{3.86} = 1.9647, ψ=π−arctan⁡(0.5/1.9)=2.8843\psi = \pi - \arctan(0.5 / 1.9) = 2.8843 and γ=ψ=2.8843\gamma = \psi = 2.8843. Line L4 gives a=(1−1.9647)+0.5cos⁡2.8843=−0.9647−0.4835=−1.4482a = (1 - 1.9647) + 0.5 \cos 2.8843 = -0.9647 - 0.4835 = -1.4482, and line L5 gives b=1.9647⋅(2.3562−2.8843)−0.5sin⁡2.8843=−1.0376−0.1273=−1.1648b = 1.9647 \cdot (2.3562 - 2.8843) - 0.5 \sin 2.8843 = -1.0376 - 0.1273 = -1.1648. So vF=1.6970v_F = 1.6970 m/s and ωF=7.578\omega_F = 7.578 rad/s. The follower drives fast and turns hard to the left, toward its spot.

The run. With RK4 and h=0.002h = 0.002 s, ll and ψ\psi follow ld+(l(0)−ld)e−tl_d + (l(0) - l_d) e^{-t} and ψd+(ψ(0)−ψd)e−t\psi_d + (\psi(0) - \psi_d) e^{-t}:

tt (s) ll (m) ψ\psi (rad) θL−θF\theta_L - \theta_F (rad) follower (x,y)(x, y) (m)
0 1.9647 2.8843 0 (−2,0.5)(-2, 0.5)
0.5 1.5851 2.6765 -0.1830 (−1.2651,0.6928)(-1.2651, 0.6928)
1 1.3549 2.5505 -0.0419 (−0.7249,0.7509)(-0.7249, 0.7509)
2 1.1306 2.4277 0.0359 (0.0456,0.7439)(0.0456, 0.7439)
4 1.0177 2.3659 0.0123 (1.1735,0.7138)(1.1735, 0.7138)
8 1.0003 2.3564 0.0003 (3.1925,0.7072)(3.1925, 0.7072)

At t=16t = 16 s the leader is at (8,0)(8, 0), the follower at (7.1929,0.7071)(7.1929, 0.7071), and PF−pL=(−0.7071,0.7071)P_F - p_L = (-0.7071, 0.7071): 1 m away at 135 degrees from the heading of the leader, with the same heading. The heading error first grows, because the follower turns to move its point, and then decays at the rate vL/d=5v_L / d = 5 per second of the zero dynamics.

Zero dynamics. If the follower starts exactly at its target ll and ψ\psi with a heading error e(0)e(0), the errors stay zero, and e=θL−θFe = \theta_L - \theta_F obeys e˙=−(vL/d)sin⁡e\dot e = -(v_L / d) \sin e, so tan⁡(e/2)=tan⁡(e(0)/2) e−vLt/d\tan(e/2) = \tan(e(0)/2)\, e^{-v_L t / d}. The tests check this closed form within 10−610^{-6} for e(0)=0.5e(0) = 0.5, 22 and −2.8-2.8.

Correctness

The proof derives the rates l˙\dot l and ψ˙\dot\psi of the pair from the unicycle model, and shows that lines L6 and L7 invert them, because the input matrix has determinant d/l>0d / l > 0. The closed loop is then l˙=α1(ld−l)\dot l = \alpha_1 (l_d - l) and ψ˙=α2(ψd−ψ)\dot\psi = \alpha_2 (\psi_d - \psi), so both errors decay exponentially for any motion of the leader, and ll stays positive. On a tree, the same holds for every edge. The heading of the follower is the internal state. On the set where the outputs are at their targets, behind a leader that drives straight forward, it obeys e˙=−(vL/d)sin⁡e\dot e = -(v_L / d) \sin e and converges to zero from every ∣e(0)∣<π|e(0)| < \pi. The stability of the internal dynamics from other starts and for turning leaders is the analysis of Desai, Ostrowski and Kumar, and the proof does not repeat it.

Proof: The separation-bearing law makes the separation and bearing errors of each follower decay exponentially, and the heading error decays behind a straight leaderfull

Each robot is a unicycle with state (x,y,θ)(x, y, \theta) and inputs (v,ω)(v, \omega): x˙=vcos⁡θ\dot x = v \cos\theta, y˙=vsin⁡θ\dot y = v \sin\theta, θ˙=ω\dot\theta = \omega. A follower FF with leader LL controls its point PF=(xF+dcos⁡θF,yF+dsin⁡θF)P_F = (x_F + d \cos\theta_F, y_F + d \sin\theta_F) with d>0d > 0. The separation l>0l > 0 and the bearing ψ\psi are defined by PF−(xL,yL)=l (cos⁡(θL+ψ),sin⁡(θL+ψ))P_F - (x_L, y_L) = l\,(\cos(\theta_L + \psi), \sin(\theta_L + \psi)), and γ=θL+ψ−θF\gamma = \theta_L + \psi - \theta_F. The follower knows vLv_L and ωL\omega_L. Line numbers refer to the pseudocode on the leader-follower formation control page.

Lemma 1 (kinematics of the separation and bearing).

l˙=vFcos⁡γ+dωFsin⁡γ−vLcos⁡ψ,ψ˙=vLsin⁡ψ−vFsin⁡γ+dωFcos⁡γl−ωL.\dot l = v_F \cos\gamma + d \omega_F \sin\gamma - v_L \cos\psi, \qquad \dot\psi = \frac{v_L \sin\psi - v_F \sin\gamma + d \omega_F \cos\gamma}{l} - \omega_L.

Proof. Let ϕ=θL+ψ\phi = \theta_L + \psi and w=PF−pL=l(cos⁡ϕ,sin⁡ϕ)w = P_F - p_L = l (\cos\phi, \sin\phi). Then w˙=l˙(cos⁡ϕ,sin⁡ϕ)+lϕ˙(−sin⁡ϕ,cos⁡ϕ)\dot w = \dot l (\cos\phi, \sin\phi) + l \dot\phi (-\sin\phi, \cos\phi), so l˙=w˙⋅(cos⁡ϕ,sin⁡ϕ)\dot l = \dot w \cdot (\cos\phi, \sin\phi) and lϕ˙=w˙⋅(−sin⁡ϕ,cos⁡ϕ)l \dot\phi = \dot w \cdot (-\sin\phi, \cos\phi). Also P˙F=vF(cos⁡θF,sin⁡θF)+dωF(−sin⁡θF,cos⁡θF)\dot P_F = v_F (\cos\theta_F, \sin\theta_F) + d \omega_F (-\sin\theta_F, \cos\theta_F) and p˙L=vL(cos⁡θL,sin⁡θL)\dot p_L = v_L (\cos\theta_L, \sin\theta_L). With ϕ−θF=γ\phi - \theta_F = \gamma and ϕ−θL=ψ\phi - \theta_L = \psi, the dot products are (cos⁡θF,sin⁡θF)⋅(cos⁡ϕ,sin⁡ϕ)=cos⁡γ(\cos\theta_F, \sin\theta_F) \cdot (\cos\phi, \sin\phi) = \cos\gamma, (−sin⁡θF,cos⁡θF)⋅(cos⁡ϕ,sin⁡ϕ)=sin⁡γ(-\sin\theta_F, \cos\theta_F) \cdot (\cos\phi, \sin\phi) = \sin\gamma, (cos⁡θL,sin⁡θL)⋅(cos⁡ϕ,sin⁡ϕ)=cos⁡ψ(\cos\theta_L, \sin\theta_L) \cdot (\cos\phi, \sin\phi) = \cos\psi, and in the normal direction −sin⁡γ-\sin\gamma, cos⁡γ\cos\gamma and −sin⁡ψ-\sin\psi. So l˙=vFcos⁡γ+dωFsin⁡γ−vLcos⁡ψ\dot l = v_F \cos\gamma + d \omega_F \sin\gamma - v_L \cos\psi and lϕ˙=−vFsin⁡γ+dωFcos⁡γ+vLsin⁡ψl \dot\phi = -v_F \sin\gamma + d \omega_F \cos\gamma + v_L \sin\psi. Finally ψ˙=ϕ˙−ωL\dot\psi = \dot\phi - \omega_L. □\square

Lemma 2 (the inputs invert the kinematics). With aa and bb from lines L4 and L5, the inputs of lines L6 and L7 give vFcos⁡γ+dωFsin⁡γ=av_F \cos\gamma + d \omega_F \sin\gamma = a and −vFsin⁡γ+dωFcos⁡γ=b-v_F \sin\gamma + d \omega_F \cos\gamma = b.

Proof. Line L6 gives vF=acos⁡γ−bsin⁡γv_F = a \cos\gamma - b \sin\gamma, and line L7 gives dωF=asin⁡γ+bcos⁡γd \omega_F = a \sin\gamma + b \cos\gamma. This is the rotation of (a,b)(a, b) by −γ-\gamma, so its inverse rotation gives back (a,b)(a, b): vFcos⁡γ+dωFsin⁡γ=a(cos⁡2γ+sin⁡2γ)=av_F \cos\gamma + d\omega_F \sin\gamma = a (\cos^2\gamma + \sin^2\gamma) = a, and −vFsin⁡γ+dωFcos⁡γ=b-v_F \sin\gamma + d \omega_F \cos\gamma = b. The map from (vF,ωF)(v_F, \omega_F) to (l˙,ψ˙)(\dot l, \dot\psi) has the matrix (cos⁡γdsin⁡γ−sin⁡γ/ldcos⁡γ/l)\begin{pmatrix} \cos\gamma & d \sin\gamma \\ -\sin\gamma / l & d \cos\gamma / l \end{pmatrix}, whose determinant is d/l>0d / l > 0, so the inversion is possible exactly because d>0d > 0 and l>0l > 0. □\square

Theorem 1 (exponential decay of the output errors). Let l(0)>0l(0) > 0, ld>0l_d > 0, α1,α2>0\alpha_1, \alpha_2 > 0, and ∣ψ(0)−ψd∣<π|\psi(0) - \psi_d| < \pi. For any leader inputs, the closed loop gives

l(t)=ld+(l(0)−ld)e−α1t,ψ(t)−ψd=(ψ(0)−ψd)e−α2t.l(t) = l_d + (l(0) - l_d) e^{-\alpha_1 t}, \qquad \psi(t) - \psi_d = (\psi(0) - \psi_d) e^{-\alpha_2 t}.

Proof. By Lemmas 1 and 2, l˙=a−vLcos⁡ψ=α1(ld−l)\dot l = a - v_L \cos\psi = \alpha_1 (l_d - l) and ψ˙=(vLsin⁡ψ+b)/l−ωL=α2(ψd−ψ)\dot\psi = (v_L \sin\psi + b)/l - \omega_L = \alpha_2 (\psi_d - \psi), as long as l>0l > 0. These are linear equations with the stated solutions. The solution for ll is a convex combination of l(0)l(0) and ldl_d for every tt, so it stays positive, and the formulas hold for all t≥0t \ge 0. The bearing error keeps its sign and shrinks, so it stays in (−π,π)(-\pi, \pi) and the wrapping of angles in line L5 never changes it. □\square

Theorem 2 (trees of followers). Let the robots form a directed tree in which each follower has one leader, and let each follower run the law with the actual inputs of its leader. Then the separation and bearing errors of every edge decay as in Theorem 1, whatever the root does.

Proof. Theorem 1 holds for any leader inputs, so it applies to each edge on its own, whether the leader is the root or another follower. □\square

Lemma 3 (zero dynamics behind a straight leader). Let the leader move straight, with constant vL>0v_L > 0 and ωL=0\omega_L = 0, and let the follower start with l=ldl = l_d and ψ=ψd\psi = \psi_d. Then the heading error e=θL−θFe = \theta_L - \theta_F satisfies e˙=−(vL/d)sin⁡e\dot e = -(v_L / d) \sin e, and tan⁡(e(t)/2)=tan⁡(e(0)/2) e−vLt/d\tan(e(t)/2) = \tan(e(0)/2)\, e^{-v_L t / d}. So e(t)→0e(t) \to 0 for every ∣e(0)∣<π|e(0)| < \pi, and the follower ends with the heading of the leader.

Proof. By Theorem 1, l=ldl = l_d and ψ=ψd\psi = \psi_d for all tt. Then lines L4 and L5 give a=vLcos⁡ψa = v_L \cos\psi and b=−vLsin⁡ψb = -v_L \sin\psi, and line L7 gives dωF=vL(cos⁡ψsin⁡γ−sin⁡ψcos⁡γ)=vLsin⁡(γ−ψ)=vLsin⁡ed \omega_F = v_L (\cos\psi \sin\gamma - \sin\psi \cos\gamma) = v_L \sin(\gamma - \psi) = v_L \sin e, since γ−ψ=θL−θF\gamma - \psi = \theta_L - \theta_F. So e˙=θ˙L−θ˙F=−(vL/d)sin⁡e\dot e = \dot\theta_L - \dot\theta_F = -(v_L/d) \sin e. For e∈(−π,π)e \in (-\pi, \pi) with e≠0e \ne 0, ddtln⁡∣tan⁡(e/2)∣=e˙/sin⁡e=−vL/d\frac{d}{dt} \ln |\tan(e/2)| = \dot e / \sin e = -v_L / d, which gives the formula, and the case e=0e = 0 is an equilibrium. ■\blacksquare

What is not proved here. Lemma 3 is the motion on the set where the outputs are already at their targets, the zero dynamics. When the outputs start away from their targets, the heading error is driven also by terms that decay exponentially, and the stability of that cascade for a leader that moves straight, or with bounded turning, is the subject of the analysis of Desai, Ostrowski and Kumar (2001). That analysis, and the case of a leader that moves backward (vL<0v_L < 0), where the heading error grows, are not proved here.

Complexity

  • Per step. Each follower does O(1)O(1) work and receives one message with (vL,ωL)(v_L, \omega_L), so a step costs O(r)O(r) operations in the real RAM model and r−1r - 1 messages. The tree is fixed, so there is no other communication.
  • Rate, proved. The separation and bearing errors decay as e−α1te^{-\alpha_1 t} and e−α2te^{-\alpha_2 t} for every follower, independent of the depth of the tree.
  • Propagation. Errors in the targets do not decay down the tree: a follower at depth kk is placed by kk pairs of targets, so its position error relative to the root is the sum of kk terms, and a disturbance of the root reaches every robot. This is a structural property, not a bound in the source.

Properties

  • Exponential tracking of separation and bearing, for any motion of the leader, as long as d>0d > 0 and l>0l > 0.
  • Uses one leader per follower. The graph is a directed tree, so the sensing load is one neighbor per robot.
  • Needs the inputs of the leader. The law uses vLv_L and ωL\omega_L; a follower that only sees its leader must estimate them.
  • Internal heading dynamics. They are stable behind a leader that moves forward, and unstable when the leader moves backward.
  • Deterministic and continuous time. In a sampled implementation the step must be short compared with d/vLd / v_L and 1/α1/\alpha.

Variants and improvements

  • Separation-separation (l-l) control. A follower keeps two distances to two leaders, which places it by two circles instead of a distance and an angle (Desai, Ostrowski and Kumar 2001).
  • Switching formations. The tree and the targets can change, for example to pass a narrow gap in a column and to spread out after it.
  • Estimation of the leader motion. Observers or sliding mode terms replace the received vLv_L and ωL\omega_L when there is no communication.
  • Virtual leaders. The root can be a virtual point that moves along a planned path, so that every real robot is a follower.
  • Distance-based and consensus-based laws. These spread the work over an undirected graph and avoid a single point of failure, at the cost of global or local convergence conditions; see formation control.

When to use it

Use leader-follower control for wheeled robots that must drive in a column, a wedge or a line behind one robot that a person or a planner controls, and when each robot can see one other robot and receive its speed and turn rate. It is simple to tune and has an exact exponential rate. Use distance-based formation control when no robot should depend on a single other robot, and consensus-based control when the robots share a compass.

Pitfalls

  • Small offset. The input ωF\omega_F is divided by dd. A small dd gives large turn rates, as the 7.58 rad/s in the example shows, and real robots saturate.
  • Reverse driving. When the leader drives backward the internal heading dynamics are unstable, and the follower can spin even while ll and ψ\psi look correct.
  • Bearing near the wrap. If ψd\psi_d is near π\pi, the measured bearing jumps between π\pi and −π-\pi. Wrap the difference ψd−ψ\psi_d - \psi, not ψ\psi alone, as line L5 does.
  • Error propagation. A long chain amplifies disturbances of the root, so the robots far down the tree see larger transients.
  • Collisions. The law keeps the point PFP_F at its target, but nothing stops two followers or a follower and its leader from touching during the transient.

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.

leader_follower_formation_control.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/leader-follower-formation-control/. The authors are listed in prov/P77YSV.jsonl.

"""Separation-bearing (l-psi) leader-follower control of unicycle robots.

A robot has the state (x, y, theta) and the inputs (v, omega), with
x' = v cos(theta), y' = v sin(theta), theta' = omega.

The follower controls its point P_F = (x_F + d cos theta_F, y_F + d sin theta_F),
at the distance d > 0 ahead of its wheel axle. The separation l and the bearing
psi are defined by P_F - p_L = l (cos(theta_L + psi), sin(theta_L + psi)), so psi
is measured from the heading of the leader. With gamma = theta_L + psi - theta_F,

    l'   = v_F cos(gamma) + d omega_F sin(gamma) - v_L cos(psi)
    psi' = (v_L sin(psi) - v_F sin(gamma) + d omega_F cos(gamma)) / l - omega_L.

The control below makes l' = a1 (l_d - l) and psi' = a2 (psi_d - psi).
"""

import math
from typing import Callable, Sequence

State = tuple  # (x, y, theta)


def wrap(a: float) -> float:
    """Wrap an angle to [-pi, pi)."""
    return (a + math.pi) % (2 * math.pi) - math.pi


def separation_bearing(leader: State, follower: State, d: float) -> tuple[float, float, float]:
    """Return (l, psi, gamma) of the follower with respect to its leader."""
    xl, yl, tl = leader
    xf, yf, tf = follower
    px, py = xf + d * math.cos(tf) - xl, yf + d * math.sin(tf) - yl
    l = math.hypot(px, py)
    psi = wrap(math.atan2(py, px) - tl)
    return l, psi, wrap(tl + psi - tf)


def follower_input(leader: State, vl: float, wl: float, follower: State, ld: float, psid: float,
                   d: float, a1: float, a2: float) -> tuple[float, float]:
    """The l-psi control law of one follower. Needs d > 0 and l > 0."""
    if d <= 0:
        raise ValueError("the offset d must be positive")
    l, psi, g = separation_bearing(leader, follower, d)  # @line L3
    a = a1 * (ld - l) + vl * math.cos(psi)  # @line L4
    b = l * (a2 * wrap(psid - psi) + wl) - vl * math.sin(psi)  # @line L5
    v = a * math.cos(g) - b * math.sin(g)  # @line L6
    w = (a * math.sin(g) + b * math.cos(g)) / d  # @line L7
    return v, w


def unicycle_rate(s: State, v: float, w: float) -> tuple[float, float, float]:
    return v * math.cos(s[2]), v * math.sin(s[2]), w


def all_inputs(states: Sequence[State], parent: Sequence[int], targets: Sequence[tuple],
               root_input: Callable[[float], tuple[float, float]], t: float, d: float,
               a1: float, a2: float) -> list[tuple[float, float]]:
    """Inputs of all robots. Robot 0 is the root; parent[i] < i for every follower i >= 1."""
    u = [root_input(t)]
    for i in range(1, len(states)):  # @line L2
        p = parent[i]
        ld, psid = targets[i]
        u.append(follower_input(states[p], u[p][0], u[p][1], states[i], ld, psid, d, a1, a2))
    return u


def simulate(states0: Sequence[State], parent: Sequence[int], targets: Sequence[tuple],
             root_input: Callable[[float], tuple[float, float]], t_end: float, h: float,
             d: float = 0.1, a1: float = 1.0, a2: float = 1.0) -> list[list[float]]:
    """Integrate the closed loop of a leader-follower tree with RK4 and fixed step h."""
    s = [list(map(float, q)) for q in states0]
    steps = max(1, round(t_end / h))
    h = t_end / steps
    t = 0.0

    def f(st, tt):
        u = all_inputs(st, parent, targets, root_input, tt, d, a1, a2)
        return [unicycle_rate(q, *ui) for q, ui in zip(st, u)]

    def add(st, k, c):
        return [[q[j] + c * kq[j] for j in range(3)] for q, kq in zip(st, k)]

    for _ in range(steps):  # @line L1
        k1 = f(s, t)
        k2 = f(add(s, k1, h / 2), t + h / 2)
        k3 = f(add(s, k2, h / 2), t + h / 2)
        k4 = f(add(s, k3, h), t + h)
        s = [[q[j] + h / 6 * (a[j] + 2 * b[j] + 2 * c[j] + e[j]) for j in range(3)]  # @line L8
             for q, a, b, c, e in zip(s, k1, k2, k3, k4)]
        t += h
    return s  # @line L9
test_leader_follower_formation_control.py 106 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/leader-follower-formation-control/. The authors are listed in prov/P77YSV.jsonl.

import math
import random

import pytest

from leader_follower_formation_control import (
    follower_input,
    separation_bearing,
    simulate,
    unicycle_rate,
    wrap,
)


def test_kinematics_match_finite_differences() -> None:
    # l' and psi' from the formulas against central differences of the geometry
    rng = random.Random(1)
    d, h = 0.2, 1e-6
    for _ in range(50):
        sl = (rng.uniform(-2, 2), rng.uniform(-2, 2), rng.uniform(-3, 3))
        sf = (sl[0] + rng.uniform(-3, 3), sl[1] + rng.uniform(-3, 3), rng.uniform(-3, 3))
        vl, wl, vf, wf = (rng.uniform(-1, 1) for _ in range(4))
        l, psi, g = separation_bearing(sl, sf, d)
        if l < 0.3 or abs(abs(psi) - math.pi) < 0.1:
            continue

        def at(c):
            a = [p + c * q for p, q in zip(sl, unicycle_rate(sl, vl, wl))]
            b = [p + c * q for p, q in zip(sf, unicycle_rate(sf, vf, wf))]
            return separation_bearing(a, b, d)

        lp, pp, _ = at(h)
        lm, pm, _ = at(-h)
        dl = (lp - lm) / (2 * h)
        dpsi = wrap(pp - pm) / (2 * h)
        assert abs(dl - (vf * math.cos(g) + d * wf * math.sin(g) - vl * math.cos(psi))) < 1e-6
        want = (vl * math.sin(psi) - vf * math.sin(g) + d * wf * math.cos(g)) / l - wl
        assert abs(dpsi - want) < 1e-5


def test_hand_value() -> None:
    # leader at the origin heading +x; follower at (-1, 0) heading +x; d = 0.5
    # P_F = (-0.5, 0), so l = 0.5, psi = pi wrapped to -pi, gamma = -pi
    l, psi, g = separation_bearing((0, 0, 0), (-1, 0, 0), 0.5)
    assert abs(l - 0.5) < 1e-15 and abs(psi + math.pi) < 1e-15 and abs(g + math.pi) < 1e-15
    v, w = follower_input((0, 0, 0), 1.0, 0.0, (-1, 0, 0), 0.5, -math.pi, 0.5, 2.0, 1.0)
    # a = 2 (0.5 - 0.5) + cos(-pi) = -1, b = 0 - sin(-pi) = 0, v = a cos(-pi) = 1, w = 0
    assert abs(v - 1.0) < 1e-12 and abs(w) < 1e-12


def test_errors_decay_like_exponentials() -> None:
    rng = random.Random(2)
    d, a1, a2 = 0.1, 1.5, 0.8
    for _ in range(4):
        leader = (0.0, 0.0, rng.uniform(-1, 1))
        follower = (rng.uniform(-3, -1), rng.uniform(-1, 1), rng.uniform(-0.5, 0.5))
        ld, psid = 1.0, 2.5
        l0, psi0, _ = separation_bearing(leader, follower, d)

        def root(t):
            return 0.5, 0.2 * math.sin(t)

        for t in (0.5, 2.0):
            s = simulate([leader, follower], [-1, 0], [None, (ld, psid)], root, t, 0.002, d, a1, a2)
            l, psi, _ = separation_bearing(s[0], s[1], d)
            assert abs(l - (ld + (l0 - ld) * math.exp(-a1 * t))) < 1e-6
            assert abs(wrap(psi - psid) - wrap(psi0 - psid) * math.exp(-a2 * t)) < 1e-6


def test_internal_dynamics_closed_form() -> None:
    # Start at the target l and psi with a heading error e0, leader straight at v_L.
    # Then theta_L - theta_F = e obeys e' = -(v_L / d) sin e, so tan(e/2) = tan(e0/2) exp(-v_L t / d).
    d, vl, ld, psid = 0.25, 0.4, 1.0, math.pi * 0.8
    for e0 in (0.5, 2.0, -2.8):
        tf = -e0
        px, py = ld * math.cos(psid), ld * math.sin(psid)
        follower = (px - d * math.cos(tf), py - d * math.sin(tf), tf)
        for t in (1.0, 3.0):
            s = simulate([(0.0, 0.0, 0.0), follower], [-1, 0], [None, (ld, psid)],
                         lambda _t: (vl, 0.0), t, 0.002, d, 1.0, 1.0)
            e = wrap(s[0][2] - s[1][2])
            want = 2 * math.atan(math.tan(e0 / 2) * math.exp(-vl * t / d))
            assert abs(e - want) < 1e-6


def test_chain_of_three_reaches_formation() -> None:
    # a column: robot 1 behind robot 0, robot 2 behind robot 1, leader drives straight
    d = 0.1
    targets = [None, (1.0, math.pi), (1.0, math.pi)]
    s0 = [(0.0, 0.0, 0.0), (-2.0, 0.7, 0.4), (-3.5, -0.6, -0.3)]
    s = simulate(s0, [-1, 0, 1], targets, lambda _t: (0.5, 0.0), 30.0, 0.01, d, 1.0, 1.0)
    for i in (1, 2):
        l, psi, _ = separation_bearing(s[i - 1], s[i], d)
        assert abs(l - 1.0) < 1e-6 and abs(wrap(psi - math.pi)) < 1e-6
        assert abs(wrap(s[i][2] - s[0][2])) < 1e-3
    # in a straight column with equal headings the axles are 1 m apart on the line y = 0
    assert abs(s[1][1]) < 1e-3 and abs(s[2][1]) < 1e-3
    assert abs((s[0][0] - s[1][0]) - (1.0 + d)) < 1e-3


def test_rejects_bad_offset() -> None:
    with pytest.raises(ValueError):
        follower_input((0, 0, 0), 1.0, 0.0, (-1, 0, 0), 1.0, math.pi, 0.0, 1.0, 1.0)

History

Desai, Ostrowski and Kumar (2001) modeled formations of nonholonomic robots as graphs of leader-follower pairs and gave two laws: one that keeps a separation and a bearing to one leader, and one that keeps two separations to two leaders. They used input-output linearization of an off-axis point and studied the stability of the remaining heading dynamics. Later work, summarized in the survey of Oh, Park and Ahn (2015), added estimation of the leader motion, switching between formations and undirected alternatives.

References

  1. Jaydev P. Desai, James P. Ostrowski and Vijay Kumar, Modeling and control of formations of nonholonomic mobile robots, IEEE Transactions on Robotics and Automation 17(6), 2001, pages 905 to 908 (the separation-bearing and separation-separation controllers, input-output linearization, and the stability of the internal dynamics)
  2. Kwang-Kyo Oh, Myoung-Chul Park and Hyo-Sung Ahn, A survey of multi-agent formation control, Automatica 53, 2015, pages 424 to 440 (leader-follower structures among the formation control methods)
  3. Mehran Mesbahi and Magnus Egerstedt, Graph Theoretic Methods in Multiagent Networks, Princeton University Press, 2010, Chapter 6, Formation Control (formations defined by relative states on a graph)

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 time.total
  • a full proof of space.extra
  • a full proof of messages.per_step
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