眼睛#

介紹#

VEX 123 機器人內建眼部感應器,可偵測物體和顏色。它還可以報告反射回感測器的光量以及檢測到的顏色色調值。

眼睛感應器帶有一個可以打開或關閉的指示燈,以幫助它更清晰地識別物體和顏色。

眼動追蹤感測器的編碼方式有很多種。以下列出了所有眼動追蹤方法:

變異器 — 調整眼部感應器設定。

獲取器 — 返回眼部感應器偵測到的內容。

  • get_hue — 返回眼睛感應器偵測到的色調。

  • get_brightness — 傳回眼睛感應器偵測到的亮度。

  • is_object_detected — 返回眼感應器是否偵測到範圍內的物體。

  • is_color_detected — 返回眼睛感應器是否偵測到特定顏色。

  • is_bright — 返回眼睛感應器是否偵測到明亮物體。

突變體#

set_light#

set_light 開啟或關閉眼部感應器的燈。

Usage:
robot.eye.set_light(state)

參數

描述

state

眼部感應器的指示燈狀態: OFF 或 ON。

# Continuously blink the light.
while True:
    robot.eye.set_light(ON)
    wait(2, SECONDS)
    robot.eye.set_light(OFF)
    wait(2, SECONDS)

set_light_power#

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

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

如果眼部感應器的燈是關閉的,將燈光功率設定為 0% 以上即可開啟燈。

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

Usage:
robot.eye.set_light_power(value)

參數

描述

value

設定眼部感光元件燈光的亮度,範圍從 0% 到 100%。請使用整數。

# Set the light to different power levels.
robot.eye.set_light_power(25)
wait(2, SECONDS)
robot.eye.set_light_power(50)
wait(2, SECONDS)
robot.eye.set_light_power(100)

獲取器#

get_hue#

get_hue 返回眼睛感應器偵測到的顏色,以 0 到 359.00 度之間的整數表示。

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

一個圓形色輪,顯示完整的色調光譜,邊緣標有度數,從頂部的 0° 到 360°,以 30° 為增量遞增。

Usage:
robot.eye.get_hue()

參數

描述

此方法沒有參數。

# Display if an object is pink.
while True:
    console.clear()
    if 290 < robot.eye.get_hue() < 350:
        console.print("Pink!")
    else:
        console.print("Not pink!")
    wait(0.1, SECONDS)

get_brightness#

get_brightness 傳回偵測到的光的亮度,以 0% 到 100% 之間的整數表示。

百分比越高,表示反射回眼部感應器的光線越多;百分比越低,代表反射回的光線越少。

Usage:
robot.eye.get_brightness()

參數

描述

此方法沒有參數。

# Display whether a detected object is bright.
robot.eye.set_light_power(100)
robot.drive(FORWARD)
while not robot.eye.is_object_detected():
    wait(0.1, SECONDS)
wait(0.1, SECONDS)
if robot.eye.get_brightness() < 70:
    console.print("Object not bright.")
else:
    console.print("Bright object!")

is_object_detected#

is_object_detected 回傳一個 Boolean,報告眼感測器是否偵測到範圍內的物體。

  • True — 眼部感應器偵測到物體。

  • False — 眼部感應器未偵測到物體。

Usage:
robot.eye.is_object_detected()

參數

描述

此方法沒有參數。

# Stop driving after detecting an object.
robot.eye.set_light(ON)
robot.drive(FORWARD)
wait(0.1, SECONDS)
while not robot.eye.is_object_detected():
    wait(0.1, SECONDS)
robot.stop()

is_color_detected#

is_color_detected 回傳一個 Boolean,該值根據偵測到的色調值報告眼睛感測器是否偵測到指定的顏色。

  • True — 眼睛感應器偵測到所選顏色。

  • False — 眼睛感應器未偵測到所選顏色。

當物體距離較近且光線充足時,眼部感應器工作效果最佳。

Usage:
robot.eye.is_color_detected(color)

參數

描述

color

The color for the Eye Sensor to check:

  • BLUE – Hue value between 160° and 254°.
  • GREEN – Hue value between 75° and 154°.
  • NONE
  • RED – Hue value between 340° and 20°.

# Stop driving after detecting a green object.
robot.eye.set_light(ON)
robot.drive(FORWARD)
wait(0.1, SECONDS)
while not robot.eye.is_color_detected(GREEN):
    wait(0.1, SECONDS)
robot.stop()

is_bright#

is_bright 回傳一個 Boolean,報告由 Eye Sensor 偵測到的物件是否明亮。

當物體反射回人眼感測器的亮度超過其亮度的 70% 時,該物體被認為是明亮的。

  • True — 偵測到的物體反射超過 70% 的亮度。

  • False — 偵測到的物體反射亮度低於 70%。

Usage:
robot.eye.is_bright()

參數

描述

此方法沒有參數。

# Display whether a detected object is bright.
robot.eye.set_light_power(100)
robot.drive(FORWARD)
while not robot.eye.is_object_detected():
    wait(0.1, SECONDS)
wait(0.1, SECONDS)
if robot.eye.is_bright():
    console.print("Bright object!")
else:
    console.print("Object not bright.")