In this lesson we will program a car to drive around a test track with no one driving it except a computer program!
What is an autonomous car?
An autonomous car (also known as a driverless car, self-driving car or robotic car) is a car that is capable of sensing what is around it and navigating without a person driving.
How does it work?
Autonomous cars use a variety of techniques to detect their surroundings, such as radar, laser light, GPS, odometry and computer vision. They have advanced control systems that interpret sensory information to identify appropriate navigation paths, as well as obstacles and relevant signage.
The following 2 minute video shows some of the different cameras that a Tesla autonomous car uses to sense where the road is and all the different objects that it comes close to such as people and other cars.
Create a new Scratch project and delete the cat sprite.
Go to the Scratch website using the link below and click on the 'Create' link in the blue bar at the top.
By default, each new project starts with the cat sprite already added. To delete the cat click on the x in the blue circle beside the cat in the sprite list.
Included with this step is a car picture that we're going to upload into our Scratch project. First download the picture file onto your computer and then upload it into your project.
You can upload images and sprite files into your project. To upload a sprite follow these steps:
The new sprite will upload into your project and appear in the stage area.
As the car sprite is very large, we'll need to shrink it so that it has room to move around. We're going to shrink it to 10% of it's original size.
Add the following code to the car sprite:
when green flag clicked
set size to (10) %
We're going to use the backdrop editor to draw a racing track. To open the backdrop editor do the following:
Next we're going to draw a race track. First we will colour the background green and then we will draw a black track. Follow these steps:
Drag your car in the stage area and place it on your track.
We want the car to always start in this position when we click the green flag so add the following code to the car sprite underneath the set size to 10 % block:
when green flag clicked
set size to (10) % // add new code under here
go to x (5) y (126) // this will be automatically set to the current position of the car
point in direction (90)
We're going to create and use a 'speed' variable to control how fast or slow the car is going.
Create a new variable called 'speed' and then set the speed variable to 5.
when green flag clicked
set size to (10) %
go to x (5) y (126)
point in direction (90)
set [speed v] to (5) // add this block
In the Variables palette, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
You may have noticed that the purple car has red, blue and orange parts at the front of it. These are the cars sensors and we're going to use them to sense where the road is and drive the car around it.
First we're going to use the blue sensor to detect if the car is on the road, we'll do this by checking if it's currently touching the black of the road. If it is then we'll make the car move forward.
Add the following code underneath the set [speed] to 5 block.
when green flag clicked
set size to (10) %
go to x (5) y (126)
point in direction (90)
set [speed v] to (5) // add new blocks under here
forever
if < color [#2634df] is touching [#000000]? > then // set this to the blue sensor and the black road
move (speed) steps
end
end
Once you have done this, click on the green flag to run your code. You car should drive forward until it drives off the track and the blue sensor is no longer touching the black of the road.
Next we will program the red sensor to detect if it's touching the green grass. If it is we will need to turn our car right so it stays on the track.
Add the following code inside the forever block.
when green flag clicked
set size to (10) %
go to x (5) y (126)
point in direction (90)
set [speed v] to (5)
forever
if < color [#2634df] is touching [#000000]? > then
move (speed) steps
end
if < color [#df2626] is touching [#5fb949]? > then // set this to the red sensor and the green grass
turn cw (10) degrees
end
end
Click the green flag once you've done this to run your code. Your car should drive forward and the turn right when the red sensor touches the green grass!
Now we will add similar code to use the orange sensor and turn left.
Add the following new code inside the forever block.
when green flag clicked
set size to (10) %
go to x (5) y (126)
point in direction (90)
set [speed v] to (5)
forever
if < color [#2634df] is touching [#000000]? > then
move (speed) steps
end
if < color [#df2626] is touching [#5fb949]? > then
turn cw (10) degrees
end
if < color [#d9a61c] is touching [#5fb949]? > then // set this to the orange sensor and the green grass
turn ccw (10) degrees
end
end
Click the green flag once you've done this to run your code. Your car should drive forward and turn right and left when the red and orange sensors touch the green grass!
At the moment your car should be driving around the track at a steady pace. Let's see what will happen if we speed it up! We're going to program the up and down arrows on your keyboard to speed up and slow down the car.
Add the following code to the car sprite:
when [up arrow v] key pressed
change [speed v] by (1)
when [down arrow v] key pressed
change [speed v] by (-1)
Once you've added these, click the green flag and then use the up and down arrows to speed up and slow down the car. If you speed it up enough it will probably run off the track before the sensor code has time to make it turn. We'll need to add some code to reverse the car and get it back on the track!
We can detect if the car has run off the track by checking to see if the blue sensor is touching the green grass. If it is then we'll program the car to reverse and turn slightly. It's important the we turn the car slightly when we reverse, otherwise the car will just drive straight back off the track!
Add the following new code inside the forever block.
when green flag clicked
set size to (10) %
go to x (5) y (126)
point in direction (90)
set [speed v] to (5)
forever
if < color [#2634df] is touching [#000000]? > then
move (speed) steps
end
if < color [#df2626] is touching [#5fb949]? > then
turn cw (10) degrees
end
if < color [#d9a61c] is touching [#5fb949]? > then
turn ccw (10) degrees
end
if < color [#2634df] is touching [#5fb949]? > then // set this to the blue sensor and the green grass
move (-2) steps // reverse
turn cw (5) degrees // turn so you don't go into the same spot
end
end
Click the green flag once you've done this to run your code. Your car should drive forward and the turn right when the red sensor touches the green grass!
Have you any ideas to improve the project or make it better?
Maybe add some extra backdrops with different tracks and see if your autonomous car can navigate them.