เซ็นเซอร์แสง#

การแนะนำ#

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

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

optical Optical = optical(index);

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

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

virtual ~optical();

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

พารามิเตอร์

พิมพ์

คำอธิบาย

index

int32_t

พอร์ตอัจฉริยะที่เชื่อมต่อกับเซ็นเซอร์แสง จะเขียนในรูปแบบ PORTxโดยที่ x คือหมายเลขพอร์ต (ตัวอย่างเช่น PORT1)

ตัวอย่าง#

// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);

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

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

  • setLight — เปิดหรือปิดไฟ LED ของเซ็นเซอร์แสง

  • setLightPower — ตั้งค่าระดับพลังงานแสงของเซ็นเซอร์แสง

  • isNearObject — ส่งคืนว่าเซ็นเซอร์แสงตรวจพบวัตถุภายในระยะหรือไม่

  • color — ส่งคืนสีที่ตรวจพบโดยเซ็นเซอร์แสง

  • brightness — ส่งคืนปริมาณแสงที่สะท้อนจากวัตถุ

  • hue — ส่งคืนค่าสีที่ตรวจพบโดยเซ็นเซอร์แสง

  • objectDetected — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์แสงตรวจพบวัตถุ

  • objectLost — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์แสงสูญเสียวัตถุ

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

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

// Create an optical object in Port 1
optical Optical1 = optical(PORT1);

setLight#

ตั้งค่าไฟ LED ของเซ็นเซอร์แสงให้เปิดหรือปิด แสงไฟจะช่วยให้เซ็นเซอร์แสงตรวจจับวัตถุและสีได้ชัดเจนยิ่งขึ้น

Available Functions
void  setLight(ledState state);

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

state

ledState

ตั้งค่าสถานะไฟ LED ว่าจะเปิดหรือปิด ledState::on เปิดไฟ LED ที่ความสว่าง 100% ledState::off ปิดไฟ LED

Return Values

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

Examples
// Turn on LED with previous intensity.
Optical.setLight(ledState::on);

setLightPower#

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

เปอร์เซ็นต์ที่สูงขึ้นจะทำให้แสงสว่างมากขึ้น เปอร์เซ็นต์ที่ต่ำลงจะทำให้แสงสว่างน้อยลง

หากไฟแสดงสถานะของเซ็นเซอร์แสงดับอยู่ การปรับกำลังไฟให้สูงกว่า 0% จะทำให้ไฟแสดงสถานะติดขึ้น

หากไฟแสดงสถานะของเซ็นเซอร์แสงติดอยู่ การตั้งค่ากำลังไฟเป็น 0% จะทำให้ไฟแสดงสถานะดับลง

Available Functions
void setLightPower( 
      int32_t      intensity, 
      percentUnits units = percent );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

intensity

int32_t

สามารถปรับระดับความสว่างได้ตั้งแต่ 0 – 100

units

percentUnits

The unit that represents the light intensity: percent / pct — percent

Return Values

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

Examples
// Set the light power to 50 percent.
Optical.setLightPower(50, percent);

isNearObject#

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

Available Functions
bool  isNearObject();

Parameters

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

Return Values

ฟังก์ชันนี้จะส่งคืนค่าบูลีนที่ระบุว่าตรวจพบวัตถุที่อยู่ใกล้เคียงหรือไม่:

  • true — ตรวจพบวัตถุอยู่ใกล้เคียง

  • false — ไม่พบวัตถุ

Examples
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
  Brain.Screen.print("near object");
}

color#

ส่งคืนสีที่ตรวจจับได้โดยเซ็นเซอร์แสง โดยอิงตามค่าเฉดสีที่ตรวจพบ

Available Functions
vex::color  color();

Parameters

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

Return Values

สีที่ตรวจพบโดยเซ็นเซอร์แสงเป็นอินสแตนซ์ของ คลาสสี

สีที่เป็นไปได้ ได้แก่:

  • blue – ค่าสีระหว่าง 196° ถึง 229°

  • blue_green – ค่าเฉดสีระหว่าง 140° ถึง 195°

  • blue_violet – ค่าสีระหว่าง 230° ถึง 239°

  • green – ค่าสีระหว่าง 84° ถึง 139°

  • orange – ค่าสีระหว่าง 21° ถึง 30°

  • red – ค่าสีระหว่าง 350° ถึง 14°

  • red_orange – ค่าเฉดสีระหว่าง 15° ถึง 20°

  • red_violet – ค่าสีระหว่าง 281° ถึง 350°

  • violet – ค่าสีระหว่าง 240° ถึง 280°

  • yellow – ค่าสีระหว่าง 54° ถึง 74°

  • yellow_green – ค่าสีระหว่าง 75° ถึง 83°

  • yellow_orange – ค่าเฉดสีระหว่าง 31° ถึง 53°

Examples
// Set a variable, detectColor, to the color detected by the
// Optical Sensor
color detectColor = Optical.color();

// Print the color detected by the Optical Sensor
// to the Brain's screen
Brain.Screen.print(detectColor);

brightness#

ส่งคืนค่าปริมาณแสงที่สะท้อนกลับไปยังเซ็นเซอร์แสง

เปอร์เซ็นต์ที่สูงขึ้นหมายความว่ามีแสงสะท้อนกลับไปยังเซ็นเซอร์แสงมากขึ้น เปอร์เซ็นต์ที่ต่ำลงหมายความว่ามีแสงสะท้อนกลับน้อยลง

Available Functions
double  brightness(bool bRaw = false);

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

bRaw

bool

ค่าบูลีนสำหรับอ่านข้อมูลความสว่างดิบแทนที่จะเป็นเปอร์เซ็นต์ ค่าเริ่มต้นคือ false)

Return Values

ค่าตัวเลขทศนิยมสองตำแหน่งที่แสดงค่าความสว่างที่ตรวจจับได้โดยเซ็นเซอร์แสงในช่วง 0 - 100% หรือค่าตัวเลขทศนิยมสองตำแหน่งที่แสดงข้อมูลดิบที่ตรวจจับได้โดยเซ็นเซอร์

Examples
// Set a variable, brightness, to the value of the brightness
// detected by the Optical Sensor
double brightness = Optical.brightness();

// Print the brightness detected by the Optical Sensor to the
// Brain's screen
Brain.Screen.print(brightness);

hue#

ส่งคืนค่าสีที่ตรวจจับได้โดยเซ็นเซอร์แสง

เฉดสี คือวิธีการอธิบายสีโดยใช้ตัวเลขรอบวงล้อสี

วงล้อสี VEX แสดงระดับสีรอบวงกลม โดยสีแดงอยู่ที่ 0 องศา และค่อยๆ ไล่สีไปตามรุ้งเมื่อค่าเพิ่มขึ้น

Available Functions
double hue();

Parameters

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

Return Values

ค่าตัวเลขทศนิยมที่แสดงค่าเฉดสีที่ตรวจจับได้โดยเซ็นเซอร์แสงในช่วง 0 — 359.00

Examples
// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();

objectDetected#

ลงทะเบียนฟังก์ชันเรียกกลับ (callback function) สำหรับกรณีที่ตรวจพบวัตถุ

Available Functions
void objectDetected(void (* callback)(void));

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void)

A previously defined function that executes when the Optical Sensor detects a new object.

Return Values

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

Examples

กำหนดฟังก์ชันเรียกกลับ (นอก int main())

// Called when the Optical Sensor detects an object
void detected() {
  // The Brain will print that the Optical Sensor detected an
  // object to the Brain's screen.
  Brain.Screen.print("object detected");
}

ลงทะเบียนฟังก์ชันเรียกกลับภายใน int main()

/* vexcodeInit() is only required when using VEXcode.
Remove vexcodeInit() if compiling in VS Code. */
int main() {

  // Run when the Optical Sensor detects an object.
  Optical.objectDetected(detected);
}

objectLost#

ลงทะเบียนฟังก์ชันเรียกกลับสำหรับกรณีที่วัตถุสูญหาย

Available Functions
void objectLost(void (* callback)(void));

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void)

A previously defined function that executes when the Optical Sensor loses a detected object.

Return Values

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

Examples

กำหนดฟังก์ชันเรียกกลับ (นอก int main())

// Called when the Optical Sensor loses an object
void lost() {
  // The Brain will print that the Optical Sensor lost an
  // object to the Brain's screen.
  Brain.Screen.print("object lost");
}

ลงทะเบียนฟังก์ชันเรียกกลับภายใน int main()

/* vexcodeInit() is only required when using VEXcode.
Remove vexcodeInit() if compiling in VS Code. */
int main() {

  // Run lost when the Optical Sensor loses an object.
  Optical.objectLost(lost);
}