FTC:Encoders

From Project Robotica
Jump to navigation Jump to search

Many of the motors used in FTC have encoders preinstalled and ready to use. Encoders simply track a motor’s rotation, which can be useful to create a more accurate program (especially in autonomous).


General

Encoders are measured in “ticks.” Think of ticks as degrees. Every motor has a different amount of ticks per rotation. It’s useful to know the ticks per rotation when working with encoders. The specific number of ticks per rotation can be found on the website where the motor was purchased.

Software

In the FTC framework, there are four main encoder settings a team can use:[1]

  • RUN_WITHOUT_ENCODERS
  • RUN_USING_ENCODER
  • RUN_TO_POSITION
  • STOP_AND_RESET_ENCODER

RUN_WITHOUT_ENCODERS is when the motor does not use an encoder but runs using speed and time. It is commonly used by newer teams because it is simple to code; however, it is less reliable than using encoders due to the unpredictability of a robot’s battery power and friction. Battery power and friction can vary from time to time, making the overall code much less reliable.

RUN_USING_ENCODER is when the motor keeps a constant speed that will not be changed by battery power. By monitoring how many ticks pass per second, the motor is able to keep the same speed for a set amount of time.

RUN_TO_POSITION focuses on the position of the motor’s rotation. This setting can be used to set an encoder value for the motor to rotate to, and then causes the motor to rotate to that encoder value.

STOP_AND_RESET_ENCODER resets the motor encoder value to 0. This does not make the motor move; it just makes whatever encoder position the motor is currently at the new 0. This should be used at the beginning and end of a sequence of code.

Sample Code

Here are excerpts of FTC Team 19566 Coding the Cosmics’ autonomous code for the 2021-2022 season, showing how to use the different types of encoders:

RUN_WITHOUT_ENCODERS

//Robot moves forwards- to move to warehouse
robot.rightMotor.setPower(0.5);
robot.leftMotor.setPower(0.5);
sleep(750);
//this makes the motors move at 0.5 speed for 750 milliseconds (0.75 seconds)


//Robot stops- to park in warehouse
robot.rightMotor.setPower(0);
robot.leftMotor.setPower(0);
sleep(30000);
//this makes the motors stop (setPower(0))for 30000 milliseconds (30 seconds)

RUN_TO_POSITION[2]

// Reset the encoder
//start
leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);


//1st - turn towards carousel
leftMotor.setTargetPosition(1250);
rightMotor.setTargetPosition(150);
//sets the target position to 1250 for the left motor and 150 right just takes a lot of experimentation :)

// Switch to RUN_TO_POSITION mode
leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//this is switching the encoder mode to RUN_TO_POSITION

//SPEED
leftMotor.setPower(TURN_SPEED);
rightMotor.setPower(TURN_SPEED);
//this tells the motors how fast to go. You can assign variables at the beginning (see extra code at the bottom) of the code or just put a number in.


while (leftMotor.isBusy() && rightMotor.isBusy()) {
   leftMotor.setPower(TURN_SPEED);
   rightMotor.setPower(TURN_SPEED);
}
leftMotor.setPower(0);
rightMotor.setPower(0);
//this makes the motors continue to run until they reach the correct encoder value. 

//from there, we repeat the process for each step of code

Extra code for TURN_SPEED

static final double FORWARD_SPEED = 0.25; 
static final double TURN_SPEED = 0.5;
//this just defines the speed of the code

RUN_USING_ENCODER

leftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
rightMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
//the motor is told to use the RUN_USING_ENCODER mode
//Make sure to be using DcMotorEx, not DcMotor

//Move forward slowly
leftMotor.setVelocity(200);
rightMotor.setVelocity(200);
//The motors are told the setVelocity is 200 ticks per second

sleep(2000);
//the motors are told to stop after 2 seconds

Troubleshooting

The most common problem with RUN_USING_ENCODER is not setting the DC Motor class to DcMotorEx.

Incorrect

leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);

leftMotor.setVelocity(200);
rightMotor.setVelocity(200);
//If the motor class (DcMotor in the first two lines) is set to DcMotor, setVelocity will not work.

Correct

leftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
rightMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);

leftMotor.setVelocity(200);
rightMotor.setVelocity(200);
//The motor class (DcMotorEx in the first two lines) must be set to DcMotorEx otherwise setVelocity will not work.

Seeking Help

There are fabulous people on Reddit and Discord that would love to help! Look up any FTC Reddit or Discord channel!

Additional Resources

References

  1. ">"Motor Encoder Guide". docs.google.com. Retrieved 15 January 2022.
  2. ">"FTC Tutorials: Encoders". youtube.com. Retrieved 15 January 2022.