視覺感測器#
介紹#
視覺感測器是一種基於攝影機的感測器,能夠偵測和追蹤顏色特徵和顏色代碼。它使機器人能夠分析周圍環境並對視覺輸入做出反應。
對於以下範例,配置的視覺感測器將被命名為 vision_1,配置的顏色簽章物件(例如 RED_BOX)將在本 API 文件的所有後續範例中引用 Vision 類別方法時使用。
以下是所有方法的清單:
資料擷取器 — 從視覺感測器取得資料。
take_snapshot— 擷取特定簽章的資料。installed— 視覺感測器是否連接到 IQ(第二代)大腦。
屬性 — 從 take_snapshot 傳回的物件資料。
largest_object— 立即從視覺感測器快照中選擇最大的物件。.exists— 物件是否存在於目前偵測中。.width— 偵測到的物件的寬度。.height— 偵測到的物體的高度。.centerX— 物件中心的 X 座標。.centerY— 物體中心的 Y 座標。.angle— 顏色代碼的方向。.originX— 物件左上角的 X 座標。.originY— 物件左上角的 Y 座標。
建構函數-手動初始化和配置感測器。
獲取器#
take_snapshot#
take_snapshot 將來自視覺感測器幀的資料過濾為單一簽名(感測器可以識別的事物的保存描述,例如配置的顏色簽名或顏色代碼),並傳回一個元組。
要使用此方法,必須先在視覺實用程式中配置顏色簽名和顏色代碼。
该元组存储的对象按宽度从大到小排序,从索引 0 开始。每个对象的 属性 可以通过其索引访问。如果未检测到匹配的对象,则返回一个空元组。
Usage:
vision_1.take_snapshot(SIGNATURE)
參數 |
描述 |
|---|---|
|
Filters the dataset to only include data of the given signature. Available signatures are:
|
# Move forward if a red object is detected
while True:
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
顏色特徵#
顏色特徵碼是視覺感測器可以識別的唯一顏色。這些特徵碼使視覺感測器能夠根據物體的顏色來偵測和追蹤物體。配置顏色特徵碼後,感測器就可以在其視野範圍內辨識具有該特定顏色的物體。顏色特徵碼與 take_snapshot 結合使用,可即時處理並偵測彩色物體。
若要在專案中使用已配置的顏色簽名,其名稱必須為視覺感測器的名稱,後面跟著兩個下劃線,再加上顏色簽名的名稱。例如: vision_1__RED_BOX。
# Display if any objects match the RED_BOX signature
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Signature
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
brain.screen.print("Color signature detected!")
wait(100, MSEC)
顏色代碼#
顏色編碼是一種由特定順序排列的顏色訊號所構成的結構化模式。這些編碼使視覺感測器能夠識別預先定義的顏色模式。顏色編碼可用於識別複雜物件或為自主導航建立獨特的標記。
若要在專案中使用已配置的顏色代碼,其名稱必須為感測器名稱,後面跟著兩個下劃線,然後再跟著顏色代碼的名稱。例如: vision_1__BOX_CODE。
# Display if any objects match the BOX_CODE code
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Code
box_code = vision_1.take_snapshot(vision_1__BOX_CODE)
if box_code[0].exists:
brain.screen.print("Color code detected!")
wait(100, MSEC)
installed#
installed 返回一個布林值,指示視覺感測器目前是否連接到 IQ(第二代)大腦。
True— 視覺感測器與 IQ(第二代)大腦相連。False— 視覺感測器未連接到 IQ(第二代)大腦。
Usage:
vision_1.installed()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display a message if the Vision Sensor is connected
if vision_1.installed():
brain.screen.print("Installed!")
特性#
largest_object#
largest_object 從最近一次使用 take_snapshot 傳回的元組中檢索偵測到的最大物件以取得資料。
此方法可用於始終從元組中取得最大對象,而無需指定索引。
Usage:
vision_1.largest_object()
# Turn slowly until the largest object is centered in
# front of the Vision Sensor
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)
while True:
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if 140 < vision_1.largest_object().centerX < 180:
drivetrain.stop()
wait(10,MSEC)
Tuple Properties#
使用 take_snapshot 後,儲存在元組中的每個物件都包含八個屬性。
所有屬性值描述了在拍攝 take_snapshot 時,視覺感測器視野中偵測到的物體的位置和大小。這些值以像素為單位進行測量,基於感測器的 316 x 212 像素解析度。
存在#
.exists 傳回一個布林值,指示索引是否存在於元組中。
True— 索引存在。False— 索引不存在。
# Check if at least one red objects is detected
# You will receive an error if no objects are detected
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
red_objects = vision_1.take_snapshot(vision_1__RED_BOX)
if red_objects[0].exists:
brain.screen.print("At least 1")
else:
brain.screen.print("No red objects")
wait(0.5, SECONDS)
。寬度#
.width 傳回偵測到的物件在像素中的寬度,該值是 1 到 316 之間的整數。
# Move towards a blue object until its width is
# larger than 100 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].width < 100:
drivetrain.drive_for(FORWARD, 10, MM)
else:
drivetrain.stop()
wait(50, MSEC)
。高度#
.height 傳回偵測到的物件的高度(以像素為單位),該值是 1 到 212 之間的整數。
# Move towards a blue object until its height is
# larger than 100 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].height < 100:
drivetrain.drive_for(FORWARD, 10, MM)
else:
drivetrain.stop()
wait(50, MSEC)
.centerX#
.centerX 傳回偵測到的物件中心的 x 座標(以像素為單位),該值是 0 到 316 之間的整數。
# Turn slowly until the largest blue object is centered
# in front of the Vision Sensor.
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if 140 < vision_1.largest_object().centerX < 180:
drivetrain.stop()
wait(10,MSEC)
.centerY#
.centerY 傳回偵測到的物件中心的 y 座標(以像素為單位),該值是 0 到 212 之間的整數。
# Move towards a blue object until its
# center y-coordinate is more than 140 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].centerY < 140:
drivetrain.drive(FORWARD)
else:
drivetrain.stop()
wait(50, MSEC)
。角度#
.angle 傳回偵測到的物件的方向(以度為單位),該值是介於 0 和 359 之間的整數。
# Turn left or right depending on how a
# configured box code is rotated
while True:
box_code = vision_1.take_snapshot(vision_1__BOX_CODE)
if box_code[0].exists:
if 50 < box_code[0].angle < 100:
drivetrain.turn(RIGHT)
elif 270 < box_code[0].angle < 330:
drivetrain.turn(LEFT)
else:
drivetrain.stop()
else:
drivetrain.stop()
wait(50, MSEC)
.originX#
.originX 傳回偵測到的物件邊界框左上角的 x 座標(以像素為單位),該值是 0 到 316 之間的整數。
# Display if a red object is to the
# left or the right
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1,1)
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if red_box[0].originX < 160:
brain.screen.print("To the left!")
else:
brain.screen.print("To the right!")
wait(50, MSEC)
.originY#
.originY 傳回偵測到的物件邊界框左上角的 y 座標(以像素為單位),該值是 0 到 212 之間的整數。
# Display if a red object is close or far
# from the robot
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1,1)
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if red_box[0].originY < 80:
brain.screen.print("Far")
else:
brain.screen.print("Close")
wait(50, MSEC)
建構函數#
建構函數用於手動建立 Vision Signature和 Code 對象,這些物件對於在 VEXcode 之外配置感測器是必要的。
Vision Sensor#
Vision 創建視覺感測器。
用法
Vision(port, brightness, sigs)
參數 |
描述 |
|---|---|
|
視覺感測器連接到哪個智慧型端口,從 1 到 12。 |
|
可選。視覺感測器的亮度值,範圍從 1 到 100。 |
|
例子
# Create a new Signature "RED_BOX" with the Signature class
RED_BOX = Signature(1, -3911, -3435, -3673,10879, 11421, 11150,2.5, 0)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the "RED_BOX" Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)
# Move forward if a red object is detected
while True:
red_object = vision_1.take_snapshot(RED_BOX)
if red_object[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
Color Signature#
Signature 建立顏色簽名。視覺感測器最多可同時儲存七種不同的顏色簽名。
用法:
Signature(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)
範圍 |
描述 |
|---|---|
|
|
|
Vision Utility 中的數值介於 |
|
Vision Utility 中的數值介於 |
|
視覺實用工具中的 uMean 值介於 |
|
Vision Utility 中的值從 |
|
Vision Utility 中的數值介於 |
|
視覺實用程式中的值從 |
|
視覺實用程式中的值從 |
|
Vision Utility 中的 |
若要取得建立顏色簽名所需的值,請前往視覺實用程式。配置顏色簽名後,從配置視窗複製參數值。
例子
# Create a new Signature RED_BOX with the Signature class
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the RED_BOX Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)
# Move forward if a red object is detected
while True:
red_object = vision_1.take_snapshot(RED_BOX)
if red_object[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
Color Code#
代碼Code 分別用於建立顏色代碼。使用顏色代碼需要至少兩個已定義的顏色簽名。視覺感測器最多可以同時儲存八個不同的顏色代碼。
用法:
Code(sig1, sig2, sig3, sig4, sig5)
範圍 |
描述 |
|---|---|
|
先前建立的顏色簽名。 |
|
先前建立的顏色簽名。 |
|
可選。 先前建立的顏色簽名。 |
|
可選。 先前建立的顏色簽名。 |
|
可選。 先前建立的顏色簽名。 |
例子
# Create two new Signatures for a red and blue box
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440, 2.5, 1)
BLUE_BOX = Signature(2, -4443, -3373, -3908,6253, 7741, 6997, 2.5, 1)
# Create a Color Code for a red box to the left of a blue box
RED_BLUE = Code(RED_BOX, BLUE_BOX)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the red_box and blue_box Signatures.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX, BLUE_BOX)
# Display a message if Color Code is detected
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Code
box_code = vision_1.take_snapshot(RED_BLUE)
if box_code[0].exists:
brain.screen.print("Color code detected!")
wait(100, MSEC)