สวิตช์จำกัด#

การแนะนำ#

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

สวิตช์จำกัด 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);
}