軸#

介紹#

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