保险杠开关#
介绍#
bumper 类与保险杠开关配合使用,保险杠开关是一种机械开关,按下时会接通电路,使机器人能够检测到按下和释放动作。

类构造函数#
bumper(
triport::port &port );
类析构函数#
销毁 bumper 对象并释放相关资源。
virtual ~bumper();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
保险杠开关连接的 3 线端口,写成 |
例子#
// Create a bumper instance in Port A
bumper BumperA = bumper(Brain.ThreeWirePort.A);
成员功能#
bumper 类包含以下成员函数:
在调用任何 bumper 成员函数之前,必须先创建一个 bumper 实例,如下所示:
/* 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 Functionsint32_t pressing();
此函数不接受任何参数。
Return Values返回 int32_t ,表示是否按下了保险杠开关。
1— 保险杠开关被按下。0— 保险杠开关未被按下。
pressed#
注册一个在按下保险杠开关时要调用的函数。
Available Functionsvoid pressed( void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
指向一个函数的指针,该函数将在按下保险杠时调用。 |
此函数不返回值。
Examples定义回调函数(在
int main()之外)// Display a message when bumper is pressed void BumperPressed() { Brain.Screen.print("Bumper pressed"); }在
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 Functionsvoid released( void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
指向一个函数的指针,该函数将在保险杠释放时调用。 |
此函数不返回值。
Examples定义回调函数(在
int main()之外)// Display a message when bumper is released void BumperReleased() { Brain.Screen.print("Bumper released"); }在
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); }