编码器#
介绍#
The encoder class is used with the Optical Shaft Encoder, which 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.

类构造函数#
encoder(
triport::port &port );
类析构函数#
Destroys the encoder object and releases associated resources.
virtual ~encoder();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
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 |
示例#
// 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 encoder’s current position to a specified value.position— Returns how far the encoder has rotated.velocity— Returns the current rotational velocity of the encoder.
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 Functionsvoid setPosition(
double value,
rotationUnits units);
参数 |
类型 |
描述 |
|---|---|---|
|
|
要设置的旋转位置。 |
|
|
The unit that represents the position:
|
此函数不返回值。
position#
返回与编码器相连的轴旋转了多远。
Available Functionsdouble position(
rotationUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the position:
|
Returns a double representing the rotation in the specified units.
velocity#
返回编码器的旋转速度。
Available Functionsdouble velocity(
velocityUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent the velocity:
|
Returns a double representing the velocity in the specified units.