Guidance, Navigation and Control · Theory · M3
M3-T5 — Loop Shaping: Lead, Lag and PID as Compensators
A controller is a filter chosen to give the loop the gain and the phase it needs at the frequencies that matter. Lead adds phase, lag adds low-frequency gain, and PID is the two in disguise.
Hopper's pitch plant has a phase of −180° at every frequency, before the servo has added its lag. A proportional controller therefore leaves a loop with no phase margin and no hope. The controller must supply phase, and it must do so around the frequency where the loop gain crosses 1. This lesson designs it as a filter: a lead compensator that adds a chosen phase at a chosen frequency, derived from two formulas. It then shows that the proportional-integral-derivative controller used on almost every vehicle is the same idea with fewer parameters.
Learning objectives
After this lesson, you should be able to:
- Choose a crossover frequency and a phase margin and compute the phase a compensator must add.
- Derive the maximum phase and the centre frequency of a lead compensator, and design one for a given plant.
- Say what lag, lead and PID terms do to the loop's gain and phase, and what each costs.
- Verify a design by computing the loop's margins and the sensitivity peak.
The intuition
The PID controller is already familiar from the UAV track: UAV M5-T6: PID controllers explains what the three terms do and how to tune them by watching a response, and the PID labs of this site practise it. This lesson answers a different question: what is the controller for, in terms of the loop? The answer is that a controller shapes the open loop so that it has the gain and phase the closed loop needs.
The two requirements, and the one that is hard
A loop meets its goals if two conditions hold.
- Gain. is large at low frequencies (for rejection and tracking) and small at high frequencies (for noise), crossing 1 at the chosen bandwidth .
- Phase. At the crossover the phase of is at least , for a margin PM.
The gain can usually be set with one number, the gain of the controller. The phase cannot, because the plant and the actuator decide it. Hopper's pitch plant has a phase of −180° and the servo adds to it: −185.7° at 4 rad/s. A loop with a phase margin needs +50° or more of phase lead from the controller there.
A filter that adds phase
A lead compensator has one zero and one pole, with the zero at a lower frequency than the pole: with . Between the two corner frequencies it rises in gain, like a derivative, and in that band it adds positive phase. Outside it, the effect fades. The figure shows the phase it adds: a bump centred halfway between and on the logarithmic axis.
A pure derivative term adds 90° at every frequency, but its gain rises without limit, amplifying all high-frequency noise. A lead compensator is a derivative that stops: the pole at flattens the gain above that frequency, so the amplification of high-frequency noise is limited to , about 10 here. That is why real controllers use a filtered derivative and not a pure one.
| Term | Transfer function | What it does to the loop | Cost |
|---|---|---|---|
| Proportional | Sets the gain, hence the crossover frequency | None, but sets no phase | |
| Derivative / lead | Adds phase lead near the crossover (up to 90° for the pure derivative) | Raises high-frequency gain: sensor noise | |
| Integral / lag | (integral), or a lag with | Raises low-frequency gain: better tracking and rejection of slow disturbances | Adds phase lag below its corner, and (for the integral) windup |
| PID | Lag at low frequency, lead at high frequency: both of the above | Three numbers to choose, and the sum of the costs |
A controller is a filter that gives the loop the gain and the phase it needs where it needs them. Lead puts phase where the loop crosses 1, lag puts gain where the loop is slow, and PID is both with three knobs.
Definitions
- Lead compensator
- with zero and pole (rad/s) and gain .
- Lead ratio
- The ratio of zero to pole, . The smaller it is, the more phase the lead can add and the more it amplifies high frequencies by .
- Maximum phase and centre frequency
- The most phase the lead adds, and the frequency at which it does.
- Crossover and margin PM
- The chosen bandwidth and phase margin of the loop (previous lessons).
The phase of the lead is the angle of the zero factor minus the angle of the pole factor. It peaks at the geometric mean of the corners:
Inverted, the formulas turn a required phase into the lead and a required crossover into its position:
The required phase comes from the plant: the lead has to supply what lies between the plant's phase and the margin:
Derivation: the maximum phase of a lead and the design formulas
The design formulas come from finding where the phase of the lead peaks.
The lead is a zero factor over a pole factor. The angle of is , and dividing by the pole factor subtracts its angle.
Set the derivative of the phase to zero and clear the denominators. The peak is at the geometric mean of the zero and the pole, the midpoint on a logarithmic axis, as the figure shows.
Use at , where the denominator is 2.
From and , the hypotenuse of the right triangle with those sides is , hence the sine.
Solve for the ratio of the corners.
At the centre frequency and . The ratio of the two square roots simplifies to . Setting the loop gain to 1 there gives .
Choose the crossover and the margin, and the plant's phase tells you how much lead is needed. The lead ratio follows from one formula, and the price is a high-frequency gain 1/α larger than at DC: the more phase, the more noise.
A worked example
Design a lead for Hopper's pitch loop with a crossover of 4 rad/s (the pitch-angle bandwidth of M1-T2: Nested Loops and Separation of Timescales) and a phase margin of 50°. The plant, with the servo, is .
- Plant phase at 4 rad/s. The pitch plant is real and negative, −180°, and the servo adds : a total of −185.7°.
- Phase to add. For a margin of 50° the loop must have a phase of −130° at the crossover, so the lead must add .
- The lead. , so rad/s and rad/s. The plant gain at 4 rad/s is 1.184, so .
- Check the lead. Its phase at 4 rad/s is 55.7°, its maximum, at rad/s.
- Check the loop. The crossover is at 4.00 rad/s with a phase margin of exactly 50.0°, as designed. The peak of the sensitivity is 1.42, and the loop is stable for gains from (−20.2 dB) to (+20.8 dB): both margins are over 20 dB. The closed-loop poles are at −1.74, −41.7 and −4.74 ± 3.23j.
- Compare with the PD design. The PD controller of the last two lessons used and and crossed at 6.07 rad/s with 55.5°. It has a pure derivative, with noise gain rising without limit. The lead holds the high-frequency gain at , ten times its low-frequency gain, and no more.
import cmath, math
import gnckit as g
b, lam, wm, target = 19.62, 0.7, 4.0, 50.0 # crossover wanted (rad/s) and phase margin wanted (deg)
plant_phase = -180 - math.degrees(math.atan(wm / 40)) # (jw)^2 - lam^2 is real and negative; the servo lags
add = target - (180 + plant_phase) # phase the lead must supply at wm
alpha = (1 - math.sin(math.radians(add))) / (1 + math.sin(math.radians(add)))
z, p = wm * math.sqrt(alpha), wm / math.sqrt(alpha)
plant_gain = b / ((wm ** 2 + lam ** 2) * math.sqrt(1 + (wm / 40) ** 2))
K = math.sqrt(alpha) / plant_gain
print(f'plant phase {plant_phase:.1f} deg at {wm:g} rad/s, so the lead must add {add:.1f} deg')
print(f'alpha = {alpha:.4f}, zero z = {z:.3f} rad/s, pole p = {p:.3f} rad/s, gain K = {K:.4f}')
lead = g.TF([K / z, K], [1 / p, 1])
print(f'lead phase at {wm:g} rad/s: {math.degrees(cmath.phase(lead.freq(wm))):.1f} deg (maximum, at sqrt(z p) = {math.sqrt(z * p):.2f})')
L = g.TF(g.poly_mul([b], lead.num), g.poly_mul(g.poly_mul([1, 0, -lam ** 2], [1 / 40, 1]), lead.den))
ws = g.logspace(-2, 3, 20000)
wgc = [ws[i] for i in range(len(ws) - 1) if (abs(L.freq(ws[i])) - 1) * (abs(L.freq(ws[i + 1])) - 1) < 0][0]
pm = 180 + math.degrees(cmath.phase(L.freq(wgc)))
ms = g.peak_gain(lambda w: g.sensitivity(L, w), ws)[0]
stable = [k for k in [10 ** (i / 100) for i in range(-300, 300)] if all(complex(r).real < 0 for r in g.poly_roots(g.poly_add(L.den, [k * c for c in L.num])))]
print(f'loop with the lead: crossover {wgc:.2f} rad/s, phase margin {pm:.1f} deg, peak |S| {ms:.2f}')
print(f'stable for gains from x{min(stable):.3f} ({20 * math.log10(min(stable)):.1f} dB) to x{max(stable):.1f} ({20 * math.log10(max(stable)):.1f} dB)')
poles = g.poly_roots(g.poly_add(L.den, L.num))
print('closed-loop poles', sorted((round(complex(r).real, 2), round(complex(r).imag, 2)) for r in poles))plant phase -185.7 deg at 4 rad/s, so the lead must add 55.7 deg alpha = 0.0952, zero z = 1.234 rad/s, pole p = 12.966 rad/s, gain K = 0.2606 lead phase at 4 rad/s: 55.7 deg (maximum, at sqrt(z p) = 4.00) loop with the lead: crossover 4.00 rad/s, phase margin 50.0 deg, peak |S| 1.42 stable for gains from x0.098 (-20.2 dB) to x11.0 (20.8 dB) closed-loop poles [(-41.74, 0.0), (-4.74, -3.23), (-4.74, 3.23), (-1.74, 0.0)]
In robotics
Every flight controller is a shaped loop, and the common structures are special cases of the lead and the lag.
- PID with a filtered derivative. The derivative term of PX4 and ArduPilot is low-pass filtered, which is exactly a lead with a pole: the filter's cutoff is . Choosing the cutoff of that filter is the choice of in this lesson.
- Integral action as a lag. The integral term raises the gain at low frequency and adds phase lag below its corner. A good design puts the integral corner a decade or so below the crossover, so the lag at the crossover stays small (about 6° for a corner a decade below). Its price, the windup of a saturating actuator, is the subject of lesson 7.
- Aircraft autopilots. The pitch-rate and attitude loops of a fixed-wing aircraft use lead filters and a lag-like integrator for altitude hold. Module 10 builds them by successive loop closure.
- Notch filters. A notch is a lead and a lag combined, with a deep gain dip at one frequency: it removes a structural mode from the loop. Lesson 7 shows the cost, again a phase lag below the notch.
- Tuning by response and by loop shape. The tuning labs of this site, Tune a PID Controller and Fix the Oscillating Robot, adjust gains by watching a response. This lesson says what that response is telling you about the loop: the overshoot is the phase margin, the speed is the crossover, the noise is the high-frequency gain.
The limits are the topic of the next lesson. A lead can add at most 90° and never that much in practice. The plant and the delay together may demand more phase than any realizable compensator supplies, and then the only options are a lower crossover or a better actuator.
Common misconceptions
“A derivative term is the way to add phase lead.”
It adds 90° at every frequency, but its gain rises 20 dB per decade forever, so it amplifies every high-frequency noise source. A lead adds phase only in a band and stops, so the noise gain is bounded. In practice a derivative is always a lead.
“A lead compensator can add as much phase as I need.”
It adds , which for ratios of 10, 100 and 1000 is 55°, 79° and 87°, and never 90°. The high-frequency gain rises by the same ratio, so large leads are paid for in noise. Need more than about 65° from one lead and the design is asking too much.
“The lead's phase is the loop's phase margin.”
The margin is what is left after the plant's lag has been subtracted: . For Hopper the lead adds 55.7° and the plant and servo take away 185.7° from 180°, leaving 50°.
“Raising the gain only moves the crossover.”
It also changes the phase at the crossover, because the plant's phase depends on frequency. Hopper's servo lags more at a higher crossover, so a higher gain lowers the margin. Gain and phase cannot be designed separately.
“PID and lead-lag are different families.”
A PID with a filtered derivative is a lead-lag compensator with a particular pole and zero placement. Knowing both views lets you tune by response and check by loop shape.
Key takeaways
- A controller shapes the loop: gain and phase where they are needed. The plant and actuator decide the phase, so the controller must supply the missing lead at the crossover.
- A lead adds a phase of up to at .
- To design one: required phase , then , , .
- Hopper's pitch loop needs 55.7° at 4 rad/s: zero 1.23 rad/s, pole 12.97 rad/s, gain 0.2606, giving a margin of 50.0° and gain margins of −20 dB and +21 dB.
- Lead costs high-frequency gain (), lag costs phase below its corner, and PID is lag plus lead.
- A pure derivative is a lead that never stops; in practice it is always filtered.
References and further reading
Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.
- K. J. Åström and R. M. Murray, Feedback Systems, 2nd edition, Princeton University Press, 2021 , the chapters on PID control and on loop shaping. Free to read as the authors' PDF; the book is copyrighted.
- B. Douglas, “Lead/Lag Compensators” (video series) . Short lectures on exactly this design. Free to watch.
- J. How and E. Frazzoli, MIT 16.30 Feedback Control Systems (OpenCourseWare) , the lectures on lead and lag compensation. Free; licence CC BY-NC-SA 4.0.
- G. F. Franklin, J. D. Powell and A. Emami-Naeini, Feedback Control of Dynamic Systems, 8th edition, Pearson, 2019, the chapter on frequency-response design. A standard text. Paid book.
Knowledge check
Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A lead compensator has its zero at 2 rad/s and its pole at 20 rad/s. What is its maximum phase, at what frequency, and by what factor does it amplify high frequencies relative to DC?
Explanation
The ratio is p/z = 10, so sin φm = (20 − 2) / (20 + 2) = 0.818 and φm = 54.9°. It occurs at √(z p) = √40 = 6.32 rad/s. At high frequency the gain is K × (p/z) = 10 K against K at DC, so noise above 20 rad/s is amplified ten times more than the DC gain, which is the price of the 55° of phase.
Question 2
A plant has a phase of −170° at the chosen crossover of 5 rad/s. Design the lead for a 45° phase margin: what phase must it add, and what are z and p?
Explanation
The loop must have a phase of −135° at the crossover, so the lead must add 45° − (180° − 170°) = 35°. Then sin 35° = 0.574, α = (1 − 0.574)/(1 + 0.574) = 0.2706, √α = 0.520, so z = 5 × 0.520 = 2.60 rad/s and p = 5 / 0.520 = 9.61 rad/s. The gain K is then chosen so that |L(j5)| = 1, as in the example.
Question 3
Why can an engineer not just add a pure derivative term of any size to get arbitrary phase lead on a vehicle with a noisy gyroscope?
Explanation
The derivative gives 90° of lead at all frequencies, but its gain rises 20 dB per decade without limit, so it amplifies gyroscope noise at high frequency more and more, drives the actuator with noise, and in practice saturates it. Real controllers filter the derivative, which makes it a lead: phase is added in a band around the crossover and the high-frequency gain stops rising at K/α. The filter's pole is the design variable that sets the trade between lead and noise.
What’s next
Related content
Nothing below is required to finish this lesson.
- BeforeM3-T4: The Nyquist Criterion and Stability Margins
Decide closed-loop stability from the open-loop frequency response, and compute gain, phase and delay margins.
- LabM3-L1: Rescue the Phase Margin
Hopper's pitch loop rings after a heavier engine was fitted. Measure the margins from the open-loop response and add a lead compensator that restores 45 degrees and 6 dB.
Other courses on this site treat neighbouring topics from their own angle:
- Lab: Tune a PID Controller
- Lab: Fix the Oscillating Robot
- UAV M5-T6: PID controllers
- UAV M7-T6: End-to-end latency
This course leaves the following to the pages that already teach them:
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.