Encoders

Encoders are devices that measure the amount a motor rotates. Luckily for us, the Spark motors have built-in encoders!

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 function exactly like SparkIDs- they are here to differentiate Encoder objects from each other. Luckily for you, like the Spark objects, the Encoders have already been referenced 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);

Encoder Configuration

You might find something like this in the RomiDrivetrain.

Encoders, every so often, update their positions, so the user can do important calculations involving them. Every time the encoder does this, it is called a pulse. Different encoder brands set their encoders to get pulses

An example of something we can do with pulses is finding out the distance an encoder travels.

If we want to make encoders return the distance they move in other units, such as inches and meters, we'll have to calculate how much a point on the edge of the wheel would move when the Spark's wheel does a full rotation. Then, divide it by the number of revolutions every pulse.

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);

To do this, we can just use the formula of the circumference, C=πdC = πd, using the diameter of the wheel as d. Then, we divide it by the number of pulses every revolution.


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.

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


Challenge: The moveDistance() Method

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

Conditions:

Hint #1

You'll only need to use one of the two encoders to calculate the distance. Having two would end up being somewhat redundant.

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

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

Last updated