Inercial#

Introducción#

The IQ (2nd gen) Brain includes a 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.

This page uses brain_inertial as the example Inertial Sensor name. Replace it with your own configured name as needed.

A continuación se muestra una lista de todos los métodos disponibles:

Orientación: lee y controla el rumbo, la rotación y la calibración del sensor.

  • heading — Returns the direction the sensor is facing, from 0 to 359.99 degrees.

  • rotation — Returns how far the sensor has turned.

  • calibrate — Calibrates the Inertial Sensor.

  • set_heading — Sets the Inertial Sensor’s current heading to a new heading value.

  • set_rotation — Sets the Inertial Sensor’s current rotation to a new rotation value.

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

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

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

  • changed — Registers a function to run when the Inertial Sensor’s heading changes.

Motion — Measure acceleration, rotation speed, and tilt.

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

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

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

Constructores: Inicialice y configure manualmente un sensor inercial.

  • Inertial — Creates an Inertial Sensor.

Orientación#

heading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. heading 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.

Usage:
brain_inertial.heading(units)

Parámetros

Descripción

units

Optional. The heading unit: DEGREES (default) or TURNS

# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())

rotation#

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. rotation returns the Inertial Sensor’s current rotation.

Turning right increases the rotation, and turning left decreases the rotation. For example, making two full turns to the right will return a rotation of 720 degrees. Turning one full turn to the left from 0 degrees will return a rotation of -360 degrees.

Usage:
brain_inertial.rotation(units)

Parámetros

Descripción

units

Optional. The rotation unit: DEGREES (default) or TURNS

# Display the rotation value after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.rotation())

calibrate#

calibrate calibrates the Inertial Sensor. All subsequent lines will wait for calibration to complete before running. Calibration helps the sensor measure turns correctly by measuring and compensating for sensor noise and drift over a specified period. During this time, the Brain must remain completely still on a stable surface. Movement during calibration can produce inaccurate results.

El programa IQ Brain muestra la pantalla inclinada hacia la derecha, con los ejes x, y y z representados por flechas que parten del centro de la pantalla y se extienden hacia afuera. Una flecha roja con la letra x apunta hacia la derecha, una flecha verde con la letra y apunta hacia adelante y una flecha azul con la letra z apunta hacia abajo.La misma imagen que la anterior, ahora con la pantalla del cerebro inclinada hacia la izquierda y las flechas x, y, z manteniendo su orientación.La misma imagen que la anterior, ahora con la pantalla cerebral hacia arriba, como si estuviera plana sobre una superficie. La orientación x, y, z es la misma.

The VEX Brain automatically attempts to calibrate the built-in Inertial Sensor at the start of every project. However, if the robot is being carried or moved during project start, the sensor may fail to calibrate properly or yield incorrect calibration.

Usage:
brain_inertial.calibrate()

Parámetro

Descripción

Este método no tiene parámetros.

# Start calibration
brain_inertial.calibrate()
# Print after calibration
while brain_inertial.is_calibrating():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Inertial Sensor")
    brain.screen.next_row()
    brain.screen.print("Calibrating")
    wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")

set_heading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. set_heading() 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.

Usage:
brain_inertial.set_heading(value, units)

Parámetros

Descripción

value

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

units

Optional. The heading unit: DEGREES (default) or TURNS

# Turn the robot around
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)

set_rotation#

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. set_rotation() 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.

Usage:
brain_inertial.set_rotation(value, units)

Parámetros

Descripción

value

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

units

Optional. The rotation unit: DEGREES (default) or TURNS

# Turn the robot around
brain_inertial.set_rotation(-180)
drivetrain.turn_to_rotation(0)

is_calibrating#

is_calibrating returns whether the Inertial Sensor is currently calibrating.

  • True — The Inertial Sensor is calibrating.

  • False — The Inertial Sensor is not calibrating.

Usage:
brain_inertial.is_calibrating()

Parámetro

Descripción

Este método no tiene parámetros.

# Start calibration
brain_inertial.calibrate()
# Print while waiting for calibration
while brain_inertial.is_calibrating():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Inertial Sensor")
    brain.screen.next_row()
    brain.screen.print("Calibrating")
    wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")

reset_heading#

reset_heading resets the Inertial Sensor’s current heading to 0 degrees.

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

Usage:
brain_inertial.reset_heading()

Parámetros

Descripción

Este método no tiene parámetros.

# Turn the robot before and after resetting the heading
drivetrain.turn_to_heading(90, DEGREES)
wait(0.5, SECONDS)
brain_inertial.reset_heading()
drivetrain.turn_to_heading(90, DEGREES)

reset_rotation#

reset_rotation resets the Inertial Sensor’s current rotation to 0 degrees.

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

Usage:
brain_inertial.reset_rotation()

Parámetros

Descripción

Este método no tiene parámetros.

# Turn the robot before and after resetting the rotation
drivetrain.turn_to_rotation(-90, DEGREES)
wait(0.5, SECONDS)
brain_inertial.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)

changed#

changed registers a function to be called when the Inertial Sensor’s heading changes.

Usage:
brain_inertial.changed(callback, arg)

Parámetros

Descripción

callback

A function that runs when the Inertial Sensor heading changes.

arg

Opcional. Una tupla de argumentos para pasar a la función de devolución de llamada.

def heading_changed():
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print("my heading ")
    brain.screen.next_row()
    brain.screen.print("has changed!")
    wait(0.1, SECONDS)
# Call the function when the inertial heading is changed
wait(1, SECONDS)
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
brain_inertial.changed(heading_changed)
wait(15, MSEC)

Movimiento#

acceleration#

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

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. The axis options are XAXIS, YAXIS, and ZAXIS.

Usage:
brain_inertial.acceleration(axis)

Parámetros

Descripción

eje

The axis to measure acceleration on:

  • XAXIS
  • YAXIS
  • ZAXIS

# Display acceleration after moving
vexcode_brain_precision = 2
drivetrain.set_drive_velocity(100, PERCENT)
brain.screen.print("Resting: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))
brain.screen.next_row()
wait(1, SECONDS)
drivetrain.drive_for(FORWARD, 500, MM, wait=False)
wait(0.01, SECONDS)
brain.screen.print("Startup: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))

gyro_rate#

Gyro rate is how fast the Inertial Sensor is rotating. gyro_rate 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.

Usage:
brain_inertial.gyro_rate(axis, units)

Parámetros

Descripción

axis

The axis to return the gyro rate from:

  • XAXIS
  • YAXIS
  • ZAXIS

units

Optional. The gyro rate unit:

  • DPS (default) — degrees per second
  • RPM — rotations per minute
  • PERCENT

# Display the z-axis gyro rate
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(brain_inertial.gyro_rate(ZAXIS, RPM))
drivetrain.stop()

orientation#

Orientation is the Inertial Sensor’s current angle on a selected turning axis. orientation returns the roll, pitch, or yaw of the Inertial Sensor.

Roll, pitch, and yaw describe different ways the sensor can tilt or turn.

Usage:
brain_inertial.orientation(axis, units)

Parámetros

Descripción

axis

The orientation angle to return:

  • ROLL
  • PITCH
  • YAW

units

Optional. The orientation unit: DEGREES (default) or TURNS

# Display the roll, pitch, and yaw of the Brain as it
# is rotated by hand
while True:
    brain.screen.clear_screen()
    brain.screen.print(brain_inertial.orientation(OrientationType.ROLL))
    brain.screen.next_row()
    brain.screen.print(brain_inertial.orientation(OrientationType.PITCH))
    brain.screen.next_row()
    brain.screen.print(brain_inertial.orientation(OrientationType.YAW))
    brain.screen.next_row()
    brain.screen.set_cursor(1, 1)
    wait(0.1, SECONDS)

Constructores#

Constructors are used to manually create Inertial objects, such as when configuring an Inertial Sensor outside of VEXcode.

Inertial#

Inertial creates an Inertial Sensor.

Usage:
Inertial(smartport)

Parámetro

Descripción

smartport

Optional. If using the IQ (2nd gen) Brain’s built-in Inertial Sensor, a Smart Port is not needed. If connecting an external Inertial Sensor, specify the Smart Port that the Inertial Sensor is connected to, written as PORTx, where x is the port number.

# Create a new object "brain_inertial" with the
# Inertial class
brain_inertial = Inertial()
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)

drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)

brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)