$m\ddot{d}= mg - F_{mag}+ F_{ext}$
PID: $u = K_p e + K_i\!\int\!e\,dt + K_d\dot{e}$
Real-time magnetic levitation simulation. Experience passive levitation instability and PID-controlled active stabilization. From Earnshaw's theorem to maglev train principles.
Kp slider first; you'll see oscillation. Then add Kd to calm it down.F_ext for a second—you'll see the controller struggle to reject this disturbance, which is exactly what engineers test for.The core physics is Newton's second law applied to the levitating object. The net force determines its acceleration. The magnetic force is governed by the electromagnet's characteristics and the instantaneous current.
$$ m\ddot{d}= mg - F_{mag}+ F_{ext}$$Where:
$m$: Mass of the levitating object (kg).
$\ddot{d}$: Second derivative of the gap (acceleration).
$g$: Gravitational acceleration (≈ 9.81 m/s²).
$F_{mag}$: Upward magnetic force.
$F_{ext}$: Any additional external force (e.g., a push).
The magnetic force is not constant; it depends on the coil current $i$ and the gap $d$ in a specific, non-linear way. Simultaneously, a PID controller calculates the required control current based on the error between the target and actual gap.
$$ F_{mag}= K \cdot \frac{i^2}{d^2}\quad \text{and}\quad u = K_p e + K_i\int e\,dt + K_d\dot{e} $$Where:
$K$: Force constant of the electromagnet (N·m²/A²).
$i$: Coil current (A).
$d$: Instantaneous gap (m).
$u$ : Controller output (which sets $i$).
$e = d_0 - d$: Gap error.
The squaring of current in the force law is key to the instability.
Maglev Trains: Electromagnetic Suspension (EMS) systems, like the one modeled here, are used in some maglev trains (e.g., Transrapid in Shanghai). Electromagnets in the train's undercarriage attract it upwards to a guide rail, and a sophisticated version of this PID control maintains a stable 10mm gap at speeds over 400 km/h.
Contactless Bearings in Turbomachinery: High-speed centrifuges, compressors, and flywheel energy storage systems use magnetic bearings. Levitating the rotor eliminates mechanical friction and wear, allowing for higher speeds, lower maintenance, and operation in vacuum chambers.
Vibration Isolation Platforms: Precision manufacturing (e.g., semiconductor lithography) and scientific experiments (e.g., gravitational wave detection) require ultra-stable platforms. Active magnetic levitation can isolate sensitive equipment from ground vibrations more effectively than passive springs.
Product Demonstrators and Haptic Interfaces: The compelling visual of a floating object makes maglev a popular educational tool. Furthermore, the principle is used in advanced haptic interfaces, where magnetic force can provide touch feedback to a user's hand in mid-air, enabling interaction with 3D virtual objects.
First, the idea that "simply increasing the P-gain makes it stabilize faster" is a major misconception. While raising Kp does make the system react more quickly to error, going too far causes severe oscillation (hunting), as you saw in the simulator. This means the system is in an "over-controlled" state. In a real device, this can cause coils to overheat or the controller to oscillate, leading to failure. A good rule of thumb is that the value just before oscillation subsides is the first step toward an "optimal gain."
Next, there's a tendency to think "the larger the D-gain, the better it suppresses vibration," but this is also a pitfall. The derivative term looks at the "rate of change" of the error, so it can drastically amplify sensor noise (tiny measurement errors). For example, if there's 0.01mm of noise in the gap measurement, a large Kd can turn that into huge disturbances in the control signal. In practice, "incomplete differentiation" is often used as a countermeasure against noise.
Finally, the mindset of "since the I-gain can eliminate steady-state error, let's set it high from the start." This might be the most dangerous one. The integral term continuously accumulates past errors. So, for instance, after a sudden external force is applied, even after the error is corrected, the accumulated value (integral windup) remains, causing significant overshoot in the control. Implementing an "anti-windup" measure is essential. In the simulator, if you increase Ki and then suddenly apply and release an external force, you should be able to experience this phenomenon firsthand.
Steel ball (0.015 kg) levitating in electromagnet: initial gap = 15 mm, K = 1.2 N/mm, Kp = 2.5. Gravity pulls ball downward at 0.147 N. With Kp too low (0.8), coil current oscillates 0.6–2.1 A and gap drifts ±3 mm (unstable per Earnshaw). Increasing Kp to 2.5 produces steady coil current of 1.4 A maintaining gap at 15.2 mm ± 0.2 mm. Further increase to Kp = 5.0 causes overshoot; gap cycles 14–16 mm with 0.8 A ripple.