#

介绍#

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.

IQ 第二代控制器,四个摇杆轴已高亮显示。左侧摇杆的轴 3 为垂直方向,轴 4 为水平方向。右侧摇杆的轴 1 为水平方向,轴 2 为垂直方向。

使用权#

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

轴对象

用法示例

描述

Axis1

Controller.Axis1.position()

右摇杆水平轴。

Axis2

Controller.Axis2.position()

右摇杆垂直轴。

Axis3

Controller.Axis3.position()

左摇杆垂直轴。

Axis4

Controller.Axis4.position()

左摇杆水平轴。

笔记#

  • 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();

成员功能#

The axis class includes the following member functions:

获取器 — 读取操纵杆轴位置。

  • position — Returns the position of the joystick axis.

回调函数——当摇杆沿某个轴的位置发生变化时,运行一个函数。

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

Getter#

位置#

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

可用功能

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

范围

类型

描述

units

percentUnits

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

返回值

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.

例子

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

打回来#

已更改#

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

可用功能

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

范围

类型

描述

callback

void (*)(void)

每次操纵杆沿该轴位置发生变化时运行的函数。

返回值

此函数不返回值。

例子

Define the callback function outside of main.

// Play a sound when the left joystick moves
void playSound() {
  Brain.playSound(siren);
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run playSound when the joystick's position changes along Axis4
  Controller.Axis4.changed(playSound);
}