Autonomous Car

ScratchHard

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

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

We've created a starter project that has a race track backdrop and a car sprite with sensors already setup. Open this starter project by clicking on the following link:

https://scratch.mit.edu/projects/297643442/editor

3

Place your car on the track

We want the car to always start on the track and pointed in the right directions add the following code to the car sprite:

when green flag clicked go to x [-100] y [130] point in direction [90]

4

Create a speed variable

We're going to create and use a 'speed' variable to control how fast or slow the car is going.

  • 'speed' this will store the speed value

When you create your first variable you will notice that new blocks for variables become available to use in the Data palette. Add the following code to the car sprite at the bottom of the code blocks:

when green flag clicked go to x [-100] y [130] point in direction [90] // insert after this set [speed v] to (5)

5

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 to the car sprite at the bottom of the:

when green flag clicked go to x [-100] y [130] point in direction [90] set [speed v] to (5) // insert after this block forever if < color [#2634df] is touching [#000000] > then // use the blue of the sensor and the black of the 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.

6

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 but after the 'if then' block you added in the last step:

when green flag clicked go to x [-100] y [130] point in direction [90] set [speed v] to (5) forever if < color [#2634df] is touching [#000000] > then // insert after this block move (speed) steps end if < color [#df2626] is touching [#6b953c] > then // use the red of the sensor and the green of the 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!

7

Turn left

Now we will add similar code to use the orange sensor and turn left.

Add the following code inside the 'forever' block but after the 'if then' block you added in the last step:

when green flag clicked go to x [-100] y [130] 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 [#6b953c] > then // insert after this block turn cw (10) degrees end if < color [#d9a61c] is touching [#6b953c] > then // use the orange of the sensor and the green of the 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 touches the green grass!

8

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!

9

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 code inside the 'forever' block but after the last 'if then' block:

when green flag clicked go to x [-100] y [130] 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 [#6b953c] > then turn cw (10) degrees end if < color [#d9a61c] is touching [#6b953c] > then // insert after this block turn ccw (10) degrees end if < color [#2634df] is touching [#6b953c] > then // use the blue of the sensor and the green of the grass move (-2) steps turn cw (5) degrees end end

Click the green flag once you've done this to run your code. If your car runs off the track (the blue sensor is touching the green of the grass) then the car should reverse and turn and then continue to drive on the track.

10

Improve it!

Have you any ideas to improve the project or make it better?

Some ideas for improving the project are:

  1. Add a second car driving the opposite way. Get them to detect and avoid each other.
  2. Add extra and more complicated tracks.
End of lesson
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