轴#

介绍#

axis 类是 controller 类的派生类。它提供对控制器操纵杆轴值和操纵杆轴更改回调的访问。

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

第一代 IQ 控制器,四个摇杆轴高亮显示。

使用权#

controller 类提供以下轴对象。每个对象都是 axis 类的一个实例。

轴对象

用法示例

描述

Axis1

Controller.Axis1.position()

右摇杆水平轴。

Axis2

Controller.Axis2.position()

右摇杆垂直轴。

Axis3

Controller.Axis3.position()

左摇杆垂直轴。

Axis4

Controller.Axis4.position()

左摇杆水平轴。

笔记#

  • 在使用 axis 成员函数之前,请创建一个 controller 对象。

  • 位置position 返回的值介于 -100 到 100之间。

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

成员功能#

axis 类包含以下成员函数:

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

  • position — 返回操纵杆轴的位置。

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

  • changed — 当操纵杆的位置沿该轴改变时运行函数。

Getter#

position#

position 返回操纵杆轴的位置,值范围为 -100 到 100。

可用功能

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

范围

类型

描述

units

percentUnits

可选。返回的轴值所使用的单位。默认值为 percentUnits::pct。

返回值

Returns an int32_t value from -100 to 100.

  • 100 — 操纵杆轴已完全向正方向移动。

  • 0 — 操纵杆轴居中。

  • -100 — 操纵杆轴完全向负方向移动。

例子

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

changed 当操纵杆的位置沿该轴改变时运行一个函数。

可用功能

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

范围

类型

描述

callback

void (*)(void)

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

范围

类型

描述

callback

void (*)(void)

预先定义的回调函数,当轴值发生变化时会自动调用。该函数必须符合所需的回调函数签名。有关更多信息,请参阅回调函数。

Return Values

此函数不返回值。

例子

在 main之外定义回调函数。

定义回调函数(在 int main()之外)

// Play a sound when the left joystick moves
 void sounds() {
     Brain.playSound(siren);
     wait(1, seconds);
 }

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