Axis#

Introduction#

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

A VEX V5 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

Description

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 a V5 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

This function does not accept any parameters.

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

Parameter

Description

callback

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
// Play a rumble pattern when the left joystick moves
void rumblePattern() {
    Controller.rumble("..--");
    wait(1, seconds);
}
// Call the rumble function when the left joystick moves
Controller.Axis4.changed(rumblePattern);