컬러 센서#

소개#

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 – 감지된 색조 값을 반환합니다.

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 — ledState 사용하여 LED를 켜거나 끕니다.

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

빛의 강도를 나타내는 단위: percent 퍼센트 pct 퍼센트

Return Values

이 함수는 값을 반환하지 않습니다.

Examples
// Set the light power to 50 percent.
Color1.setLight(50, percent);

isNearObject#

컬러 센서가 근처에 물체를 감지하면 true를 반환합니다.

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

유효한 사용자 지정 색상 또는 위에 나열된 미리 정의된 색상 중 하나입니다.

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 프로젝트 내에서만 사용할 수 있으며 다른 환경에서는 작동하지 않습니다.

  • VEXcode 외부에서 colorname 사용할 때는 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 false)입니다.

Return Values

컬러 센서가 감지한 밝기를 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#

색상 센서가 감지한 색조를 반환합니다.

색조는 색상환을 중심으로 숫자를 사용하여 색을 설명하는 방법입니다.

VEX 컬러 휠은 원형으로 색상의 단계를 보여주며, 빨간색이 0도이고 값이 증가함에 따라 무지개를 거쳐 변화합니다.

Available Functions
double hue();

Parameters

이 함수는 매개변수를 받지 않습니다.

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();