accelerometer#

Initializing the accelerometer Class#

An Accelerometer is created by using one of the following constructors:

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

Parameter

Description

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.

Parameter

Description

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.

For optimal functionality, it is recommended to use separate Accelerometers for each axis, initializing them individually:

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

Class Methods#

acceleration()#

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

Returns: A double in the range +/- 6G, or +/-2G if high sensitivity mode is set.

// 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.

Parameters

Description

callback

A function that will be called when the Accelerometer’s axis value changes.

Returns: An instance of the Event class.

// 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);
}