限位開關#

介紹#

limit 類與限位開關一起使用,限位開關是一種小型數位感測器,用於偵測與物體的物理接觸。

VEX V5 限位開關。

類別建構函數#

limit(
  triport::port &port);

類別析構函數#

銷毀 limit 物件並釋放相關資源。

virtual ~limit();

參數#

範圍

類型

描述

port

triport::port &

限位開關連接的 3 線端口,寫成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

例子#

// Create a limit instance in Port A
limit LimitSwitchA = limit(Brain.ThreeWirePort.A);

成員功能#

limit 類別包含以下成員函數:

  • pressing – 返回限位開關是否被按下。

  • pressed – 註冊函數,當限位開關被按下時呼叫函數。

  • released – 註冊一個函數,當限位開關釋放時要呼叫函數。

在呼叫任何 limit 成員函數之前,必須建立一個 limit 實例,如下所示:

/* This constructor is required when using VS Code.
Limit Switch configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a limit instance in Port A
limit LimitSwitchA = limit(Brain.ThreeWirePort.A);

pressing#

返回限位開關目前是否被按下。這可用於檢查機器人是否碰撞到其他物體。

Available Functions
int32_t pressing();

Parameters

此函數不接受任何參數。

Return Values

返回 int32_t ,表示限位開關是否被按下。

  • 1 — 限位開關被按下。

  • 0 — 限位開關未按下。

pressed#

註冊一個在按下限位開關時要呼叫的函數。

Available Functions
void pressed(
  void (* callback)(void) );

Parameters

範圍

類型

描述

callback

void (*)(void)

指向一個函數的指針,該函數將在按下限位開關時被呼叫。

Return Values

此函數不傳回值。

Examples

定義回呼函數(在 int main()之外)

void switchPressed() {
  // The Brain will print that the Limit Switch was pressed
  // on the Brain's screen.
  Brain.Screen.print("switch pressed");
}

在 int main()中註冊回呼函數

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run switchPressed when the Limit Switch is pressed.
  LimitSwitchA.pressed(switchPressed);
}

released#

註冊一個在限位開關釋放時要呼叫的函數。

Available Functions
void released(
  void (* callback)(void) );

Parameters

範圍

類型

描述

callback

void (*)(void)

指向一個函數的指針,該函數將在限位開關釋放時被呼叫。

Return Values

此函數不傳回值。

Examples

定義回呼函數(在 int main()之外)

void switchReleased() {
  // The Brain will print that the Limit Switch was released
  // on the Brain's screen.
  Brain.Screen.print("switch released");
}

在 int main()中註冊回呼函數

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run switchReleased when the Limit Switch is released.
  LimitSwitchA.released(switchReleased);
}