陀螺仪#

初始化陀螺仪类#

使用以下构造函数创建陀螺仪传感器:

The gyro constructor creates a gyro object in the specified Three Wire Port:

范围

描述

port

陀螺仪传感器所连接的 3 线端口,无论是 Brain 上的端口,还是 3 线扩展器 上的端口。

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

// Create the Brain.
brain Brain;
// Construct a Gyro Sensor "Gyro" with the
// gyro class.
gyro Gyro = gyro(Brain.ThreeWirePort.A);

This Gyro object will be used in all subsequent examples throughout this API documentation when referring to gyro class methods.

类方法#

calibrate()#

该方法通过以下方式调用:

The calibrate() method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

**返回:**无。

// Start calibration.
Gyro.calibrate();

The calibrate(value) method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

参数

描述

价值

设置校准时间。默认值为 0。

**返回:**无。

// Start calibration with the calibration time set to 3 seconds.
Gyro.calibrate(3);

isCalibrating()#

The isCalibrating() method checks if the Gyro Sensor is currently calibrating.

Returns: true if the Gyro is calibrating. false if it is not.

// Start calibration.
Gyro.calibrate();
// Print that the Gyro is calibrating while waiting for it to
// finish calibrating.
while(Gyro.isCalibrating()){
    Brain.Screen.clear();
    Brain.Screen.print("Gyro Calibrating");
    wait(50, msec);
}

resetAngle()#

该方法通过以下方式调用:

The resetAngle() method resets the angle of the Gyro Sensor to 0.

**返回:**无。

The resetAngle(value, units) method resets the angle of the Gyro Sensor to a specified value.

参数

描述

价值

用于新角度的值。

单位

有效的 rotationUnit

**返回:**无。

resetHeading()#

The resetHeading() method resets the heading of the Gyro Sensor to 0.

**返回:**无。

resetRotation()#

The resetRotation() method resets the rotation of the Gyro Sensor to 0.

**返回:**无。

setHeading()#

The setHeading(value, units) method sets the heading of the Gyro Sensor to a specific value.

参数

描述

价值

用于新标题的值。

单位

有效的 rotationUnit

**返回:**无。

angle()#

The angle(units) method returns the current angle of the Gyro Sensor.

参数

描述

单位

A valid rotationUnit. The default is degrees.

**返回:**以指定单位表示陀螺仪传感器当前角度的双精度值。

// Get the current angle for the Gyro.
double value = Gyro.angle();

heading()#

The heading(units) method returns the current heading of the Gyro Sensor.

参数

描述

单位

A valid rotationUnit. The default is degrees.

**返回:**以指定单位表示陀螺仪传感器当前航向的双精度值。

// Get the current heading for the Gyro.
double value = Gyro.heading();

setRotation()#

The setRotation(value, units) method sets the rotation of the Gyro Sensor to a specific value.

参数

描述

价值

用于新旋转的值。

单位

有效的 rotationUnit

**返回:**无。

rotation()#

The rotation(units) method returns the current rotation of the Gyro Sensor.

参数

描述

单位

A valid rotationUnit. The default is degrees.

**返回:**表示陀螺仪传感器当前以指定单位旋转的双精度值。

// Get the current rotation for the Gyro.
double value = Gyro.rotation();

getTurnType()#

The getTurnType() method returns the direction that returns positive heading values with the Gyro Sensor.

**返回:**指定单位的陀螺仪传感器的转动类型。

changed()#

The changed(callback) method registers a callback function for when the Gyro Sensor’s heading changes.

参数

描述

打回来

当陀螺仪传感器的航向改变时调用的回调函数。

**返回:**无。

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

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

  // Turn the robot right.
  Drivetrain.turn(right);

  // Run GyroChanged when the value of the
  // Gyro Sensor changes.
  Gyro.changed(GyroChanged);
}