加速度计#
要使加速度计命令出现在 VEXcode V5 中,必须在“设备”窗口中配置 3 线加速度计传感器。
更多信息,请参阅以下文章:
初始化加速度计类#
加速度计可以通过以下构造函数之一创建:
The accelerometer constructor creates an Accelerometer in a specified Three Wire Port with High Sensitivity Mode defaulted to false.
范围 |
描述 |
|---|---|
|
The 3-Wire Port that the Accelerometer is connected to, whether it’s a port on the |
// 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.
范围 |
描述 |
|---|---|
|
The 3-Wire Port that the Accelerometer is connected to, whether it’s a port on the |
|
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);
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Accelerometer Class constructor.
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.
参数 |
描述 |
|---|---|
|
当加速度计的轴值发生变化时,将调用该函数。 |
返回值: Event 类的一个实例。
// 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);
}