Giroscopio#

Inicializando la clase Gyro#

Un sensor giroscópico se crea utilizando el siguiente constructor:

Gyro(port)

Este constructor utiliza un parámetro:

Parámetro

Descripción

port

El puerto de 3 cables al que está conectado el sensor giroscópico, ya sea un puerto en el Cerebro o un Expandedor de 3 cables.

Primero se debe crear un Brain o un 3-Wire Expander antes de poder usarlos para crear un objeto con el constructor de la clase Gyro.

# Create the Brain.
brain = Brain()
# Construct a Gyro Sensor "gyro" with the
# Gyro class.
gyro = Gyro(brain.three_wire_port.a)

This gyro object will be used in all subsequent examples throughout this API documentation when referring to Gyro class methods.

Métodos de clase#

calibrate()#

The calibrate() method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

Devoluciones: Ninguna.

# Start Gyro Sensor calibration.
gyro.calibrate()
# Print that the Gyro Sensor is calibrating while waiting for it to finish calibrating.
while gyro.is_calibrating():
    brain.screen.clear()
    brain.screen.print("Gyro Sensor is calibrating.")
    wait(50, MSEC)

is_calibrating()#

The is_calibrating() method checks if the Gyro Sensor is currently calibrating.

Returns: True if the Gyro Sensor is calibrating. False if it is not.

# Start Gyro Sensor calibration.
gyro.calibrate()
# Print that the Gyro Sensor is calibrating while waiting for it to finish calibrating.
while gyro.is_calibrating():
    brain.screen.clear()
    brain.screen.print("Gyro Sensor is calibrating.")
    wait(50, MSEC)

reset_heading()#

The reset_heading() method resets the heading of the Gyro Sensor to 0. Returns: None.

reset_rotation()#

The reset_rotation() method resets the rotation of the Gyro Sensor to 0.

Devoluciones: Ninguna.

set_heading()#

The set_heading(value, units) method sets the heading of the Gyro Sensor to a specific value.

Parámetros

Descripción

valor

El valor a utilizar para el nuevo encabezado.

unidades

Optional. A valid RotationUnits type. The default is DEGREES.

Devoluciones: Ninguna.

# Set the value of the Gyro Sensor's heading to 180 degrees.
gyro.set_heading(180)

set_rotation()#

The set_rotation(value, units) method sets the rotation of the Gyro Sensor.

Parámetros

Descripción

valor

El valor de rotación a establecer.

unidades

Optional. A valid RotationUnits type. The default is DEGREES.

Devoluciones: Ninguna.

# Set the value of the Gyro Sensor's rotation to 180 degrees.
gyro.set_rotation(180)

heading()#

The heading(units) method returns the current heading of the Gyro Sensor.

Parámetros

Descripción

unidades

Optional. A valid RotationUnits type. The default is DEGREES.

Devuelve: El rumbo actual del sensor giroscópico en las unidades especificadas.

# Print the value of the current heading of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.heading())

rotation()#

The rotation(units) method returns the current rotation of the Gyro Sensor.

Parámetros

Descripción

unidades

Optional. A valid RotationUnits type. The default is DEGREES.

Devuelve: La rotación actual del sensor giroscópico en las unidades especificadas.

# Print the value of the current rotation of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.rotation())

set_turn_type()#

The set_turn_type(turntype) method sets the direction that returns positive values for the heading.

Parámetros

Descripción

tipo de giro

Un TurnType válido.

Devoluciones: Ninguna.

# Set the turn type to left.
gyro.set_turn_type(LEFT)

# Set the turn type to right.
gyro.set_turn_type(RIGHT)

get_turn_type()#

The get_turn_type() method returns the direction that returns positive values for heading.

Devuelve: El tipo de giro del sensor giroscópico en las unidades especificadas.

changed()#

The changed(callback, arg) method registers a callback function for when the Gyro Sensor’s heading changes.

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.

Devuelve: Una instancia de la clase Event.

# Define a function gyro_changed.
def gyro_changed():
    # The Brain will print that the heading changed on
    # the Brain's screen.
    brain.screen.print("heading changed") 
# Run gyro_changed() when the Gyro Sensor's heading changes.
gyro.changed(gyro_changed)