Levitation - 2D feedback control

A magnet can only pull, and its pull grows as the gap shrinks, so a hanging ball is unstable. See how a fast controller on the coil current tames it.

Switch to 3D

Presets

Playback

Ball and magnet

The hold current is set so the magnet pull equals the weight at the set-point gap.

Controller and hardware

Current command = i0 + Kp (gap error) + Kd (velocity), limited to 0-3 times i0.

Disturbances

You can also flick the ball directly on the canvas.

Layers

Model checks

The science

Force and equilibrium

For a small ball under a pole face the pull is modelled as F = k i² / z², with coil current i and gap z. The ball hangs from the magnet, so m z̈ = m g - k i²/z². Equilibrium needs i0 = z0 √(m g / k).

Instability

Linearising about (z0, i0) gives δz̈ = (2g/z0) δz - (2g/i0) δi. At fixed current there is a real positive pole at s = +√(2g/z0): any displacement grows exponentially (Earnshaw's theorem rules out stable static levitation with fixed magnets).

Feedback

With δi = Kp δz + Kd δż, the characteristic equation is s² + (2g Kd/i0) s + 2g(Kp/i0 - 1/z0) = 0. It is stable only if Kp > i0/z0 and Kd > 0; natural frequency and damping follow from the coefficients. The coil adds a first-order lag τ = L/R that eventually destabilises high gains.

Honest simplifications: a point-like inverse-square force law (real magnets have fringing and saturation), a single vertical degree of freedom, current limited to 0 and 3 i0, a perfect position sensor apart from Gaussian noise and a fixed 1 kHz controller. The flux lines are qualitative. Not a model of a specific commercial maglev train.

What to try
  • Press Too weak (falls): Kp is below i0/z0, the controller cannot hold the ball.
  • Underdamped: lower Kd and watch the ringing in the phase-plane spiral.
  • Slow coil: raise the lag and see a well-tuned loop go unstable.
  • Flick the ball on the canvas and read the recovery time.
Gap over the last 6 seconds
Coil current over the last 6 seconds
Phase plane: velocity vs gap
Magnet force vs gap at the hold current