Skip to main content
emobility.academy
B.Tech / BELane keeping & lateralIntermediate About 5 weeksADAS & AUTOSAR

Lane Keeping Assist with a PID Steering Controller

Build a lane keeping assist (LKA) function in Simulink that steers a bicycle-model vehicle back to the lane centre with a PID controller on lateral offset and heading error. It is tested on straight and curved roads built in the Driving Scenario Designer.

Tools: Simulink · Automated Driving Toolbox

Lane Keeping Assist with a PID Steering Controller: Students studying an electric car with DIYguru trainers in the workshop
Representative photo from DIYguru's EV lab, not of this specific project.

The problem

Lane departure warning only alerts the driver; lane keeping assist applies corrective steering. A simple PID controller is the usual starting point, but it must cope with road curvature, speed changes and sensor noise without oscillating or saturating the steering, which makes it a good introduction to lateral control design.

Objectives

  • Model the lateral vehicle dynamics with a kinematic or dynamic bicycle model.
  • Build test roads and lane sensing in the Driving Scenario Designer.
  • Design and tune a PID controller on a combined lateral and heading error.
  • Add curvature feedforward and compare tracking error with and without it.
  • Evaluate maximum offset, settling time and steering effort across speeds.

What you need

Tools and toolboxes

  • MATLAB — Parameter scripts, tuning sweeps and plotting results.
  • Simulink — The closed-loop model with PID Controller, Saturation and Rate Limiter blocks.
  • Automated Driving Toolbox — Driving Scenario Designer for roads, lane markings and the ego trajectory; Scenario Reader and Vision Detection Generator blocks for simulated lane sensing; Bicycle Model block for vehicle dynamics.
  • Simulink Control Design (optional) — PID Tuner and linearisation to check stability margins.

Software

  • MATLAB and Simulink
  • Automated Driving Toolbox
  • Simulink Control Design (optional)

Extra parts and data

None beyond the software above.

Key concepts

Bicycle model
A planar model that lumps each axle into one wheel. Its kinematic form relates steering angle to yaw rate through the wheelbase; its dynamic form adds tyre cornering stiffness and lateral velocity.
Lateral offset and heading error
Offset is how far the car is from the lane centre; heading error is the angle between the car's direction and the lane direction. Feeding back both gives damping that offset alone cannot.
Curvature feedforward
On a curve the car needs a steady steering angle of roughly wheelbase times curvature. Adding it directly lets the feedback loop correct only small errors.
Integral windup
When the steering saturates, the integral term keeps growing and causes overshoot later. Clamping or back-calculation anti-windup prevents this.

Step-by-step guide

  1. 1

    Set requirements

    Define the operating range, for example 40 to 100 km/h on roads with curve radii above 250 m and clear lane markings. Set targets: maximum lateral offset below 0.3 m in steady curves, settling to within 0.1 m after a 0.5 m initial offset in under 3 s, steering angle within plus or minus 30 degrees at the road wheel and steering rate limited to a realistic value. Specify that the driver can override and that the function disengages when lane confidence is lost.

    Checkpoint: A requirements table with speed range, curvature range, accuracy targets and actuator limits.

  2. 2

    Build the test roads

    Open the Driving Scenario Designer and create three roads: a 1 km straight, an S-curve with 300 m radii and a road with a curvature step. Add lane markings with the lane specification tool. Place the ego vehicle with a trajectory slightly offset from the centre so the controller has something to correct. Export each scenario to a MATLAB function or a .mat file so the Simulink model can load it with a Scenario Reader block.

    Checkpoint: Three saved scenarios that play back correctly in the Designer with visible lane markings.

  3. 3

    Model the vehicle

    In Simulink, add the Bicycle Model block configured for a passenger car or small EV: mass, yaw inertia, wheelbase split, and front and rear cornering stiffness from a textbook or published data, with sources noted. Drive it with longitudinal speed held constant from a parameter. Feed the vehicle pose back to the Scenario Reader so the sensed lane is computed relative to the simulated car. Verify the model by applying a fixed steering angle and comparing the turn radius with the kinematic prediction at low speed.

    Checkpoint: Steady turning radius at low speed matches wheelbase divided by steering angle within a few per cent.

  4. 4

    Simulate lane sensing

    Add a Vision Detection Generator block configured to report lane boundaries, mounted at the windscreen. Convert its lane boundary outputs into lateral offset and heading error at a look-ahead point, using the boundary coefficients the block provides. Add a small Gaussian noise term and a 50 ms delay to represent a real camera. Plot the sensed offset against the true offset from the scenario so you know the sensing chain is correct before closing the loop.

    Checkpoint: Sensed offset tracks true offset with only the added noise and delay.

  5. 5

    Design the PID controller

    Form a combined error such as e = offset + L times heading error, where L is a look-ahead distance. Start with a P controller, increase gain until the response is fast but not oscillatory, then add D for damping and a small I term to remove steady offset on long curves. Add Saturation and Rate Limiter blocks on the steering command and enable clamping anti-windup in the PID Controller block. Optionally linearise the loop and check gain and phase margins.

    Checkpoint: A step in initial offset settles within the target time with less than 20 per cent overshoot at 60 km/h.

  6. 6

    Add curvature feedforward

    Compute the feedforward steering from the lane curvature reported by the sensor: wheelbase times curvature, plus an understeer correction if using the dynamic model. Add it to the PID output before the saturation. Run the S-curve scenario with and without feedforward and compare maximum offset. Check that noise in the curvature estimate does not cause steering jitter; if it does, low-pass filter the curvature signal.

    Checkpoint: Maximum offset on the S-curve is clearly lower with feedforward than without.

  7. 7

    Add engagement and override logic

    Use a simple Stateflow chart or logic blocks with states Off, Standby and Active. Engage only above a minimum speed with valid lanes on both sides; disengage when lane confidence drops for more than a set time, or when a simulated driver torque input exceeds a threshold. Ramp the controller output in and out over a short time so the steering does not jump. Test each transition with scripted inputs.

    Checkpoint: Scripted tests show each mode transition happening at the right time without steering jumps.

  8. 8

    Sweep speeds and scenarios

    Write a MATLAB script that runs every scenario at 40, 60, 80 and 100 km/h, with and without noise, using sim() in a loop. Record maximum and RMS offset, settling time, peak steering angle and peak steering rate for each run. Note where performance degrades, typically at high speed if gains were tuned at low speed, and consider gain scheduling with speed if needed.

    Checkpoint: A results table for every speed and scenario, with failures against requirements highlighted.

  9. 9

    Report

    Present the model, controller structure, tuning process and sweep results against each requirement. Include time plots of offset and steering for representative cases and explain any speed-dependent behaviour. Discuss limitations: constant speed, a linear tyre model, idealised sensing and no driver model. State that results are from simulation only and outline what a hardware-in-the-loop step would add. Add a short appendix with the final gains, block parameters and the sweep script so another student can reproduce every plot.

    Checkpoint: A report with requirement-by-requirement pass or fail and supporting plots.

Testing & validation

  • Maximum and RMS lateral offset on straight and curved roads.
  • Settling time and overshoot after an initial offset.
  • Peak steering angle and rate against actuator limits.
  • Correct engagement and disengagement in scripted mode tests.

Expected results

A Simulink LKA model with a tuned PID controller and curvature feedforward that keeps the simulated vehicle within the target offset across the speed range, with documented limits.

Take it further

  • Schedule gains with speed and compare against fixed gains.
  • Replace PID with a model predictive controller.
  • Add a driver model and study how the assist feels during override.
  • Generate C code for the controller with Embedded Coder.

Viva and review questions

Why include heading error and not just lateral offset?

Offset alone gives a poorly damped response because the controller reacts only after the car has moved. Heading error predicts where the car is going and adds damping.

What does curvature feedforward do?

It supplies the steady steering angle a curve needs, so the feedback loop only corrects small errors and offset on curves drops sharply.

Why limit steering rate?

Real steering actuators and drivers cannot accept sudden angle changes; a rate limit keeps the command realistic and avoids unsettling the vehicle.

Learn it at DIYguru

These DIYguru courses and programmes teach what this project needs.

Related EV projects

All 100 ADAS & AUTOSAR projects