Acelerómetro#
Inicialización de la clase de acelerómetro#
Un acelerómetro se crea utilizando el siguiente constructor:
Accelerometer(port, sensitivity)
Este constructor utiliza dos parámetros:
Parámetro |
Descripción |
|---|---|
|
The 3-Wire Port that the Accelerometer is connected to, whether it’s a port on the |
|
Optional. Enables high sensitivity mode (+/- 2g) on the Accelerometer. |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Accelerometer Class constructor.
# Create the Brain.
brain = Brain()
# Construct an Accelerometer "accel" with the
# Accelerometer class.
accel = Accelerometer(brain.three_wire_port.a)
This accel object will be used in all subsequent examples throughout this API documentation when referring to Accelerometer class methods.
Para una funcionalidad óptima, se recomienda utilizar acelerómetros separados para cada eje, inicializándolos individualmente:
accel_x = Accelerometer(brain.three_wire_port.a)
accel_y = Accelerometer(brain.three_wire_port.b)
accel_z = Accelerometer(brain.three_wire_port.c)
Métodos de clase#
acceleration()#
The acceleration() method reads the value of the Accelerometer scaled to units of gravity.
Devuelve: Un valor en el rango +/- 6G, o +/-2G si está configurado el modo de alta sensibilidad.
# Drive the robot forward.
drivetrain.drive(FORWARD)
# Get the Accelerometer's acceleration.
value = accel.acceleration()
changed()#
The changed(callback, arg) method registers a function to be called when the value of the accelerometer changes.
Parámetros |
Descripción |
|---|---|
|
Una función que se llamará cuando cambie el valor del eje. |
|
Opcional. Una tupla que se utiliza para pasar argumentos a la función de devolución de llamada. |
Devuelve: Una instancia de la clase Event.
# Drive the robot forward.
drivetrain.drive(FORWARD)
# Define the function accelerometer_changed.
def accelerometer_changed():
# The Brain will print that the Accelerometer's value
# changed on the Brain's screen.
brain.screen.print("accelerometer changed")
# Run accelerometer_changed when the value of the
# Accelerometer changes.
accel.changed(accelerometer_changed)