LogoLogo
  • Introduction
  • Getting Started with Control Hub
    • Connect to the Robot Controller Console
    • Updating Wi-Fi Settings
    • Connecting Driver Station to Control Hub
    • Wiring Diagram
    • Next Steps
  • Getting Started with Driver Hub
  • Adding More Motors
    • SPARKmini Motor Controller
    • Adding an Expansion Hub
  • Troubleshooting the Control System
    • General Troubleshooting
    • Control Hub Troubleshooting
    • Driver Hub Troubleshooting
      • Driver Hub Battery Troubleshooting
    • Expansion Hub Troubleshooting
    • Status LED Blink Codes
  • System Overview
    • Control Hub Specifications
    • Expansion Hub Specifications
    • Driver Hub Specifications
    • Port Pinouts
    • Protection Features
    • Cables and Connectors
      • XT-30 - Power Cable
      • JST VH - Motor Power
      • JST PH - Sensors and RS485
    • Integrated Sensors
    • Dimensions and Important Component Locations
  • Updating and Managing
    • Managing Wi-Fi on the Control Hub
    • REV Hardware Client
    • Updating Firmware
      • Firmware Changelog
    • Updating Operating System
      • Control Hub Operating System Changelog
    • Updating Robot Controller Application
      • Updating Robot Controller Application via Android Studio
    • Updating the Driver Hub
      • Driver Hub OS - Change Log
    • Accessing Log Files
    • Android Studio - Deploying Code Wirelessly
  • Hello Robot - Intro to Blocks Programming
    • Welcome to Hello Robot!
    • Where to Program - Client vs. Browser
      • What is an OpMode?
    • Setting up a Configuration
      • Common Errors in Configuration
    • Using a Gamepad
    • Part 1: Tackling the Basics
      • Tackling the Basics Directory - Blocks
      • Creating an OpMode - Blocks
      • Programming Essentials
      • Programming Servos
        • Programming Servo Basics
        • Using a Gamepad with a Servo
        • Programming Servo Telemetry
      • Programming Motors
        • Programming Motors Basics
        • Programming a Motor with a Gamepad
        • Programming Motor Telemetry
      • Programming Touch Sensors
      • Programming Color Sensors
        • Color Sensor Telemetry
        • Detecting Color
    • Part 2: Robot Control
      • Robot Control Blocks Directory
      • Programming Drivetrain Motors
      • Arcade Style TeleOp - Blocks
        • Establishing Variables in Blocks
        • Motor Power vs. Robot Movement
        • Programming Arcade Drive
      • Arm Control - Blocks
        • Adding a Limit Switch
      • Robot Control Full Program
    • Part 3: Autonomous and Encoders
      • ElapsedTime - Blocks
        • ElapsedTime Setup
        • ElapsedTime Logic
        • ElapsedTime - Multiple Movements
      • Encoder Basics
      • Drivetrain Encoders - Blocks
        • Converting Encoder Ticks to a Distance
        • Moving to a Target Distance
        • Setting Velocity
        • Turning the Drivetrain Using RUN_TO_POSITION
      • Arm Control with Encoders - Blocks
        • Estimating the Position of the Arm
        • Calculating Target Position
        • Using Limits to Control Range of Motion
    • Part 4: Going Beyond!
      • Exploring Functions
      • Programming Mecanum - Simplified
      • Programming Mecanum - Refined
  • Hello Robot - Intro to OnBot Java Programming
    • Welcome to Hello Robot!
    • Where to Program - Client vs. Browser
      • What is an OpMode?
    • Setting up a Configuration
      • Common Errors in Configuration
    • Using a Gamepad
    • Part 1: Tackling the Basics
      • Tackling the Basics Directory - OnBot
      • Creating an OpMode - OnBot
      • Programming Essentials
      • Programming Servos
        • Programming Servo Basics
        • Using a Gamepad with a Servo
        • Programming Servo Telemetry
      • Programming Motors
        • Programming Motor Basics
        • Programming a Motor with a Gamepad
        • Programming Motor Telemetry
      • Programming Touch Sensors
    • Part 2: Robot Control
      • Robot Control OnBot Java Directory
      • Programming Drivetrain Motors
      • Arcade Style TeleOp - OnBot Java
        • Establishing Variables in OnBot Java
        • Motor Power vs. Robot Movement
        • Programming Arcade Drive
      • Arm Control - OnBot Java
        • Adding a Limit Switch
      • Robot Control Full Program
    • Part 3: Autonomous and Encoders
      • ElapsedTime - OnBot Java
        • ElapsedTime Setup
        • ElapsedTime Logic
        • ElapsedTime - Multiple Movements
      • Encoder Basics
      • Drivetrain Encoders - OnBot Java
        • Converting Encoder Ticks to a Distance
        • Moving to a Target Distance
        • Setting Velocity
        • Turning the Drivetrain Using RUN_TO_POSITION
      • Arm Control with Encoders - OnBot Java
        • Estimating the Position of the Arm
        • Calculating Target Position
        • Using Limits to Control Range of Motion
  • Sensors
    • Introduction to Sensors
    • Digital
    • Analog
    • I2C
      • IMU
        • Orientating the IMU
      • Adding an External IMU to your Hub
    • Encoders
      • REV Motor Encoders
      • Through Bore Encoder
    • Using 3rd Party Sensors
      • Sensor Compatibility Chart
  • Useful Links
    • REV DUO Build System
  • Legacy Documentation
    • Configuring Your Android Devices
    • Expansion Hub with Android Device Robot Controller
      • Driver Station and Robot Controller Pairing
      • Wiring Diagram
      • Configuration
    • REV Hub Interface Software
Powered by GitBook
On this page
  • Setting up the Drivetrain Encoders
  • RUN_TO_POSITION
  • Quick Check!
  • STOP_AND_RESET_ENCODERS
  • Setting up the whileLoop

Was this helpful?

Export as PDF
  1. Hello Robot - Intro to OnBot Java Programming
  2. Part 3: Autonomous and Encoders

Drivetrain Encoders - OnBot Java

Moving the motors to a specific position, using the encoders, removes any potential inaccuracies or inconsistencies from using Elapsed Time. The focus of this section is to move the robot to a target position using encoders.

Setting up the Drivetrain Encoders

For this tutorial, our OpMode is named HelloRobot_Encoder!

The OpMode structure below is simplified and only includes the necessary components needed to create the encoder based code.

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;

@Autonomous //sets the op mode as an autonomous op mode 

public class HelloRobot_Encoder extends LinearOpMode {
    private DcMotor leftmotor;
    private DcMotor rightmotor;
    
    @Override
    public void runOpMode() {
        leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
        rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
        
        // Wait for the game to start (driver presses PLAY)
        waitForStart();

        // run until the end of the match (driver presses STOP)
        while (opModeIsActive()){
        
        }
    }
}

In our example, we are addingrightmotor.setDirection(DcMotor.Direction.REVERSE); to the code as seen below:

public void runOpMode() {
        leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
        rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
        
        rightmotor.setDirection(DcMotor.Direction.REVERSE);
        
        waitForStart();

RUN_TO_POSITION

The first step is setting target position. To do so, add the lines leftmotor.setTargetPosition(1000); and rightmotor.setTargetPosition(1000); to the OpMode after the waitForStart(); command.

waitForStart();

leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);

while (opModeIsActive()){
        
        }

If we want our robot to travel a specific distance we will need to do a bit of math beforehand to calculate the TargetPosition. But for now let's start simple by setting the target position to 1000 ticks.

The next step is to set both motors to the RUN_TO_POSITIONmode. Add the lines leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);and rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);to your code, beneath the setTargetPosition code lines.

waitForStart();

leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);

leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);

while (opModeIsActive()){
        
        }

Order matters! The TargetPosition block must come before RUN_TO_POSITION mode is set or it will result in an error.

Add the lines to set the power of both motors to 80% of duty cycle.

waitForStart();

leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);

leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);

leftmotor.setPower(0.8);
rightmotor.setPower(0.8);

while (opModeIsActive()){
        
        }

Quick Check!

Build your OpMode and give it a test. What happens once you press play? What happens if you stop the program then start it again?

What happens when testing?

Likely your motors turned on when testing out the code to spin until they've reached the set position.

Some may have turned off once the position was reached, but you may also experience the motors twitching or making small adjustments in an attempt to reach the position. Then when starting the code again, the motor either continued twitching or did not move at all.

Recall we may need to reset our encoder to zero before running a program! The motor will continuously try to adjust until it hits the set position, but if it's already there it won't move!

Adjusting the power may help prevent the motor from overshooting the position and needing to repeatedly adjust.

STOP_AND_RESET_ENCODERS

For our demo code we will want to request our motors reset their encoders during the initialization process of the program.

Right before waitForStart(); we can add leftmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); and rightmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); to our OpMode.

public void runOpMode() {
        leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
        rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
        
        rightmotor.setDirection(DcMotor.Direction.REVERSE);
        
        leftmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        rightmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        
        waitForStart();

Setting up the whileLoop

Let's say we want our program to run only for however long it takes for the motors to reach designated position. Or maybe we intend for the robot to do something else after reaching the destination. For this we will need to edit our whileLoop block!

Recall that, within a linear OpMode, a whileLoop must always have the opModeIsActive() Boolean as a condition. This condition ensures that the whileLoop will terminate when the stop button is pressed.

To the whileLoop let's add the leftmotor.isBusy() and righmotor.isBusy()functions. This will check if the left motor and right motor are busy running to a target position. Once either motor reaches the target position the program will stop.

while (opModeIsActive() && (leftmotor.isBusy() && rightmotor.isBusy())) {

}

Build your OpMode and give it a try!

As soon as the motors hit the desired position the program will end instead of continuously run in the event they do not perfectly hit the position.

Right now the whileLoop is waiting for either motor to reach the target. There may be occasions when you want to wait for both motors to reach their target position. In this case the following loop can be used:

while (opModeIsActive() && (leftmotor.isBusy() || rightmotor.isBusy()))

PreviousEncoder BasicsNextConverting Encoder Ticks to a Distance

Last updated 4 months ago

Was this helpful?

Before diving in too far, recall that for certain , like the , one of the motors needs to be reversed as the motors are mirrored.

As introduced in , using RUN_TO_POSITION mode requires a three step process.

As mentioned, normally there would be more math involved to help determine how fast the motors should move to reach the desired position. But for testing purposes, we are going to start by keeping it simple! Since the setPower function was covered in and will communicate to the system what relative speed (or in this case duty cycle) is needed to get to the target, this can be used in the place of setVelocity for now.

drivetrains
Class Bot V2
Using Encoders
previous sections