acelerómetro#

Inicializando la clase del acelerómetro#

Un acelerómetro se crea utilizando uno de los siguientes constructores:

The accelerometer constructor creates an Accelerometer in a specified Three Wire Port with High Sensitivity Mode defaulted to false.

Parámetro

Descripción

port

The 3-Wire Port that the Accelerometer is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

// Create the Brain.
brain Brain;

// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A);

The Accelerometer(port, sensitivity) constructor creates an Accelerometer and can enable High Sensitivity Mode.

Parámetro

Descripción

port

The 3-Wire Port that the Accelerometer is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

sensitivity

Enables high sensitivity mode (+/- 2g) on the Accelerometer. true to enable high sensitivity. The default sensitivity is (+/- 6g).

// Create the Brain.
brain Brain;

// Construct an accelerometer "accel" with the
// accelerometer class.
accelerometer accel = accelerometer(Brain.ThreeWirePort.A, true);

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Accelerometer Class constructor.

The 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:

accelerometer accel_x = accelerometer(Brain.ThreeWirePort.A);
accelerometer accel_y = accelerometer(Brain.ThreeWirePort.B);
accelerometer accel_z = accelerometer(Brain.ThreeWirePort.C);

Métodos de clase#

acceleration()#

The acceleration() method reads the value of the Accelerometer scaled to units of gravity.

Devuelve: Un doble en el rango +/- 6G, o +/-2G si está configurado el modo de alta sensibilidad.

// Drive the robot forward.
Drivetrain.drive(forward);

// Get the acceleration of accel in the range +/- 6G.
double accelerationValue = accel.acceleration();

changed()#

The changed(callback) method registers a function to be called when the value of the Accelerometer changes.

Parámetros

Descripción

callback

Una función que se llamará cuando cambie el valor del eje del acelerómetro.

Devuelve: Una instancia de la clase Event.

// Define the accelerometerChanged function with a void
// return type, showing it doesn't return a value.
void accelerometerChanged() {
  // The Brain will print that the Accelerometer changed on
  // the Brain's screen.
  Brain.Screen.print("accelerometer changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive the robot forward.
  Drivetrain.drive(forward);

  // Run accelerometerChanged when the value of the 
  // Accelerometer changes.
  accel.changed(accelerometerChanged);
}