限位开关#
介绍#
limit 类与限位开关一起使用,限位开关是一种小型数字传感器,用于检测与物体的物理接触。

类构造函数#
limit(
triport::port &port);
类析构函数#
销毁 limit 对象并释放相关资源。
virtual ~limit();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
限位开关连接的 3 线端口,写成 |
例子#
// Create a limit instance in Port A
limit LimitSwitchA = limit(Brain.ThreeWirePort.A);
成员功能#
limit 类包含以下成员函数:
在调用任何 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 Functionsint32_t pressing();
此函数不接受任何参数。
Return Values返回 int32_t ,表示限位开关是否被按下。
1— 限位开关被按下。0— 限位开关未被按下。
pressed#
注册一个在按下限位开关时要调用的函数。
Available Functionsvoid pressed(
void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
指向一个函数的指针,该函数将在按下限位开关时被调用。 |
此函数不返回值。
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 Functionsvoid released(
void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
指向一个函数的指针,该函数将在限位开关释放时被调用。 |
此函数不返回值。
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); }