Giroscopio#

Introducción#

El sensor giroscópico es un sensor de movimiento que detecta la rotación sobre un solo eje. Puede medir cuánto ha girado el robot, lo que le permite rastrear su orientación y dirección.

For the examples below, the configured Gyro Sensor will be named drivetrain_gyro and will be used in all subsequent examples throughout this API documentation when referring to Gyro class methods.

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

Acciones: calibrar o ajustar el rumbo y la rotación del sensor giroscópico.

  • calibrate – Calibrates the Gyro Sensor for stable heading tracking.

  • set_heading – Sets the Gyro Sensor’s heading to a specified value.

  • set_rotation – Sets the Gyro Sensor’s rotation value.

  • reset_heading – Sets the heading of the Gyro Sensor to 0.

  • reset_rotation – Sets the rotation of the Gyro Sensor to 0.

Obtenedores: leen el rumbo, la rotación y el estado del sensor giroscópico.

  • heading – Returns the current heading.

  • rotation – Returns the cumulative rotation.

  • rate – Returns the angular velocity around the x, y, or z axis.

  • is_calibrating – Returns whether or not the Gyro Sensor is calibrating.

  • installed – Returns whether or not a Gyro Sensor is connected to the Brain.

Devolución de llamada: ejecuta el código cuando el sensor giroscópico detecta movimiento o cambio.

  • changed – Registers a function to call when the Gyro Sensor detects change.

Constructores: inicializan y configuran manualmente un sensor giroscópico.

  • Gyro – Creates a Gyro Sensor.

Comportamiento#

calibrate#

calibrate calibrates the Gyro Sensor. All subsequent lines will wait for the calibration to complete before executing. Calibration is an internal procedure that measures and compensates for sensor noise and drift over a specified period. During this time, the Gyro Sensor must remain completely still (i.e., on a stable surface without any external movement). Movement during calibration will produce inaccurate results.

Usage:
calibrate()

Parámetro

Descripción

Este método no tiene parámetros.

# Start Gyro Sensor calibration
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
brain.screen.print("Done!")

set_heading#

set_heading sets the heading of the Gyro Sensor to a specific value.

Usage:
set_heading(value, units)

Parámetros

Descripción

valor

El valor del encabezado a establecer.

unidades

Optional. The unit used to represent the new heading:

  • DEGREES (default)
  • TURNS
# Turn the robot around
drivetrain_gyro.set_heading(180)
drivetrain.turn_to_heading(0)

set_rotation#

set_rotation sets the rotation of the Gyro Sensor to a specific value.

Usage:
set_rotation(value, units)

Parámetros

Descripción

valor

El valor del encabezado a establecer.

unidades

Optional. The unit used to represent the new rotation value:

  • DEGREES (default)
  • TURNS
# Turn the robot around
drivetrain_gyro.set_rotation(-180)
drivetrain.turn_to_rotation(0)

reset_heading#

reset_heading resets the heading of the Gyro Sensor to 0.

Usage:
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)
drivetrain_gyro.reset_heading()
drivetrain.turn_to_heading(90, DEGREES)

reset_rotation#

reset_rotation resets the rotation of the Gyro Sensor to 0.

Usage:
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)
drivetrain_gyro.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)

Captadores#

heading#

heading returns the current heading of the Gyro Sensor.

Usage:
heading(units)

Parámetros

Descripción

unidades

Optional. The unit used to represent the heading:

  • DEGREES (default)
  • TURNS
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.heading())

rotation#

rotation returns the current rotation of the Gyro Sensor.

Usage:
rotation(units)

Parámetros

Descripción

unidades

Optional. The unit used to represent the rotation:

  • DEGREES (default)
  • TURNS
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.rotation())

rate#

rate returns the current rate of change of the Gyro Sensor’s rotation in specified units.

Usage:
rate(units)

Parámetros

Descripción

unidades

Optional. The unit used to represent the gyro rate:

  • DPS (default)
  • RPM
  • PERCENT
# Display the rate of change of 
# rotation while turning
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(drivetrain_gyro.rate())
drivetrain.stop()

is_calibrating#

is_calibrating checks if the Gyro Sensor is currently calibrating.

  • True - The Gyro Sensor is calibrating.

  • False - The Gyro Sensor is not calibrating.

Usage:
is_calibrating()

Parámetro

Descripción

Este método no tiene parámetros.

# Start Gyro Sensor calibration.
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
brain.screen.print("Done!")

installed#

installed returns a Boolean indicating whether the Gyro Sensor is connected to the Brain.

  • True - The Gyro Sensor is connected to the Brain.

  • False - The Gyro Sensor is not connected to the Brain.

Usage:
installed()

Parámetros

Descripción

Este método no tiene parámetros.

# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
    brain.screen.print("Installed!")

Llamar de vuelta#

changed#

changed registers a callback function for when the Gyro Sensor’s heading changes.

Usage:
changed(callback, arg)

Parámetros

Descripción

llamar de vuelta

La función de devolución de llamada que se llamará cuando cambie el rumbo del sensor giroscópico.

arg

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

def gyro_changed():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Heading: ")
    brain.screen.print(drivetrain_gyro.heading()) 
# Display the heading when the gyro detects a change
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
drivetrain_gyro.changed(gyro_changed)

Constructores#

Constructors are used to manually create Gyro objects, which are necessary for configuring an Gyro Sensor outside of VEXcode.

Gyro#

Gyro creates a Gyro Sensor.

Usage:
Gyro(port)

Parámetro

Descripción

port

Which Smart Port that the Gyro Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

# Create the Brain
brain = Brain()
# When constructing Gyro Sensor "drivetrain_gyro" with a 
# SmartDrive "drivetrain"
drivetrain_gyro = Gyro(Ports.PORT1)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, drivetrain_gyro, 200)

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