🧲 Magnetic Levitation Control: Stabilizing an Unstable System
Interactive maglev control simulator: a real nonlinear F ∝ I²/x² electromagnet-and-ball plant that is inherently unstable open-loop. Tune a PID feedback controller to hold the ball at a setpoint, push it, and watch it crash or drop with the controller off.
About this simulation
This simulation models the classic magnetic levitation control benchmark: an electromagnet pulls a ferromagnetic ball upward with the real nonlinear force law F = k·I²/x², where I is coil current and x is the gap. Gravity pulls the ball down with a constant mg. Position and velocity are integrated from the actual net force every tick — this is not a scripted hover. Near any equilibrium, dF/dx < 0: moving closer increases the pull further and moving away weakens it further, so the open-loop plant is genuinely, provably unstable. A PID feedback loop measures the gap and continuously adjusts I to hold the ball at the setpoint, rejecting disturbance pushes; turn the controller off and watch the same physics diverge into a crash or a drop.
🔬 What it shows
A real electromagnet-and-ball plant integrated in real time: the coil's current sets F = k·I²/x², gravity supplies mg, and the ball's gap x is updated from the net force every physics tick. The scope below traces gap x(t) against the setpoint over a rolling window, and the status pill reports LEVITATING, CRASHED or DROPPED live.
🎮 How to use
Set a target gap and ball mass, tune Kp/Ki/Kd, and leave the controller ON to watch the PID loop hold the ball steady — press Push down/up to test disturbance rejection. Switch the controller OFF and the same physics runs open-loop: the ball will drift, since the equilibrium current only balances gravity at one exact point and any deviation grows instead of shrinking.
💡 Did you know?
Earnshaw's theorem shows no static arrangement of magnets and steel can levitate stably on its own — active maglev systems like this one, and real ones such as Maglev trains' guidance electromagnets or magnetic bearings, all rely on a fast feedback loop sampling position and adjusting current thousands of times per second, not on a magnet that simply "floats."
Frequently asked questions
Why is this system inherently unstable without a controller?
The attractive force follows F = k·I²/x², which grows as the gap x shrinks. If the ball drifts slightly closer to the coil at a fixed current, F increases, pulling it closer still — and if it drifts slightly farther away, F decreases, letting gravity pull it away further. This is positive feedback: linearizing F around any equilibrium gap gives a restoring-force term with the wrong sign, so the open-loop system has no stable operating point. It will either snap up into the coil or fall away, never settle on its own.
How is this different from the site's other Magnetic Levitation simulation?
The other Magnetic Levitation simulation is about the underlying physics of levitation itself — the Meissner effect and flux pinning in superconductors, which sidestep Earnshaw's theorem through passive electromagnetic response. This simulation is about the control problem: it uses an ordinary electromagnet and ball, a setup that is definitely unstable on its own, and focuses entirely on the PID feedback loop that has to run continuously to hold it up. The two pages don't duplicate each other; they look at magnetic levitation from physics and control-engineering angles respectively.
What does the PID controller actually compute here?
Each control tick it measures the position error e = xset − x (target gap minus measured gap) and computes the coil current as I = I₀ − Kp·e − Ki·∫e·dt − Kd·de/dt, clamped to [0, Imax] with anti-wind-up. I₀ is a feed-forward term — the exact current that would hold the ball motionless at the setpoint — while the Kp/Ki/Kd terms correct any error away from that unstable equilibrium. Because I₀ alone is not enough (the equilibrium it defines is unstable), the feedback terms have to run every tick, not just once.
What happens when I turn the controller off?
The simulation holds the coil at exactly the feed-forward current that balances gravity at the current setpoint — the same unstable equilibrium the controller would otherwise correct around. A small simulated perturbation is added each tick, the same way real sensor noise, air currents or power-supply ripple would perturb a real open-loop maglev rig, and the genuine F ∝ I²/x² physics does the rest: the ball drifts toward the coil (and crashes) or away from it (and drops) within a few seconds, exactly as the instability argument predicts.
Why does increasing Kd help, and why not just set it very high?
The derivative term Kd·de/dt reacts to how fast the gap error is changing, adding damping that is essential here because the plant itself has no natural damping — without it, a proportional-only loop tends to oscillate or ring around the setpoint. But Kd amplifies any noise in the velocity estimate, so pushing it too high makes the coil current chatter and can destabilize the loop in a different way; real maglev controllers typically pair a moderate Kd with a filtered velocity estimate rather than raising it without bound.
Why does a heavier ball need more current to hold at the same gap?
The feed-forward current is I₀ = √(g·m·xset²/k): at a fixed target gap, a heavier ball needs a larger magnetic force to support its weight, and since F ∝ I², the required current scales with the square root of mass. This simulation recomputes I₀ live whenever you change the mass or target-gap sliders, which is why resetting the ball after such a change is necessary — the previous equilibrium current no longer matches the new physical situation.
What is the "Push" button testing?
Push down/up applies an instantaneous velocity impulse to the ball, simulating a sudden bump — a hand nudging it, a gust of air, a small mechanical shock. With the controller on, a well-tuned PID loop should measure the resulting gap error, recompute the coil current, and pull the ball back to the setpoint within a fraction of a second. With the controller off, the same push is simply an initial condition for the unstable open-loop dynamics, and the ball will not recover on its own.
A real nonlinear F ∝ I²/x² electromagnet-and-ball plant that is inherently unstable open-loop — tune a PID feedback controller to hold the ball at a setpoint.
2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install