Self-Driving Cars
Topic 22 · Part 5: Making it safe · ⏱ about 6 h

Capstone: Your Self-Driving Car Against 20 Scenarios

Put the whole course together. Write the decision-making and control of a complete self-driving car in Python, driving on perception outputs like the ones your networks produce, and test it against 20 scenarios: three tracks, four kinds of weather, traffic, traffic lights, pedestrians, a broken-down car, a car pulling out and a runner in the road. Pass all 20.

Softmaxima Simulator StudioTry this topic's code in the Studio: it opens with the traffic world project, ready to run.Open
Show:
In this topic
Beginner

In plain words

Time to put it all together. You write the part of the car that decides: given what the camera found (the lane, the car ahead, the traffic light, any pedestrians), how much to steer and how hard to press the pedals. Then your car is tested in 20 situations, from an empty road to a child running out at night.

By the end of this topic you will be able to build a complete self-driving car and prove it passes 20 test scenarios.

Start with: press Run all 20 scenarios with the starter code, then fix one failure at a time.

Beginner

The brief

You have built every part of a self-driving car in this course. In the capstone you connect them and prove the result works. Your job is the decision-making and control: a Python function drive(car) that is called 15 times a second and returns a steering angle and a throttle. It must complete one lap of each scenario:

  • without hitting a car or a pedestrian,
  • without driving through a red light,
  • without leaving the road,
  • within the time limit.

Every pass scores at least 70 points, plus up to 15 for speed and 15 for staying in your lane. Every failure scores up to 30 for how far you got. The maximum is 2000.

Beginner

What your car knows

Your function receives what the perception and localisation you built would report, with realistic errors that grow at night and in rain:

FieldWhat it isWhich part of the course makes it
car.lane30 points on your lane's centre line, every 1 m ahead, in the car's frame (x forward, y left), with noisethe U-Net's own-lane pixels, projected onto the road
car.curvaturesthe road's curvature every 1 m ahead (1/radius, + = left)the map, and localisation on it
car.leadNone, or {"distance", "speed"} of the nearest car ahead in your lane (bumper to bumper, metres; m/s)the detector, the tracker and the depth estimate
car.lightNone, or {"distance", "state"} for the next traffic light: metres to the stop line; "red", "amber", "green" or sometimes "unknown"the detector and the traffic-light classifier
car.pedestrianNone, or {"distance", "offset", "moving"}: metres ahead, metres to your left (negative: to your right), and walking speed (+ = towards your left). Sometimes missed for a frame.the detector and the tracker
car.speed, car.cte, car.heading_error, car.dt, car.tspeed (m/s), offset from the lane centre (m, + = left), heading error (rad), time step (s), time (s)wheel odometry and the Kalman filter

Return steer, throttle: steering in radians (+ = left, at most 0.55), and throttle from −1 (full brake, 7 m/s²) to 1 (full power, 3.5 m/s²).

Beginner

The 20 scenarios

  • 1–6: empty roads on all three tracks, in day, dusk, night and rain. Lights and pedestrian crossings are always there.
  • 7–11: traffic: cars ahead in your lane stop at lights and crossings, and oncoming cars pass close by.
  • 12–13: a broken-down car stopped in your lane. It starts again after a while.
  • 14–15: a car pulls out 16 m ahead of you and drives off slowly.
  • 16–18: someone runs into the road, at day, dusk and night.
  • 19–20: several of these at once.

The starter code drives, follows cars and stops at red lights. It passes about 14 of the 20. Run it first, then use Watch to see each failure.

Try it

Your car: write, run, watch

Edit the code, then press Run all 20 scenarios, which takes about a minute. To debug one case, choose it under Watch and press Run this one: the replay follows your car from above, with pedestrians as pink dots (the runner in red) and the special cars in amber. Set Perception to perfect to check your logic alone, then to poor to check that it is robust.

To watch one scenario with the readings live, or to grow your car into several files, open it in the Simulator Studio (Traffic world).

Interactive: this part runs in your browser.

Beginner

Hints, if you are stuck

  • Pedestrians: treat anyone within about 5 m of your path as a reason to stop before them. Perception can miss a person for a frame, so remember the last sighting for a second or so and keep counting the distance down by your speed.
  • Stopping distance: from speed vv with braking bb, you need v2/2bv^2 / 2b metres. A target speed of 2b d\sqrt{2 b\, d} for a gap dd slows you smoothly so that you stop exactly in time.
  • Amber: stop if you can do so comfortably (d>v2/2bd > v^2/2b); otherwise go through.
  • Unknown light: what is the safe assumption? Keeping the last known state, or assuming red, both work.
  • Braking hard enough: a throttle proportional to the speed error can be too gentle when a gap closes fast. Check how close you got in the replay.
  • Night and rain: the lane points are noisier further ahead. A look-ahead that grows with speed smooths the steering.
Beginner

Going further

  • Use your own networks end to end. The simulator demos in Part 2 already run your exported detector, U-Net, light classifier and PilotNet live. Record a drive, run your networks on it in Colab, and compare their outputs with the labels: that measures the real errors of your perception, which you can then compare with the noise the capstone assumes.
  • Move to a full simulator. The next topic packs your whole car (this drive(), your networks and the perception code) into one project, and runs it in CARLA, a free, open-source 3D driving simulator.
  • Write a safety case for your car, as in the previous topic: its operational design domain, the hazards in these 20 scenarios, how your code handles each, and the evidence (your test results). Then design a 21st scenario that breaks it.

Once your car passes all 20, you have built, from scratch, every layer of a self-driving car: control, perception, localisation, planning, learning and safety testing. The final topic takes it out of this page.

Check yourself

Quiz

1. Your car is at 12 m/s and brakes at 4 m/s². About how far does it need to stop?

2. Perception misses a pedestrian for one frame. What should drive() do?

3. The light state is "unknown" 20 m before the stop line. Which choice is safest?

4. Why test with perception set to "poor" as well as "realistic"?

Go deeper

📚 Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready