Autonomous Car (part 1)

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

Create a new Scratch project and delete the cat sprite by right clicking on the cat and then clicking on the 'delete' option in the menu that appears.

https://scratch.mit.edu


3

Add the car sprite

Included with this lesson is a car picture that we're going to upload into our Scratch project. To first download the picture file onto your computer and then upload it into your project follow these steps:

  1. Right click on the purple car image and then click 'Save image as...'
  2. Choose a location on your computer such as your desktop and save the file.
  3. In your Scratch project click on the 'Upload sprite from file' button (by clicking the sprite library button highlighted in yellow in the figure on the right) .
  4. Go to the folder you saved your car picture file.
  5. Select the file and click 'Open'

The car sprite will upload into your project and appear in the stage area.



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:

  1. events when clicked 
  2. looks 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 left. A blue box should appear around it.
  2. Click on the backdrops tab in the Palette area. 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 on the colour green in the colour palette.
  2. Click on the paint bucket tool.
  3. Click on the white backdrop to change it's colour to green.
  4. Click on the colour black in the colour palette.
  5. Click on the paint brush tool.
  6. Move the thickness slider to the right to increase the size of your paint brush.
  7. 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:

  1. motion go to x: 0 y: 0 
    (the x and y values will be already have the current position of your car)
  2. motion point in direction 90 
    (this points your car to the right (90 degrees)


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. 

In the Data palette, create a new variable by clicking the 'Make a Variable' button.

  1. '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 at the bottom of the when  clicked code blocks:

  1. data set [speed] to 5 


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 at the bottom of the when  clicked code blocks:

  1. control forever 
    1. control if then 
      (set sensing  color   is touching color   ? as the condition)
      (change the first color box to the blue of the car sensor and the second color box to the black of the track)
      1. motion move 10 steps 
        (replace [10] with data speed)

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

  1. control if then 
    (set sensing color   is touching color   ? as the condition)
    (change the first color box to the red of the car sensor and the second color box to the green of the grass)
    1. motion turn 10 degrees 

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

  1. control if then 
    (set sensing color   is touching color   ? as the condition)
    (change the first color box to the orange of the car sensor and the second color box to the green of the grass)
    1. motion turn 10 degrees 

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!


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:

  1. events when [up arrow] key pressed 
  2. data change [speed] by 1 
  1. events when [down arrow] key pressed 
  2. data change [speed] 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 code inside the forever block but after the last if then block:

  1. control if then 
    (set sensing color   is touching color   ? as the condition)
    (change the first color box to the blue of the car sensor and the second color box to the green of the grass)
    1. motion move -2 steps 
    2. motion turn 5 degrees 

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?

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