光學感測器#

介紹#

optical 類用於存取來自 V5 光學感測器 的數據。它利用反射光來偵測物體、識別顏色以及測量亮度和色調。

類別建構函數#

optical Optical = optical(index);

類別析構函數#

銷毀 optical 物件並釋放相關資源。

virtual ~optical();

參數#

範圍

類型

描述

index

int32_t

光感測器連接的智慧型端口,記為 PORTx,其中 x 是端口號(例如, PORT1)。

範例#

// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);

成員功能#

optical 類別包含以下成員函數:

  • setLight — 開啟或關閉光學感測器的 LED。

  • setLightPower — 設定光學感測器的光功率等級。

  • isNearObject — 返迴光學感測器是否偵測到範圍內的物體。

  • color — 返迴光學感測器偵測到的顏色。

  • brightness — 傳回物體反射的光量。

  • hue — 返迴光學感測器偵測到的色調。

  • objectDetected — 註冊一個當光學感測器偵測到物體時要呼叫的函數。

  • objectLost — 註冊一個當光學感測器遺失物體時要呼叫的函數。

在呼叫任何 optical 成員函數之前,必須建立一個 optical 實例,如下所示:

// Create an optical object in Port 1
optical Optical1 = optical(PORT1);

setLight#

設定光學感測器的 LED 指示燈亮起或熄滅。指示燈可以幫助光學感測器更清晰地偵測物體和顏色。

Available Functions
void  setLight(ledState state);

Parameters

範圍

類型

描述

state

ledState

Sets the LED on or off. ledState::on turns the LED on at 100% brightness. ledState::off turns it off.

Return Values

此函數不傳回值。

Examples
// Turn on LED with previous intensity.
Optical.setLight(ledState::on);

setLightPower#

設定光學感測器的LED亮度。燈光可以幫助光學感測器更清晰地偵測物體和顏色。

如果光感應器的燈熄滅,將燈光功率設定為 0% 以上即可開啟燈。

如果光感應器的指示燈亮著,將光功率設定為 0% 將關閉指示燈。

百分比越高,燈光越亮;百分比越低,燈光越暗。

Available Functions
void setLightPower( 
      int32_t      intensity, 
      percentUnits units = percent );

Parameters

範圍

類型

描述

intensity

int32_t

燈光亮度級別,範圍從 0 到 100。

units

percentUnits

表示光照強度的單位: percent / pct%

Return Values

此函數不傳回值。

Examples
// Set the light power to 50 percent.
Optical.setLightPower(50, percent);

isNearObject#

返迴光學感測器偵測到附近物體時的值。

Available Functions
bool  isNearObject();

Parameters

此函數不接受任何參數。

Return Values

此函數傳回布林值,指示是否偵測到附近物體:

  • true – 光學感測器偵測到物體。

  • false – 光學感測器未偵測到物體。

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

color#

返迴光學感測器檢測到的顏色。

Available Functions
vex::color  color();

Parameters

此函數不接受任何參數。

Return Values

光學感測器偵測到的顏色是顏色類別的一個實例。

可選顏色有:

  • black

  • white

  • red - 色調值介於 340° 至 20° 之間

  • green - 色調值介於 80° 至 140° 之間

  • blue - 色調值介於 200° 至 240° 之間

  • yellow - 色調值介於 40° 至 60° 之間

  • orange - 色調值介於 20° 至 40° 之間

  • purple - 色調值介於 240° 至 280° 之間

  • cyan - 色調值介於 140° 至 200° 之間

Examples
// Set a variable, detectColor, to the color detected by the
// Optical Sensor
color detectColor = Optical.color();

// Print the color detected by the Optical Sensor
// to the Brain's screen
Brain.Screen.print(detectColor);

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

// Print the brightness detected by the Optical Sensor to the
// Brain's screen
Brain.Screen.print(brightness);

hue#

傳回偵測到的顏色的色調值。

色相是用色輪上的數字來描述顏色的一種方法。

VEX 色輪,顯示圍繞圓週的顏色度數,紅色為 0 度,隨著數值增加,顏色依序過渡到彩虹的顏色。

Available Functions
double hue();

Parameters

此函數不接受任何參數。

Return Values

表示光學感測器偵測到的色調值的雙精度浮點數,範圍為 0 — 359.99。

Examples
// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();

objectDetected#

註冊一個回調函數,用於在偵測到物件時執行該回呼函數。

Available Functions
void objectDetected(void (* callback)(void));

Parameters

範圍

類型

描述

callback

void (*)(void)

先前定義的 函數,當光學感測器偵測到新物體時執行。

Return Values

此函數不傳回值。

Examples

定義回呼函數(在 int main()之外)

// Display a message when the Optical Sensor detects an object.
void detected() {
  Brain.Screen.print("object detected");
}

在 int main()中註冊回呼函數

int main() {
  // Run detected when the Optical Sensor detects an object.
  Optical.objectDetected(detected);
}

objectLost#

註冊一個物件遺失時的回調函數。

Available Functions
void objectLost(void (* callback)(void));

Parameters

範圍

類型

描述

callback

void (*)(void)

先前定義的 函數,當光學感測器遺失偵測到的物件時執行。

Return Values

此函數不傳回值。

Examples

定義回呼函數(在 int main()之外)

// Display a message when the Optical Sensor loses an object.
void lost() {
  Brain.Screen.print("object lost");
}

在 int main()中註冊回呼函數

int main() {
  // Run lost when the Optical Sensor loses an object.
  Optical.objectLost(lost);
}