#

Introduction#

The axis class is derived from the controller base class and provides access to the EXP Controller’s joysticks, allowing your robot to monitor changes in position along the four available axes.

A VEX EXP Controller with the axes around the joysticks labeled. Axis 1 and 2 are around the right joystick, and Axis 3 and 4 are around the left.

Access#

The controller class provides four axis objects (Axis1Axis4). Each object is an instance of the axis class and can be accessed through a controller instance.

Axis Object

Example Usage

描述

Axis1

Controller.Axis1

Right joystick horizontal axis.

Axis2

Controller.Axis2

Right joystick vertical axis.

Axis3

Controller.Axis3

Left joystick vertical axis.

Axis4

Controller.Axis4

Left joystick horizontal axis.

Notes#

  • The axis object is provided by the controller. It is not constructed directly.

Member Functions#

The Axis class includes the following member functions:

  • position — Returns the position of the joystick’s specified axis.

  • changed — Registers a function to be called when the joystick’s position changes.

Before calling any axis member functions, a controller instance must be created, as shown below:

// Create an EXP controller instance
controller Controller = controller();

position#

Returns the position of a joystick axis as an integer from -100 to 100, representing a percentage.

Available Functions
int32_t  position( 
  percentUnits units = percentUnits::pct ) const

Parameters

范围

Type

描述

units

percentUnits

The unit that represents the position:

  • percent / pct — percent

Return Values

This function returns an int32_t as a percent from -100 to 100.

Examples

This function can be called on any controller axis object, for example:

  • Controller.Axis1
  • Controller.Axis2
  • Controller.Axis3
  • Controller.Axis4
while (true) {
    if (Controller.Axis4.position() > 10) {
        Drivetrain.turn(right);
    } else if (Controller.Axis4.position() < -10) {
        Drivetrain.turn(left);
    } else {
        Drivetrain.stop();
    }
    wait(20, msec);
}

changed#

Registers a callback function that runs when a joystick axis position changes.

Available Functions

The controller provides four axis objects (Axis1Axis4):

void changed( 
  void (* callback)(void) ) const;

Parameters

范围

Type

描述

callback

void (*)(void)

A previously defined callback function that is called automatically when the axis value changes. The function must match the required callback signature. See Callback Functions for more information.

Return Values

This function does not return a value.

Examples

This function can be called on any controller axis object, for example:

  • Controller.Axis1
  • Controller.Axis2
  • Controller.Axis3
  • Controller.Axis4

Define the callback function (outside of int main())

// Play a rumble pattern when the left joystick moves
void rumblePattern() {
    Controller.rumble("..--");
    wait(1, seconds);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call the rumble function when the left joystick moves
  Controller.Axis4.changed(rumblePattern);
}