惯性#

介绍#

惯性传感器用于测量V5机器人的旋转和姿态。该传感器可以报告沿x、y和z轴的运动,并可用于确定航向、旋转和倾斜。该传感器还可以测量其沿x、y和z轴的加速度。

要使惯性命令出现在 VEXcode V5 中,必须在“设备”窗口中配置惯性传感器。

This page uses Inertial1 as the example Inertial Sensor name. If using a smart drivetrain, the example Inertial Sensor name will be DrivetrainInertial. Replace these with your own configured name as needed.

以下是可用方法列表:

方向控制 — 读取和控制航向、旋转和传感器校准。

  • heading — Returns the current heading.

  • rotation — Returns the current rotation.

  • setHeading — Sets the Inertial Sensor’s heading to a specified value.

  • setRotation — Sets the Inertial Sensor’s rotation value.

  • calibrate — Calibrates the Inertial Sensor for stable heading tracking.

  • resetHeading — Sets the heading of the Inertial Sensor to 0.

  • resetRotation — Sets the rotation of the Inertial Sensor to 0.

  • isCalibrating — Returns whether or not the Inertial Sensor is calibrating.

  • changed — Registers a callback function for when the Inertial Sensor’s heading changes.

  • installed — Checks if the Inertial Sensor is installed.

运动学——测量加速度、角速度和倾斜度。

  • acceleration — Returns the linear acceleration along the x, y, or z axis.

  • gyroRate — Returns the gyro rate for one axis of the Inertial Sensor.

  • orientation — Returns the orientation for one axis of the Inertial Sensor.

  • collision — Registers a callback function for when the Inertial Sensor detects a collision.

  • setTurnType — Sets the direction that returns positive values for the heading.

  • getTurnType — Returns the direction that returns positive values for heading.

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

  • inertial — Creates an Inertial Sensor.

方向#

heading#

heading returns the current heading of the Inertial Sensor in the specified units as a float.

用法:

DrivetrainInertial.heading(units)

参数

描述

units

The unit used to represent the heading:

  • degrees
  • turns

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

  // Turn, then show the current heading
  Drivetrain.turnFor(right, 450, degrees);
  Brain.Screen.print("%f", DrivetrainInertial.heading(degrees));
}

rotation#

rotation returns the current rotation of the Inertial Sensor in the specified units as a float.

用法:

DrivetrainInertial.rotation(units)

参数

描述

units

The unit used to represent the rotation:

  • degrees
  • turns

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

  // Turn, then show the current rotation
  Drivetrain.turnFor(right, 450, degrees);
  Brain.Screen.print("%f", DrivetrainInertial.rotation(degrees));
}

setHeading#

setHeading sets the heading of the Inertial Sensor to a specified value.

用法:

DrivetrainInertial.setHeading(value, units);

参数

描述

value

要设置的标题值,范围为 0 到 359,值为双精度浮点数。

units

The unit used to represent the heading:

  • degrees
  • turns

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

  // Turn the robot around
  DrivetrainInertial.setHeading(180, degrees);
  Drivetrain.turnToHeading(0, degrees);
}

setRotation#

setRotation sets the rotation of the Inertial Sensor to a specified value.

用法:

DrivetrainInertial.setRotation(value, units);

参数

描述

value

要设置的旋转值,类型为双精度浮点数。

units

The unit used to represent the rotation:

  • degrees
  • turns

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

  // Pretend we have already turned 180 degrees
  DrivetrainInertial.setRotation(-180, degrees);
  Drivetrain.turnToRotation(0, degrees);
}

calibrate#

calibrate calibrates the Inertial Sensor. Calibration should be done when the Inertial Sensor is not moving. Allow at least 2 seconds for calibration to complete.

用法:

Inertial1.calibrate();

参数

描述

此方法没有参数。

resetHeading#

resetHeading resets the heading of the Inertial Sensor to 0.

用法:

Inertial1.resetHeading();

参数

描述

此方法没有参数。

resetRotation#

resetRotation resets the rotation of the Inertial Sensor to 0.

用法:

Inertial1.resetRotation();

参数

描述

此方法没有参数。

isCalibrating#

isCalibrating checks if the Inertial Sensor is currently calibrating.

  • True — The Inertial Sensor is calibrating.

  • False — The Inertial Sensor is not calibrating.

用法:

Inertial1.isCalibrating()

参数

描述

此方法没有参数。

changed#

changed registers a callback function that runs when the Inertial Sensor’s heading value changes.

Usage:
DrivetrainInertial.changed(callback);

参数

描述

callback

先前定义的 函数,当航向值改变时执行。

Callback Signature:
void callback();

论点

描述

此回调函数没有参数。

// Called when the inertial sensor's heading value changes
void onHeadingChanged() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Heading: %.2f", DrivetrainInertial.heading());
}

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

  // Register the onHeadingChanged callback
  // Rotate the robot by hand to see the heading change
  DrivetrainInertial.changed(onHeadingChanged);
}

installed#

installed checks if the Inertial Sensor is installed.

  • true — The Inertial Sensor is installed.

  • false — The Inertial Sensor is not installed.

Usage: Inertial1.installed()

参数

描述

此方法没有参数。

运动#

acceleration#

acceleration returns the acceleration of the Inertial Sensor along the specified axis as a double.

用法:

Inertial1.acceleration(axis)

参数

描述

axis

The axis to return the acceleration from:

  • xaxis
  • yaxis
  • zaxis

gyroRate#

gyroRate returns the gyro rate for one axis of the Inertial Sensor in the specified units as a double.

用法:

DrivetrainInertial.gyroRate(axis, units)

参数

描述

axis

The axis to return the gyro rate from:

  • xaxis
  • yaxis
  • zaxis

units

The unit used to represent the gyro rate:

  • dps — degrees per second
  • rpm — revolutions per minute
  • velocityUnits::pct — %

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the rate of change of rotation while turning in dps
  Drivetrain.turn(right);
  wait(1, seconds);
  
  Brain.Screen.print(DrivetrainInertial.gyroRate(zaxis, dps));

  Drivetrain.stop();
}

orientation#

orientation returns the orientation for one axis of the Inertial Sensor in the specified units as a double.

用法:

DrivetrainInertial.orientation(type, units)

参数

描述

type

The axis to return the orientation from:

  • pitch
  • roll
  • yaw

units

The unit used to represent the orientation:

  • degrees
  • turns

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

  // Let the Inertial Sensor calibrate
  wait(2, seconds);

  while (true) {
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1,1);
    // Show the Inertial Sensor's orientation value
    // for roll in degrees
    double orient = DrivetrainInertial.orientation(roll, degrees);
    Brain.Screen.print("%f", orient);
    wait(0.1, seconds);
  }
}

collision#

collision registers a callback function that runs when the Inertial Sensor detects a sudden acceleration change indicative of a collision.

Usage: DrivetrainInertial.collision(callback);

参数

描述

callback

预先定义的回调函数,当轴值发生变化时会自动调用。该函数必须符合所需的回调函数签名。有关更多信息,请参阅回调函数

Callback Signature:
void callback(axisType axis, double x, double y, double z);

论点

描述

axisType

The axis on which the collision was detected:

  • xaxis
  • yaxis
  • zaxis

x

碰撞时 x 轴上的加速度读数为双精度值。

y

碰撞时 y 轴上的加速度读数为双精度浮点数。

z

碰撞时 z 轴上的加速度读数为双精度值。

void onCollision(axisType axis, double x, double y, double z) {
  Brain.Screen.print("Collision detected!");
}

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

  // Firmly move the robot by hand briefly to simulate
  // a collision and call the onCollision function
  DrivetrainInertial.collision(onCollision);
}

setTurnType#

setTurnType sets the direction that returns positive values for the heading.

Usage: Inertial1.setTurnType(direction);

参数

描述

direction

The turn direction that will return positive values:

  • left
  • right

getTurnType#

getTurnType returns the direction that returns positive values for heading.

  • left — The turn type is left.

  • right — The turn type is right.

用法:

Inertial1.getTurnType();

参数

描述

此方法没有参数。

构造函数#

inertial#

inertial creates an Inertial Sensor.

Usage: inertial(smartport)

参数

描述

port

The Smart Port that the AI Vision Sensor is connected to, written as PORTx where x is the number of the port.

// Create a new object "inertial" with the inertial class.
inertial Inertial1 = inertial(PORT1);