컬러 센서#
소개#
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– 감지된 색조 값을 반환합니다.
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 —
ledState사용하여 LED를 켜거나 끕니다.void setLight( ledState state );
Parameters2 — LED를 특정 밝기로 켜줍니다.
void setLight( int32_t intensity, percentUnits units = percent );
매개변수 |
유형 |
설명 |
|---|---|---|
|
|
LED를 켜거나 끕니다. |
|
|
조명의 밝기를 0~100 사이의 값으로 설정할 수 있는 전력 레벨입니다. |
|
|
빛의 강도를 나타내는 단위: |
이 함수는 값을 반환하지 않습니다.
Examples// Set the light power to 50 percent.
Color1.setLight(50, percent);
isNearObject#
컬러 센서가 근처에 물체를 감지하면 true를 반환합니다.
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 );
매개변수 |
유형 |
설명 |
|---|---|---|
|
|
유효한 색상:
|
|
|
유효한 사용자 지정 색상 또는 위에 나열된 미리 정의된 색상 중 하나입니다. |
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 |
convertColorToStringVEXcode에서 장치 창에 색상 센서를 구성할 때 자동으로 생성됩니다. 이 함수는 VEXcode IQ 프로젝트 내에서만 사용할 수 있으며 다른 환경에서는 작동하지 않습니다.VEXcode 외부에서
colorname사용할 때는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% 범위로 나타내는 double형 값 또는 센서가 감지한 원시 데이터를 나타내는 double형 값입니다.
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색상 센서가 감지한 색조 값을 나타내는 double형 값으로, 0에서 359.00 사이의 값을 가집니다.
Examples// Set a variable, hue, to the value of the hue detected
// by the Color Sensor.
double hue = Color1.hue();