Self-Driving Cars
Topic 2 · Part 1: How a self-driving car works

The Simulator: Run Your Code

Where your code goes and how the simulator runs it: write control(car) in Python, press Run, and watch your car drive a lap. In this page, with nothing to install, or on your own computer with the same results.

Softmaxima Simulator StudioWrite your car in Python, with as many files and trained networks as it needs, and watch it drive. In your browser, nothing to install.Open
Show:
In this topic
Beginner

In plain words

Every controller in this course is one small Python function. This page is where you run it: you write the function, press Run, and the simulator drives the car with it, 30 times a second, for a whole lap. You can also download the same simulator and run it on your own computer.

By the end of this topic you will be able to run any controller on any track, here or on your own computer, and fix the common errors.

Start with: the demo Try it: the simulator: press Run with the PID starter code.

Beginner

How the simulator runs your code

Every controller in this course is one Python function, control(car). The simulator calls it 30 times a second:

  1. it measures the car and hands you a car object with the readings below;
  2. your function returns two numbers: steer (radians, + turns left, at most ±0.55, about 31°) and throttle (from −1, full brake, to 1, full power);
  3. the simulator moves the car 1/30 of a second with the bicycle model, including the steering-rate and grip limits, and repeats.
ReadingWhat it is
car.ctedistance from the lane centre in metres; + means left of it
car.heading_errorangle between where the car points and where the road goes, in radians
car.speedmetres per second (11 m/s is about 40 km/h)
car.path30 points on the lane centre ahead, 1 m apart, in the car's frame: x forward, y left
car.curvatureshow sharply the road bends at those points (1 / radius; + bends left)
car.dt, car.tthe time step (1/30 s) and the time since the start

A lap is complete when the car has driven once round. It is a crash if the car gets more than 2.6 m from the lane centre (off the road, or across the oncoming lane). Each run lasts at most 120 seconds. Anything you keep between calls (a previous error, a sum) goes in a variable outside the function, as in the PID example.

Try it

Try it: the simulator

How to use it: pick a track and a start, choose some starter code (or write your own) in the editor, and press ▶ Run on the track. The first run downloads Python for the browser, which takes a few seconds; after that, a lap takes a moment. The car then replays the lap, with a chart of its distance from the lane centre underneath.

Your code is kept in this browser, so it is still here next time. Save as my_controller.py keeps a copy on your computer, and Open a .py file loads one back, and Download the simulator gets the same simulator for your own computer (see below). Anything you print() appears under the result: print a few values to see what your controller is doing.

Interactive: this part runs in your browser.

Beginner

Bigger projects: the Simulator Studio

This page runs one function. Later in the course your car grows: several files, trained networks in a models/ folder, a camera instead of exact readings. For that there is the Simulator Studio: a code editor with a file explorer and tabs next to this same simulator.

  • Exact sensors: the readings above, for Part 1 (start from the PID or pure pursuit template).
  • Traffic world: other cars, traffic lights and pedestrians, with perception readings, for the capstone.
  • Camera + your networks: your U-Net, detector and traffic-light classifier on the camera image, then your decisions: the whole car. Upload your own trained networks to use them.

main.py is the file that runs; it can import your other files. Projects are kept in your browser and export as a .zip that runs on your own computer too.

Beginner

On your own computer

The same simulator also comes as plain Python, with nothing to install except Python 3.8 or later. It is a line-by-line copy of the one in this page, so the same controller gives the same lap, to the centimetre.

  1. Download the zip below and unzip it. It contains softmaxima_sim.py (the simulator), run.py, and my_controller.py (the PID example).
  2. Open a terminal in that folder and run:
    python run.py my_controller.py
    It prints the result of one lap of the City loop, starting 1.2 m off-centre at 40 km/h.
  3. Choose the track and the start with --track city|hairpin|oval, --speed (m/s) and --offset (metres). Add --show to watch the lap; that needs pip install matplotlib.

Edit my_controller.py in any editor (or put your controller in a new file and pass its name). You can also use the simulator from your own Python code: from softmaxima_sim import simulate, then simulate("city", control) returns the lap's frames and numbers.

Interactive: this part runs in your browser.

Beginner

If something goes wrong

  • "No function called control(car) was found": your code must define a function with exactly that name: def control(car):.
  • "control(car) must return two numbers": end with return steer, throttle, on every path through the function.
  • A Python error: the last lines of the error are shown, with the line number. Fix that line and run again.
  • The car swings and crashes: the gains are too high, or the sign is wrong (a car that is left of the centre, with a positive car.cte, must steer right, negative). Try smaller numbers.
  • The car slides wide in bends ("tyres sliding"): it is going too fast for the bend; plan the speed as in Better Steering.
  • Python does not load: the first run needs an internet connection to download Python (about 10–30 MB). If your network blocks it, use the simulator on your own computer instead.
Check yourself

Quiz

1. How often does the simulator call control(car)?

2. The car is 0.4 m to the left of the lane centre. What is car.cte, and which way should it steer?

3. What does control(car) return?

Ready