สวิตช์จำกัด#
การแนะนำ#
คลาส limit ใช้กับสวิตช์จำกัด (Limit Switch) ซึ่งเป็นเซ็นเซอร์ดิจิทัลขนาดเล็กที่ตรวจจับการสัมผัสทางกายภาพกับวัตถุ

คอนสตรัคเตอร์คลาส#
limit(
triport::port &port);
ตัวทำลายคลาส#
ทำลายอ็อบเจ็กต์ limit และปล่อยทรัพยากรที่เกี่ยวข้อง
virtual ~limit();
พารามิเตอร์#
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
พอร์ต 3 สายที่เชื่อมต่อกับสวิตช์จำกัด เขียนเป็น |
ตัวอย่าง#
// 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 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); }