Quarky Obstacle Avoidance Robot

Description
Learn how to assemble and program an autonomous obstacle avoidance robot with Quarky. Follow the steps to connect the servo motor and ultrasonic sensor to the Quarky and code the robot to scan the area around it.

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. The Quarky Obstacle Avoidance Robot can scan the area around it and decide to move according and avoid obstacles.

Assembly

This assembly is going to be used in our upcoming activities. The required components that will aid us in the making of this robot are as follows:

  • Quarky Robot
  • Ultrasonic Sensor (HC-SR04)
  • Servo Motor
  • Blue Plastic Parts – B7, B4, and B2
  • Screw Driver

Follow the steps:

We will start with the normal horizontal robot:

  1. Begin by detaching the Castor Wheel from the Robot using the Screw Driver.
  2. Now, fasten the B4 Plate and the Castor Wheel using the same bolt.
  3. Snap the Servo Motor onto B4 Plate.
    Note: Make sure that the white shaft from the Servo Motor is pointing upwards and aligned with the cross marked on the B4 Plate.

  4. Then, snap the Ultrasonic Sensor into the B2 Plate.
  5. Push-fit the B2 Plate in the B4 Plate as shown. Make sure the parts are properly aligned with each other.
  6. Connect the Servo motor to the Quarky Servo Connector. There are two servo ports on Quarky. Always make sure that the brown wire is on your left side.
  7. Make the following code in PictoBlox to set the Servo Motor to 90 degrees:
  8. Run the script. The servo head should get aligned properly.
  9. Now, place the above-mentioned sub-assembly on the Servo Motor’s Shaft.
  10. Optionally, you can put the Paper Cutout of the Tiger on the above Robot to get make it attractive!

Our assembly is complete.

Connecting Ultrasonic Sensor

Now we will connect the Ultrasonic Sensor to the Robot. The sensor and the robot have the following pins:

  1. Ultrasonic Sensor Pins:
    1. VCC
    2. GND
    3. Trig
    4. Echo
  2. Quarky Pins:
    1. GND
    2. V
    3. D1
    4. D2

We will start with connecting the ultrasonic sensor with Quarky using the 4 set wire provided in the kit. But, first, make the connection in the following way:

  • First, connect the VCC of the ultrasonic sensor with the V pin on the Quarky.
  • Connect the GND of the ultrasonic sensor with the Ground pin on the Quarky. 
  • Connect Trig of the ultrasonic sensor with D1 pin on the Quarky.
  • Finally, connect the Echo of the ultrasonic sensor with the D2 pin on the Quarky.

 

The logic of Obstacle Avoidance Robot

Let’s understand how exactly the robot works.

Quarky detects an obstacle first, then checks left and right, turning in that direction if the distance is larger than 30 cm. Let’s take a closer look at how it works.

  1. Go forward if there is no obstacle.
  2. If an obstacle comes at a defined distance. The ultrasonic sensor will detect the obstacle and stop.
  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.
  4. If there is no obstacle in a range of 30 cm at the 45-degrees angle. Quarky will take a right turn by 45 degrees.
  5. If an obstacle is detected on the right side at the 45-degrees angle. Change the angle to 135 degrees.
  6. If there is no obstacle in a range of 30 cm at the 135-degrees angle. Quarky will take a left turn by 45 degrees.
  7. If an obstacle is detected on the right side at the 135-degrees angle. Change the angle to 0 degrees.
  8. If there is no obstacle in a range of 30 cm at the 0-degrees angle. Quarky will take a right turn by 90 degrees.
  9. If an obstacle is detected on the right side at the 0-degrees angle. Change the angle to 180 degrees.
  10. If there is no obstacle in a range of 30 cm at the 180-degrees angle. Quarky will take a left turn by 90 degrees.
  11. If an obstacle is detected on the right side at a 180-degrees angle, then take the reverse.

Flowchart

Below is the flowchart representation of our logic. We will write a script for the robot in the next topic.

Coding Steps

Now it’s time to program our Obstacle Avoidance Robot! Follow the steps given below to make a script for the Obstacle Avoidance robot.

Initialize Robot

  1. Create two variables named Max, and Min from the Variables palette. 
  2. Drag and drop the when green flag clicked block from the Events palette.
  3. Then, add the set () to() block. Set to Min to 5.
  4. Duplicate this block and set Max to 30. So, we are initializing maximum and minimum distances of 30 cm and 5 cm.
  5. Create a block named – Initialize Robot. Place the block.
  6. Now, to initialize the pins go to the Sensors palette and add connect Ultrasonic ()  to trig (), echo()  below the Initialize Robot hat block.
  7. 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.
  8. Add wait () second below-set servo on () to () angle.

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 Min variable in the second space. This block will help compare the sensor value with the minimum distance.

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 delay of 0.5 seconds by introducing wait () seconds.
  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 Operators palette and put the ()>() block inside the if () then else block. 
  7. Drag and drop the get ultrasonic () distance block from the Sensorspalette within the first space of the ()>() block and 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 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 direction to 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 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,  speed as 50%.

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.

Finally, our robot is ready to run!

Testing

Run the robot and check if it is working properly.

Uploading the Code to Quarky

We can also upload the code to Quarky to make the robot run independently of PictoBlox. This we can do only on Laptop. Follow the steps:

  1. Switch to Upload Mode.
  2. Replace the when green flag clicked block with when Quarky starts up block.
  3. Upload the Code with the Upload Code button.

That’s it. Now your robot can work independently.

Conclusion

In conclusion, we have successfully assembled and programmed the Quarky Obstacle Avoidance Robot. We have learned how to connect the servo motor and ultrasonic sensor to the Quarky. We also coded the robot to scan the area around it and take the appropriate actions when it found an obstacle. Now, we can use this robot in various applications.

Table of Contents