Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Self-Driving Car AI - Making Right Turns (Difficulty: 4)

    Tutorials
    1
    1
    380
    Loading More Posts
    • 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.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

       
      In the previous tutorial, we created an AI driver that can steer the car toward the center of the lane with the help of a lane marker.

      In this tutorial, we will make the car stop at the stop sign at the end of the current street and then make a right turn.

      fsd3step7

       
       

      Step 1 - Remix the Starting Project

       

      Please open the following project and remix it into your own:

      play.creaticode.com/projects/67abea71019e21d07d95ed17

       
      In this project, the AI sprite already contains the logic from the previous tutorial:

      cd0ed377-5bf7-41c1-9697-d4afa190590e-image.png

       
      When you click “Start”, the car will go back into the center of its lane, and stop after 5 seconds. We will use this code as our starting point.

       
       

      Step 2 - Read information about the stop sign

       

      The stop sign is a very important object. It not only tells us where is the end of the current street, but also requires our car to make a full stop. However, we should not deal with stop signs unless we are close to one, otherwise it will be a waste of time to constantly review all stop signs on the map.

      For this purpose, our simulation environment provides the following information to the AI driver:

      1. When the lane marker arrives at the stop sign, it will stop moving forward, and also turn red.
      2. In the “marker info” table, the column “stop” will become 1 when the lane marker stops at the stop sign.
      3. In the “marker info” table, these 3 columns will tell us the starting point of the next lane if the car needs to turn left/right or continue forward.

       
      Now let’s observe these information by running the program. When you click the “Start” button, you will see the the lane marker will stop moving at the stop sign and turn red:

      fsd3step2

       
      At this point, turn on the monitor of the “marker info” table, and you will see these columns on the right:

      • stop: this column is 1 when the lane marker is stopped at a stop sign, and empty otherwise;
      • leftxy: this is the x and y positions of the starting point of the next lane if the car needs to turn left. It is empty when there is no way to turn left or when that information is not provided.
      • forwardxy: this is the x and y positions of the starting point of the next lane if the car needs to continue forward. It is empty when there is no way to continue in the same direction or when that information is not provided.
      • rightxy: this is the x and y positions of the starting point of the next lane if the car needs to turn right. It is empty when there is no way to turn right or when that information is not provided.

      6b5e92fa-e55c-4c22-b284-f9dc5d93ad0e-image.png

       
      As you can see, “stop” is 1, indicating the lane marker is at a stop sign. Also, the “rightxy” column is “260_2427”, which means for the car to turn right, the next lane will start at the position of (260, 2427).

      We can add 2 new variables that are related to the stop sign:

      • is stop: whether the lane marker is stopped at a stop sign;
      • distance: distance to the lane marker;

       
      1698fbc2-8009-47a6-b9a7-67650b79609c-image.png

       
      Next, we will use this information to control the car.

       
       

      Step 3 - The Driving Mode

       

      To make our car stop at the stop sign, our AI will give different commands to the car than to make the car go forward. To clearly indicate what our AI driver is doing, we will use a new variable “driving mode”, which will have these values:

      • It will have a value of “normal” when the car is going forward
      • It will have a value of “stopsign” when we need to stop the car for a stop sign.

      When the AI driver starts, it will be in the “normal” mode:

      c4d986bd-77fa-426c-9234-c9e037743451-image.png

       
      And we will use 2 “if” statements to organize our logic based on the driving mode:

      • If the driving mode is “stop sign”, we set the brake level to stop the car
      • If the driving mode is “normal,” we apply our existing logic of keeping the car in the lane center and under the 100 speed limit.

       

      blocks (44).png

       
       

      Step 4 - Switch to the “stopsign” driving mode

       

      We are still missing an important logic: how do we switch from the “normal” mode to the “stopsign” mode?

      While we are driving the car normally, we need to keep an eye on the stop sign information. If the lane marker has stopped at a stop sign, and if our car is very close to the lane marker (distance < 80), then it is time to switch to the “stopsign” mode. In code, this logic is the following:

      9bb5a40f-8c76-4d80-b3f7-898db871b903-image.png

       
      Now our logic for stopping is complete. We no longer depend on the timer value, and the car automatically stops before the stop sign.

      fsd3step4

       
       

      Step 5 - Switch to the “turnright” driving mode

       

      After the car has stopped, we need to make the car turn right. We can introduce a new driving mode of “turnright”, so that we can put all the code for turning the car right in that mode.

      We need to carefully consider when to switch to the “turnright” mode. There are 2 issues we need to handle:

      • When the AI set the brake level to 100, the car may not immediately stop. Depending on its speed, it might take some time to come to a full stop.
      • Even after the car has fully stopped, we should not start the right turn right away. It is better to wait some time.

      To handle these issues, we can use the following logic to switch from the “stopsign” mode to the “turnright” mode. Basically, we will wait until the car’s speed is almost 0 (less than 1), and then wait another 1 second, and then switch to the “turnright” mode.

      1339b174-2ab6-44af-8ea1-9ab1f7a8f960-image.png

       

       
       

      Step 6 - Make the car turn right

       

      Now we can start to add logic to control the car in the “turnright” mode. To start simple, we can simply set a small engine force and steer the car to the right by 25 degrees:

      99f85474-b7a3-4cf6-99ba-8d06d6f7121b-image.png

       
      Now let’s observe what happens:

      fsd3step6

       
      Our AI has successfully made the car turn right. Next, we will need to think about when to exit the “turnright” mode so the car can drive normally on the new street.

       
       

      Step 7 - “car dir” and “target dir”

       

      One simple way to determine if the car has fully turned right is to check if its direction is aligned with the new street’s direction.

      First, let’s add the “car dir” variable, which will store the direction of the car (read from the “car info” table):

      3693e54d-3323-475f-a6fd-a06923731b7e-image.png

       
      Next, when we switch to the “turnright” mode, we can use another variable “target dir” to store the desired driving direction of the car, which is the current direction of the car plus 90 degrees (since we are turning right by 90 degrees):

      1b087c95-bebc-4d0e-9c11-48804b5f6a8c-image.png

       
       

      Step 8 - Compare “car dir” and “target dir”

       

      The last step is to keep checking if the car’s direction is aligned with the target direction, and when that happens, switch back to the “normal” driving mode.

      To do that, we can first calculate the absolute difference between the car’s direction and the target direction. The “abs” operator ensures we only look at their difference, and it does not matter which value is greater.

      132ad763-c768-4401-b1e3-7179a0cd34b2-image.png

       
      Next, if the direction difference is very small, then we can switch back to the “normal” mode:

      fd10bfc7-ee45-4088-b2d2-40dff10d10e0-image.png

       
      Note that we also check if the direction difference is greater than 355. This is because the direction value is always between -180 and 180 degrees, so if 2 directions are 179 degrees and -178 degrees, their difference is 357 degrees, but they are actually only 3 degrees apart.

      In summary, here is the full logic for handling the “turnright” driving mode:

      6b0cedff-3b30-4ead-94e9-7e8c2d90acf6-image.png

       
      Now our car will keep driving happily in a large square:

      fsd3step7

       
       

      Additional Challenges

       

      Here are some challenges for you to improve our AI driver further:

      1. Currently the car will cut into the corner of the curb. Can you improve the “turnright” mode further so the car makes a clean right turn?
      2. Can you try to make the car turn into the left lane in the new road? It is the lane with the yellow line on its left, which requires a bigger turn for the car.
      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post