เครื่องวัดระยะ#

การแนะนำ#

โซนาร์คลาส sonar ก ซึ่งวัดระยะห่างจากวัตถุที่ใกล้ที่สุดโดยใช้คลื่นเสียง หลักการทำงานคือการส่งคลื่นอัลตราโซนิกออกไปและวัดระยะเวลาที่เสียงสะท้อนกลับมา

เครื่องวัดระยะ VEX V5

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

sonar(
  triport::port &port);

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

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

virtual ~sonar();

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

พารามิเตอร์

พิมพ์

คำอธิบาย

port

triport::port &

พอร์ต 3 สายแรกของกลุ่มพอร์ตสองสายที่จำเป็นซึ่งใช้โดยตัวค้นหาระยะ (Range Finder) ตัวค้นหาระยะจะใช้พอร์ตที่อยู่ติดกันสองพอร์ต (A–B, C–D, E–F หรือ G–H) บนตัวประมวลผลหลัก (Brain) หรือตัวขยายสัญญาณ 3 สาย (3-Wire Expander) ระบุพอร์ตแรกในคู่ (A, C, E หรือ G) โดยเขียนเป็น Brain.ThreeWirePort.X หรือ ExpanderName.X โดยที่ X คือตัวอักษรของพอร์ต (ตัวอย่างเช่น Brain.ThreeWirePort.A หรือ Expander1.C)

ตัวอย่าง#

// Create a sonar instance in Ports A and B
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);

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

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

  • foundObject — ส่งคืนว่า Range Finder ตรวจพบวัตถุหรือไม่

  • distance — ส่งคืนระยะทางระหว่าง Range Finder กับวัตถุที่ตรวจพบที่ใกล้ที่สุด

ก่อนเรียกใช้ฟังก์ชันสมาชิก sonar ใดๆ จะต้องสร้างอินสแตนซ์ sonar ก่อน ดังแสดงด้านล่าง:

/* This constructor is required when using VS Code.
Range Finder configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a sonar instance in Ports A and B
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);

foundObject#

ส่งคืนค่าว่าเครื่องวัดระยะตรวจพบวัตถุภายในขอบเขตการมองเห็นหรือไม่

Available Functions
bool foundObject();

Parameters

ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ

Return Values

ส่งคืนค่าบูลีน bool ซึ่งแสดงว่าตัวค้นหาระยะตรวจพบวัตถุหรือไม่:

  • true — เครื่องวัดระยะกำลังตรวจจับวัตถุ

  • false — เครื่องวัดระยะไม่พบวัตถุ

Examples
// Check if an object is detected.
if (RangeFinderA.foundObject()){
    // Print to screen on the brain that an object was found.
    Brain.Screen.print("object found");
}

distance#

ส่งคืนระยะห่างที่วัดได้ไปยังวัตถุที่ใกล้ที่สุดที่เซ็นเซอร์ตรวจพบ

Available Functions
double distance(
  distanceUnits units );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

units

distanceUnits

หน่วยที่ใช้แสดงระยะทาง: inches หรือ mm มิลลิเมตร

Return Values

ส่งคืนค่า double ซึ่งแสดงถึงระยะทางในหน่วยที่ระบุ

Examples
// Get the Range Finder distance in millimeters.
double value = RangeFinderA.distance(mm);

// Print the current distance detected by the Range Finder to the
// Brain's screen.
Brain.Screen.print(value);