kₓ = 2F/x₀ (destabilizing)
kᵢ = 2F/I₀ (control sensitivity)
Simulate electromagnetic levitation with real-time PID control. Tune coil parameters, air gap, mass and PID gains to explore closed-loop step response and the stability of this inherently unstable plant.
The fundamental challenge is balancing the electromagnetic force against gravity. The attractive force from the coil on the ferromagnetic ball is approximately inversely proportional to the square of the air gap.
$$F_{mag}= k \frac{I^2}{x^2}$$Where $F_{mag}$ is the magnetic force (N), $k$ is the Coil Constant (N·m²/A²) set by the electromagnet's design, $I$ is the coil current (A), and $x$ is the Gap distance (m) from the coil to the ball. This nonlinear relationship is the source of the instability.
The system is stabilized by a PID controller, which calculates the required coil current based on the error between the desired gap and the measured gap.
$$I(t) = K_p e(t) + K_i \int_0^t e(\tau) d\tau + K_d \frac{de(t)}{dt}$$Where $e(t) = x_{desired}- x_{actual}$ is the gap error. $K_p$ (Proportional Gain) reacts to present error, $K_i$ (Integral Gain) eliminates steady-state offset by accumulating past error, and $K_d$ (Derivative Gain) predicts future error by looking at its rate of change, adding damping. Tuning these three gains in the simulator is the key to achieving a fast, stable, and accurate levitation.
Maglev Transportation: High-speed trains like Japan's SCMaglev use superconducting electromagnets and sophisticated control systems to levitate and propel trains at speeds over 600 km/h, eliminating wheel-rail friction for unparalleled efficiency and smoothness.
Contactless Bearings in Vacuum & Cleanrooms: Magnetic levitation is used for frictionless bearings in high-speed centrifuges, flywheel energy storage, and semiconductor manufacturing equipment, where physical contact would cause contamination or wear.
Magnetic Levitation for Display & Demonstration: Popular science exhibits and luxury product displays (like levitating speakers or planters) use small, controlled electromagnets to create the striking visual effect of objects floating in mid-air.
Advanced Material Processing: In containerless processing, materials are levitated to be melted and solidified without touching a crucible, which can introduce impurities or cause unwanted crystallization. This is used in metallurgy and pharmaceutical research.
First, it is a dangerous misconception to think that "the system will stabilize if you just make the P-gain strong enough". It's true that increasing Kp strengthens the restoring force toward the target position. However, if it's too strong, the levitating object will overshoot the target and be pushed hard to the opposite side, where a strong force in the reverse direction acts... This cycle repeats, causing severe oscillation. For example, try running a simulation with Kp at its maximum and Kd near zero. The graph will show wild instability, and in reality, the object would certainly collide with the electromagnet. The key to stabilization lies in the balance between Kp (restoring force) and Kd (damping force).
Next, note that starting parameter tuning from the 'mass' often leads to failure. First, fix the mass to a standard value and use the 'coil turns' and 'pole area' to ensure a rough estimate of the static attractive force needed for levitation. For instance, if you double the mass, you need to increase the turns N or area A (or raise the initial current) to more than double the attractive force. If this "static balance" isn't achieved, tweaking the PID gains won't even allow levitation to occur in the first place.
Finally, it's crucial to understand the decisive gap between the simulator and a real machine. This tool uses an ideal "single degree of freedom" model. However, in a real device, significant issues arise from the levitating object's "rotation (pitch/roll)", "sensor measurement noise", and "the limited response speed of the amplifier generating the control current". Gains perfectly tuned in the simulator are not guaranteed to work directly on the actual hardware. In practice, the golden rule is to use the simulation for rough design and then start with much smaller gains on the real machine, increasing them cautiously.
A 1.2 kg steel ball levitated in an 800-turn solenoid with initial gap x0 = 12 mm and pole area poleA = 0.015 m². At i0 = 1.0 A, the magnetic force balances weight. Tuning PID with Kp = 8.5, Ki = 1.2, Kd = 0.35 produces a 0.5 mm undershoot and 280 ms settling time when a 50 mm step input (new setpoint) is applied. Reducing Kd to 0.25 increases overshoot to 3.8 mm; increasing to 0.45 causes sluggish response with 600 ms settling.