The block sets the head servo motor angle to the specified angle.
Explore the surroundings with our obstacle avoidance Mars Rover that uses an ultrasonic sensor to detect and avoid obstacles. Learn how the robot moves, detects obstacles, and navigates its way through them.
This project of obstacle avoidance is for a robot that will move around and look for obstacles. It uses an ultrasonic sensor to measure the distance. If the distance is less than 20 cm, it will stop and look in both directions to see if it can move forward. If it can, it will turn left or right. If not, it will make a U-turn.
Logic
- This code is making a robot move around and explore its surroundings. It has an ultrasonic sensor that can measure the distance between objects.
- We will first initialize the servos of the Mars Rover with the block “Set head pins()”.
- Then we will make all the servos rotate to 90 degrees if they are not initialized.
- Thereafter we will initialize the ultrasonic sensors and define the minimum and maximum distance variables.
- The main logic of the code is that it first checks whether the distance is less than the minimum distance. If it is, the head servo will move to 45 degrees and check again if the distance is greater than the maximum distance, hence moving in the right direction.
- The robot with the help of the head servo, will check the distance for the conditions 90 degrees, 45 degrees, 135 degrees, 0 degrees and 180 degrees in the same order as stated.
- Whenever the distance measured will be less than minimum distance the head servo will change the direction to the next set of degree to check distance.
- In the last case scenario where all the angles contain obstacles as such, in that case the robot will change its direction to reverse by rotating to 180 degrees. By this way the robot will be able to navigate its own way through each and every obstacles.
Code:
Main Functions:
Final Main Logic:
Output
Read More