编码器#

介绍#

encoder 类与光学轴编码器配合使用,用于测量轴的旋转位置和速度。它利用内部光学传感器检测轴的旋转角度和速度,因此可用于跟踪车轮旋转、机械臂位置或其他机构。

VEX 光学轴编码器。

类构造函数#

encoder(
  triport::port &port );

类析构函数#

销毁 encoder 对象并释放相关资源。

virtual ~encoder();

参数#

范围

类型

描述

port

triport::port &

光学轴编码器使用的所需双端口组中的第一个三线端口。编码器占用主板或三线扩展器上的两个相邻端口(A-B、C-D、E-F 或 G-H)。指定端口对中的第一个端口(A、C、E 或 G),格式为 Brain.ThreeWirePort.X 或 ExpanderName.X ,其中 X 为端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.C)。

示例#

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

成员功能#

encoder 类包含以下成员函数:

  • setPosition — 将编码器的当前位置设置为指定的值。

  • position — 返回编码器旋转了多远。

  • velocity — 返回编码器的当前旋转速度。

在调用 encoder 成员函数之前,必须先创建 encoder 实例,如下所示:

/* 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 position:

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

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

返回一个 double 的双精度浮点数,表示以指定单位表示的旋转角度。

velocity#

返回编码器的旋转速度。

Available Functions
double velocity(
  velocityUnits units );

Parameters

参数

类型

描述

units

velocityUnits

速度单位:

  • percent 百分比 pct 百分比
  • rpm — 每分钟转数
  • dps — 每秒度数

Return Values

返回一个 double 之间的双精度浮点数,表示以指定单位表示的速度。