The problem
Closed-loop testing of an ADAS function needs a vehicle, sensors and traffic that react to the function's commands. A 3D simulator such as CARLA provides all of these safely and repeatably, so a student can test a braking function in situations that would be dangerous to stage on a real vehicle.
Objectives
- Set up CARLA in synchronous mode with a fixed time step.
- Spawn an ego vehicle with radar, camera and collision sensors.
- Implement AEB logic from radar detections using time-to-collision.
- Script repeatable lead-vehicle and pedestrian scenarios.
- Measure stopping distance, minimum TTC and collision rate.
What you need
Tools and toolboxes
- CARLA — 3D driving simulator providing the world, vehicles, pedestrians and sensors.
- Python — CARLA client scripts for scenarios, the AEB function and logging.
- NumPy and pandas — Processing radar detections and analysing logged results.
- OpenCV — Saving and annotating camera frames for the report.
Software
- CARLA simulator and its Python API
- Python 3 with NumPy, pandas, matplotlib and OpenCV
Extra parts and data
- PC with a dedicated GPU — CARLA renders camera views with Unreal Engine and runs too slowly for useful testing on integrated graphics.
Key concepts
- Synchronous mode
- The client advances the simulation one fixed step at a time with world.tick(), so sensor data and control commands line up and runs are repeatable.
- Radar detection
- CARLA's radar sensor returns points with depth, azimuth, altitude and relative velocity, from which range and closing speed to an object can be computed.
- Staged AEB
- Production systems usually warn first, then apply partial braking, then full braking as TTC falls. Staging reduces harsh braking while still avoiding collisions.
- Collision sensor
- A CARLA sensor that fires an event when the vehicle it is attached to hits something, used here as the ground truth for a failed test.
Step-by-step guide
- 1
Specify the AEB function
Write the requirements: the function acts on objects in the ego lane ahead; it warns at TTC below 2.6 s, brakes at 40 per cent at TTC below 1.6 s and fully at TTC below 0.9 s; it must avoid collision with a stationary car from 30 km/h and reduce impact speed at higher speeds. Define the ODD as a dry, straight or gently curved town road in daylight. These thresholds are starting values for tuning, not standard figures.
Checkpoint: A requirements table with TTC thresholds, brake levels and target outcomes per test speed.
- 2
Set up CARLA in synchronous mode
Start the CARLA server, connect with carla.Client and load a town map with long straight roads. Apply world settings with synchronous_mode set to true and fixed_delta_seconds of 0.05 s, and set the Traffic Manager to synchronous mode as well. Write a context manager that restores asynchronous settings and destroys all spawned actors on exit, so a crashed script does not leave the server in a bad state.
Checkpoint: A script that ticks the world 200 times at a fixed step and exits cleanly with no leftover actors.
- 3
Spawn the ego vehicle and sensors
Spawn the ego car at a chosen spawn point and attach a radar (sensor.other.radar) on the front bumper with a horizontal field of view around 30 degrees and a range of about 100 m. Attach an RGB camera for visual checks and a collision sensor. Put sensor callbacks into queues and read them after each tick so data stays in step. Plot radar points for a parked car to check the mounting and the axes.
Checkpoint: Radar points appear at the correct range for a parked car placed at a known distance.
- 4
Script repeatable scenarios
Write scenario functions that place a lead vehicle at a set distance and give it scripted behaviour: stationary, moving slower, or braking hard after a delay, by setting its control or target velocity each tick. Add a pedestrian crossing scenario using a walker with a scripted direction and speed. Drive the ego car at a constant target speed with a simple longitudinal controller until AEB acts. Fix random seeds and spawn points so each run is identical.
Checkpoint: Running the same scenario twice gives the same ego and lead trajectories to within a few centimetres.
- 5
Process radar into a lead-object estimate
Convert each radar point to x and y in the vehicle frame from depth, azimuth and altitude. Keep points whose lateral offset lies within half a lane width and whose height is above the road surface. Cluster the remaining points by range, take the nearest cluster as the lead object, and average its range and relative velocity. Smooth range rate with a short moving average or an alpha-beta filter to reduce noise.
Checkpoint: Estimated range and closing speed follow the ground-truth values from actor locations, with error reported.
- 6
Implement the AEB state machine
Compute TTC as range divided by closing speed when closing speed is positive. Implement states: inactive, warning, partial brake and full brake, with hysteresis so the state does not flicker near thresholds. In brake states, override the driver controller by sending carla.VehicleControl with throttle zero and the brake value. Log state, TTC, commanded brake and ego speed every tick. Release brakes only when the ego car has stopped or the threat has cleared.
Checkpoint: In the stationary-car scenario at 30 km/h the ego car stops before the target with logged state changes.
- 7
Run the test matrix
Run every scenario at ego speeds of 20, 30, 40, 50 and 60 km/h with at least three repeats. For each run record collision or not, impact speed if any, minimum TTC, minimum gap, peak deceleration and the time from first warning to stop. Save everything to a CSV file. Include no-threat runs, such as a car in the adjacent lane, to count false activations. Keep the scenario list and parameters in one configuration file.
Checkpoint: A CSV with one row per run covering all scenarios, speeds and repeats.
- 8
Analyse and tune
Plot collision rate and impact speed against ego speed for each scenario, and plot minimum gap for the avoided cases. Identify failures and explain them: late detection on curves, pedestrians detected too late because of the narrow field of view, or slow brake build-up. Change one threshold at a time and rerun the matrix to show the trade-off between avoidance and false or early activations. Record every tuning change with its effect.
Checkpoint: Before-and-after tables for at least two tuning changes with an explanation of each.
- 9
Report
Document the setup, the radar processing, the AEB logic and the test matrix with results. Include camera frames from interesting runs annotated with TTC and state. State the limits plainly: a simulated radar, simple vehicle dynamics, no wet roads and scripted rather than reactive traffic. Explain that real AEB testing happens on test tracks with soft targets and supervision, never on public roads. Provide all scripts so the matrix can be rerun.
Checkpoint: A report whose figures can be regenerated from the released scripts and CSV.
Testing & validation
- Radar range and closing speed compared with ground truth.
- Collision rate and impact speed per scenario and speed.
- Minimum TTC and gap for avoided cases.
- False activation count in no-threat runs.
- Repeatability of runs with fixed seeds.
Expected results
A working radar-based AEB in CARLA with a test matrix showing at which speeds and in which scenarios it avoids or mitigates collisions, and where it fails.
Take it further
- Fuse camera detections with radar to improve pedestrian performance.
- Add wet-road friction and compare stopping margins.
- Run the same function in a ROS 2 node through the CARLA ROS bridge.
- Compare a TTC-based trigger with a required-deceleration trigger.
Viva and review questions
Why use synchronous mode?
Without it the server runs at its own pace, so sensor data and control commands drift relative to each other and runs are not repeatable. Synchronous mode advances the world one fixed step per tick.
Why does AEB use staged braking?
A warning or partial braking may be enough and is less harsh, while full braking is reserved for when a collision is otherwise unavoidable. Staging also limits the impact of a false activation.
How do you separate an in-lane object from one in the next lane?
By converting radar points to lateral offset in the vehicle frame and keeping only those within roughly half a lane width of the ego path.
What is the main weakness of a pure TTC trigger?
TTC assumes constant speeds, so it reacts late to a lead vehicle that starts braking hard. Including relative acceleration or required deceleration helps.
Learn it at DIYguru
These DIYguru courses and programmes teach what this project needs.
