编码器#

初始化编码器类#

使用以下构造函数创建编码器传感器:

encoder 构造函数在指定的三线端口中创建一个编码器对象:

范围

描述

端口

编码器传感器所连接的三线端口“对”,可以是 Brain 上的端口,也可以是 3 线扩展器 上的端口。2
注意:**编码器传感器使用两个相邻的三线端口。这些端口对分别是 a/b、c/d、e/f 和 g/h。输入端口对作为参数时,请使用端口对的首字母,例如:“Brain.ThreeWirePort.A” 指的是 a/b 对。

必须先创建 Brain3-Wire Expander,然后才能使用编码器类构造函数创建对象。

// Create the Brain.
brain Brain;
// Construct an Encoder Sensor "Encoder" with the
// encoder class.
encoder Encoder = encoder(Brain.ThreeWirePort.A);

当引用编码器类方法时,此“Encoder”对象将在整个 API 文档的所有后续示例中使用。

类方法#

重置旋转()#

resetRotation() 方法将编码器传感器的旋转重置为 0。

**返回:**无。

设置旋转()#

setRotation(value, units) 方法设置编码器传感器的旋转。

参数

描述

价值

要设置的旋转值。

单位

有效的 rotationUnit

**返回:**无。

// Set the Encoder Sensor's value of rotation to 180 degrees.
Encoder.setRotation(180);

设置位置()#

setPosition(value, units) 方法设置编码器传感器的位置。

参数

描述

价值

要设置的旋转值。

单位

有效的 rotationUnit

**返回:**无。

// Set the Encoder Sensor's value of position to 180 degrees.
Encoder.setPosition(180);

旋转()#

rotation(units) 方法返回编码器传感器的旋转值。

参数

描述

单位

有效的 rotationUnit

**返回:**表示编码器当前旋转的双精度数。

// Get the current encoder rotation.
double value = Encoder.rotation();

位置()#

position(units) 方法返回编码器传感器的当前位置。

参数

描述

单位

有效的 rotationUnit

**返回:**编码器传感器当前位置的指定单位值。

// Get the current Encoder Sensor's position.
double value = Encoder.position();

速度()#

velocity(units) 方法返回编码器传感器的当前速度。

参数

描述

单位

有效的 velocityUnit

**返回:**以指定单位表示编码器传感器当前速度的双精度值。

// Get the current Encoder Sensor's velocity in rpm.
double value = Encoder.velocity();

已更改()#

changed(callback) 方法注册一个函数,当编码器传感器的值发生变化时调用该函数。

参数

描述

打回来

当编码器传感器的轴值发生变化时将调用的函数。

**返回:**事件类的一个实例。

// Define the encoderChanged function with a void return
// type, showing it doesn't return a value.
void encoderChanged() {
  // The Brain will print that the Encoder Sensor changed on
  // the Brain's screen.
  Brain.Screen.print("encoder changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive the robot forward.
  Drivetrain.drive(forward);

  // Run encoderChanged when the value of the
  // Encoder Sensor changes.
  Encoder.changed(encoderChanged);
}