编码器#
初始化编码器类#
使用以下构造函数创建编码器传感器:
The encoder constructor creates an encoder object in the specified Three Wire Port:
范围 |
描述 |
|---|---|
|
The 3-Wire Port “pair” that the Encoder Sensor is connected to, whether it’s a port on the |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Encoder Class constructor.
// Create the Brain.
brain Brain;
// Construct an Encoder Sensor "Encoder" with the
// encoder class.
encoder Encoder = encoder(Brain.ThreeWirePort.A);
This Encoder object will be used in all subsequent examples throughout this API documentation when referring to encoder class methods.
类方法#
resetRotation()#
The resetRotation() method resets the rotation of the Encoder Sensor to 0.
**返回:**无。
setRotation()#
The setRotation(value, units) method sets the rotation of the Encoder Sensor.
参数 |
描述 |
|---|---|
|
要设置的旋转值。 |
|
A valid |
**返回:**无。
// Set the Encoder Sensor's value of rotation to 180 degrees.
Encoder.setRotation(180);
setPosition()#
The setPosition(value, units) method sets the position of the Encoder Sensor.
参数 |
描述 |
|---|---|
|
要设置的旋转值。 |
|
A valid |
**返回:**无。
// Set the Encoder Sensor's value of position to 180 degrees.
Encoder.setPosition(180);
rotation()#
The rotation(units) method returns the rotation value of the Encoder Sensor.
参数 |
描述 |
|---|---|
|
A valid |
**返回:**表示编码器当前旋转的双精度数。
// Get the current encoder rotation.
double value = Encoder.rotation();
position()#
The position(units) method returns the current position of the Encoder Sensor.
参数 |
描述 |
|---|---|
|
A valid |
**返回:**编码器传感器当前位置的指定单位值。
// Get the current Encoder Sensor's position.
double value = Encoder.position();
velocity()#
The velocity(units) method returns the current velocity of the Encoder Sensor.
参数 |
描述 |
|---|---|
|
A valid |
**返回:**以指定单位表示编码器传感器当前速度的双精度值。
// Get the current Encoder Sensor's velocity in rpm.
double value = Encoder.velocity();
changed()#
The changed(callback) method registers a function to be called when the value of the Encoder Sensor changes.
参数 |
描述 |
|---|---|
|
当编码器传感器的轴值发生变化时将调用的函数。 |
**返回:**事件类的一个实例。
// 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);
}