[FTC]: FTC Android Studio java encoder help.


Hey, me and my buddy are first time FTC mentors and do not understand how to program the encoders in autonomous. Any examples we find are coded in different libraries or use different Op Modes. I anyone can point us in the right direction or give us tips, it would be much appreciated.


This is our teams encoder test program for 2 wheels. If using Mencanum this might not work. We use 2 main methods with encoders, one being saying a distance to go, which lets the motors stop automatically. The second way to use an encoder is to track it and to look at the value to stop your self.

In this program we set a distance and let the motors stop automatically. For Mencanum wheels I would recommend tracking the values on one wheel.
//This is the Hardware base, in this we show we connected the hardware
// to our program.
public class HardwareTestingBase
/* Public OpMode members. */
public DcMotor left_drive = null;
public DcMotor right_drive = null;
public NormalizedColorSensor colorSensor = null;

/* local OpMode members. */
HardwareMap hardwareMap           =  null;

private static final double     COUNTS_PER_MOTOR_REV    = 537.6 ;  // eg: Countable events per revolution of Output shaft
private static final double     DRIVE_GEAR_REDUCTION    = 1.0 ;     // This is < 1.0 if geared UP
private static final double     WHEEL_DIAMETER_INCHES   = 4.0 ;     // For figuring circumference
public static final double     COUNTS_PER_INCH         = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
        (WHEEL_DIAMETER_INCHES * 3.1415);

private ElapsedTime period  = new ElapsedTime();

/* Constructor */
public HardwareTestingBase(){


/* Initialize standard Hardware interfaces */
public void init(HardwareMap ahwMap) {
    // Save reference to Hardware map
    hardwareMap = ahwMap;

    // Define and Initialize Motors
    left_drive  = hardwareMap.get(DcMotor.class, "left");
    right_drive = hardwareMap.get(DcMotor.class, "right");

    left_drive.setDirection(DcMotor.Direction.FORWARD); // Set to REVERSE if using AndyMark motors
    right_drive.setDirection(DcMotor.Direction.REVERSE);// Set to FORWARD if using AndyMark motors

    // Get a reference to our sensor object.
    colorSensor = hardwareMap.get(NormalizedColorSensor.class, "colorsensor");

    // Set all motors to zero power

    // Set all motors to run without encoders.
    // May want to use RUN_USING_ENCODERS if encoders are installed.

//This is the encoder program!!!
//In this program we use a state machine (because our mentor made us! But now it is helpful when running things in parallel).
// This program was based off of the sample code by the sample program
package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.Range;

@Autonomous(name=“TestingBase: Encoder Iterative Test”, group=“TestingBase”)

public class TestingBaseEncoder_Iterative extends OpMode {

/* Declare OpMode members. */
HardwareTestingBase robot = new HardwareTestingBase(); // use the class created to define a Pushbot's hardware

static final double FORWARD_SPEED = 0.1;
static final double TURN_SPEED = 0.25;

enum RobotState
RobotState state = RobotState.Setup;

 * Code to run ONCE when the driver hits INIT
public void init() {
    int newLeftTarget;
    int newRightTarget;

    /* Initialize the hardware variables.
     * The init() method of the hardware class does all the work here

    // Send telemetry message to signify robot waiting;
    telemetry.addData("Say", "Hello Driver");    //

 * Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
public void init_loop() {

public void start() {

 * Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
public void loop() {
    telemetry.addData("Current State", state.toString());

    switch (state)
        case Setup:
            encoderDrive_Start(FORWARD_SPEED, 12, 12);  // S1: Forward 47 Inches with 5 Sec timeout
            state = RobotState.Start;

        case Start:
            if (encoderDrive_IsDone()) {
                state = RobotState.Finish;

        case Finish:
            state = RobotState.Done;

 * Code to run ONCE after the driver hits STOP
public void stop() {

public void encoderDrive_Start(double speed,
                               double leftInches, double rightInches) {

    int newLeftTarget;
    int newRightTarget;

    // Determine new target position, and pass to motor controller
    newLeftTarget = robot.left_drive.getCurrentPosition() + (int) (leftInches * HardwareTestingBase.COUNTS_PER_INCH);
    newRightTarget = robot.right_drive.getCurrentPosition() + (int) (rightInches * HardwareTestingBase.COUNTS_PER_INCH);

    // Turn On RUN_TO_POSITION

    // reset the timeout time and start motion.

public boolean encoderDrive_IsDone() {
    return !(robot.left_drive.isBusy() && robot.right_drive.isBusy());

public void encoderDrive_Complete() {
    // Stop all motion;

    // Turn off RUN_TO_POSITION

// I hope this helps!


Or go to TestingBaseEncoder



and here is the sample code from ftc



Noticed the titles were not correct…