Lesson 12 – Encoders

Python Concepts

Review

Teacher Materials

Get Access

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.

Using the Encoders

Before the Finch makes a movement that you want to measure, you need to reset the encoders. You do this by calling the resetEncoders() method. Resetting the encoders sets the value of both encoders to 0.

bird.resetEncoders()

After you have reset them, you can read the value of each encoder using the getEncoder() method. This method returns the number of wheel rotations that the wheel has moved since it was last reset. Forward movements are positive, and backward movements are negative.

bird.getEncoder('L') 
The getEncoder() function takes one parameter that must be either 'L' or 'R' to indicate the left or right encoder.

For example, the code below measures how far each wheel turns if the robot moves for 1 second with both wheels at 50% speed. First, the encoders are reset. Then the robot moves. Finally, the encoders are used to report how many wheel rotations each wheel has moved. 

bird.resetEncoders() # Set encoders to 0 
bird.setMotors(50,50) # Move forward for 1 s 
sleep(1) 
bird.stop() 
# Print how far each wheel moved 
print('Left Wheel Rotations: ', bird.getEncoder('L')) 
print('Right Wheel Rotations: ', bird.getEncoder('R'))

Exercise 1

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.

bird.resetEncoders() # Set encoders to 0 
bird.setMotors(50,50) # Move forward for 1 s 
sleep(1) 
bird.stop() 
# Print how far each wheel moved 
print('Left Wheel Distance: ', getWheelDistance('L')) 
print('Right Wheel Distance: ', getWheelDistance('R'))

Exercise 2

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. 

This is a diagram showing the Finch moving on a circle of radius w, where w is the width of the Finch. The right Finch wheel is moving, while the left Finch wheel is stationary at the center of the circle. The arc length d that the right wheel travels as the Finch turns through an angle theta is given by the equation d = w * (theta in radians). This equation is equivalent to d = (pi*w*theta in degrees)/(180). Solving for theta in degrees, theta = (180*d)/(pi*w)

Exercise 3

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. 

Exercise 4

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.

Exercise 5

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?

Back to Top