Scratch
Hard
60 mins
445 points
What you need:
  • Computer/laptop

Autonomous Car

In this lesson we will program a car to drive around a test track with no one driving it except a computer program!

This code along has finished! ⌚

You can watch it back by playing the video!

The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

1 - How an autonomous car works

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.


2 - Create a new Scratch project

Create a new Scratch project and delete the cat sprite.

https://scratch.mit.edu

3 - Add the car sprite

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 may notice the red, blue and orange ellipses sticking out of the front of the car. We will be using these as the car's sensors to navigate around the track.

4 - Shrink the car

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) %

5 - Draw a test track

We're going to use the backdrop editor to draw a racing track. To open the backdrop editor do the following:

  1. Click on the stage backdrop in the bottom right. A blue box should appear around it.
  2. Click on the backdrops tab in the top left. You should now see the backdrop editor.

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:

  1. Click the Convert to Bitmap button.
  2. Select the colour green in the Fill tool.
  3. Click on the Paint Bucket tool.
  4. Click on the backdrop to change it's colour to green.
  5. Click on the Convert to Vector button.
  6. Click on the Brush tool.
  7. Select the colour black in the Fill tool.
  8. Set the thickness of the brush to be 90.
  9. Draw a simple loop track by holding down the left button, moving your mouse and then releasing the left button.


6 - Place your car on the track

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)

When you use a go to x: 0 y: 0 block, the x and y values will be pre-filled with the current position of your car. This is why you should position your car on the track first and then drag in this block.

7 - Create a speed variable

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

8 - Drive forward

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.



9 - Turn right

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!


10 - Turn left

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!


11 - Speed up and slow down

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!


12 - Reverse

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!


13 - Improve it

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.

Join our club 😃

Create your profile, earn points & badges and learn how to code!

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu
Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more