การควบคุมแบบ cascade และการปรับจูน flight controller
Rate/attitude/position loops, filters and log-based tuning
Overview: why a flight controller runs loops inside loops
A multirotor can only control its position indirectly. To move forward it must tilt; to tilt it must rotate at the right rate; to rotate it must change the thrust of individual motors. Flight controllers therefore use cascade control: several feedback loops nested inside one another, with each outer loop producing the setpoint for the loop inside it.
Understanding this structure lets you tune the right thing. Tuning means choosing gains and filters so that the aircraft responds quickly, does not oscillate, and does not overheat its motors. A poorly tuned drone may jitter enough to blur its camera, lag behind gusts, or oscillate until it crashes.
The four loops of a multirotor
| Loop | Input setpoint | Output | Typical controller |
|---|---|---|---|
| Position | Desired position | Desired velocity | P |
| Velocity | Desired velocity | Acceleration, i.e. tilt and thrust | PID |
| Attitude | Desired angle | Desired body rate | P |
| Rate | Desired body rate | Torque to the mixer | PID with feedforward |
PX4 and ArduPilot follow the same idea. ArduPilot uses a P controller on angle that feeds a desired rate to the rate PID, and limits the angular acceleration of commands so they are smooth. PX4 chains position P, velocity PID, attitude P and rate PID.
The key rule is that each inner loop must be much faster than the loop around it. The outer loop treats the inner loop as if it followed commands instantly. If the inner loop is slow, the outer loop demands changes faster than it can deliver, and the result is overshoot and oscillation. That is why tuning always proceeds from the inside out. A poor rate loop damages every flight mode.
Feedforward sends the setpoint straight to the output without waiting for an error to build. If 100 deg/s is commanded, torque proportional to that request is applied immediately and the PID corrects only the remainder. Feedforward makes command response faster without raising feedback gains to the point of oscillation.
Filters: less noise, more delay
A gyro measures both real rotation and propeller vibration. Fed straight into a PID, the D term amplifies high-frequency noise, so motors twitch and heat up. Flight controllers therefore stack several filters:
- A low-pass filter passes slow signals and removes fast ones. Separate low-pass filters are applied to the gyro and to the D term.
- A notch filter removes a narrow band, such as the motor rotation frequency. ArduPilot's harmonic notch moves its centre frequency with throttle, ESC RPM telemetry or in-flight FFT analysis.
The price of filtering is phase lag. A first-order low-pass with cutoff fc delays a signal at frequency f by arctan(f/fc). Lost phase eats into the phase margin, the distance from the point where the loop oscillates. Setting a very low cutoff to get a clean gyro trace can make the loop oscillate instead.
A tuning sequence
- Fix mechanical causes first: balanced propellers, a stiff frame, a properly isolated flight controller, and vibration levels in the log within the documented limits.
- Set up filters: find vibration peaks in the log spectrum and configure the notch before touching gains.
- Tune the rate loop: raise P and D in steps until response is crisp without jitter, then add I to remove slow drift. The PX4 guide suggests 20 to 30% steps, then 5 to 10% for fine adjustment.
- Tune the attitude loop: raise angle P until the aircraft reaches the target quickly with little overshoot.
- Tune velocity and position: test in Position or Loiter mode.
- Confirm each step in the log, and save the parameter file before and after.
Both systems offer automatic tuning (ArduPilot AutoTune and PX4 autotune), which twitches the aircraft in a pattern and computes gains. It works well once the frame and filters are sound, but it does not fix mechanical vibration.
Reading logs for tuning
The core technique is to compare desired and actual values for each loop. In ArduPilot the RATE message records desired and measured body rates and ATT records angles; in PX4 the Flight Review tool shows the same comparison. Typical signatures:
| What the log shows | Likely meaning |
|---|---|
| Actual oscillates around desired at a steady frequency | Gain too high or too much delay |
| Actual lags and never reaches desired | Gain too low, or too little I |
| Gyro trace has constant high-frequency spikes | Mechanical vibration or unsuitable filtering |
| Motor outputs often hit maximum or minimum | Saturation: not enough authority, or commands too aggressive |
| Problem appears only in Position mode | Velocity or position loop, not the rate loop |
Worked example: choosing a gyro filter cutoff
Example: a phase-margin budget for the rate loop
Assume a rate loop with an 8 Hz crossover and 60° of phase margin before any filtering, 5 ms of other delay, and motors spinning at 6,000 rpm in hover. All values are assumed.
- Vibration frequency: 6,000 rpm ÷ 60 = 100 Hz. Two-blade propellers also excite 2 × 100 = 200 Hz.
- 5 ms of delay costs 360 × 8 × 0.005 = 14.4° at crossover.
- Low-pass at 20 Hz: lag at 8 Hz is arctan(8/20) = 21.8°, leaving 60 − 21.8 − 14.4 = 23.8° of margin. Vibration at 100 Hz is reduced to 0.196 of its amplitude (−14.1 dB).
- Low-pass at 40 Hz: lag is arctan(8/40) = 11.3°, leaving 34.3°, but 100 Hz vibration is only reduced to 0.371 (−8.6 dB).
The 20 Hz filter removes more vibration but leaves the loop close to oscillation. A better approach is a notch at 100 Hz, which removes the vibration while adding little lag at 8 Hz, combined with a higher low-pass cutoff. These numbers are for practice; real values must be confirmed from logs.
Common pitfalls
- Tuning outer loops before inner ones, or changing rate gains to fix a problem that appears only in Position mode.
- Raising gains to mask mechanical vibration instead of fixing propellers or mounting.
- Lowering filter cutoffs too far and forgetting the added phase lag.
- Adding D without filtering until the motors run hot.
- Changing many values at once without saving the original parameter file.
- Using parameter names or units from a different firmware version.
- Running aggressive tuning flights near people or obstacles.
Continue in the lessons
- UAT 202 Module 5: Automation in drones: an overview of the nested loops in a flight controller.
- UAT 305 Module 2: Cascade control: the effect of outer-loop gain and angular acceleration limits in ArduPilot.
- UAT 314 Module 3: Feedback control: PID, Ziegler–Nichols and the PX4 loop structure.
Key references
- PX4 Autopilot. Controller diagrams. PX4 user guide (main). link
- PX4 Autopilot. Multicopter PID tuning guide (manual/advanced). PX4 user guide (main). link
- ArduPilot Dev Team. Tuning process instructions. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Managing gyro noise with the dynamic harmonic notch filters. ArduPilot Copter documentation. link
- PX4 Autopilot. Log analysis using Flight Review. PX4 guide (main). link
- Beard, R. W., & McLain, T. W. (2012). Small unmanned aircraft: Theory and practice. Princeton University Press. link