Autonomous Encoder Based Movement
Time based movement (turning the motors on and waiting some amount of time) is not very precise. The distance travelled can vary depending on the current voltage of the robot’s battery. Encoders are a sensor that makes robot movements more repeatable and does not depend on the the battery voltage. If the robot has Mecanum wheels, encoders are less reliable.
This is a tutorial on creating FIRST Tech Challenge (FTC) autonomous programs for the CENTERSTAGE game.
Visit the FTC docs page on Creating Op Modes in blocks if you need help getting started and to create your first driver controlled program.
Encoders can be used to reliably place the purple pixel on a spike mark.
Centerstage Program Tutorials
Encoders
Encoders are a type of sensor that detect rotations of the motor axle. They’re usually quite precise, with over a thousand encoder ‘ticks’ per single rotation of the axle. Different motors have different specifications, the AndyMark NeveRest Classic 40 Gearmotor says it has 1120 ticks per revolution. With encoders, you can set them to report the current position, or set a target position, turn the motor on and the motor will spin until it hits that target position.
Attach an encoder cable to the motor and insert the other end into the encoder port that matches the motor power port for that motor. e.g. If the motor power is plugged into port 0, the encoder cable for that motor should be plugged into the encoder connection for port 0.
The SDK includes a Java program that demonstrates using encoders, but there is no corresponding Blocks program. Let’s create a similar program in Blocks.
The blocks that work with encoders are found under Actuators and DcMotor. They are:
- Set Mode RunMode – is used to reset the encoder(s) back to zero, or change the mode used by the encoder. Also used to turn the motors on when using encoders. There is a dual motor version of this block. The various encoder modes are:
- STOP_AND_RESET_ENCODER – this zeros out the current encoder position and stops anything running.
Note: after an op mode stops the motor encoders still retain their current position, so when you start an op mode you should normally reset all your motor encoders in your initialization section. - RUN_WITHOUT_ENCODER – sets the mode to run without encoder (using time and power power)
- RUN_USING_ENCODER – sets the mode to run using an encoder
- RUN_TO_POSITION – starts the motor turning until it reaches the target position
- STOP_AND_RESET_ENCODER – this zeros out the current encoder position and stops anything running.
- Set Target Position – is used to set the target position in encoder ‘ticks’. There is a dual motor version of this block.
- Current Position – get the current encoder reading
Encoder Math
Let’s do a little math to determine how many encoder ticks it will take to drive 48 inches.
- 1120 ticks per revolution – for a NeveRest Classic 40 Gearmotor
- wheel size: Tetrix 4″ wheels
- C=2πr Circumference equals 2 times Pi times the Radius.
- or C=πd Circumference equals Pi times the diameter
- our diameter = 4″, π = 3.14159
- C = 3.14159 x 4″ = 12.566″ – this is the distance the robot should move if we turn the axle one revolution.
We can set up a little equation that relates distance to encoder ticks.
12.566" x ______ = ___ 1120 ticks y
If the robot moves 12.566 inches for every 1120 ticks, and the robot needs to move 48″, how many ticks does it need?
Let’s plug x=48 into the equation above and then re-write the equation to solve for y.
y = 1120 x 48 _________ 12.566
y = 4278 ticks (we can round to the nearest whole number).
Let’s set our target position to 4278 encoder ticks and the robot should move 48 inches.
Test Encoders Blocks Program
Create a new Blocks autonomous program and call it TestEncoders. It will turn the motors on, drive for 48 inches, and stop. Set a fairly low power level. With a higher power level, the robot didn’t seem to stop at the target distance. Perhaps the wheels skid as it stops.
Make a note of where your robot starts, say at the field perimeter wall, or a tape mark on the floor. Then run the program and then measure how far it actually drives.
At first the robot drove in a noticeable curve. It turned out that one wheel was actually 3 and 7/8″ in diameter and the other was 4″. The robot drove straight, even if it drove too far.
If you find your robot doesn’t move as you expect, double check ALL of your numbers. Maybe you don’t have the correct ticks per revolution for your motor, double check the specification sheet for that motor. Maybe you’ve made a math mistake, or maybe you have a hardware problem like a wheel that rubs on the chassis or you require the Logic Level Converter (if using old AndyMark motors).
Plus or minus one quarter inch should be good enough. You don’t need to be super accurate here. Driving by encoders is more accurate than time-based movement, but it still has problems like wheel slippage (especially when turning) so movement will never be totally accurate.
If everything checks out and the robot still doesn’t drive the expected distance, you can adjust the encoder ticks per revolution number you are using until you get the robot to move the expected distance. For this robot, reducing the number of ticks per revolution solved the problem. Setting the ticks per revolution to 1100 instead of 1120 worked.
Using 1100 encoder ticks to get 12.556 inches of movement.
y = 1100/12.566×48 = 4202 ticks
Set the target position to 4202 to get 48″ of movement.
Next Steps
Experiment with this program to see how driving with encoders works.
- Try setting a different encoder target for each motor. Note: you’ll need to change the isBusy AND isBusy logic test to an OR test.
- Try setting one motor distance to 0, the robot should turn with one wheel not moving.
- What happens if the motor power levels are not the same.
- Try setting one motor to positive value and the other to a negative value. The robot will turn, but it will not spin in place because the driving wheels are not centered for this robot.
More information about using encoders can be found on the FTC Docs encoders page.
Now the robot moves using encoders and they are working correctly, the next step is to create the RobotAutoDriveByEncoder_Blocks program. This will allow us to drive by specifying how far we want to drive and the program will calculate the target encoder ticks.
Note: as of the 2024 INTO THE DEEP season RobotAutoDriveByEncoder is now included as Blocks sample program that you can just copy when creating a new Blocks program.
Getting Help
It is often possible to use Google (or other search engine) to get help or solve problems. There are lots of resources online. If you’re still stuck you can ask for help here.