inertial#
Introduction#
The Inertial Sensor measures the rotation and orientation of the V5 robot. The sensor can report movement along the x, y, and z axes and can be used to determine heading, rotation, and tilt. The sensor can also measure its acceleration along the x, y, and z axes.
To make Inertial commands appear in VEXcode V5, an Inertial Sensor must be configured in the Devices window.
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.
Below is a list of available methods:
Orientation — Read and control heading, rotation, and sensor calibration.
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.
Motion — Measure acceleration, angular velocity, and tilt.
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.
Constructor — Manually initialize and configure an Inertial Sensor.
inertial— Creates an Inertial Sensor.
Orientation#
heading#
heading returns the current heading of the Inertial Sensor in the specified units as a float.
Usage:
DrivetrainInertial.heading(units)
Parameters |
Description |
|---|---|
|
The unit used to represent the heading:
|
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.
Usage:
DrivetrainInertial.rotation(units)
Parameters |
Description |
|---|---|
|
The unit used to represent the rotation:
|
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.
Usage:
DrivetrainInertial.setHeading(value, units);
Parameters |
Description |
|---|---|
|
The heading value to set from 0 to 359 as a double. |
|
The unit used to represent the heading:
|
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.
Usage:
DrivetrainInertial.setRotation(value, units);
Parameters |
Description |
|---|---|
|
The rotation value to set as a double. |
|
The unit used to represent the rotation:
|
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.
Usage:
Inertial1.calibrate();
Parameters |
Description |
|---|---|
This method has no parameters. |
resetHeading#
resetHeading resets the heading of the Inertial Sensor to 0.
Usage:
Inertial1.resetHeading();
Parameters |
Description |
|---|---|
This method has no parameters. |
resetRotation#
resetRotation resets the rotation of the Inertial Sensor to 0.
Usage:
Inertial1.resetRotation();
Parameters |
Description |
|---|---|
This method has no parameters. |
isCalibrating#
isCalibrating checks if the Inertial Sensor is currently calibrating.
True— The Inertial Sensor is calibrating.False— The Inertial Sensor is not calibrating.
Usage:
Inertial1.isCalibrating()
Parameters |
Description |
|---|---|
This method has no parameters. |
changed#
changed registers a callback function that runs when the Inertial Sensor’s heading value changes.
Usage:
DrivetrainInertial.changed(callback);
Parameters |
Description |
|---|---|
|
A previously defined function that executes when the heading value changes. |
Callback Signature:
void callback();
Arguments |
Description |
|---|---|
This callback function has no arguments. |
// 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()
Parameters |
Description |
|---|---|
This method has no parameters. |
Motion#
acceleration#
acceleration returns the acceleration of the Inertial Sensor along the specified axis as a double.
Usage:
Inertial1.acceleration(axis)
Parameters |
Description |
|---|---|
|
The axis to return the acceleration from:
|
gyroRate#
gyroRate returns the gyro rate for one axis of the Inertial Sensor in the specified units as a double.
Usage:
DrivetrainInertial.gyroRate(axis, units)
Parameters |
Description |
|---|---|
|
The axis to return the gyro rate from:
|
|
The unit used to represent the gyro rate:
|
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.
Usage:
DrivetrainInertial.orientation(type, units)
Parameters |
Description |
|---|---|
|
The axis to return the orientation from:
|
|
The unit used to represent the orientation:
|
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);
Parameters |
Description |
|---|---|
|
A previously defined callback function that is called automatically when the axis value changes. The function must match the required callback signature. See Callback Functions for more information. |
Callback Signature:
void callback(axisType axis, double x, double y, double z);
Arguments |
Description |
|---|---|
|
The axis on which the collision was detected:
|
|
Acceleration reading on the x-axis at the time of the collision as a double. |
|
Acceleration reading on the y-axis at the time of the collision as a double. |
|
Acceleration reading on the z-axis at the time of the collision as a double. |
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);
Parameters |
Description |
|---|---|
|
The turn direction that will return positive values:
|
getTurnType#
getTurnType returns the direction that returns positive values for heading.
left— The turn type is left.right— The turn type is right.
Usage:
Inertial1.getTurnType();
Parameters |
Description |
|---|---|
This method has no parameters. |
Constructors#
inertial#
inertial creates an Inertial Sensor.
Usage:
inertial(smartport)
Parameters |
Description |
|---|---|
|
The Smart Port that the AI Vision Sensor is connected to, written as |
// Create a new object "inertial" with the inertial class.
inertial Inertial1 = inertial(PORT1);