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

类构造函数#
encoder(
triport::port &port );
类析构函数#
销毁 encoder 对象并释放相关资源。
virtual ~encoder();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
光学轴编码器使用的所需双端口组中的第一个三线端口。编码器占用主板或三线扩展器上的两个相邻端口(A-B、C-D、E-F 或 G-H)。指定端口对中的第一个端口(A、C、E 或 G),格式为 |
示例#
// 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 Functionsvoid setPosition(
double value,
rotationUnits units);
参数 |
类型 |
描述 |
|---|---|---|
|
|
要设置的旋转位置。 |
|
|
The unit that represents the position:
|
此函数不返回值。
position#
返回与编码器相连的轴旋转了多远。
Available Functionsdouble position(
rotationUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the position:
|
返回一个 double 的双精度浮点数,表示以指定单位表示的旋转角度。
velocity#
返回编码器的旋转速度。
Available Functionsdouble velocity(
velocityUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
速度单位:
|
返回一个 double 之间的双精度浮点数,表示以指定单位表示的速度。