顏色感測器#

介紹#

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 的亮滅狀態。 0 ledState::on 將 LED 以 100% 亮度開啟。 2 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#

如果顏色感測器偵測到附近物體,則傳回該值。

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
  • 當在「裝置」視窗中配置顏色感測器時,VEXcode 會自動建立函數 convertColorToString 此函數僅在 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。

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