惯性传感器#

介绍#

The inertial class is used to control and access data from an EXP Inertial Sensor. EXP can use the Brain’s built-in Inertial Sensor or an external Inertial Sensor connected to a Smart Port.

An Inertial Sensor measures how the robot is moving and turning. The gyroscope measures turning, such as heading, rotation, gyro rate, and orientation. The accelerometer measures changes in motion, such as speeding up or slowing down along the x-, y-, and z-axes.

类构造函数#

inertial(
    int32_t  index,
    turnType dir = turnType::right );

类析构函数#

Destroys the inertial object and releases associated resources.

~inertial();

参数#

范围

类型

描述

index

int32_t

Optional. The Smart Port that an external Inertial Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1). Do not include this parameter when using the Brain’s built-in Inertial Sensor.

dir

turnType

Optional. The direction that returns positive values:

  • right (default) — Right turns return positive values.
  • left — Left turns return positive values.

示例#

// Use the Brain's built-in Inertial Sensor.
inertial BrainInertial = inertial();

// Use an external Inertial Sensor connected to Port 1.
inertial Inertial1 = inertial(PORT1);

成员功能#

The inertial class includes the following member functions:

  • heading — Returns the direction the sensor is facing.

  • rotation — Returns how far the sensor has turned.

  • setHeading — Sets the sensor’s current heading to a new heading value.

  • setRotation — Sets the sensor’s current rotation to a new rotation value.

  • calibrate — Calibrates the Inertial Sensor.

  • resetHeading — Resets the sensor’s current heading to 0 degrees.

  • resetRotation — Resets the sensor’s current rotation to 0 degrees.

  • isCalibrating — Returns whether the Inertial Sensor is currently calibrating.

  • changed — Registers a function to be called when the heading changes.

  • installed — Returns whether the Inertial Sensor is installed.

  • acceleration — Returns how quickly the sensor is speeding up or slowing down on the selected axis.

  • gyroRate — Returns how fast the sensor is rotating on the selected axis.

  • orientation — Returns the Inertial Sensor’s roll, pitch, or yaw angle.

  • collision — Registers a function to run when a collision is detected.

  • setTurnType — Sets which turn direction returns positive values.

  • getTurnType — Returns which turn direction produces positive values.

Before calling any inertial member functions, an inertial instance must be created, as shown below:

/* This constructor is required when using VS Code.
Inertial Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Use the Brain's built-in Inertial Sensor.
inertial BrainInertial = inertial();

heading#

Returns the current heading of the Inertial Sensor.

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. If the sensor turns past 359.99 degrees, the heading wraps back to 0 degrees.

Available Functions
double heading( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

The heading unit:

  • deg / degrees (default) — degrees
  • turns / rev — revolutions

Return Values

Returns a double representing the current heading of the sensor in the specified units.

Notes
  • The heading value is wrapped between 0 and 359.99 degrees. Use rotation to access the continuous rotation value.

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

rotation#

Returns the current rotation of the Inertial Sensor.

Rotation is how much the sensor has turned. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. For example, making two full turns to the right returns a rotation of 720 degrees. Turning one full turn to the left from 0 degrees returns a rotation of -360 degrees.

Available Functions
double rotation( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

The rotation unit:

  • deg / degrees (default) — degrees
  • turns / rev — revolutions

Return Values

Returns a double representing the current rotation in the specified units.

Notes
  • The rotation value is continuous and does not wrap between 0 and 359.99 degrees. Use heading to access the wrapped heading value.

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

setHeading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. This function changes the Inertial Sensor’s current heading to a new heading value.

For example, if the sensor has turned to face right, setting the heading to 0 degrees makes that right-facing position the new 0 degrees. Then the sensor can track other headings based on that new direction.

Available Functions
void setHeading( double value, rotationUnits units );

Parameters

范围

类型

描述

value

double

The heading value, in degrees, to set for the sensor. This can be a value from 0 to 359.99.

units

rotationUnits

The heading unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

此函数不返回值。

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

setRotation#

Rotation is how much the sensor has turned, measured in degrees. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. At the beginning of a project, the rotation value is set to 0 degrees. This function changes the Inertial Sensor’s current rotation to a new value.

For example, if the sensor has made two full turns to the right, its rotation value will be 720 degrees. Setting the rotation to 0 degrees will reset that rotation from 720 to 0 degrees. Then the sensor can track rotations based on that new value.

Available Functions
void setRotation( double value, rotationUnits units );

Parameters

范围

类型

描述

value

double

The rotation value, in degrees, to set for the sensor. This can be an integer or a decimal.

units

rotationUnits

The rotation unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

此函数不返回值。

Examples
// Turn the robot around
myInertial.setRotation(-180, degrees);
Drivetrain.turnToRotation(0, degrees);

calibrate#

Calibrates the Inertial Sensor.

Calibration helps the sensor measure turns correctly. Keep the sensor still during calibration. If the sensor moves during calibration, heading, rotation, gyro rate, and orientation values may not measure correctly.

Available Functions
void calibrate( );

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 校准过程中,传感器应保持静止。

  • Use isCalibrating to determine when calibration has completed.

resetHeading#

Resets the Inertial Sensor’s current heading to 0 degrees.

After this function is used, the sensor’s current direction becomes the new 0 degree heading.

Available Functions
void resetHeading();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

resetRotation#

Resets the Inertial Sensor’s current rotation to 0 degrees.

After this function is used, the sensor tracks future turns from the new 0 degree rotation value.

Available Functions
void resetRotation();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

isCalibrating#

返回惯性传感器当前是否正在校准。

Available Functions
bool isCalibrating();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示惯性传感器是否正在校准:

  • true — The sensor is currently calibrating.
  • false — The sensor is not currently calibrating.

changed#

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

Available Functions
void changed( void (* callback)(void) );

Parameters

范围

类型

描述

callback

void (*)(void)

A pointer to a function that will be called when the heading value changes. The function must take no parameters and return void.

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

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

Register the callback inside int main()

/* vexcodeInit() is only required when using VEXcode.
Remove vexcodeInit() if compiling in VS Code. */
int main() {

  // Turn the robot by hand briefly
  // to change the robot's heading
  myInertial.changed(onHeadingChanged);
}

installed#

Returns whether the Inertial Sensor is installed and connected.

Available Functions
bool installed();

Parameters

此函数不接受任何参数。

Return Values

Returns a Boolean indicating whether the Inertial Sensor is installed and connected:

  • true — The sensor is connected and responding.
  • false — The sensor is not connected or not detected.

acceleration#

Acceleration is how quickly the sensor is speeding up or slowing down. This function returns the acceleration of the Inertial Sensor on the selected axis, from -4.0 G to 4.0 G.

A G is a unit used to measure acceleration. 1 G is about the acceleration you feel from gravity while sitting still. The value can be positive or negative depending on the direction of acceleration on the selected axis.

Available Functions
double acceleration( axisType axis );

Parameters

范围

类型

描述

axis

axisType

The axis to measure acceleration on:

  • xaxis
  • yaxis
  • zaxis

Return Values

Returns a double representing the acceleration along the specified axis in G (gravitational units).

Notes
  • Acceleration values are reported in units of G.

gyroRate#

Gyro rate is how fast the Inertial Sensor is rotating. This function returns the current rotation speed of the Inertial Sensor on the selected axis.

The value can be positive or negative depending on the direction the sensor is rotating on that axis.

Available Functions
double gyroRate( axisType axis, velocityUnits units );

Parameters

范围

类型

描述

axis

axisType

The axis to return the gyro rate from:

  • xaxis
  • yaxis
  • zaxis

units

velocityUnits

The gyro rate unit:

  • dps — degrees per second
  • rpm — revolutions per minute

Return Values

Returns a double representing the current rotation speed along the specified axis in the selected units.

Examples
// Display the rate of change of rotation while turning in dps
Drivetrain.turn(right);
wait(1, seconds);

Brain.Screen.print(myInertial.gyroRate(zaxis, dps));

Drivetrain.stop();

orientation#

Orientation is the Inertial Sensor’s current angle on a selected axis. This function returns the orientation of the Inertial Sensor, from -180.00 to 180.00 degrees.

The selected x-, y-, or z-axis corresponds to one of the sensor’s orientation axes.

Available Functions
double orientation(
  axisType      axis,
  rotationUnits units );

Parameters

范围

类型

描述

axis

axisType

The axis for which orientation is returned:

  • xaxis
  • yaxis
  • zaxis

units

rotationUnits

The rotation unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

Returns a double representing the selected orientation angle in the specified units.

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

collision#

Registers a callback function that runs when the Inertial Sensor detects a sudden impact or collision.

Available Functions
void collision( void (* callback)(axisType, double, double, double) );

Parameters

范围

类型

描述

callback

void (*)(axisType, double, double, double)

A pointer to a function that will be called when a collision is detected. The callback function must accept the following parameters:

  • axisType — The axis on which the collision was detected.
  • Acceleration values at the time of impact, in order:
    • double — Acceleration along the x-axis (in G).
    • double — Acceleration along the y-axis (in G).
    • double — Acceleration along the z-axis (in G).
The function must return void.

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

// Called when the Inertial Sensor detects a collision
void onCollision(axisType axis, double x, double y, double z) {
  Brain.Screen.print("Collision detected!");
}

Register the callback inside int main()

/* vexcodeInit() is only required when using VEXcode.
Remove vexcodeInit() if compiling in VS Code. */
int main() {

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

setTurnType#

Sets which turn direction returns positive values.

Available Functions
void setTurnType( turnType dir );

Parameters

范围

类型

描述

dir

turnType

The direction that returns positive values:

  • right
  • left

Return Values

此函数不返回值。

Notes

getTurnType#

Returns which turn direction produces positive values.

Available Functions
turnType getTurnType();

Parameters

此函数不接受任何参数。

Return Values

Returns a turnType indicating which direction produces positive values:

  • right — Clockwise rotation returns positive values.
  • left — Counterclockwise rotation returns positive values.
Notes