Self-Driving Car AI - Lane Centering (Difficulty: 3)
-
Introduction
In the previous tutorial, you learned how to drive the car forward by setting its engine force (similar to pressing the gas pedal) and how to stop the car by setting its brake level. You have also learned how to read the car’s speed and stop accelerating if the speed is fast enough.
In this tutorial, we will look at how to steer the car by controlling its steering wheel. Specifically, we will try to steer the car towards the center of a lane, which is a very common feature in self-driving cars.
Step 1 - Remix and Starting Project
Please open and remix the following project:
play.creaticode.com/projects/67aa4af6ea863449f6972ab5
When you run it, it will make the car drive for 15 seconds at a maximum speed of 100:
When you run the project and click Start, the car will drive forward for 15 seconds, then stop. You can see that this car is not driving within the lane, and that’s the problem we will fix.
Step 2 - Make the car steer to the right
We will build up our solution step by step. First, let’s try to simply steer the car to the right to go back into the lane. To do that, we need to set the “set steering” command, and the parameter will be the angle of the steering wheel. For example, to turn the wheel 3 degrees to the right, we just need to set the parameter to 3:
When you click Start, the car will gradually turn to the right, which means our steering command is working:
Feel free to try other steering angles and observe what happens.
Step 3 - Steer the car left and then center
Obviously, we do not want to keep the car steering to the right forever. Once the car is back to the center of the lane, we need to steer it to the left, and then keep the car running straight.
We can still rely on a timer-based solution: we steer to the right for 3 seconds, then to the left at 3 degrees for another 3 seconds (so the total timer value will be 6), and then set the steering angle to 0:
When you click Start, the car will do better, but still not perfect: it will stray to the left.
Now, before continuing, can you try to adjust the steering commands or the timing of them so the car will stay at the center of the lane?
Step 4 - A working solution
There are many ways to fine-tune the parameters, and one simple solution is to make the car steer to the left for less than 3 seconds. For example, after steering right for 3 seconds, we can make it steer left for another 2.3 seconds (so the total timer value is 5.3):
Here is the result:Note that although this works, it is only meant to illustrate how to steer the car back to its lane, and it is NOT a good solution in general because it relies on the timer values (3s and 5.3s) that are specific to this setup. If the car is starting from a different position or heading direction, these timer values would not steer the car into the center of the lane.
Next, let’s work out a more generic solution that works all the time.
Step 5 - Display the lane marker
In a real-world self-driving system, some sensors will detect the lane borders, and the AI program can make steering decisions based on that sensor information.
[image source: extremetech.com]
In this simulation project we are working with, a similar sensor system is available, and it will calculate a “lane marker” based on the position of the lane and the car. As the car moves, the lane marker will always stay ahead of the car at a distance of about 250 units, and it will always be placed at the center of the lane.To view the lane marker (for testing or debugging purpose), you can make it visible by sending the “show marker” command like this:
Please also remove all the steering code we added earlier, so the car will go straight ahead:
Now you should see a small cyan sphere at the center of the lane that always stays ahead of the car:The lane marker makes our job much easier: we just need to make sure the car is aligned with the lane marker as much as possible.
Step 6 - Read from the “marker info” table
To find out information about the lane marker, you can read from the “marker info” table. Turn on its monitor on the stage, and you will see its content like this:
The meaning of the columns are:- name: The name of the marker. It will be “my marker” for the lane the car is currently in. In the future, you will be working with lane markers for other lanes as well.
- x and y: The position of the lane marker.
- distance: The distance between the lane marker and the car.
- angle: The angle between 2 lines: the line from the car to the lane marker and the center line of the lane.
- dir: the direction of the lane.
Out of all these columns, which information is the most useful to us if we need to bring the car to the center of the lane?
[thinking space …]
Several of the columns can be used to align the car with the lane marker, but the most useful information is the “angle”: if the car is going along the center of the lane, then the angle should be 0. On the other hand, if the car is not at the center of the lane, then the angle will not be 0:
You can store the angle value in a new variable like this:
Next, can you add the logic to steer the car to the lane center?
[thinking space …]
Step 7 - Steer the car based on the lane marker’s angle
Here is one example solution: whenever the angle is greater than 1, we steer the car to the left, and whenever the angle is less than -1, we steer to the right. If the angle is in-between -1 and 1, we will not steer the car.
You will get a result like this:
Additional Challenges
Here are some additional challenges for you:
-
Currently, the car steers back to the lane center very slowly. Can you try to make it go back faster?
-
If there is not a lot of runway ahead, it might be necessary for the car to slow down (to a lower speed limit) while going back to the lane center. Can you implement that idea?
To test your solution:
-
Try different speed limit
-
Try different starting position for the car: Go to the “car” sprite, and change the starting x position of the car in this “move” block below. For example, you can change it to 160 or 234. See if the solution still works
-
info-creaticode