Encoder#
Introduction#
The encoder class is used to measure shaft rotation using the VEX Optical Shaft Encoder connected to a V5 3-Wire port. It tracks rotational movement by detecting changes in the encoder’s internal optical disk.
Class Constructor#
encoder(
triport::port &port );
Class Destructor#
Destroys the encoder object and releases associated resources.
virtual ~encoder();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The first 3-Wire Port of a required two-port group used by the Optical Shaft Encoder. The encoder occupies two adjacent ports (A–B, C–D, E–F, or G–H) on either the Brain or a 3-Wire Expander. Specify the first port in the pair (A, C, E, or G), written as |
Examples#
// Create an encoder instance in Ports A and B
encoder EncoderA = encoder(Brain.ThreeWirePort.A);
Member Functions#
The encoder class includes the following member functions:
setPosition— Sets the recorded position of the Encoder Sensor.position— Returns the current position of the Encoder Sensor.velocity— Returns the velocity of the Encoder Sensor.
Before calling any encoder member functions, a encoder instance must be created, as shown below:
/* This constructor is required when using VS Code.
Encoder configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an encoder instance in Ports A and B
encoder EncoderA = encoder(Brain.ThreeWirePort.A);
setPosition#
Sets the position of the Encoder Sensor.
Available Functionsvoid setPosition(
double value,
rotationUnits units);
Parameters |
Type |
Description |
|---|---|---|
|
|
The value to represent the new position. |
|
|
The unit that represents the new position:
|
This function does not return a value.
position#
Returns the current position of the Encoder Sensor.
Available Functionsdouble position(
rotationUnits units );
Parameters |
Type |
Description |
|---|---|---|
|
|
The unit that represents the position:
|
Returns a double representing the rotation in the specified units.
velocity#
Returns the current velocity of the Encoder Sensor.
Available Functionsdouble velocity(
velocityUnits units );
Parameters |
Type |
Description |
|---|---|---|
|
|
The unit used to represent the velocity:
|
Returns a double representing the velocity in the specified units.