Self-Driving Car AI - Making Right Turns (Difficulty: 4)
-
Introduction
In the previous tutorial, we created an AI driver that can steer the car toward the center of the lane with the help of a lane marker.In this tutorial, we will make the car stop at the stop sign at the end of the current street and then make a right turn.
Step 1 - Remix the Starting Project
Please open the following project and remix it into your own:
play.creaticode.com/projects/67abea71019e21d07d95ed17
In this project, the AI sprite already contains the logic from the previous tutorial:
When you click “Start”, the car will go back into the center of its lane, and stop after 5 seconds. We will use this code as our starting point.
Step 2 - Read information about the stop sign
The stop sign is a very important object. It not only tells us where is the end of the current street, but also requires our car to make a full stop. However, we should not deal with stop signs unless we are close to one, otherwise it will be a waste of time to constantly review all stop signs on the map.
For this purpose, our simulation environment provides the following information to the AI driver:
- When the lane marker arrives at the stop sign, it will stop moving forward, and also turn red.
- In the “marker info” table, the column “stop” will become 1 when the lane marker stops at the stop sign.
- In the “marker info” table, these 3 columns will tell us the starting point of the next lane if the car needs to turn left/right or continue forward.
Now let’s observe these information by running the program. When you click the “Start” button, you will see the the lane marker will stop moving at the stop sign and turn red:
At this point, turn on the monitor of the “marker info” table, and you will see these columns on the right:- stop: this column is 1 when the lane marker is stopped at a stop sign, and empty otherwise;
- leftxy: this is the x and y positions of the starting point of the next lane if the car needs to turn left. It is empty when there is no way to turn left or when that information is not provided.
- forwardxy: this is the x and y positions of the starting point of the next lane if the car needs to continue forward. It is empty when there is no way to continue in the same direction or when that information is not provided.
- rightxy: this is the x and y positions of the starting point of the next lane if the car needs to turn right. It is empty when there is no way to turn right or when that information is not provided.
As you can see, “stop” is 1, indicating the lane marker is at a stop sign. Also, the “rightxy” column is “260_2427”, which means for the car to turn right, the next lane will start at the position of (260, 2427).We can add 2 new variables that are related to the stop sign:
- is stop: whether the lane marker is stopped at a stop sign;
- distance: distance to the lane marker;
Next, we will use this information to control the car.
Step 3 - The Driving Mode
To make our car stop at the stop sign, our AI will give different commands to the car than to make the car go forward. To clearly indicate what our AI driver is doing, we will use a new variable “driving mode”, which will have these values:
- It will have a value of “normal” when the car is going forward
- It will have a value of “stopsign” when we need to stop the car for a stop sign.
When the AI driver starts, it will be in the “normal” mode:
And we will use 2 “if” statements to organize our logic based on the driving mode:- If the driving mode is “stop sign”, we set the brake level to stop the car
- If the driving mode is “normal,” we apply our existing logic of keeping the car in the lane center and under the 100 speed limit.
Step 4 - Switch to the “stopsign” driving mode
We are still missing an important logic: how do we switch from the “normal” mode to the “stopsign” mode?
While we are driving the car normally, we need to keep an eye on the stop sign information. If the lane marker has stopped at a stop sign, and if our car is very close to the lane marker (distance < 80), then it is time to switch to the “stopsign” mode. In code, this logic is the following:
Now our logic for stopping is complete. We no longer depend on the timer value, and the car automatically stops before the stop sign.
Step 5 - Switch to the “turnright” driving mode
After the car has stopped, we need to make the car turn right. We can introduce a new driving mode of “turnright”, so that we can put all the code for turning the car right in that mode.
We need to carefully consider when to switch to the “turnright” mode. There are 2 issues we need to handle:
- When the AI set the brake level to 100, the car may not immediately stop. Depending on its speed, it might take some time to come to a full stop.
- Even after the car has fully stopped, we should not start the right turn right away. It is better to wait some time.
To handle these issues, we can use the following logic to switch from the “stopsign” mode to the “turnright” mode. Basically, we will wait until the car’s speed is almost 0 (less than 1), and then wait another 1 second, and then switch to the “turnright” mode.
Step 6 - Make the car turn right
Now we can start to add logic to control the car in the “turnright” mode. To start simple, we can simply set a small engine force and steer the car to the right by 25 degrees:
Now let’s observe what happens:
Our AI has successfully made the car turn right. Next, we will need to think about when to exit the “turnright” mode so the car can drive normally on the new street.
Step 7 - “car dir” and “target dir”
One simple way to determine if the car has fully turned right is to check if its direction is aligned with the new street’s direction.
First, let’s add the “car dir” variable, which will store the direction of the car (read from the “car info” table):
Next, when we switch to the “turnright” mode, we can use another variable “target dir” to store the desired driving direction of the car, which is the current direction of the car plus 90 degrees (since we are turning right by 90 degrees):
Step 8 - Compare “car dir” and “target dir”
The last step is to keep checking if the car’s direction is aligned with the target direction, and when that happens, switch back to the “normal” driving mode.
To do that, we can first calculate the absolute difference between the car’s direction and the target direction. The “abs” operator ensures we only look at their difference, and it does not matter which value is greater.
Next, if the direction difference is very small, then we can switch back to the “normal” mode:
Note that we also check if the direction difference is greater than 355. This is because the direction value is always between -180 and 180 degrees, so if 2 directions are 179 degrees and -178 degrees, their difference is 357 degrees, but they are actually only 3 degrees apart.In summary, here is the full logic for handling the “turnright” driving mode:
Now our car will keep driving happily in a large square:
Additional Challenges
Here are some challenges for you to improve our AI driver further:
- Currently the car will cut into the corner of the curb. Can you improve the “turnright” mode further so the car makes a clean right turn?
- Can you try to make the car turn into the left lane in the new road? It is the lane with the yellow line on its left, which requires a bigger turn for the car.
-
info-creaticode