加速度计#

初始化加速度计类#

使用以下构造函数之一可以创建加速度计:

accelerometer 构造函数在指定的三线端口中创建一个加速度计,高灵敏度模式默认为 false

范围

描述

端口

加速度计连接到的 3 线端口,无论它是 大脑 上的端口,还是 3 线扩展器 上的端口。

// Create the Brain.
brain Brain;

// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A);

Accelerometer(port, sensitivity) 构造函数创建一个加速度计并可以启用高灵敏度模式。

范围

描述

端口

加速度计连接到的 3 线端口,无论它是 大脑 上的端口,还是 3 线扩展器 上的端口。

敏感性

在加速度计上启用高灵敏度模式 (+/- 2g)。设置为“true”表示启用高灵敏度。默认灵敏度为 (+/- 6g)。

// Create the Brain.
brain Brain;

// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A, true);

必须先创建 Brain3-Wire Expander,然后才能使用加速度计类构造函数创建对象。

当引用加速度计类方法时,本 API 文档中的所有后续示例中都将使用“accel”对象。

为了获得最佳功能,建议为每个轴使用单独的加速度计,并单独初始化它们:

accelerometer accel_x = accelerometer(Brain.ThreeWirePort.A);
accelerometer accel_y = accelerometer(Brain.ThreeWirePort.B);
accelerometer accel_z = accelerometer(Brain.ThreeWirePort.C);

类方法#

加速度()#

acceleration() 方法读取加速度计按重力单位缩放的值。

返回: 范围内的双精度数 +/- 6G,或如果设置了高灵敏度模式则为 +/-2G。

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

// Get the acceleration of accel in the range +/- 6G.
double accelerationValue = accel.acceleration();

已更改()#

changed(callback) 方法注册一个函数,当加速度计的值发生变化时调用该函数。

参数

描述

打回来

当加速度计的轴值改变时调用的函数。

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

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

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

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

  // Run accelerometerChanged when the value of the 
  // Accelerometer changes.
  accel.changed(accelerometerChanged);
}