Quad PID Lab

Quad PID Lab

A 5-inch class quadcopter with a cascaded angle → rate PID. Fly it, disturb it, and watch every loop respond while the Bode, Nyquist and step plots update with your gains.

Throttle · Yaw
Pitch · Roll
Mode 2 sticks. Keyboard: WS throttle, AD yaw, arrows pitch/roll, R reset, Space pause, G gust, K kick. A USB gamepad or radio in joystick mode works too.

Block diagram · roll

Live signals for the selected axis. Click a block to jump to its setting.

Outer loop (angle mode only). Stick sets a target angle; the level gain Kₐ turns angle error into a rate target. In Acro the stick sets the rate target directly and this loop is bypassed.

Inner loop. P and I act on rate error. D acts on the measured rate only, so stick moves don't produce a D spike. The sum u is a fraction of available motor authority.

Plant. The mixer splits u into thrust differences, motors lag by τm, torque accelerates the inertia J, and the gyro reports it Td late with noise. Wind, kicks and CG offset enter as torque.

System wiring

Every signal in the simulator, from stick to motor to sensor and back.

Frequency analysis · roll

Bode · open loop L(jω)

Solid: open-loop gain and phase. Dashed: closed-loop |T|. Phase margin is read at the 0 dB crossover; gain margin at −180°.

Nyquist

Radius compressed beyond 1 (rings at 1, 10, 100). The loop is stable when the curve keeps the −1 point on its left. Closest approach to −1 is 1/Ms.

Step response (linear)

Unit step in the setpoint through the linearised model, including delay and motor lag. Band is ±2%.

Telemetry · last 10 s

Starting state

Position & velocity
Attitude (°) & body rates (°/s)

Saved in this browser and used on every Reset. Throttle starts at hover (or centre with alt hold) when the start altitude is above the floor.

Environment

Impulses

A kick is a 40 ms torque pulse. A gust is a 1.6 s burst of extra wind plus a small roll torque. CG offset is a constant roll torque that only the I term can cancel.

Airframe

How to tune it

What each term does

  • P pushes back in proportion to rate error. It sets the crossover frequency, which is how quickly the quad follows the sticks. Too much and the loop runs out of phase: it rings, then oscillates.
  • D pushes against rate change. It adds phase lead near crossover, which damps overshoot and lets you run more P. It also amplifies gyro noise and delay effects at high frequency, so too much D oscillates fast and heats motors.
  • I accumulates error over time. It cancels steady torques (off-centre battery, wind) that P alone leaves as a permanent error. Too much adds phase lag at low frequency: slow wobble, bounce-back after flips.
  • D filter smooths the D term. Lower cutoff means less noise but more lag.
  • Level gain Kₐ is the angle-mode outer loop. Keep its crossover at least 4× below the rate loop's.

A tuning recipe

  1. Start in Acro with I and D at zero. Raise P until a kick produces ringing, then back off by a third.
  2. Add D until the step-response overshoot is under about 20% and the Bode phase margin is above 45°.
  3. Set CG offset to 10 mm. Raise I until the roll angle no longer drifts, keeping phase margin above 45°.
  4. Check gain margin is above 6 dB and Ms below 1.6. Then add gyro noise and confirm the motor trace stays calm.
  5. Switch to Angle mode and raise Kₐ until the angle step has little overshoot.

Reading the numbers

NumberGoodMeaning
Phase margin45–70°Extra lag the loop tolerates at crossover. Low means overshoot and ringing.
Gain margin> 6 dBHow much more gain until oscillation at the −180° frequency.
Delay margin> 5 msExtra latency the loop can absorb. Compare with the loop delay.
Ms< 1.6Peak disturbance amplification. 1/Ms is the closest the Nyquist curve gets to −1.
Crossover—Where |L| = 1. Roughly the loop's bandwidth.

Experiments

  • Load Example: hover at 5 m, then the P too high preset and press Kick roll. Slow to 0.25× and watch the motor arrows fight.
  • With Locked-in, raise loop delay to 12 ms. Watch the phase margin collapse and the Nyquist curve swing toward −1.
  • Pick No I term, set CG offset to 10 mm and hover. The quad leans and slides. Add I back and it recovers.
  • Pick D too high and raise gyro noise. The motor chart turns into a band of noise before it becomes unstable.