Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. Self-Driving Car AI - Following Driving Directions (Difficulty: 4)

Self-Driving Car AI - Following Driving Directions (Difficulty: 4)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

    Introduction

     
    After the previous tutorial, our AI driver can automatically control the car to stay in lane, limit its speed, stop at the stop sign, and turn right at a crossing.

    In this tutorial, we will extend it further so that it can follow a list of driving directions to turn left/right or go straight.

    fsd4step8b

     
     

    Step 1 - Remix the Starting Project

     

    Please remix the following project:

    play.creaticode.com/projects/67ad8b0919667583370d2d81

     
    Note that the AI sprite contains the same logic from the previous tutorial, but the blocks have been divided into a few custom blocks to make them more readable:

    52fa37bb-5b5c-43ac-859d-fd8f89f8e8b3-image.png

     
     

    Step 2 - The “driving directions” list

     

    For a self-driving car in the real world, it will typically receive a destination from the passenger first, and it will calculate a driving direction based on that.

    We won’t get into how to calculate the driving direction in this tutorial. Instead, we will assume the directions have already been calculated, and we will represent them using a list named “driving directions”.

    Each item in this list will be a letter, which represents the direction at each crossroad:

    • L: turn left at the next crossroad;
    • R: turn right at the next crossroad;
    • S: go straight at the next crossroad;

    For example, suppose the driving directions list contains these 3 letters:

    4552f915-04cc-4f64-ba21-8f77622a4de1-image.png

     
    That means the car is expected to turn left at the next crossroad, go straight at the crossroad after that, turn right at the third crossroad, and then stop there.

     
     

    Step 3 - Read the next direction

     

    To keep our AI code simpler, we would only focus on the first direction from the list. After it is completed, we move on to the next direction on the list.

    We can use a new variable “next direction index”, which represents the index number of the next direction we have to follow. We will need to initialize this variable to 1 at the start:

    90de46b8-fb74-47d5-bda5-0e931a402e80-image.png

     
    We will use another new variable “next direction” to represent the actual direction. We will initialize it at the start as well, and we will update it after this direction has been fulfilled:

    32edc7f1-f708-45b5-9cfd-80018c043799-image.png

     
     

    Step 4 - Check for the lane on the left

     

    In the “normal” driving mode, we need to prepare for the next turn. For example, suppose the next direction is “L” for a left turn. If there are 2 lanes and our car is on the right lane, we need to switch to the left lane first, since we can not make a left turn from the right lane.

    To find out if there is another lane to our car’s left, we can use the “marker info” table, and look for the marker named “left marker”. If it exists, that means there is another lane to the left going in the same direction.

    Let’s define a new custom block named “update marker”, and run it at the beginning of the normal mode handler:

    9118325f-29fc-49b9-9140-3358e310720f-image.png

     
    In that block’s definition, whenever the next direction is “L”, we will get the index of the “left marker” from the “marker info” table:

    a71db503-64a1-4d3a-af9d-b8a9d93855e4-image.png

     
     

    Step 5 - Temporarily change the lane marker

     

    Suppose we have found another lane to our left, how do we ask the car to switch to that lane? The solution is very simple: we just need to aim at the lane marker from that lane, until the car enters that lane. After that, assuming there is no other lane on the left, our car will resume the old logic of following the lane marker in this new lane it has just entered.

    In code, we check if the index of the “left marker” is greater than 0, and if so, we would temporarily set the “marker angle” to be the angle relative to the marker in the left lane, not the current lane:

    33dce53b-8289-4486-b3aa-60d46d940e06-image.png

     
    That is all we need to make the car switch to the left lane. You can see that the “left marker index” is 2 when there is another lane on the left, and it changes to -1 right after the car enters the new lane. After that, the car reverts back to the normal mode of aligning to the lane marker in the lane.

    fsd4step5

     
     

    Step 6 - A new driving mode of “turnleft”

     

    To make the car turn left, we need a new mode of “turnleft”, similar to the “turnright” mode.

    Currently, when the AI is in “stopsign” mode, it always switch to the “turnright” mode after the car has fully stopped. We need to change that to determine which mode to use based on the next direction. The new “target dir” will also be different when the car needs to turn left: it will be the current direction of the car minus 90 degrees rather than plus 90 degrees.

    62d6532c-4651-4ed4-a2cf-52987ddeabe0-image.png

     
     

    Step 7 - Handle the “turnleft” mode

     

    We will define a new custom block to handle the logic of turning left at a crossroad, similar to the “handle turn right” block.

    cb83cb0c-3a69-4ceb-b262-e49078daa8a7-image.png

    d83fe299-dbee-4910-8b77-cdca701d098e-image.png

     
    To make the car turn left, we can reuse the same code for turning right, but change the engine force and steering angle. Can you give it a try?
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    [please try it yourself before looking at the next step…]

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

     
     

     
     

    Step 8 - The logic for turning left

     

    You should see that turning left involves a larger turn with a longer radius. There are many ways to set the engine force and the steering angle, and one example solution is the following, where the engine force is 10 and the steering angle is -9.5:

    40a80859-3f10-4329-b590-0d72fb780f2d-image.png

     
    This is what we will get now:

    fsd4step8b

     
     

    Step 9 - Move on to the next driving direction

     

    After the turn is completed, when we switch back to the “normal” mode, we should also update the “next direction” to the next item in the list:

    d46ec7d6-aa7b-4711-b304-a1ebddcada4d-image.png

     
    Note that this needs to be done in both the “handle turn left” and “handle turn right” blocks

     
     

    Additional Challenges

     

    At this point, our AI can drive the car to switch to the left lane and also follow the driving directions to turn left or right. There are a few missing pieces, so please use them as practice:

    1. Handle “go straight”: If the driving direction is “S”, the car should go straight across the crossroad. Please add logic to handle this case. For a hint, you can use the car’s distance from the starting point of the lane ahead (the “forwardxy” column of the lane marker) as a threshold: if the car is close enough to this point, you can switch back to normal mode.

    2. Switch to the right lane: when the next direction is “R”, and our car is still on the left lane out of 2 lanes, it should switch to the right lane.

    3. Stop at the end: when all the driving directions on the list have been fulfilled, the “next direction” becomes empty, then the car should switch to the right lane if it is not in that lane and then stop.

    You can test your AI with a list of directions like this: L, S, R, R, S, R, R, S

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode