Inertial Sensor#
Introducción#
The inertial class is used to control and access data from the IQ (2nd gen) Brain’s built-in Inertial Sensor. This sensor measures how the Brain is moving and turning.
The Inertial Sensor uses two parts to do this. 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.
Class Constructors#
inertial(
turnType dir = turnType::right );
Class Destructor#
Destroys the inertial object and releases associated resources.
~inertial();
Parámetros#
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
Optional. The direction that returns positive values:
|
Examples#
// Create the inertial instance
inertial myInertial = inertial(
right); // dir
Member Functions#
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 callback function that runs 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 callback function that runs when a collision is detected.setTurnType— Sets which turn direction returns positive heading 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 with the IQ (2nd gen) Brain. Replace the values
as needed. */
// Create the inertial instance
inertial myInertial = inertial();
heading#
A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. This function returns the sensor’s current heading.
The starting heading is 0 degrees. If the sensor turns past 359.99 degrees, the heading wraps back to 0 degrees.
Available Functionsdouble heading( rotationUnits units = degrees );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The heading unit:
|
Returns a double representing the current heading of the sensor in the specified units.
The heading value is wrapped between 0 and 359.99 degrees. Use
rotationto access the continuous rotation value.
// 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 Functionsdouble rotation( rotationUnits units = degrees );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The rotation unit:
|
Returns a double representing the current rotation in the specified units.
The rotation value is continuous and does not wrap between 0 and 359.99 degrees. Use
headingto access the wrapped heading value.
// 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 Functionsvoid setHeading( double value,
rotationUnits units );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The heading value, in degrees, to set for the sensor. This can be a value from 0 to 359.99. |
|
|
The heading unit:
|
This function does not return a value.
Examples// Turn the robot around
myInertial.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);
setRotation#
Sets the Inertial Sensor’s current rotation to a specified value.
Rotation is how much the sensor has turned. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees.
Available Functionsvoid setRotation( double value,
rotationUnits units );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The rotation value, in degrees, to set for the sensor. This can be an integer or a decimal. |
|
|
The rotation unit:
|
This function does not return a value.
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 Functionsvoid calibrate( );
This function does not accept any parameters.
Return ValuesThis function does not return a value.
NotesThe sensor should remain stationary while calibration is in progress.
Use
isCalibratingto 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 Functionsvoid resetHeading();
This function does not take any parameters.
Return ValuesThis function does not return a value.
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 Functionsvoid resetRotation();
This function does not take any parameters.
Return ValuesThis function does not return a value.
isCalibrating#
Returns whether the Inertial Sensor is currently calibrating.
Available Functionsbool isCalibrating();
This function does not take any parameters.
Return ValuesReturns a Boolean indicating whether the Inertial Sensor is calibrating:
-
true— The sensor is currently calibrating. -
false— The sensor is not currently calibrating.
changed#
Registers a callback function that runs when the heading value of the Inertial Sensor changes.
Available Functionsvoid changed( void (* callback)(void) );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
A pointer to a function that will be called when the heading value changes. The function must take no parameters and return |
This function does not return a value.
Examples// 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());
}
myInertial.changed(onHeadingChanged);
installed#
Returns whether the Inertial Sensor is installed and connected.
Available Functionsbool installed();
This function does not take any parameters.
Return ValuesReturns 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.
double acceleration( axisType axis );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The axis to measure acceleration on:
|
Returns a double representing the acceleration along the specified axis in G (gravitational units).
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 Functionsdouble gyroRate( axisType axis,
velocityUnits units );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The axis to return the gyro rate from:
|
|
|
The gyro rate unit:
|
Returns a double representing the current rotation speed along the specified axis in the selected units.
// 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 turning axis. This function returns the current orientation of the Inertial Sensor for the specified axis.
Roll, pitch, and yaw describe different ways the sensor can tilt or turn.
Available Functionsdouble orientation(
orientationType type,
rotationUnits units );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The orientation angle to return:
|
|
|
The orientation unit:
|
Returns a double representing the selected orientation angle in the specified units.
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(roll, 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 Functionsvoid collision( void (* callback)(axisType, double, double, double) );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
A pointer to a function that will be called when a collision is detected. The callback function must accept the following parameters:
void. |
This function does not return a value.
Examples// Called when the Inertial Sensor detects a collision
void onCollision(axisType axis, double x, double y, double z) {
Brain.Screen.print("Collision detected!");
}
// 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 Functionsvoid setTurnType( turnType dir );
Parámetro |
Type |
Descripción |
|---|---|---|
|
|
The direction that returns positive values:
|
This function does not return a value.
NotesThis setting affects how
headingandrotationreport positive and negative values.Calling this function overrides the
dirvalue specified when constructing theinertialobject.
getTurnType#
Returns which turn direction produces positive values.
Available FunctionsturnType getTurnType();
This function does not take any parameters.
Return ValuesReturns a turnType indicating which direction produces positive values:
-
right— Clockwise rotation returns positive values. -
left— Counterclockwise rotation returns positive values.
This value is set during construction of the
inertialobject or by callingsetTurnType.