编码器#

介绍#

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.

类构造函数#

encoder(
  triport::port &port );

类析构函数#

Destroys the encoder object and releases associated resources.

virtual ~encoder();

参数#

范围

类型

描述

port

triport::port &

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 Brain.ThreeWirePort.X or ExpanderName.X where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.C).

示例#

// Create an encoder instance in Ports A and B
encoder EncoderA = encoder(Brain.ThreeWirePort.A);

成员功能#

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#

设置编码器传感器的位置。

Available Functions
void setPosition(
  double        value, 
  rotationUnits units);

Parameters

参数

类型

描述

value

double

表示新位置的值。

units

rotationUnits

The unit that represents the new position:

  • degrees
  • turns
Return Values

此函数不返回值。

position#

返回编码器传感器的当前位置。

Available Functions
double position(
  rotationUnits units );

Parameters

参数

类型

描述

units

rotationUnits

The unit that represents the position:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

Returns a double representing the rotation in the specified units.

velocity#

返回编码器传感器的当前速度。

Available Functions
double velocity(
  velocityUnits units );

Parameters

参数

类型

描述

units

velocityUnits

The unit used to represent the velocity:

  • percent / pct — percent
  • rpm — rotations per minute
  • dps — degrees per second

Return Values

Returns a double representing the velocity in the specified units.