Encoder#

Introduction#

The Optical Shaft Encoder measures the rotational position and speed of a shaft. It uses internal optical sensors to detect how far and how fast the shaft turns, making it useful for tracking wheel rotation, arm position, or other mechanisms.

The VEX Optical Shaft Encoder.

This page uses encoder_a as the example Optical Shaft Encoder name. Replace it with your own configured name as needed.

Below is a list of available methods:

  • set_position – Sets the encoder’s current position to a specific value.

  • reset_position – Sets the encoder’s current position to 0.

  • position – Returns how far the encoder has rotated.

  • velocity – Returns the current rotational speed of the encoder.

Constructor – Manually initialize and configure an Optical Shaft Encoder.

  • Encoder – Create an Optical Shaft Encoder.

set_position#

set_position sets the encoder’s current position to a specified value.

Usage:
encoder_a.set_position(position, units)

Parameter

Description

position

The encoder’s position to set as a number.

units

Optional. The unit to set the position with:

  • DEGREES (default)
  • TURNS

# Set the encoder's position to 3 turns
encoder_a.set_position(3, TURNS)

reset_position#

reset_position sets the encoder’s current position to 0.

Usage:
encoder_a.reset_position()

Parameters

Description

This method has no parameters.

position#

position returns how far the shaft connected to the encoder has rotated.

Usage:
encoder_a.position(units)

Parameters

Description

units

Optional. The unit to return the position with:

  • DEGREES (default) – Returns an integer
  • TURNS – Returns a float.

velocity#

velocity returns the rotational speed of the encoder as a float.

Usage:
encoder_a.velocity(units)

Parameter

Description

units

Optional. The unit to return the velocity with:

  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second.

Constructor#

Constructors are used to manually create Encoder objects, which are necessary for configuring an Optical Shaft Encoder outside of VEXcode.

Encoder#

Encoder creates an Optical Shaft Encoder.

Usage:
Encoder(port)

Parameter

Description

port

The first of the two 3-Wire Ports that the Optical Shaft Encoder is connected to:

  • On the V5 BrainPorts.PORTx where x is the number of the port.
  • On a 3-Wire Expanderexpander.a where expander is the name of the expander instance.

# Create an Optical Shaft Encoder in Port A and B
encoder_a = Encoder(brain.three_wire_port.a)