限位开关#

介绍#

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