Loops, Conditional statements, Lists, Variables, Functions/Modules, Encoders, Distance sensor
Distance sensors, or ultrasonic sensors, are used for more than just obstacle detection, like in this maze solver activity. It is also used for vehicle detection for car wash and automotive assembly, as well as object detection on conveyor belts in factories. For navigation purposes, they are used in autonomous cleaning robots that avoid obstacles.
Create a maze with at least two turns. You can mark a maze on the ground with masking tape. Program a navigation sequence using only setMove() and setTurn() methods for Python, or only move and turn blocks for Snap!. Iterate until your robot can get from start to finish without running into any walls.
When this is accomplished, try solving the maze again, but using one Finch as a remote control to control the second one to move through the maze. Click here to learn how to connect multiple Finches with Python and here to connect with Snap!.
2. Use if blocks within the repeat until block that moves Finch B in a different way depending on the orientation of Finch A. When Finch A's beak is facing down, Finch B will move forward. When Finch A's beak is facing up, Finch B will move backwards. When Finch A is tilted to the left, Finch B will turn left, and when Finch A is tilted right, Finch B will turn right. When Finch A is not in any of these orientations, Finch B will stop moving.
3. Add a Finch stop block after the repeat until block to stop Finch B's motion.
1. bird1 will the 'remote control' and bird2 will be the Finch that moves through the maze.
2. Use a while loop that breaks when button A is pressed.
3. Use an if-elif-else statement with a total of 3 elifs that trigger depending on the orientation of bird1. When bird1's beak is facing down, bird2 will move forward. When bird1's beak is facing up, bird2 will move backwards. When bird1 is tilted to the left, bird2 will turn left, and when bird1 is tilted right, bird2 will turn right. When bird1 is not in any of these orientations, bird2 will stop moving.
4. When the while loop breaks, use the bird2.stopAll() function to stop bird2's motion.
Create a maze with at least two turns, then use cardboard to build maze walls for the distance sensor to detect. These mazes should not have branches or forks in them if the Finch is to solve it by itself.
Program your Finch to solve a maze automatically! The Finch should:
· Move forward until it finds an obstacle.
· Turn right or left by 90 degrees if an obstacle is detected.
· If, after it has completed its first turn, it still sees an obstacle, then the Finch should turn 180 degrees. If it does not see an obstacle, it should move forward.
· Stop automatically at the end of the maze. Use a black line to indicate the end of the maze and detect the line with the line sensors.
Once your maze solver is working, rewrite it so that the Finch can determine how far it had to travel to solve the maze:
· Each time the Finch moves forward, it should reset the encoders, measure the distance it moves in centimetres, and add it to a list named distanceList.
· The program should move through distanceList and add each measurement to a variable called distanceSum. Then the program should display distanceSum (the total distance travelled) to the user.
1. Create a block called getMeanWheelDistance. It should return the mean values of the measured distance travelled by the two Finch wheels, in centimetres. Distance travelled by the Finch in one wheel rotation is equal to 5π cm.
3. Use a Finch Reset Encoders block.
5. Within the repeat until block insert a if-else block.
6. The if block will trigger when the Finch distance sensor detects that the Finch is still far from the wall. The specific distance will depend on your maze. The Finch will move straight as long as no obstacle is detected.
7. Should the Finch detect an obstacle, it will come to a stop. Add getMeanWheelDistance to distanceList.
8. Make the Finch turn 90 degrees to either right or left. If an obstacle is detected, the Finch will turn 180 degrees.
9. The last block within the else block should be the Finch Reset Encoders block.
10. The Finch is to come to a stop after the repeat until block stops. Add getMeanWheelDistance to distanceList.
11. Add all the measurements in distanceList to distanceSum.
12. Use the say block to print the value of distanceSum to the screen.
1. Declare two variables distancethreshold and lightthreshold. These 2 variables are the distance between the Finch and the surrounding maze walls, and the threshold below which the Finch detects as a black line respectively. The values of these 2 variables can be adjusted depending on your maze and your lighting conditions.
2. Make an empty list called distanceList.
3. Define a function append_distanceList() that appends to distanceList the mean of the distance measurements of both Finch wheels, in centimetres. Distance travelled by the Finch in one wheel rotation is equal to 5π cm.
4. Use the bird.resetEncoders() function to reset the value of the Finch wheel encoders.
5. Use a while loop that breaks when the Finch detects a black line. This black line will be the maze exit.
6. Use an if-else statement within the while loop. The if statement is triggered when the distance sensed by the distance sensor is larger than the distancethreshold value, and should make the Finch constantly move forwards.
7. Under the else statement, the Finch should come to a stop as it has detected a wall directly in front of it. Then, use the append_distanceList() function to add to the list the distance travelled by the Finch.
8. Afterwards, turn the Finch 90 degrees to the right or left. If the Finch still detects an obstacle, it should turn 180 degrees. The last part of the else statement should reset the Finch encoders.
9. When the while loop breaks, stop the Finch using bird.stop() and use the append_distanceList() function for the last time. Print the sum of the measurements in distanceList to the screen using "print(sum(distanceList))".