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 |
|---|---|
|
The 3-Wire Port that the Gyro Sensor is connected to, whether it’s a port on the |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Gyro Class constructor.
# 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 |
|---|---|
|
El valor a utilizar para el nuevo encabezado. |
|
Optional. A valid |
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 |
|---|---|
|
El valor de rotación a establecer. |
|
Optional. A valid |
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 |
|---|---|
|
Optional. A valid |
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 |
|---|---|
|
Optional. A valid |
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 |
|---|---|
|
A valid |
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 |
|---|---|
|
La función de devolución de llamada que se llamará cuando cambie el rumbo del sensor giroscópico. |
|
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)