惯性#

介绍#

VEX IQ(第二代)大脑内置一个用于测量旋转运动的三轴陀螺仪和一个用于检测运动变化的三轴加速度计。这些传感器使机器人能够追踪其方向、航向和加速度。

以下是所有可用方法的列表:

方向——测量惯性传感器的旋转运动。

  • heading – 返回当前标题。

  • rotation – 返回累积旋转角度。

  • setHeading – 将惯性传感器的航向设置为指定值。

  • setRotation – 设置惯性传感器的旋转值。

  • calibrate – 校准惯性传感器以实现稳定的航向跟踪。

  • isCalibrating – 返回惯性传感器是否正在校准。

  • resetHeading – 将惯性传感器的航向设置为 0。

  • resetRotation – 将惯性传感器的旋转角度设置为 0。

运动——检测运动的变化。

  • 加速度 – 返回沿 x、y 或 z 轴的线性加速度。

  • gyroRate – 返回绕 x、y 或 z 轴的角速度。

  • orientation – 根据倾斜和旋转返回滚动、俯仰或偏航。

  • 已更改 – 注册一个函数,当惯性传感器检测到变化时调用。

构造函数——手动初始化和配置惯性传感器。

  • inertial – Creates an Inertial Sensor.

方向#

标题#

heading returns the current heading of the Inertial Sensor in degrees.

Usage:
heading()

参数

描述

该方法没有参数。

// Display the heading after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", BrainInertial.heading());

Overload:
heading(units)

参数

描述

units

The unit used to represent the heading:

  • turns
// Display the heading after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", BrainInertial.heading(turns));

旋转#

rotation returns the current rotation of the Gyro Sensor in degrees.

Usage:
rotation()

参数

描述

该方法没有参数。

// Display the rotation after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.rotation());

Overload:
rotation(units)

参数

描述

units

The unit used to represent the rotation:

  • turns
// Display the rotation after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.rotation(turns));

设置标题#

setHeading 将惯性传感器的航向设置为特定值。

Usage:
setHeading(value, units)

参数

描述

value

要设置的航向值。

units

表示新航向的单位:

  • </li><li>
// Turn the robot around
BrainInertial.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);

设置旋转#

setRotation 将惯性传感器的旋转设置为特定值。

Usage:
setRotation(value, units)

参数

描述

value

要设置的旋转值。

units

表示新旋转的单位:

  • </li><li>
// Turn the robot around
BrainInertial.setRotation(-180, degrees);
Drivetrain.turnToRotation(0, degrees);

校准#

“calibrate” 用于校准惯性传感器。所有后续代码行都将等待校准完成后再执行。校准是一个内部过程,用于测量并补偿指定时间段内的传感器噪声和漂移。在此期间,Brain 必须保持完全静止(即,在稳定的表面上,没有任何外部运动)。校准期间的移动会导致结果不准确。

../../_images/IQ_right_orientation.png../../_images/IQ_left_orientation.png../../_images/IQ_top_orientation.png

VEX 机器人会在每次项目开始时尝试自动校准。但是,如果在项目启动期间搬运或移动机器人,传感器可能无法正确校准或产生错误的校准结果。

Usage:
calibrate()

范围

描述

该方法没有参数。

// Start calibration
BrainInertial.calibrate();
// Print after calibration
while (BrainInertial.isCalibrating()){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Inertial Sensor");
  Brain.Screen.newLine();
  Brain.Screen.print("Calibrating");
  wait(50, msec);
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Done!");

正在校准#

isCalibrating 检查惯性传感器当前是否正在校准。

  • 1-惯性传感器正在校准。

  • 0——惯性传感器未校准。

用法:
isCalibrating()

范围

描述

该方法没有参数。

// Start calibration
BrainInertial.calibrate();
// Print while calibrating
while (BrainInertial.isCalibrating()){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Inertial Sensor");
  Brain.Screen.newLine();
  Brain.Screen.print("Calibrating");
  wait(50, msec);
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Done!");

重置标题#

resetHeading 将惯性传感器的航向重置为 0。

Usage:
resetHeading()

参数

描述

该方法没有参数。

// Turn the robot before and after resetting the heading
Drivetrain.turnToHeading(90, degrees);
wait(0.5, seconds);
BrainInertial.resetHeading();
Drivetrain.turnToHeading(90, degrees);

重置旋转#

resetRotation 将惯性传感器的旋转重置为 0。

Usage:
resetRotation()

参数

描述

该方法没有参数。

// Turn the robot before and after resetting the rotation
Drivetrain.turnToRotation(-90, degrees);
wait(0.5, seconds);
BrainInertial.resetRotation();
Drivetrain.turnToRotation(-90, degrees);

运动#

加速度#

acceleration returns the acceleration of the Inertial Sensor.

Usage:
acceleration(axis)

参数

描述

axis

返回加速度的轴:

  • xaxis
  • yaxis
  • zaxis
// Display acceleration after moving
Drivetrain.setDriveVelocity(100, percent);
Brain.Screen.print("Resting: ");
Brain.Screen.newLine();
Brain.Screen.print("%.2f", BrainInertial.acceleration(xaxis));
Brain.Screen.newLine();
wait(1, seconds);
Drivetrain.driveFor(forward, 500, mm, false);
wait(0.01,seconds);
Brain.Screen.print("Startup: ");
Brain.Screen.newLine();
Brain.Screen.print("%.2f", BrainInertial.acceleration(xaxis));

陀螺仪速率#

gyroRate 返回惯性传感器一个轴的陀螺仪速率。

Usage:
gyroRate(axis, units)

参数

描述

axis

返回陀螺仪速率的轴:

  • xaxis
  • yaxis
  • zaxis

units

The unit used to represent the gyro rate:

  • dps
  • rpm
  • velocityUnits::pct
// Display the z-axis gyro rate
Drivetrain.turn(right);
wait(1, seconds);
Brain.Screen.print("%f", BrainInertial.gyroRate(zaxis, rpm));
Drivetrain.stop();

方向#

orientation 返回惯性传感器一个轴的方向。

Usage:
orientation(axis, units)

参数

描述

axis

返回方向的轴:

  • pitch
  • roll
  • yaw

units

用于表示方向的单位:

  • </li><li>
// Display the roll, pitch, and yaw of the Brain as it
// is rotated by hand
while (true){
    Brain.Screen.clearScreen();
    Brain.Screen.print("%f", BrainInertial.orientation(roll, degrees));
    Brain.Screen.newLine();
    Brain.Screen.print("%f", BrainInertial.orientation(pitch, degrees));
    Brain.Screen.newLine();
    Brain.Screen.print("%f", BrainInertial.orientation(yaw, degrees));
    Brain.Screen.setCursor(1, 1);
    wait(0.1,seconds);
  }

改变#

当惯性传感器的航向发生变化时,changed 注册一个回调函数。

Usage:
changed(callback)

参数

描述

callback

当惯性传感器航向改变时调用的回调函数。

void headingChanged(){
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearScreen();
  Brain.Screen.print("My heading ");
  Brain.Screen.newLine();
  Brain.Screen.print("has changed!");
  wait(0.1, seconds);
}

int main() {
  vexcodeInit();

  // Call when Inertial heading is changed
  wait(1, seconds);
  Drivetrain.turnFor(right, 90, degrees, false);
  BrainInertial.changed(headingChanged);
  wait(15, msec);
}

构造函数#

Constructors are used to manually create inertial objects, which are necessary for configuring an Inertial Sensor outside of VEXcode.

For the examples below, the configured Inertial Sensor will be named BrainInertial and will be used in all subsequent examples throughout this API documentation when referring to inertial class methods.

惯性#

inertial 创建一个惯性传感器。

Usage:
inertial()

范围

描述

该方法没有参数。

// Create a new object "BrainInertial" with the
// inertial class
inertial BrainInertial = inertial();
motor LeftDriveSmart = motor(PORT1, 1, false);
motor RightDriveSmart = motor(PORT6, 1, true);

smartdrive Drivetrain = smartdrive(LeftDriveSmart, RightDriveSmart, BrainInertial, 200);

BrainInertial.setHeading(180);
Drivetrain.turnToHeading(0);