Magnetic Levitation Simulator (PID) Back
Electromagnetics & Control Engineering

Magnetic Levitation Simulator (PID Control)

Real-time magnetic levitation simulation. Experience passive levitation instability and PID-controlled active stabilization. From Earnshaw's theorem to maglev train principles.

Levitation Mode
System Parameters
Object mass m (kg)0.10
Target gap d₀ (mm)10.0
Force constant K5.0
PID Gains
Proportional gain Kp800
Integral gain Ki200
Derivative gain Kd40
Disturbance
External force F_ext (N)0.0
Live Readings
Current Gap (mm)
Coil Current (A)
Error (mm)
Status

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}$
Magnetic Levitation Animation
Gap d(t) vs Time
Coil Current i(t) vs Time

What is Magnetic Levitation Control?

🧑‍🎓
What exactly is the "passive instability" mentioned in the simulator description? Why can't the object just float stably on its own?
🎓
Basically, it's a consequence of Earnshaw's theorem. For static magnetic fields, there's no stable equilibrium point for a magnet in free space—it's either attracted or repelled. In our simulator, the electromagnet *attracts* the steel ball upward. If the ball gets too close, the force spikes; too far, and gravity wins. It's like trying to balance a pencil on its tip. Try setting all PID gains to zero in the controls; you'll see the ball immediately crash or fall.
🧑‍🎓
Wait, really? So how does the PID controller fix this? What do the P, I, and D terms actually do in this magnetic case?
🎓
In practice, it's a constant, high-speed correction. The controller reads the gap error $e = d_0 - d$ from the sensor. The **Proportional (P)** term ($K_p e$) applies a force proportional to the error—like a spring pulling it back to position. The **Derivative (D)** term ($K_d \dot{e}$) damps the velocity, preventing overshoot and oscillation. The **Integral (I)** term ($K_i \int e\,dt$) corrects for steady-state errors, like if the ball's weight wasn't perfectly compensated. Try moving the `Kp` slider first; you'll see oscillation. Then add `Kd` to calm it down.
🧑‍🎓
That makes sense! So what's the role of the "Force constant K" and the "External force F_ext" parameters? Are they just for theory?
🎓
Great question! They're crucial for realism. The **Force constant K** defines how strong your electromagnet is. A higher $K$ means a smaller current can lift the same mass, but the system can also become more sensitive and harder to control. The **External force F_ext** lets you simulate real-world disturbances. For instance, imagine giving the levitating object a gentle poke. Try setting a positive `F_ext` for a second—you'll see the controller struggle to reject this disturbance, which is exactly what engineers test for.

Physical Model & Key Equations

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.

Real-World Applications

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.

Common Misconceptions and Points to Note

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.

Related Engineering Fields

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.

For Further Learning

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.