เซ็นเซอร์สี#

การแนะนำ#

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

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

colorsensor Color1 = colorsensor(index);

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

ทำลายอ็อบเจ็กต์ colorsensor และปล่อยทรัพยากรที่เกี่ยวข้อง

virtual ~colorsensor();

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

พารามิเตอร์

พิมพ์

คำอธิบาย

index

int32_t

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

ตัวอย่าง#

// Create an colorsensor instance in Port 1
colorsensor Color1 = colorsensor(PORT1);

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

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

  • setLight – เปิด ปิด หรือตั้งค่าระดับความสว่างของ LED ของเซ็นเซอร์สี

  • isNearObject – ส่งคืนว่าวัตถุอยู่ใกล้กับเซ็นเซอร์สีหรือไม่

  • detects – ส่งคืนว่าเซ็นเซอร์สีตรวจพบสีที่ระบุหรือไม่

  • colorname – ส่งคืนชื่อของสีที่ตรวจพบโดยเซ็นเซอร์สี

  • brightness – ส่งคืนค่าความสว่างที่ตรวจพบของวัตถุ

  • hue – ส่งคืนค่า hue ที่ตรวจพบ

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

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

// Create an colorsensor instance in Port 1
colorsensor Color1 = colorsensor(PORT1);

setLight#

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

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

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

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

Available Functions

1 — เปิดหรือปิดไฟ LED โดยใช้ ledState

void setLight( 
  ledState  state );

2 — เปิดไฟ LED ด้วยความสว่างระดับที่กำหนด

void setLight( 
  int32_t      intensity, 
  percentUnits units = percent );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

state

ledState

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

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.
Color1.setLight(50, percent);

isNearObject#

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

Available Functions
bool  isNearObject();

Parameters

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

Return Values

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

  • true – เซ็นเซอร์สีตรวจจับวัตถุได้

  • false – เซ็นเซอร์สีตรวจไม่พบวัตถุ

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

detects#

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

Available Functions

1 — Detects a colorType.

bool detects( 
  colorType  color );

2 — Detects a vex::color.

bool detects( 
  vex::color  color );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

color

colorType

สีที่ถูกต้อง:

  • blue – ค่าเฉดสีระหว่าง 196° และ 229°
  • blue_green – ค่าเฉดสีระหว่าง 140° และ 195°
  • blue_violet – ค่าเฉดสีระหว่าง 230° และ 239°
  • green – ค่าเฉดสีระหว่าง 84° และ 139°
  • orange – ค่าเฉดสีระหว่าง 21° และ 30°
  • purple
  • red – ค่าเฉดสีระหว่าง 350° และ 14°
  • red_orange – ค่าเฉดสีระหว่าง 15° และ 20°
  • red_violet – ค่าเฉดสีระหว่าง 281° และ 350°
  • violet – ค่าสีระหว่าง 240° ถึง 280°
  • white
  • yellow – ค่าสีระหว่าง 54° ถึง 74°
  • yellow_green – ค่าสีระหว่าง 75° ถึง 83°
  • yellow_orange – ค่าสีระหว่าง 31° ถึง 53°

color

vex::color

A valid custom color, or one of the predefined colors listed above.

Return Values
  • true – เซ็นเซอร์สีตรวจจับสี

  • false – เซ็นเซอร์สีไม่ตรวจจับสี

Examples
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive until a red object is seen
  Drivetrain.drive(forward);
  while (true) {
    if (Color1.detects(red)) {
      Drivetrain.stop();
      break;
    }
    wait(0.5, seconds);
  }
}

colorname#

คืนค่าสีที่ตรงกันที่สุดที่ตรวจพบ

Available Functions
colorType colorname();

Parameters

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

Return Values

เมื่อพิมพ์โดยตรง จะแสดงค่าตัวเลขที่สัมพันธ์กับสีนั้น:

สี

ค่าตัวเลข

none

0

red

1

green

2

blue

3

white

4

yellow

5

orange

6

purple

7

cyan

8

red_violet

9

violet

10

blue_violet

11

blue_green

12

yellow_green

13

yellow_orange

14

red_orange

15

black

16

transparent

17

Notes
  • ฟังก์ชัน convertColorToString จะถูกสร้างขึ้นโดยอัตโนมัติโดย VEXcode เมื่อมีการกำหนดค่าเซ็นเซอร์สีในหน้าต่างอุปกรณ์ ฟังก์ชันนี้ใช้งานได้เฉพาะภายในโปรเจ็กต์ VEXcode IQ เท่านั้น และจะไม่ทำงานในสภาพแวดล้อมอื่น

  • เมื่อใช้ colorname นอก VEXcode ควรทำการเปรียบเทียบโดยตรงกับค่า colorType — สีใดๆ ที่ขึ้นต้นด้วย colorType:: (ตัวอย่างเช่น colorType::green หรือ colorType::red)

Examples
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Continuously check the detected color
  while (true) {

    // Compare the color name with "red"
    if (convertColorToString(Color1.colorname()) == "red") {
      Brain.Screen.print("Red object!");
    } else {
      Brain.Screen.print("Not red");
    }

    wait(0.5, seconds);
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
  }
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Continuously check the detected color
  while (true) {

    // Compare the color name with the colorType
    if (Color1.colorname() == colorType::red) {
      Brain.Screen.print("Red object!");
    } else {
      Brain.Screen.print("Not red");
    }

    wait(0.5, seconds);
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
  }
}

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 Color Sensor
double brightness = Color1.brightness();

// Print the brightness detected by the Color 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 Color Sensor.
double hue = Color1.hue();