保险杠开关#

介绍#

The bumper class is used with the Bumper Switch, a mechanical switch that completes a circuit when pressed, allowing the robot to detect presses and releases.

VEX V5 碰撞开关是一款三线设备/_static/img/sensing/bumper_switch_1_new_image.png带有一个宽大的黑色底座,用于将其连接到机器人上,顶部有一个红色可按压按钮。

类构造函数#

bumper(
  triport::port &port );

类析构函数#

Destroys the bumper object and releases associated resources.

virtual ~bumper();

参数#

范围

类型

描述

port

triport::port&

The 3-Wire Port that the Bumper Switch is connected to, written as Brain.ThreeWirePort.X or ExpanderName.X, where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.A).

例子#

// Create a bumper instance in Port A
bumper BumperA = bumper(Brain.ThreeWirePort.A);

成员功能#

The bumper class includes the following member functions:

  • pressing — Returns whether the Bumper Switch is being pressed.

  • pressed — Registers a function to be called when the Bumper Switch is pressed.

  • released — Registers a function to be called when the Bumper Switch is released.

Before calling any bumper member functions, a bumper instance must be created, as shown below:

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

// Create a bumper instance in Port A
bumper BumperA = bumper(Brain.ThreeWirePort.A);

pressing#

返回碰撞开关当前是否被按下。这可用于检查机器人是否碰撞到其他物体。

Available Functions
int32_t pressing();

Parameters

此函数不接受任何参数。

Return Values

Returns an int32_t indicating whether the Bumper Switch is being pressed.

  • 1 — The Bumper Switch is being pressed.

  • 0 — The Bumper Switch is not being pressed.

pressed#

注册一个在按下保险杠开关时要调用的函数。

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

Parameters

范围

类型

描述

callback

void (*)(void)

指向一个函数的指针,该函数将在按下保险杠时调用。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

// Display a message when bumper is pressed
void BumperPressed() {
  Brain.Screen.print("Bumper pressed");
}

Register the callback inside int main()

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

  // Run BumperPressed when the value of the Bumper Switch is pressed.
  BumperA.pressed(BumperPressed);
}

released#

注册一个在释放碰撞开关时要调用的函数。

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

Parameters

范围

类型

描述

callback

void (*)(void)

指向一个函数的指针,该函数将在保险杠释放时调用。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

// Display a message when bumper is released
void BumperReleased() {
  Brain.Screen.print("Bumper released");
}

Register the callback inside int main()

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

  // Run BumperReleased when the value of the Bumper Switch is released.
  BumperA.released(BumperReleased);
}