光學感測器#
介紹#
optical 類用於存取來自光學感測器的資料。它利用反射光來偵測物體、識別顏色以及測量亮度和色調。
類別建構函數#
optical Optical = optical(index);
類別析構函數#
銷毀 optical 物件並釋放相關資源。
virtual ~optical();
參數#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
光感測器連接的智慧型端口,記為 |
範例#
// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);
成員功能#
optical 類別包含以下成員函數:
setLight— 開啟或關閉光學感測器的 LED。setLightPower— 設定光學感測器的光功率等級。isNearObject— 返迴光學感測器是否偵測到範圍內的物體。color— 返迴光學感測器偵測到的顏色。brightness— 傳回物體反射的光量。hue— 返迴光學感測器偵測到的色調。objectDetected— 註冊一個當光學感測器偵測到物體時要呼叫的函數。objectLost— 註冊一個當光學感測器遺失物體時要呼叫的函數。
在呼叫任何 optical 成員函數之前,必須建立一個 optical 實例,如下所示:
/* This constructor is required when using VS Code.
Optical Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
設定光學感測器的 LED 指示燈亮起或熄滅。指示燈可以幫助光學感測器更清晰地偵測物體和顏色。
Available Functionsvoid setLight(ledState state);
範圍 |
類型 |
描述 |
|---|---|---|
|
|
設定 LED 的亮滅狀態。 0 |
此函數不傳回值。
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
設定光學感測器的光線亮度。光線可以幫助光學感測器更清晰地偵測物體和顏色。
百分比越高,燈光越亮;百分比越低,燈光越暗。
如果光感應器的燈熄滅,將燈光功率設定為 0% 以上即可開啟燈。
如果光感應器的指示燈亮著,將光功率設定為 0% 將關閉指示燈。
Available Functionsvoid setLightPower(
int32_t intensity,
percentUnits units = percent );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
燈光亮度級別,範圍從 0 到 100。 |
|
|
表示光照強度的單位: |
此函數不傳回值。
Examples// Set the light power to 50 percent.
Optical.setLightPower(50, percent);
isNearObject#
返迴光學感測器是否偵測到範圍內的物體。
Available Functionsbool isNearObject();
此函數不接受任何參數。
Return Values此函數傳回布林值,指示是否偵測到附近物體:
true— 偵測到附近有物體。false— 未偵測到物體。
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
color#
根據偵測到的色調值,傳迴光學感測器偵測到的顏色。
Available Functionsvex::color color();
此函數不接受任何參數。
Return Values光學感測器偵測到的顏色是顏色類別的一個實例。
可選顏色有:
blue– 色調值介於 196° 和 229° 之間blue_green– 色調值介於 140° 和 195° 之間blue_violet– 色調值介於 230° 和 239° 之間green– 色調值介於 84° 和 139° 之間orange– 色調值介於 21° 和 30° 之間red– 色調值介於 350° 和 14° 之間red_orange– 色調值介於 15° 和 20° 之間red_violet– 色調值介於 281° 和 350° 之間violet– 色調值介於 240° 和 280° 之間yellow– 色調值介於 54° 和 74° 之間yellow_green– 色調值介於 75° 和 83° 之間yellow_orange– 色調值介於 31° 和 53° 之間
// 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 Functionsdouble brightness(bool bRaw = false);
範圍 |
類型 |
描述 |
|---|---|---|
|
|
一個布林值,用於讀取原始亮度資料而不是百分比。預設值為 |
表示光學感測器偵測到的亮度值的雙精度浮點數,範圍為 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#
返迴光學感測器檢測到的色調。
色相是用色輪上的數字來描述顏色的一種方法。

double hue();
此函數不接受任何參數。
Return Values表示光學感測器偵測到的色調值的雙精度浮點數,範圍為 0 — 359.00。
Examples// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();
objectDetected#
註冊一個回調函數,用於在偵測到物件時執行該回呼函數。
Available Functionsvoid objectDetected(void (* callback)(void));
範圍 |
類型 |
描述 |
|---|---|---|
|
|
先前定義的 函數,當光學感測器偵測到新物體時執行。 |
此函數不傳回值。
Examples定義回呼函數(在
int main()之外)// Called when the Optical Sensor detects an object void detected() { // The Brain will print that the Optical Sensor detected an // object to the Brain's screen. Brain.Screen.print("object detected"); }在
int main()中註冊回呼函數/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run when the Optical Sensor detects an object. Optical.objectDetected(detected); }
objectLost#
註冊一個物件遺失時的回調函數。
Available Functionsvoid objectLost(void (* callback)(void));
範圍 |
類型 |
描述 |
|---|---|---|
|
|
先前定義的 函數,當光學感測器遺失偵測到的物件時執行。 |
此函數不傳回值。
Examples定義回呼函數(在
int main()之外)// Called when the Optical Sensor loses an object void lost() { // The Brain will print that the Optical Sensor lost an // object to the Brain's screen. Brain.Screen.print("object lost"); }在
int main()中註冊回呼函數/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run lost when the Optical Sensor loses an object. Optical.objectLost(lost); }