Self-Driving Car AI - Starting and Stopping the Car (Difficulty: 2)
-
Introduction
This is the first tutorial of a mini-series that illustrates the basic logic of controlling a self-driving car. You will be building and improving an “AI driver” that sends driving commands to a car. The project will use a 3D simulation to make the display more realistic, but you do not need to use any 3D blocks.
Note that in the real world, AI systems in self-driving cars are much more complicated, but these tutorials will show you the basic ideas: the AI driver will read real-time data about the car and the road, and continuously adjust its driving commands.
In this tutorial, we will start with a very simple task: how to start the car, make it drive steadily for a while, and then stop it.
Step 1 - Remix the Simulation Project
A 3D simulation project has been created for you. Please open it and remix it into your own project:
play.creaticode.com/projects/67a98f86422ba48ed8bc59f3
When you click the green flag, it will load a 3D city street and a yellow car:
As you can see, this project contains a few sprites. but all your work will be in the “AI” sprite. Feel free to review the other sprites if you are curious how the simulation is setup.In the “AI” sprite, there are 2 event blocks:
- The “start” message is received whenever you click the “Start” button, and this is where we can add some initialization code for our AI driver.
- The “update” message is received 10 times per second after the “Start” button is clicked, and this is where we add code to send commands to the car.
Step 2 - Start the Car
Now, let’s make the car start and drive forward. To make that happen, we can set the car’s “engine force” to a positive value like 50, which will make the car go faster and faster. This can be done by sending a message “set engine” with a parameter value of 50:
Now, if you click the “Start” button, the car will start to run, and it won’t stop since we are not telling it to stop:
The car will fall off the map. You can click the green flag button again to reload the entire scene, or click the blue “Start” button to restart the car.
Step 3 - Stop the car after 3 seconds
Let’s add code to make the car run for 3 seconds, then stop. We can use the timer to keep track of time:
- When the “start” message is received, we start the timer;
- When the timer value is less than 3 seconds, we still keep its engine force at 30;
- When the timer value is at least 3 seconds, we set the car’s brake to 100% using the “set brake” message.
When you click the “Start” button, the car will run 3 seconds and then stop:
Step 4 - The “car info” table
So far, our AI driver has been sending commands to the car “blindly”, regardless of the state of the car or the road. A good AI system must “watch” what’s happening and adjust its commands in real time.
To find out more information about the car, we can look at the “car info” table, which is updated in real time. You can turn on the monitor for this table (in the “Variables” category):
You will see the table contains one row of data:
We don’t need all the columns yet. For now, it is good enough to watch these columns:- Name: when there are multiple cars in the scene, each row will represent one car, and the name of “my car” means this row is about the car controlled by the AI.
- x and y: this is the x and y position of this car
- speed: this is the speed of this car
Suppose we do not want the car to run too fast. We can check the car’s speed, and stop the engine when the car is fast enough.
We can read the car’s speed from the “car info” table into a variable “car speed”:
When you click the blue Start button, the table and the “car speed” variable will be updated in real-time:
Step 5 - Stop the engine when the car is fast enough
Now our AI driver can “see” how fast the car is running, we can set the engine force to 0 when the car speed is faster than 60. Note that this will stop the car’s acceleration rather than stopping the car. The car will still be running at a constant speed since we assume there is no friction to slow down the car.
As shown, we are making the car stop after 5 seconds so we can observe it longer. Also, when the car’s speed is less than 60, we would keep the engine force at 50 to make it accelerate, but after the car’s speed is more than 60, we would stop the engine.
Challenges
In this tutorial, you learned how to read information about the car, and how to make the car accelerate or stop by sending commands to it.
Here are some additional challenges for you:
-
Make the car stop at the stop sign with a speed limit of 100: Try to make the car stop right before the stop sign, and also make sure its speed is never more than 100. You will need to find a good timer value to start stopping the car.
-
Check the position of the car: again, try to make the car stop in front of the stop sign, but instead of using a timer to determine when to stop the car, read the x and y positions of the car, and hit the brake based on that information.
-
More precise speed limit: as you might have observed, the speed limit is not very precise. For example, when we set the engine force to 0, the car’s speed may have already passed our limit of 100. You can improve it by setting the engine force to negative for a little bit until the car speed falls below the speed limit.
-
info-creaticode