Axis#

Introduction#

The axis class is a derived class of the controller class. It provides access to controller joystick axis values and joystick axis change callbacks.

An axis object is accessed through a controller object. It is not constructed directly.

The IQ 1st gen Controller with the four joystick axes highlighted.

Access#

The controller class provides the following axis objects. Each object is an instance of the axis class.

Axis Object

Example Usage

Description

Axis1

Controller.Axis1.position()

Right joystick horizontal axis.

Axis2

Controller.Axis2.position()

Right joystick vertical axis.

Axis3

Controller.Axis3.position()

Left joystick vertical axis.

Axis4

Controller.Axis4.position()

Left joystick horizontal axis.

Notes#

  • Before using an axis member function, create a controller object.

  • position returns a value from -100 to 100.

// Create a controller object
controller Controller = controller();

Member Functions#

The axis class includes the following member functions:

Getter — Read joystick axis position.

  • position — Returns the position of the joystick axis.

Callback — Run a function when the joystick’s position changes along an axis.

  • changed — Runs a function when the joystick’s position changes along that axis.

Getter#

position#

position returns the position of the joystick axis as a value from -100 to 100.

Available Function

int32_t position(percentUnits units = percentUnits::pct) const;

Parameter

Type

Description

units

percentUnits

Optional. The unit used for the returned axis value. The default is percentUnits::pct.

Return Value

Returns an int32_t value from -100 to 100.

  • 100 — The joystick axis is fully moved in the positive direction.

  • 0 — The joystick axis is centered.

  • -100 — The joystick axis is fully moved in the negative direction.

Example

// Turn with the left joystick
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);
}

Callback#

changed#

changed runs a function when the joystick’s position changes along that axis.

Available Function

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

Parameter

Type

Description

callback

void (*)(void)

A function to run each time the joystick’s position changes along that axis.

Parameter

Type

Description

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.

Example

Define the callback function outside of main.

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

// Play a sound when the left joystick moves
 void sounds() {
     Brain.playSound(siren);
     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 function when the left joystick moves
  Controller.Axis4.changed(sounds);
}