Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: Build a Cruise Control Model in Simulink

Build a cruise control system in Simulink: vehicle dynamics, PI controller with anti-windup, a Stateflow mode manager, tests and code generation. Includes runnable Python stand-ins.

This project ties the course together: a vehicle model (plant), a PI speed controller, a Stateflow mode manager (Off, Standby, Active, Resume), tests with requirements, and code generation. You build the real model in Simulink by following the steps; since Simulink does not run on this site, each part has a Python stand-in so you can check the numbers and the logic first. The stand-ins are our own simulations, not Simulink output.

Requirements

  1. REQ-1: With cruise active the vehicle speed shall reach the set speed with an overshoot below 5 percent and a settling time below 20 s (on flat road).
  2. REQ-2: The throttle command shall stay between 0 and 100 percent.
  3. REQ-3: Pressing the brake shall deactivate cruise control within one sample (0.1 s).
  4. REQ-4: Cruise control shall be activated only above 30 km/h.
  5. REQ-5: The set speed may be changed in steps of 1 km/h with plus and minus buttons, limited to 30 to 150 km/h.

Architecture

flowchart LR
    B["Buttons: on, set, plus, minus, resume, brake"] --> M["Mode manager<br/>(Stateflow)"]
    V["Vehicle speed v"] --> M
    M -->|"setpoint, active"| C["PI controller<br/>with anti-windup"]
    V --> C
    C -->|"throttle 0..100%"| P["Vehicle model"]
    D["Road grade disturbance"] --> P
    P --> V

The mode manager decides when control is active and what the setpoint is; the PI controller does the speed regulation.

Step 1: the vehicle model

Newton's law for the longitudinal motion:

m dv/dt = F_engine - F_drag - F_roll - F_grade

with F_engine = k_t * throttle, F_drag = 0.5 * rho * Cd * A * v^2, F_roll = Crr * m * g and F_grade = m * g * sin(theta).

In Simulink: an Integrator (output v, initial condition 20), Sum blocks for the forces, a Math Function block (u^2) or Product for drag, a Saturation on the throttle, Gain blocks for parameters, and a Step or Signal Editor for the grade. Collect the parameters in a data dictionary or a params.m script:

MATLAB
m = 1500; rho = 1.2; Cd = 0.3; A = 2.2; Crr = 0.012; g = 9.81; kt = 12;
Python Stand-in: open-loop vehicle response to a throttle step

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 approaches a terminal value where engine force equals drag plus rolling resistance. Your Simulink model should show the same shape; use this as a sanity check.

Step 2: the PI controller with anti-windup

Use a PID Controller block in PI mode (see PID control design): discrete time with sample time 0.1 s, Limit output 0 to 100 and Anti-windup: clamping. Start with P = 8 and I = 1.2 (in percent of throttle per m/s), then tune using the PID Tuner on the linearized vehicle (linearize at v = 25 m/s; linearization lesson). The error is setpoint - v in m/s (convert km/h with a gain of 1/3.6).

Python Stand-in: PI cruise 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.

Read the numbers for requirement REQ-1 (overshoot relative to 25 m/s) and for how the hill is handled. If the throttle saturates on the hill, the integrator without anti-windup keeps growing and the speed overshoots after the hill.

Step 3: the Stateflow mode manager

Create a Chart block with inputs onBtn, setBtn, plus, minus, resumeBtn, brake (boolean) and speed_kmh (double), and outputs active (boolean) and setpoint_kmh (double). States:

  • Off: active = false.
  • Standby: cruise is on but not engaged (active = false).
  • Active: active = true; setpoint_kmh follows the plus and minus buttons in steps of 1 within 30 to 150.
  • Suspended: after the brake; remembers setpoint_kmh for the resume button.

Transitions: Off -> Standby [onBtn]; Standby -> Active [setBtn && speed_kmh >= 30] {setpoint_kmh = speed_kmh}; Active -> Suspended [brake]; Suspended -> Active [resumeBtn && speed_kmh >= 30 && ~brake]; any state to Off [~onBtn]. Set the chart's Update method to Discrete with sample time 0.1 and use explicit execution order (see transitions). Use the event edge detection on buttons (a held button should step the setpoint once): add a local prevPlus that you update at the end of each step, or use hasChangedTo(plus, 1) in the transition condition.

Python Stand-in: mode manager with edge detection and limits

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.

Step 4: combine and simulate

Wire the chart outputs to the controller: setpoint in km/h divided by 3.6 into the Sum, and active into an Enable or into a Switch that selects the PI throttle when active and the driver's throttle when not. Reset the integrator when cruise is not active (set External reset on the PID block to the inverse of active) so that it starts clean at engagement. Add Scopes for speed, setpoint, throttle and mode. Run these scenarios: engage at 80 km/h, apply a hill (grade step from 0 to 6 percent), press plus five times, brake, resume.

Step 5: test it

In a test harness (Simulink Test lesson) for the controller and mode manager:

  • Test Sequence: engage at 80, hold 10 s, grade 6 percent for 10 s, then brake. Verify abs(v - setpoint) < 1 after settling, throttle within 0 to 100, active == false within one sample of the brake.
  • Boundary cases: engage at 29 km/h (must not engage), setpoint 150 plus button (stays 150), simultaneous plus and minus.
  • Link tests to REQ-1 to REQ-5; collect model coverage and aim for 100 percent decision and condition coverage of the chart; use Design Verifier to generate tests for missing objectives and to prove REQ-2 and REQ-3.

Step 6: generate code

Set a fixed-step solver (0.1 s), ert.tlc, and data types: single for speeds and throttle (no double on the target), boolean for buttons. Build the controller subsystem as a model reference. Generate code (see the code generation workflow), run a SIL back-to-back test and read the report. The call from the ECU is:

C
cruise_U.speed_kmh = read_speed();
cruise_U.brake = read_brake();
cruise_step();            /* every 100 ms */
set_throttle(cruise_Y.throttle);

Extensions

  1. Add a radar distance input and make it an adaptive cruise control with a time-gap controller.
  2. Replace the constant kt by an engine map lookup table with throttle and speed.
  3. Model a gearbox with a Stateflow shift scheduler.
  4. Add a fault mode: lost speed signal makes cruise drop out with a warning.
  5. Add requirement links and a Model Advisor run for the MAAB checks.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why reset the PI integrator when cruise control becomes inactive?

    A stale integrator value would cause a throttle jump at the next engagement.
  2. 2. How should the plus button step the setpoint by one per press?

    Edge detection (such as hasChangedTo) acts once per press instead of every step.
  3. 3. Which block limits the throttle command to 0..100 percent?

    The limit enforces REQ-2, and the PID block's anti-windup uses it.
  4. 4. What does the generated cruise_step() function run at?

    The generated code does not start timers; your scheduler calls it at the base sample time.

Summary

  • Start from requirements and an architecture: mode manager (Stateflow), PI controller, vehicle plant.
  • Model the vehicle with force balance, tune the PI with anti-windup and a saturation limit, reset the integrator on engagement.
  • Use edge detection and limits in the chart, and discrete sample times for code generation.
  • Test with a harness, requirement links, coverage and Design Verifier; then generate code and verify with SIL.

Next: Project: a traffic light controller in Stateflow