# Encoders and Motor Gearing

The S3 robot has internal encoder sensors and pre-programmed logic to keep the robot going exactly where you programmed it to go. Without encoders, it would be difficult to make the S3 travel in a straight line as pairs of motors are never precisely matched on their own.

Encoder sensors monitor *encoder ticks* (explained further, below, in Did You Know?) that the S3 uses to adjust left and right motor speeds as it performs maneuvers. These sensors are what allow the robot to precisely match the speed and/or distances as defined in your program. If the surface you are working on is smooth, such as a desktop, encoders will enable the S3 to return to the programmed starting point and do precise *dead reckoning navigation*.

### Did You Know?

Encoders provide a *closed loop* control system where the drive speed may be controlled and measured continually during operation. The S3’s motors have a gearbox on one side and an optical encoder on the other. The optical encoder has four slots which are counted by a change of state in an infrared slot sensor. With the gear reduction, a full rotation of the wheel has 507.4 encoder transitions or *encoder ticks*. The S3 robot’s internal encoders are mounted on the printed circuit board (PCB), connected to the motor shaft. If you'd like, you can carefully open up your S3 and take a look – but be sure to save all hardware you remove so you can properly seal the shell when you are done.

/

Wheel Circumference and Encoder Counts

The S3 has 507.4 encoder counts per complete wheel revolution (0.491 mm / count). The diameter of the wheel is 3.15” (80 mm).

*Circumference = pi * diameter (C = π d)*

To determine how far the S3 travels when the wheel turns one complete rotation you could use the following formula to determine the circumference of the wheel:

*C = π d*

In this equation “*C*” represents the *circumference* of the wheel and “*d*” is its *diameter*. The number *π* is the mathematical constant pi, commonly approximated to 3.14159. Using only two significant digits, one which was obtained by measuring the wheel, *C* is equal to (in imperial and metric):

*C = (3.14)(3.15 in)
C = 9.89 in*

or

*C = (3.14)(80 mm)
C = 251.2 mm*

# Drive Distance Block

The **drive distance** block lets you program the S3 to move an exact distance.

- Choose your units: millimeters (mm), centimeters (cm), inches (in), tenths of an inch (1/10 in) or encoder counts.
- Enter the number of units to move each wheel for the left motor distance and right motor distance.
- Enter a top speed (1 to 100%). Maximum, or top, speed is applied to both wheels when the distance is the same value for each motor. If you are using this command for an arc or curve, the S3 will apply the top speed only to the wheel moving the furthest.

**Remember: **Your S3's speed is affected by the slope and material of the surface it is driving on. Refer back to the previous tutorial if you need a reminder about the effect of real world limitations on motion calculations.

The **drive distance** block is a time blocked command. The S3 must finish this command before continuing on to other blocks. Similar to using the **drive speed** block, the time required to reach your destination is a function of the speed you choose.

*time (t ) = distance (d) / rate (r)*

How long does it take to travel a certain distance? You are familiar with the equation *d = rt* from the prior tutorial where you used the **drive speed** block with rate and time, solving for distance (*d*). In this example, you have distance (*d*) and rate (*r*) and can solve the same equation for time (t).

An S3 robot moving a distance (*d*) of 100 cm at 100% speed (*r*) should take approximately twice as long (*t*) as an S3 moving 100 cm at 50% speed.

- Distance (
*d*) = 100 cm. Keep in mind that 100 cm = 1 m (the units we use in our equation must be consistent). - Rate (
*r*) at 100% speed is about 0.18 m/s (meters/second), and 50% speed is about 0.09 m/s.

Putting these values in the *t = d/r *formula we derived above demonstrates this point.

Going half the speed takes about twice as long as full speed, but you wind up at exactly the same place.

# Try This

- Program the S3 to move forward one meter (1 m, or 100 cm) at 100% speed and backward one meter (1 m, or 100 cm) at 50% speed. Make the LEDs green while moving forward and red while backing up to the starting point.

Your blocks might look like this:

- Determine how long it takes for the S3 to drive this distance in two different ways. First, calculate the total time:

*total time = t(first drive distance block) + t(second drive distance block)*

For the first **drive distance** block the S3 moves 1 m at 100% speed:

*time = distance / rate time
time = (1 m) / (0.18 m/s)
time = 5.56 s*

For the second **drive distance** block the S3 moves 1 m at 50% speed:

*time = distance / rate time
time = (1 m) / (0.09 m/s)
time = 11.11 s*

*total time = 5.56 s + 11.11 s = 16.67 s*

- Use a stopwatch or timer to verify that the calculated time seems about right. Were your timed results a little different than we predicted in the above calculations?

# Your Turn

- Using the
**drive distance**blocks, create a program which does the following:- Turns all three LEDs to amber.
- Drives forward 75 cm at 25% speed.
- Turns all three LEDs to green.
- Drives forward 25 cm at 100% speed.
- Turns all LEDs to red.
- Drives backward to the starting point at 50% speed.

- Calculate and add up the run-time for each of the program elements above. What is the total time you expect will be required for the robot to complete the run?

**Hint: **100% speed is 0.18 m/s and 50% speed is 0.09 m/s. 75% speed is approximately halfway between 50% and 100%.

**Hint: **You will also need to calculate the speed(25%) which is halfway between speed(0%) and speed(50%).

- Using a stopwatch or timer, verify what you calculated above.
- Do your findings agree with your predictions? Why, or why not?
**Hint:**we learned about real-world limitations in the last tutorial.

- Do your findings agree with your predictions? Why, or why not?
- Using the
**drive distance**block with encoder counts selected for distance, write a program which does the following:- Drives the S3 forward at a distance equal to one full wheel circumference.
- Drives the S3 forward at a distance equal to twice the wheel circumference.

- What is the smallest distance you can use in the drive distance command (mm or encoder counts)?

**Hint:** Re-read the "Did You Know?" section above for an explanation of encoder counts/ticks.

*Special thanks to Parallax friend Whit Stodghill for his assistance in writing, editing, and testing material for these S3 tutorials.*