陀螺仪#

初始化陀螺仪类#

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

gyro 构造函数在指定的三线端口中创建一个陀螺仪对象:

范围

描述

端口

陀螺仪传感器所连接的 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);

当引用陀螺仪类方法时,此 API 文档中的所有后续示例中都将使用此“Gyro”对象。

类方法#

校准()#

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

calibrate() 方法用于校准陀螺仪传感器。校准应在陀螺仪传感器静止不动时进行。

**返回:**无。

// Start calibration.
Gyro.calibrate();

calibrate(value) 方法用于校准陀螺仪传感器。校准应在陀螺仪传感器静止不动时进行。

参数

描述

价值

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

**返回:**无。

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

正在校准()#

isCalibrating() 方法检查陀螺仪传感器当前是否正在校准。

**返回:**如果陀螺仪正在校准,则返回“true”。如果不是,则返回“false”。

// 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() 方法将陀螺仪传感器的角度重置为 0。

**返回:**无。

resetAngle(value, units) 方法将陀螺仪传感器的角度重置为指定值。

参数

描述

价值

用于新角度的值。

单位

有效的 rotationUnit

**返回:**无。

重置标题()#

resetHeading() 方法将陀螺仪传感器的航向重置为 0。

**返回:**无。

重置旋转()#

resetRotation() 方法将陀螺仪传感器的旋转度重置为 0。

**返回:**无。

设置标题()#

setHeading(value, units) 方法将陀螺仪传感器的航向设置为特定值。

参数

描述

价值

用于新标题的值。

单位

有效的 rotationUnit

**返回:**无。

角度()#

angle(units) 方法返回陀螺仪传感器的当前角度。

参数

描述

单位

有效的 rotationUnit。默认值为“度”。

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

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

标题()#

heading(units) 方法返回陀螺仪传感器的当前航向。

参数

描述

单位

有效的 rotationUnit。默认值为“度”。

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

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

设置旋转()#

setRotation(value, units) 方法将陀螺仪传感器的旋转设置为特定值。

参数

描述

价值

用于新旋转的值。

单位

有效的 rotationUnit

**返回:**无。

旋转()#

rotation(units) 方法返回陀螺仪传感器的当前旋转。

参数

描述

单位

有效的 rotationUnit。默认值为“度”。

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

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

获取转弯类型()#

getTurnType() 方法返回使用陀螺仪传感器返回正航向值的方向。

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

已更改()#

changed(callback) 方法注册陀螺仪传感器航向改变时的回调函数。

参数

描述

打回来

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

**返回:**无。

// 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);
}