สวิตช์กันชน#

การแนะนำ#

กันชนคลาส bumper

สวิตช์กันชน VEX EXP เป็นอุปกรณ์ 3 สายที่มีฐานสีดำกว้างสำหรับติดกับหุ่นยนต์ และปุ่มสีแดงที่สามารถกดได้อยู่ด้านบน

คอนสตรัคเตอร์คลาส#

bumper(
  triport::port &port );

ตัวทำลายคลาส#

ทำลายวัตถุ bumper และปล่อยทรัพยากรที่เกี่ยวข้อง

virtual ~bumper();

พารามิเตอร์#

พารามิเตอร์

พิมพ์

คำอธิบาย

port

triport::port&

พอร์ต 3 สายที่เชื่อมต่อกับสวิตช์กันชน เขียนเป็น Brain.ThreeWirePort.X หรือ ExpanderName.Xโดยที่ X คือตัวอักษรของพอร์ต (ตัวอย่างเช่น Brain.ThreeWirePort.A หรือ Expander1.A)

ตัวอย่าง#

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

ฟังก์ชันสมาชิก#

คลาส bumper ประกอบด้วยฟังก์ชันสมาชิกต่อไปนี้:

  • pressing — ส่งคืนว่าสวิตช์กันชนถูกกดหรือไม่

  • pressed — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อกดสวิตช์กันชน

  • released — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อปล่อยสวิตช์กันชน

ก่อนเรียกใช้ฟังก์ชันสมาชิก 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 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())

// 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 Functions
void released( void (* callback)(void) );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void)

ตัวชี้ไปยังฟังก์ชันที่จะถูกเรียกเมื่อปล่อยกันชน

Return Values

ฟังก์ชันนี้ไม่ส่งค่ากลับ

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);
}