loading...
Press Step test: the current settings are simulated from rest and compared with the ideal closed form.
running...
How to use
Pick a preset, then change gains and watch the beam. The red cone marks the setpoint; click the beam to move it. The blue arrow on the ball is its velocity, the orange arrow its acceleration (toggle "Show vectors"). Tap ball gives a disturbance (keys ← →). The ultrasonic sensor at the left end reads the distance to the ball.
Try: (1) Well tuned, then lower zeta to 0.2 and see the overshoot; (2) P only: no damping term, the ball swings on; (3) Lazy servo and Delay 150 ms: the dominant pole crosses into the right half plane (the readout says UNSTABLE); (4) Noisy, raw velocity versus filtered; (5) a hollow shell against a solid ball with the same gains, or the Moon; (6) Open loop: why feedback is needed; (7) Step test to compare the simulation with the closed form. Space pauses, R resets.
The aluminium base, pivot posts, brass axle, two-rail beam, ultrasonic sensor, servo with horn, ruler strip and the striped ball were modelled in Blender with Cycles-baked textures; Three.js r160 moves the beam, horn and link from the same model as the 2D companion.
The science
The ball-and-beam is the textbook feedback problem. A ball rolls freely on a beam; a servo tilts the beam; a distance sensor reports where the ball is. With the beam level the ball stays wherever it is, with the beam tilted it accelerates downhill. Position is therefore the double integral of the tilt: the plant has two poles at zero, it is not stable on its own, and without feedback the ball always runs off the end. The same structure appears in a rocket's attitude loop, a balancing robot or a quadcopter's position hold.
The plant. A ball of mass m and radius R rolling without slipping on rails tilted by the angle θ, at distance r from the pivot, obeys (Lagrange, kinetic energy of translation plus rotation):
(1 + k) r'' = r θ'2 + g sinθ − c (1 + k) r' k = J / (m ρ2)
Mass and radius cancel. Only the shape of the mass distribution enters through k: 2/5 for a solid sphere, 2/3 for a thin shell, about 0.47 for a thick hollow ball (inner radius 0.6 R), 0 for a frictionless slider. If the ball rides on two thin rods a distance s apart it rolls on a smaller effective radius ρ = √(R2 − s2/4), so k grows by R2/ρ2: a wide rail gap slows the ball. The term rθ'2 is the centrifugal force of the rotating beam; c is an optional viscous rolling drag. For small angles r'' = aθ with a = g/(1+k): a solid sphere on a flat rail accelerates at only 5/7 g sinθ, the well-known 71 % of a sliding block.
Servo. The beam angle follows the command through a second-order lag, θ'' = ωs2(θc − θ) − 2 ζs ωs θ' with ζs = 0.7, plus a rate limit and an angle limit. The servo is treated as an ideal motion source: the small torque of the ball does not disturb it.
Controller. The sensor sample (every 5 ms, with noise and delay) gives r̂. The command is
θc = Kp (rsp − r̂) − Kd v̂ + Ki ∫(rsp − r̂) dt, Kp = ωn2/a, Kd = 2 ζ ωn/a
The sliders set the closed-loop natural frequency ωn and damping ζ; the gains follow from a, so changing the ball or gravity re-tunes the controller. The derivative acts on the measurement (no kick at a setpoint step) through a first-order filter. For an ideal servo the loop is r'' + 2ζωn r' + ωn2 r = ωn2 rsp, with overshoot e−πζ/√(1−ζ2). ζ = 0 (P only) never decays: the ball swings forever at ωn and the servo lag actually feeds the swing, so the amplitude creeps up. The fastest 2 % settling is near ζ = 0.76.
Why real loops misbehave. The pole map includes the full loop: the servo (second order), the derivative filter, drag, the integral term and the delay (sample hold, derivative difference and the sensor delay, as a second-order Padé approximation), solved with the Durand-Kerner root finder. A servo that is slow compared with ωn or a delay of the order 1/ωn pushes the poles into the right half plane. Noise is amplified by differentiation: a difference quotient of noise σ has standard deviation σ√2/Ts; with the first-order filter (α = Ts/(Tf+Ts)) it is σ√2 α/(Ts√(2−α)) (checked by Monte Carlo below). That is the reason for the velocity filter, at the price of phase lag.
Numerical checks run in the page (fresh objects, independent of what you see): (1) rolling time over 30 cm at 10° against √(2d(1+k)/(g sinθ)), for each ball and rail gap; (2) the centrifugal term against the cosh/sinh closed form with g = 0 and a Jacobi constant; (3) energy conservation at constant tilt; (4) the P-only period against 2π/ωn; (5) PD step overshoot against the damping formula; (6) the damped frequency and decay rate of a loop with servo lag from the roots of its characteristic polynomial against the time-domain simulation; (7) a 400,000-sample Monte Carlo of the velocity-estimate noise against the closed form.
Simplifications. Rolling without slipping (the page flags when the needed friction k/(1+k)·tanθ exceeds the value you set, but it does not model the slip); the ball centre sits above the beam but this offset and the Coriolis coupling are ignored; the beam, servo and sensor are ideal apart from the stated lag, limits, noise and delay; no backlash or friction in the servo; the linear pole analysis ignores saturation and sampling beyond the delay approximation, so a loop reported stable can still limit-cycle when it saturates, and the Padé delay is approximate for delays longer than about 0.3/ωn. Typical hobby servo numbers (bandwidth, rate) and the sensor noise are illustrative.