Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 · Projects and Practice

Project: Cruise Control

Build a car cruise controller step by step: model the vehicle, tune a PI controller from pole placement, reject a hill disturbance, and add anti-windup for saturated engine force.

Cruise control is the textbook control problem that everyone has used. You set a speed; the car keeps it on flat roads, uphill and downhill. In this project you build the whole thing in simulation: a physical model, a PI controller designed from the closed-loop poles, a disturbance (a hill), and the real-world complication of a limited engine force. Along the way you use ideas from the PID chapter and the practical PID lesson.

flowchart LR
    R["set speed r"] --> E(("+/-"))
    E -->|"e"| C["PI controller"]
    C -->|"force F (limited)"| V["Car: m v' = F - b v - m g sin(theta)"]
    H["hill slope theta"] --> V
    V -->|"speed v"| E

The cruise-control loop with a hill as an external disturbance.

Step 1: the model

Newton's second law along the road gives

TEXT
m·v' = F - b·v - m·g·sin(θ)

where m is the mass, F the engine force (our control input), b·v linear drag (a simplification of the real quadratic drag) and m·g·sin θ the gravity component on a slope θ. With m = 1200 kg and b = 50 N·s/m, the plant V(s)/F(s) = 1/(m s + b) is a first-order lag with a long time constant m/b = 24 s: the car coasts for a long time. The engine can deliver at most ±3000 N (braking included, to keep the example simple).

At a steady 25 m/s (90 km/h) on flat ground the engine must supply b·v = 1250 N to cancel drag. On a 5° hill it needs 1250 + m g sin 5° ≈ 2276 N, which is within the limit, so the car can hold speed on that hill.

Step 2: a PI controller from the poles

A P controller alone would leave a steady-state error (it needs a nonzero error to produce the drag force). So use PI: F = Kp·e + Ki·∫e. Closing the loop around 1/(m s + b) gives the characteristic equation

TEXT
m·s² + (b + Kp)·s + Ki = 0

Compare with the target s² + 2ζωn s + ωn²: multiply through by m to get Kp = 2ζωn·m - b and Ki = m·ωn². Choose critical damping ζ = 1 and ωn = 0.5 rad/s (a time constant of about 2 s per pole), which gives Kp = 1150 and Ki = 300.

It is tempting to choose the PI zero to cancel the plant pole (Ki/Kp = b/m). Do not. The cancelled slow mode (time constant 24 s) still reacts to disturbances: the car would take over a minute to recover from a hill. Place the closed-loop poles directly instead.

Step 3: simulate the hill

The simulation uses small Euler steps. The hill begins at t = 40 s. The controller includes anti-windup (back-calculation) because the force limit matters during the start-up.

Python PI cruise control on a flat road, then a 5 degree hill

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The speed reaches 95% of the target in about 13.5 s (the engine saturates during the first seconds, so this is slower than the linear design predicts). On the hill the car loses about 0.6 m/s and recovers to within 0.25 m/s in 6 s. The final engine force settles at 2276 N, exactly b·v + m g sin θ, which is what integral action should find without being told the slope.

Step 4: why anti-windup matters

Start from a standstill with a set speed of 25 m/s. The error is huge, so the controller demands far more force than the engine can give. Without anti-windup the integrator keeps accumulating while the force is limited, and the car overshoots the target badly.

Python The same PI controller with and without anti-windup

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without anti-windup the speed peaks at about 38.6 m/s (nearly 14 m/s above the set speed!) and takes 35 s to settle. With back-calculation the peak is the set speed itself and the settling time is 15.8 s. The cost of anti-windup is one line of code; the benefit is a car that does not run away after a standing start.

Step 5: extend the project

Try these extensions in the editors above:

  1. Quadratic drag. Replace b·v by 0.5·ρ·Cd·A·v² (for a car, about 0.4·v²). The loop gain now depends on speed; check the response at 10 and 35 m/s.
  2. Measurement noise and a speed filter. Add noise to v and see how much it moves the force; add a low-pass filter on the measured speed.
  3. Gear shifting. Make the maximum force a function of speed, as an engine has a torque curve.
  4. Comfort limit. Limit the acceleration to 2 m/s² by rate-limiting the set speed. This shortens the saturation time at the start and reduces overshoot (but keep anti-windup too).
  5. Downhill. Use a negative slope; the car must brake or coast. What happens when the engine can only push, not brake?

What you learned

  • A plant with a very slow pole should not be cancelled; use pole placement on the closed-loop polynomial.
  • Integral action finds the unknown disturbance force; the final controller output equals what the disturbance requires.
  • Actuator limits change the start-up behavior and make anti-windup essential.
  • Simple simulations expose these effects long before a test drive does.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. For the car m v' = F - b v - m g sin(theta) with a PI controller, what is the characteristic equation of the closed loop?

    Closing the loop around 1/(m s + b) with C(s) = Kp + Ki/s gives m s^2 + (b + Kp) s + Ki = 0.
  2. 2. Why does the final engine force on a hill equal b v + m g sin(theta)?

    In steady state v' = 0, so F must equal drag plus the gravity component; the integrator finds that value.
  3. 3. What goes wrong if you cancel the slow plant pole with the PI zero?

    Cancellation hides the slow mode from the set-point response but not from disturbances that enter at the plant input.
  4. 4. Which problem does anti-windup solve in this project?

    With the force limited, the integral keeps growing and causes a large overshoot after a standing start.

Summary

  • Model: m v' = F - b v - m g sin θ; a slow first-order plant with a force limit.
  • Design a PI controller by matching m s² + (b + Kp) s + Ki to the desired poles; do not cancel the plant pole.
  • Integral action rejects the hill; the integrator ends up holding the required force.
  • Anti-windup avoids a large overshoot after saturation.

Next: Project: Inverted Pendulum with LQR