encoder#

Initializing the encoder Class#

An Encoder Sensor is created by using the following constructor:

The encoder constructor creates an encoder object in the specified Three Wire Port:

Parameter

Description

port

The 3-Wire Port “pair” that the Encoder Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.
Note: An Encoder Sensor uses two adjacent 3-Wire ports. These pairs are a/b, c/d, e/f, and g/h. When entering the pair as an argument, use the first letter of the pair, ie: Brain.ThreeWirePort.A refers to the a/b pair.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Encoder Class constructor.

// Create the Brain.
brain Brain;
// Construct an Encoder Sensor "Encoder" with the
// encoder class.
encoder Encoder = encoder(Brain.ThreeWirePort.A);

This Encoder object will be used in all subsequent examples throughout this API documentation when referring to encoder class methods.

Class Methods#

resetRotation()#

The resetRotation() method resets the rotation of the Encoder Sensor to 0.

Returns: None.

setRotation()#

The setRotation(value, units) method sets the rotation of the Encoder Sensor.

Parameters

Description

value

The rotation value to set.

units

A valid RotationUnit.

Returns: None.

// Set the Encoder Sensor's value of rotation to 180 degrees.
Encoder.setRotation(180);

setPosition()#

The setPosition(value, units) method sets the position of the Encoder Sensor.

Parameters

Description

value

The rotation value to set.

units

A valid RotationUnit.

Returns: None.

// Set the Encoder Sensor's value of position to 180 degrees.
Encoder.setPosition(180);

rotation()#

The rotation(units) method returns the rotation value of the Encoder Sensor.

Parameters

Description

units

A valid RotationUnit.

Returns: A double that represents the current rotation of the Encoder.

// Get the current encoder rotation.
double value = Encoder.rotation();

position()#

The position(units) method returns the current position of the Encoder Sensor.

Parameters

Description

units

A valid RotationUnit.

Returns: The value of the current position of the Encoder Sensor in the specified units.

// Get the current Encoder Sensor's position.
double value = Encoder.position();

velocity()#

The velocity(units) method returns the current velocity of the Encoder Sensor.

Parameters

Description

units

A valid VelocityUnit.

Returns: A double representing the value of the current velocity of the Encoder Sensor in the specified units.

// Get the current Encoder Sensor's velocity in rpm.
double value = Encoder.velocity();

changed()#

The changed(callback) method registers a function to be called when the value of the Encoder Sensor changes.

Parameters

Description

callback

A function that will be called when the Encoder Sensor’s axis value changes.

Returns: An instance of the Event class.

// Define the encoderChanged function with a void return
// type, showing it doesn't return a value.
void encoderChanged() {
  // The Brain will print that the Encoder Sensor changed on
  // the Brain's screen.
  Brain.Screen.print("encoder changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive the robot forward.
  Drivetrain.drive(forward);

  // Run encoderChanged when the value of the
  // Encoder Sensor changes.
  Encoder.changed(encoderChanged);
}