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:
- Begin by detaching the Castor Wheel from the Robot using the Screw Driver.
- Now, fasten the B4 Plate and the Castor Wheel using the same bolt.
- 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.
- Then, snap the Ultrasonic Sensor into the B2 Plate.
- Push-fit the B2 Plate in the B7 Plate as shown. Make sure the parts are properly aligned with each other.
- Connect the Servo motor to the Quarky Servo Connector (Servo 1 port). There are two servo ports on Quarky. Always make sure that the brown wire is on your left side.
- Make the following code in PictoBlox to set the Servo Motor to 90 degrees:
- Run the script. The servo head should get aligned properly.
- Now, place the above-mentioned sub-assembly of B7 on the Servo Motor’s Shaft.
- 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:
- Ultrasonic Sensor Pins:
- VCC
- GND
- Trig
- Echo
- Quarky Pins:
- GND
- V
- D1
- 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 D2 pin on the Quarky.
- Finally, connect the Echo of the ultrasonic sensor with the D1 pin on the Quarky.
Note: Trig and Echo connections to D1 and D2 may vary according to your wire connections.
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.
- Go forward if there is no obstacle.
- If an obstacle comes at a defined distance. The ultrasonic sensor will detect the obstacle and stop.
- 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.
- 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.
- If an obstacle is detected on the right side at the 45-degrees angle. Change the angle to 135 degrees.
- 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.
- If an obstacle is detected on the right side at the 135-degrees angle. Change the angle to 0 degrees.
- 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.
- If an obstacle is detected on the right side at the 0-degrees angle. Change the angle to 180 degrees.
- 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.
- 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
- Create two variables named Max, and Min from the Variables palette.
- Drag and drop the when flag clicked block from the Events palette.
- Then, add the set () to() block. Set to Min to 5.
- Duplicate this block and set Max to 30. So, we are initializing maximum and minimum distances of 30 cm and 5 cm.
- Create a block named – Initialize Robot. Place the block.
- Now, to initialize the pins go to the Sensors palette and add connect Ultrasonic () to echo(), trig() below the Initialize Robot hat block.
- 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.
- Add wait () seconds below-set servo on () to () angle.
Detecting Obstacle
- Now, go to the Control palette and add a forever block below the Initialize Robot.
- After that, place the if () then block from the Control palette inside the forever block.
- Next, go to the Operators palette and put the ()<() block inside the space of the if () then block.
- 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
- Next, create a block custom block named Set Servo Angle with Delay() with Angle added as an input number.
- Put a set servo () to () angle block with the angle as Angle.
- After that add delay of 0.5 seconds by introducing wait () seconds.
- 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 comes within the minimum distance range.
- Now, we will check the maximum distance by adding if () then else block from the Control palette.
- Next, go to the Operators palette and put the ()>() block inside the if () then else block.
- Drag and drop the get ultrasonic () distance block from the Sensors palette 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.
- 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 direction as right, speed as 50%, and time as 0.6 seconds.
- 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.
- 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.
- 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.
- Lastly, place the go () at ()% speed block. From the dropdown, select the direction as forward, the speed as 50%.
Checking Extreme Conditions
- 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.
- 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.
- 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:
- Switch to Upload Mode.
- Replace the when flag clicked block with when Quarky starts up block.
- 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.