Lesson 21: Obstacle Avoidance Robot

Lesson Description

In this lesson, you will learn how to create an Obstacle Avoidance Robot. You will learn to code the robot to detect obstacles, turn in different directions and prevent collisions.

All About – Obstacle Avoidance Robot

The Obstacle Avoidance Robot is an autonomous robot that can be used to detect obstacles and avoid them by turning in different directions to prevent collisions.

They can be used for a variety of tasks that can reduce workload, such as automatic vacuuming.

Logic Behind Obstacle Avoidance

Quarky detects an obstacle, then checks left and right, and turns in that direction if the distance is > 30 cm.

STEP 1: Go forward if there is no obstacle.

STEP 2: If an obstacle comes at a Min distance. The ultrasonic sensor will detect the obstacle and stop.

STEP 3: Next, using the servo motor the robot will scan the environment. But first, it will turn the servo to 45 and check for free area obstacles.

STEP 4: If there is no obstacle in a range of 30 cm at the 45-degree angle. Quarky will take a right turn by 45 degrees.

STEP 5: If an obstacle is detected on the right side at a 45-degree angle. Change the angle to 135 degrees.

STEP 6: If there is no obstacle in a range of 30 cm at the 135-degree angle. Quarky will take a left turn by 45 degrees.

STEP 7: If an obstacle is detected on the right side at the 135-degree angle. Change the angle to 0 degrees.

STEP 8: If there is no obstacle in a range of 30 cm at the 0-degree angle. Quarky will take a right turn by 90 degrees.

STEP 9: If an obstacle is detected on the right side at the 0-degree angle. Change the angle to 180 degrees.

STEP 10: If there is no obstacle in a range of 30 cm at the 180-degree angle. Quarky will take a left turn by 90 degrees.

STEP 11: If an obstacle is detected on the right side at a 180-degrees angle, then take the reverse.

Logic Flowchart

Coding Steps

Now it’s time to program our own Obstacle Avoidance Robot!

Follow the steps below:

  1. Create a New file in PictoBlox App.
  2. Connect Quarky to PictoBlox.
  3. Create two variables named Max, and Min from the Variables palette.
  4. Drag and drop the when green flag click block from the Events palette.
  5. Then, add the set () to ()  Set to Min to 5.
  6. Duplicate this block and set Max to 30. So, we are basically initializing maximum and minimum distances of 30 cm and 5 cm.
  7. Create a block named – Initialize Robot. Place the block.
    1. Now, to initialize the pins go to the Sensors palette and add connect Ultrasonic ()  to trig (), echo()  below the Initialize Robot hat block.
    2. The next step would be to set the initial servo angle as 90 degrees, for this drag and drop the set servo on() to () angle from the Robot palette.
    3. Add wait () second below-set servo on () to () angle.
  8. Detecting Obstacle:
    1. Now, go to the Control palette and add a forever block below the Initialize Robot.
    2. After that, place the if () then block from the Control palette inside the forever block.
    3. Next, go to the Operators palette and put the ()<() block inside the space of the if () then block.
    4. Drag and drop the get ultrasonic () distance block from the Sensors palette within the first space of the ()<() block and the Min variable in the second space. This block will help compare the sensor value with the minimum distance.
  9. Checking for Free Area:
    1. Next, create a block custom block named Set Servo Angle with Delay ()with Angle as the parameter.
    2. Put a set servo () to () angle block with the angle as Angle.
    3. After that add a delay of 0.5 seconds by adding wait () seconds block.
    4. Come back to the main script. Add the stop robot from the Robot palette. Add Set Servo Angle with Delay () block with angle set as 45 degrees. This condition will stop the robot and turn the servo’s angle to 45 degrees whenever an obstacle will come within the minimum distance range.
    5. Now, we will check the maximum distance by adding if () then else block from the Control palette.
    6. Next, go to the Operator palette and put the ()>() block inside the if () then else block.
    7. Drag and drop the get ultrasonic () distance block from the Sensors palette within the first space of the ()>() block and the Max variable in the second. This block will help compare the sensor value with the maximum distance.
    8. If the condition is satisfied, the robot should turn right. This can be done by adding go () at () % speed for () seconds inside the if statement. Set the speed as 50%, and the time as 0.6 seconds.
    9. After that, duplicate the block from Set Servo Angle with Delay () block and place it in the else branch. Change the angle to 135 degrees and the robot’s direction to the left.
    10. Create a custom block named – Check Extreme. This block will check the conditions for 0 and 180 degrees. Add the block in the else branch.
    11. Next, if our robot doesn’t get any obstacles, it should keep going forward. So, for that, put the servo () to the () angle block at the end of the first if branch. Set the angle as 90 degrees.
    12. Lastly, place the go () at() speed() block. From the dropdown, select the direction as forward, and the speed as 50%.
  10. Checking Extreme Conditions:
    1. Duplicate the block from Set Servo Angle with Delay () block with the value of 135 and place it below the Check Extreme block. Change the angle to 0 degrees and the robot direction to the right and the time to 1.2 seconds.
    2. Duplicate the block from Set Servo Angle with Delay () block and place it in the else branch. Change the angle to 180 degrees and the robot’s direction to the left.
    3. Within the else-branch, add go () at ()% speed for () seconds block. This time set the direction as right, speed as 80%, and time as 1.5 seconds. This will make the robot go reverse.
  11. Finally, our robot is ready to run! Run the robot and check if it is working properly.
  12. Save the file as Obstacle Avoidance.

Activity: Output

Make sure you finish and submit the assignment, as well as take the quizbefore moving on to the next lesson.

 

Table of Contents