เซ็นเซอร์สี#
การแนะนำ#
คลาส colorsensor ใช้สำหรับเข้าถึงข้อมูลจาก เซ็นเซอร์สี โดยใช้แสงสะท้อนในการตรวจจับวัตถุ ระบุสี และวัดความสว่างและเฉดสี
คอนสตรัคเตอร์คลาส#
colorsensor Color1 = colorsensor(index);
ตัวทำลายคลาส#
ทำลายอ็อบเจ็กต์ colorsensor และปล่อยทรัพยากรที่เกี่ยวข้อง
virtual ~colorsensor();
พารามิเตอร์#
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
พอร์ตอัจฉริยะที่เชื่อมต่อกับเซ็นเซอร์สี จะเขียนในรูปแบบ |
ตัวอย่าง#
// 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 Functions1 — เปิดหรือปิดไฟ LED โดยใช้
ledStatevoid setLight( ledState state );
Parameters2 — เปิดไฟ LED ด้วยความสว่างระดับที่กำหนด
void setLight( int32_t intensity, percentUnits units = percent );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ตั้งค่าสถานะไฟ LED ว่าจะเปิดหรือปิด |
|
|
สามารถปรับระดับความสว่างได้ตั้งแต่ 0 – 100 |
|
|
The unit that represents the light intensity: |
ฟังก์ชันนี้ไม่ส่งค่ากลับ
Examples// Set the light power to 50 percent.
Color1.setLight(50, percent);
isNearObject#
ส่งคืนค่าหากเซ็นเซอร์สีตรวจพบวัตถุที่อยู่ใกล้เคียง
Available Functionsbool isNearObject();
ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ
Return Valuesฟังก์ชันนี้จะส่งคืนค่าบูลีนที่ระบุว่าตรวจพบวัตถุที่อยู่ใกล้เคียงหรือไม่:
true– เซ็นเซอร์สีตรวจจับวัตถุได้false– เซ็นเซอร์สีตรวจไม่พบวัตถุ
// If an object is detected by the Color Sensor, print
// "near object".
if (Color1.isNearObject()){
Brain.Screen.print("near object");
}
detects#
ส่งคืนค่าว่าเซ็นเซอร์สีตรวจจับสีที่ระบุได้หรือไม่
Available Functions1 — Detects a
colorType.bool detects( colorType color );
Parameters2 — Detects a
vex::color.bool detects( vex::color color );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
สีที่ถูกต้อง:
|
|
|
A valid custom color, or one of the predefined colors listed above. |
true– เซ็นเซอร์สีตรวจจับสีfalse– เซ็นเซอร์สีไม่ตรวจจับสี
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 FunctionscolorType colorname();
ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ
Return Valuesเมื่อพิมพ์โดยตรง จะแสดงค่าตัวเลขที่สัมพันธ์กับสีนั้น:
สี |
ค่าตัวเลข |
|---|---|
|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
ฟังก์ชัน
convertColorToStringจะถูกสร้างขึ้นโดยอัตโนมัติโดย VEXcode เมื่อมีการกำหนดค่าเซ็นเซอร์สีในหน้าต่างอุปกรณ์ ฟังก์ชันนี้ใช้งานได้เฉพาะภายในโปรเจ็กต์ VEXcode IQ เท่านั้น และจะไม่ทำงานในสภาพแวดล้อมอื่นเมื่อใช้
colornameนอก VEXcode ควรทำการเปรียบเทียบโดยตรงกับค่าcolorType— สีใดๆ ที่ขึ้นต้นด้วยcolorType::(ตัวอย่างเช่นcolorType::greenหรือcolorType::red)
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 Functionsdouble brightness(bool bRaw = false);
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ค่าบูลีนสำหรับอ่านข้อมูลความสว่างดิบแทนที่จะเป็นเปอร์เซ็นต์ ค่าเริ่มต้นคือ |
ค่าตัวเลขทศนิยมสองตำแหน่งที่แสดงค่าความสว่างที่ตรวจจับได้โดยเซ็นเซอร์สีในช่วง 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#
ส่งคืนค่าสีที่ตรวจจับได้โดยเซ็นเซอร์สี
เฉดสี คือวิธีการอธิบายสีโดยใช้ตัวเลขรอบวงล้อสี

double hue();
ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ
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();