Control Barrier Functions on a robot arm
The rule itself. The arm is the dot; it can never fall below the curve.
Each angle is measured against the link before it, so the angles simply add up. Everything below is, by default, a rule about that one point at the end — Expert mode extends it to the whole arm — and the formula does not care whether there are two joints or four.
Spinning one joint sweeps everything beyond it around that joint, and moves nothing before it. That single sentence is the whole Jacobian, for any number of joints, and it is why every point on the arm has one of its own — which is what lets the filter protect a link and not only the tip. With two joints J is square and det J = L₁L₂ sin q₂ vanishes when the arm is straight or folded, and the radial direction is then unreachable at any joint speed. With three or four the arm is redundant: J is wide, whole families of joint motions produce the same tip motion. The damped inverse below does not pick among them exactly: for λ > 0 it minimises ‖J u − v‖² + λ²‖u‖², trading tracking error against joint speed, and only tends to the minimum-norm solution as λ → 0.
Head straight at the target, never faster than the speed cap. This controller has never heard of the obstacle and it is never replanned — the detours you are about to see are the filter’s doing, not a smarter plan.
The cap is applied to the Cartesian velocity before the inverse, so the
unfiltered command is a straight line at constant speed until the end effector is within
vmax/kp of the target, after which it decays proportionally — the
reference the detour is read against. The realised path is close to that line but not
exactly on it, because the damped inverse trades tracking accuracy for bounded joint speed. The inverse is damped least squares: J JT is 2×2 however many
joints there are, and its determinant is at least λ⁴ > 0 for
every pose, so there is no singular case to special-case. Writing
if (det < ε) would put back exactly the discontinuity that damping exists
to remove. λ is a length and scales with the arm (0.10 m here);
it caps the joint-speed gain, so near full stretch the arm refuses the radial command and
sweeps sideways instead of exploding. Anything genuinely time-optimal would need a
double-integrator model and a higher-order CBF — a different lesson.
| h > 0 | outside — safe |
| h = 0 | exactly on the boundary |
| h < 0 | inside — the rule is violated |
One formula draws every forbidden region: ε = 1 is the ellipse you drag out yourself, and a small ε squares off the corners so the region hugs the rectangular obstacle. The obstacle and a region you drew are the same rule, one more h. The √(ab) in front turns the shape value into metres of clearance, so h is very nearly the gap you can see on screen.
Taking the n-th root of the superquadric sum is a monotone rescale that fixes zero, so the zero level set, the safe set and the CBF property are all untouched — but the raw sum grows like distancen. At ε = 0.25 that is the eighth power, so an unrooted h would reach about 106 at the edge of the workspace and throttle the arm to a fraction of a sensible approach speed while it is still half a metre away. With the root, ‖∇h‖ stays bounded: it never exceeds √(a/b), and for an ellipse (ε = 1) it never falls below √(b/a). For smaller ε it dips a little further — 0.71 rather than 0.78 on the default obstacle. h is therefore proportional to the true clearance up to a bounded factor, and for a circle it is exactly the signed distance, which is what lets one α mean much the same thing for every shape. The absolute values cause no kink for ε < 2. ∇h fails to exist at exactly one point, the centre, where the rescaled norm has a conical cusp; the code returns an outward direction there rather than dividing by zero. There is no separate clearance term: the outline you see is the h = 0 curve.
The obstacle is a rectangle and stays one; ε shapes the region around it, which is always the superellipse that just contains the rectangle — at the corner (a, b) we need (a/A)n + (b/B)n = 1, so A = 21/n·a. At ε → 0 the region is the rectangle itself; at ε = 1 it is the ellipse circumscribing it.
On vocabulary: the set where h ≥ 0 is the safe set, and that name is standard. Its complement has no single canonical name — papers say unsafe set or avoid set, applications say obstacle, and aerospace work says keep-out zone. “Forbidden region” is the phrasing used here, and it is also the established term in the virtual-fixtures literature, where a forbidden-region virtual fixture[6] is exactly this: a software constraint that keeps a tool out of a volume.
The rule is not “never get closer than a metre”. It is: the closer you are, the slower you may approach. −α(h) is an approach budget that uses itself up — on the boundary h = 0, so the budget is zero, so ḣ ≥ 0, and you are guided along the boundary instead of through it. Because h is in metres, α reads off directly: at α = 2 you may close at 2 m/s for every metre of clearance, so at 10 cm you may close at 20 cm/s. α has units of 1/s.
The plot under the picture is this line. The curve is −α(h), the fastest the clearance is allowed to fall; the dot is the arm right now. Everything above the curve is permitted, and while the safety filter is on the dot cannot drop below it. Watch it slide down into the corner as the arm arrives: at h = 0 the only permitted rate is zero.
α·h is the simplest extended class-K function: through the origin, strictly increasing, and defined for h < 0 as well, which is what makes the inequality meaningful inside a region. With that choice the comparison lemma gives the bound above — forward invariance from h(0) ≥ 0, and exponential recovery from h(0) < 0. The general form is extended class-K for every p > 0, and the sign factor is what keeps it increasing through zero: α·hp alone would be positive on both sides for even p. p = 1 is the textbook straight line and p = 2 is the paper’s h²; larger p buys caution close to the boundary and freedom far from it, and turns the recovery bound from exponential into a power law. The slider starts at p = 1 because for p < 1 the function is no longer locally Lipschitz at the origin and the comparison lemma no longer applies; in this simulation p = 0.5 lets the Euler step overshoot the boundary by up to 1.7 mm.
Because the robot is velocity-commanded, u appears already in the first derivative of h, so wherever ∇hTJ ≠ 0 the constraint has relative degree 1 and a single inequality linear in u suffices. Where that row vanishes the arm has no authority over h at all, and the filter reports it rather than pretending otherwise. Command torque instead and u drops out of ḣ entirely, and a higher-order CBF[10] is needed.
The guarantee is continuous-time. We integrate with explicit Euler at Δt = 1/240 s, where αΔt ≤ 1 keeps the linearised update hk+1 ≥ (1 − αΔt)hk non-negative; at the largest α on the slider αΔt ≈ 0.08. That is not a discrete-time proof — the neglected second-order term is what could let a trajectory dip past the boundary — but over the parameter sweeps run for this page no violation occurred for p ≥ 1, though at the tightest setting the remaining margin was under a micrometre.
If the command is already safe, the filter hands it back untouched — that is what it means when the ghost arrow is nowhere to be seen, sitting exactly under the solid one. Otherwise it makes the smallest change that obeys every rule at once.
The objective is strictly convex, so the solution is the unique Euclidean projection of unom onto the feasible polyhedron, and at the optimum some subset of the rows holds with equality. More rows than that can be tight, but the correction u − unom lies in the cone they generate, and in ℝn such a cone is generated by at most n of them, so subsets of size n suffice. Enumerating the subsets, solving the equality-constrained problem for each, and retaining the feasible candidate of least cost therefore returns the exact optimum, and requires no multiplier sign test. Most rows are eliminated before the enumeration begins: on the safe set u = 0 is feasible, so the optimum satisfies ‖u − unom‖ ≤ ‖unom‖, and any row whose slack exceeds ‖a‖·‖unom‖ cannot be active.
Feasibility is guaranteed on the safe set: if every h ≥ 0 then every right-hand side −α(h) ≤ 0, so u = 0 satisfies every row. Infeasibility therefore requires at least one h < 0, and the enumeration certifies it at no additional cost. The arm is then stopped and the condition reported. Stopping does not restore safety — with h < 0 the row demands ḣ > 0 and u = 0 delivers ḣ = 0 — but it fails visibly, whereas a slack variable would report success on a constraint it had just relaxed.
Instead of a single row for the end effector, one row per link, written at that link’s closest point to the region. h is convex along a straight line, so a ternary search locates the minimiser to about 10−5 of the link length in 28 iterations. Where that minimiser is unique, Danskin’s theorem[12] gives the derivative of the minimum as the derivative at the minimiser, so the row may be evaluated at the frozen parameter s*k; a region with a nearly flat face can make it non-unique, and there the minimum is only directionally differentiable and the row takes one element of the subdifferential. This is both tighter and cheaper than sampling points along the link. With three or four joints and the option disabled, a forearm passes through the region while the end effector goes around it.
The same construction with the manipulator as both bodies: h is the clearance between two non-adjacent links — their distance less the margin dmin, as the second formula shows — s* and t* are the parameters of their closest points, and the row is the difference of the two link Jacobians projected onto the line joining those points. Adjacent links share a joint, so their distance is identically zero and no such constraint can be imposed; the option therefore has no effect below three joints.
The objective is minimal in joint space, not in task space: the task-space correction direction is J JT∇h rather than ∇h, so the end effector also decelerates as it slides along a boundary rather than only changing direction. This is a property of the formulation, not an artefact. Safety is likewise not liveness — the arm may stall with the target behind a region, and nothing in the construction precludes it. A safety filter certifies constraint satisfaction; reaching the goal remains the planner’s task.
WHAT is forbidden lives in h. HOW CAUTIOUSLY you approach lives in α. Draw a new shape and you have changed h. Move one slider and you have changed α. Nothing else in the controller needs to know.