How do Self-Driving Cars Know What to Do?
How do driverless cars know what to do at an intersection? How do they know when they should stop and when it is their turn to go? What about yielding to pedestrians? In this activity, you will write your own algorithm, or list of steps, for a driverless car to follow when navigating through various road scenarios like stop signs, traffic lights, and roundabouts (traffic circles).
- Paper and pencil or a whiteboard/chalkboard
- Optional: Sticky notes
- There are many "rules of the road" that drivers must follow in order to drive safely. For example, they must stay between the lines, not drive over the speed limit, and not get too close to the car in front of them.
You might know some of these rules if you have paid attention while riding in a car. For example, you probably know that a red light means stop and a green light means go. The next time you are riding in a car, try to observe what rules the driver follows.
- Certain types of roads and intersections have specific rules. For example, take a four-way intersection with stop signs and pedestrian crosswalks (see picture). Cars driving through this intersection have to follow several rules:
- Stop at the stop sign.
- If there are other cars that got to the intersection before you, let them go through the intersection first.
- If there are pedestrians waiting to cross in a crosswalk, let them go first.
- Driverless cars, also called self-driving cars or autonomous vehicles, also have to follow all these rules.
A driverless car is controlled by a computer program instead of a human driver. It uses cameras and electronic sensors to see the world around it, detecting things like the road, traffic signs, other cars, and pedestrians.
Computer scientists write computer programs that tell the car what to do. These programs consist of a set of steps called an algorithm.
Sometimes algorithms are represented graphically with a flowchart. A flowchart uses blocks of text and arrows to show what should happen next. This flowchart represents an algorithm that uses the rules in Step 2 to tell a car how to drive straight through the intersection.
The first question on the flowchart is 'Have we reached the other side of the intersection?' If yes, the arrow points to a box that says 'Finish.' If no, the arrow points to a box that says 'Are we at the stop sign?' If yes, the arrow points to a box that says 'Stop.' If no, the arrow points to a box that says, 'Drive forward.' The flow chart continues with the questions 'Are there other cars at the intersection?' 'Did they get there before us?' and 'Are there pedestrians in the crosswalk?' In each case, there is an action for either yes or no.
Note that flowcharts use specific symbols for different things. A rectangle represents an action, like "stop" or "drive forward." Diamonds represent questions or decisions, like "Are we at the stop sign?" Rectangles with rounded corners represent the beginning and end of the algorithm, i.e. the "start" and "finish" blocks.
- Download driverless-car-scenarios (pdf). It contains top-down views of three different types of intersections.
- For each type of intersection, make a flowchart based on rules that would help a driverless car get safely from one side of the intersection to the other.
The Prep Work section shows an example flowchart for the intersection with stop signs, but you can make your own.
Draw your entire flowchart on a piece of paper or a whiteboard. You can also write on sticky notes, put them on a whiteboard, and then draw arrows to connect them.
- Next, ask yourself what happens if other drivers do not follow the rules in each scenario. For example:
Can you modify your flowchart to account for unexpected behavior by other drivers or pedestrians? What about cyclists?
- For the intersection with stop signs, what happens if another car goes through the intersection without stopping first?
- For the intersection with traffic lights, what happens if another car drives through while the light is red instead of stopping?
- For the roundabout (traffic circle), what happens if your car is already in the roundabout and another car fails to yield to you (let you go first) when entering?
- Try moving the Finish point in each scenario. For example, have the vehicle make a left or right turn. (You might need to ask an adult for help with the rules of the road for each scenario.) Make a new flowchart. How does your flowchart need to change?
- Make a list of other things that might happen when driving. For example, what other objects might you encounter on or near the road? What types of objects would you swerve or brake to avoid (for example, a person or an animal), and which ones would you drive over (such as a small piece of trash)? How would you decide what to do?
You probably found that designing an algorithm for a driverless car is very complicated! It might be simple to write rules for a very basic scenario, like driving straight through an intersection. However, your algorithm quickly becomes more complicated when you account for unexpected behavior by other drivers or things like turning instead of going straight. Your algorithm might fail and result in a crash if you do not account for certain possibilities, like another driver running a red light. Real driverless cars need to know how to drive in many different scenarios, ranging from winding country roads to speedy highways and busy city streets.
There are many rules of the road that drivers must learn—when to stop, when to yield to other cars, etc. These rules can all be written down and put into an algorithm or flowchart like you did in this activity. However, it is impossible to write a computer program with a list of rules that covers every single scenario a driver might encounter.
Humans are very good at reacting to new, unexpected situations and making decisions based on very little information. For example, if a human driver suddenly sees a cow in the middle of the road, they would know to hit the brakes! However, a computer program that has only been told to look for pedestrians and other cars, and has no idea what a cow is or what one looks like, might have no idea that it should stop.
A human driver would also know that it is safer to drive over a small piece of trash like a napkin than to slam on the brakes and risk being hit by another car. However, a computer program that has been told to stop if there is any obstacle in the road might stop unnecessarily.
So if it is impossible to write a program that tells a driverless car exactly what to do in every possible situation, how do these algorithms work?
Real driverless cars use machine learning to "learn" how to drive. They analyze massive amounts of data, like recordings from cameras mounted on cars and actions taken by real human drivers. They use this data to learn how to do everything from identifying street signs to predicting what cars in adjacent lanes might do next.
Check out the links in the Additional Resources section to learn more about machine learning.
Ask an Expert
For Further Exploration
- Try drawing your own map with a more complex scenario, like driving from your house to a store. (The document with scenarios contains one example.) Your map can also contain things like speed limits, obstacles in the road, and even multi-lane roads and highways. Can you write an algorithm to safely drive your car from the start to the destination? You might find that it is easier to break your algorithm out into multiple sub-processes or "functions." For example, the flowchart you made at the beginning of this activity could be a function for "driving through an intersection with a stop sign." Functions are useful because you can re-use them multiple times in a program whenever you need them.