加速度计#
初始化加速度计类#
使用以下构造函数之一可以创建加速度计:
The accelerometer
constructor creates an Accelerometer in a specified Three Wire Port with High Sensitivity Mode defaulted to false
.
范围 |
描述 |
---|---|
|
// Create the Brain.
brain Brain;
// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A);
The Accelerometer(port, sensitivity)
constructor creates an Accelerometer and can enable High Sensitivity Mode.
范围 |
描述 |
---|---|
|
|
|
Enables high sensitivity mode (+/- 2g) on the Accelerometer. |
// Create the Brain.
brain Brain;
// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A, true);
必须先创建 Brain 或 3-Wire Expander,然后才能使用加速度计类构造函数创建对象。
The accel
object will be used in all subsequent examples throughout this API documentation when referring to accelerometer class methods.
为了获得最佳功能,建议为每个轴使用单独的加速度计,并单独初始化它们:
accelerometer accel_x = accelerometer(Brain.ThreeWirePort.A);
accelerometer accel_y = accelerometer(Brain.ThreeWirePort.B);
accelerometer accel_z = accelerometer(Brain.ThreeWirePort.C);
类方法#
acceleration()#
The acceleration()
method reads the value of the Accelerometer scaled to units of gravity.
返回: 范围内的双精度数 +/- 6G,或如果设置了高灵敏度模式则为 +/-2G。
// Drive the robot forward.
Drivetrain.drive(forward);
// Get the acceleration of accel in the range +/- 6G.
double accelerationValue = accel.acceleration();
changed()#
The changed(callback)
method registers a function to be called when the value of the Accelerometer changes.
参数 |
描述 |
---|---|
打回来 |
当加速度计的轴值改变时调用的函数。 |
**返回:**事件类的一个实例。
// 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);
}