You have learned to use a number of Finch sensors, like the light sensors, that measure something about the environment around the Finch. The Finch also contains two sensors, called encoders, that measure information about the Finch itself. The encoders are part of the Finch wheels, and each encoder measures how far one of the wheels has turned. The setMove() and setTurn() methods use the encoders to determine when the Finch has completed the desired movement. In this lesson, you will learn to use the encoders directly to measure how far the Finch has moved or turned.
Lesson 12 – Encoders
Using the Encoders
The diameter of the Finch wheel is 5 cm. Write a function named getWheelDistance() that returns the distance in centimeters that a wheel has moved since the encoders were reset. This function should accept a string parameter that is equal to either ‘R’ or ‘L’. It should return the distance that the specified wheel has moved. Test your function with the code shown below.
Write a program to make that robot drive forward until it detects a black line (or modify the one you wrote in Lesson 9!). The program should report to the user how far it had to travel to find the line. Start your robot at several different distances from the line. How accurate are the robot’s measurements?
Turning with Encoders
You can also use the encoders to find out how far the robot has turned. This requires some geometry! Imagine that the left Finch wheel is stationary while the right wheel is moving. The Finch will move in a circle of radius w, where w is the width of the robot. The distance that the right wheel moves, which we call d, is an arc length along that circle. That arc length is the width times the angle 𝜃 that the robot has turned. That angle is in radians. We can convert it to degrees by multiplying it by the conversion factor 𝜋/180°. We can then solve for 𝜃, as shown below. This equation enables us to find out how far the Finch has turned based on the distance the right wheel has moved and the width of the robot, which is 10 cm.
Write a function to calculate the angle that the robot has turned, assuming that one wheel is stationary. The function should take a parameter that is the distance that the moving wheel has turned, and it should return the angle.
Use your function from Exercise 3 to write a program that makes the robot move in one complete circle with one wheel stationary and the other moving.
Hint: Your robot will be more accurate for low speeds.
The function you have written is actually even more useful. While we worked out the geometry with one wheel stationary, you can actually use this function for a more general situation. If the speeds of the right and left wheels are both positive but not equal, you can calculate the difference between the distance the faster wheel travels and the distance the slower wheel travels. If you call your function from Exercise 3 with this difference, it will give you the angle the robot has turned.
Write a function called drawCircle(). This function should accept two parameters, leftSpeed and rightSpeed. Both speeds should be greater than or equal to zero. The robot should use these two wheel speeds to turn in a circle and stop when it has completed the circle. What interesting patterns can you draw with this function?