Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Creating Encoder Objects
  • Setting Encoder Distances
  • Encoder Config Stuff
  • Accessing Encoder Values
  • Challenge: The goDistance() Method
  1. Romi Curriculum- Timed Base
  2. Cool stuff i will rename this category later

Encoders

Encoders detect how much a robot travelled depending on how fast the motor spins.

Creating Encoder Objects

Parameters:

  • 2 integers (int) -> Encoder objects have 2 IDs unlike Spark objects, these 2 integer parameters refer to the 2 IDs respectively.

The Encoder IDs act the same as the Spark IDs helping differentiate the encoders from one another. Luckily, just like the Spark motors, the encoders have already been declared in the RomiDrivetrain class. Here is the code of the Encoder objects being declared.

// The Romi has onboard encoders that are hardcoded
// to use DIO pins 4/5 and 6/7 for the left and right
private final Encoder m_leftEncoder = new Encoder(4, 5);
private final Encoder m_rightEncoder = new Encoder(6, 7);

Setting Encoder Distances

If we want to make encoders return the distance they move in inches, centimeters, or even meters, we have to make sure the encoder knows how much distance it covers based on how many degrees the motor rotates. To do this, we can use the formula of the circumference of a circle C=πdC = πdC=πd with ddd being the diameter of the circle (or in our case, the wheel) in inches. Then in the parameter divide it by the pulse per revolution. Pulses vary from manufacturer to manufacturer, but in short, encoders detect a certain amount of pulses per revolution and those pulses help encoders detect motor movement. The RomiDrivetrain class already does this for us making our distance in inches. Here is the code the template used to declare it.

private static final double kCountsPerRevolution = 1440.0;
private static final double kWheelDiameterInch = 2.75591; // 70 mm
m_leftEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);
m_rightEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);

Encoder Config Stuff

There are other configurations on an encoder, such as its max value and whether it is inverted or not. Here are the method syntaxes below:

// Configures the encoder to consider itself stopped after .1 seconds
encoder.setMaxPeriod(0.1);

// Configures the encoder to consider itself stopped when its rate is below 10
encoder.setMinRate(10);

// Reverses the direction of the encoder
encoder.setReverseDirection(true);

// Can be between 1 and 127 samples
encoder.setSamplesToAverage(5);

//resets the encoder distance to 0 (can be used at any time in code)
encoder.reset();

Accessing Encoder Values

After configuring the encoder, you can access values from the encoder giving you data that can be used in your code.

Getting encoder distance

the most useful piece of data from an encoder is the distance traveled by the motor. This is accessed through the get getDistance() method in the encoder class. This method returns a double and has no parameters. Here is an example with both the m_leftEncoder and m_rightEncoder:

double leftDistance = m_leftEncoder.getDistance()
double rightDistance = m_rightEncoder.getDistance();

Their also other pieces of data that encoders can return such as the rate of the motor and if the motor has stopped or not. Here are the method syntaxes below:

/*Gets the current rate/speed of the 
motor in (set distance unit) per second*/
double rate = vencoder.getRate();

// Gets whether the encoder is stopped
boolean = encoder.getStopped();

// Gets the current period of the encoder
double period = encoder.getPeriod();

If you want to know more about periods and pulses check the link below.


Challenge: The goDistance() Method

Now that you know how to use an encoder, let's make a new method called goDistance at the bottom of the RomiDrivetrain class.

Conditions:

After you are done making a method that you think works compare it with the answer code below.

Answer code
/*Example goDistance method (parameter names can vary
and you can use a different encoder objects)*/
public void goDistance(double speed, double distance){
    if(distance <= m_rightEncoder.getDistance()){
        go(speed,speed);
    }
    else{
        go(0,0);
    }
}

Before using the method, remember to reset the encoder values.

PreviousExternal ControllersNextCommand Based Code

Last updated 2 months ago

🕑
https://www.motioncontroltips.com/what-are-pulses-per-revolution-for-encoders/