物體感測器#

介紹#

objectdetector 類用於測量反射率並使用物體感測器的紅外線 (IR) 光檢測物體。

類別建構函數#

objectdetector( 
  triport::port &port, 
  bool           bReverse = false );

類別析構函數#

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

virtual ~objectdetector();

參數#

範圍

類型

描述

port

triport::port &

物體感測器連接的 3 線端口,寫成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

bReverse

bool

可選布林值,用於反轉物體感測器的方向:

  • false (預設值)— 不反轉方向
  • true — 反轉方向

範例#

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

成員功能#

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

  • reflectivity — 返回物體感測器偵測到的反射率。

  • isObjectDetected — 傳回物件感測器是否偵測到物件。

  • setDetectionThreshold — 設定物件感測器的偵測範圍。

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

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

在呼叫任何 objectdetector 函數之前,必須先建立 objectdetector 實例,如下所示:

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

reflectivity#

返回物體感測器測量的反射率。

Available Functions
int32_t reflectivity( 
  percentUnits units = percentUnits::pct );

Parameters

參數

類型

描述

units

percentUnits

反射率單位:

  • percent 百分比 pct 百分比

Return Values

傳回一個 int32_t 表示物體偵測器測量的反射率,以百分比表示,範圍為 0 - 100%。

Examples
// Display the detected reflectivity on the Brain's screen
int32_t value = ObjectSensorA.reflectivity();
Brain.Screen.print(value);

isObjectDetected#

傳回物件偵測器目前是否偵測到某個物件。

Available Functions
bool isObjectDetected();

Parameters

此函數不接受任何參數。

Return Values

傳回布林值,指示是否偵測到物件:

  • true — An object is detected.
  • false — No object is detected.

setDetectionThreshold#

將目標偵測器的偵測閾值設定為特定值。

Available Functions
void setDetectionThreshold( 
  int32_t      threshold, 
  percentUnits units = percentUnits::pct );

Parameters

參數

類型

描述

threshold

int32_t

目標偵測的反射率閾值,範圍為 0 - 100。

units

percentUnits

代表新閾值的單位:

  • percent pct — 百分比

Return Values

此函數不傳回值。

Examples
// Set the reflectivity threshold to 50%
ObjectSensorA.setDetectionThreshold(50);

objectDetected#

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

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

Parameters

範圍

類型

描述

callback

void (*)(void)

偵測到物件時要呼叫的回調函數。

Return Values

此函數不傳回值。

Examples
void detected() {
  Brain.Screen.print("object detected");
}

// Run detected when the Object Detector detects an object
ObjectSensorA.objectDetected(detected);

objectLost#

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

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

Parameters

範圍

類型

描述

callback

void (*)(void)

物件遺失時要呼叫的回調函數。

Return Values

此函數不傳回值。

Examples
void lost() {
  Brain.Screen.print("object lost");
}

// Run lost when the Object Detector loses an object
ObjectSensorA.objectLost(lost);