EMS Maglev Model
$F_{mag}= K \cdot \dfrac{i^2}{d^2}$$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.
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.
The core of this magnetic levitation simulator is the very concept of feedback control: "measuring a moving object with a sensor and applying force in real-time to maintain a target position." Therefore, you'll find the exact same principles applied in fields that seem to deal with completely different things.
For example, a car's cruise control uses PID control to adjust the engine throttle opening (the control output u) based on the error between the current speed measured by a speed sensor and the set speed. The same goes for the autopilot (attitude control) of drones and aircraft. A gyro sensor measures the aircraft's tilt (angle), and motor RPM is adjusted to maintain level flight. If the P-gain here is too strong, the aircraft will start pitching (oscillating up and down).
A more advanced application is the positioning stage in precision machine tools and semiconductor manufacturing equipment. Where nanometer-level accuracy is required, PID alone is often insufficient. It's common to combine it with "feedforward control," which calculates the target trajectory in advance. Also, in robot arm control, the arm's movement itself generates complex forces (inertial force, centrifugal force), so "model-based control" is used to cancel these out in advance using a mathematical model (dynamic model). Solving the magnetic levitation equation of motion $$m\ddot{d}= mg - F_{mag}+ F_{ext}$$ is precisely the first step in that direction.
Once you've fully explored this simulator, the next step is to encounter the concepts of "transfer functions" and "frequency analysis." Viewing the oscillations and responses you've seen so far in the time domain (graphs with time on the horizontal axis) in the frequency domain (graphs with frequency on the horizontal axis) reveals the system's fundamental properties clearly. For instance, you can see which frequency disturbances the system is weak against (sensitivity) and the frequency limit where control becomes ineffective (gain margin, phase margin). Understanding this allows you to theoretically design how each PID gain affects the system's "bandwidth" and "stability margins."
Mathematically, learning the Laplace transform
As a practical next topic, I recommend "state-space representation" and "observers (state estimators)." PID only looks at the error in the output (gap d), but state-space explicitly handles all the system's internal state variables, like velocity and acceleration. And when you can't measure all states with sensors (e.g., if there's no velocity sensor), a mechanism called an observer estimates the internal states (velocity $\dot{d}$) from the measurable output (gap d) for use in control. This is the gateway to modern control theory and the first step toward designing more advanced and robust control systems.