眼睛#

介绍#

VR眼部传感器可以检测物体并识别其颜色、亮度和色调。它还允许您调整检测范围和照明设置,以获得更准确的读数。

以下是所有可用方法的列表:

获取器 – 读取物体的存在、颜色、亮度和色调。

  • near_object – 返回眼传感器是否检测到范围内的物体。

  • detect – 返回眼睛传感器是否检测到特定颜色。

  • brightness – 返回物体反射的光量百分比。

回调函数——当检测到物体或物体丢失时运行的代码。

  • object_detected – 注册一个函数,当眼部传感器检测到物体时,该函数将被调用。

  • object_lost – 注册一个函数,当眼动传感器丢失物体时调用该函数。

获取器#

near_object#

near_object 返回一个 Boolean,报告眼传感器是否检测到范围内的物体。

  • True – 传感器靠近物体。

  • False ​​– 传感器附近没有物体。

Usage:
eye.near_object()

参数

描述

此方法没有参数。

def main():
    # Stop driving when Eye Sensor is 
    # near an object
    drivetrain.drive(FORWARD)
    while True:
        if front_eye.near_object():
            drivetrain.stop()
        wait(5, MSEC)

# VR threads — Do not delete
vr_thread(main)

detect#

detect 返回一个 Boolean,该值根据检测到的色调值报告眼睛传感器是否检测到指定的颜色。

  • True — 眼睛传感器检测到所选颜色。

  • False ​​— 眼睛传感器未检测到所选颜色。

Usage:
eye.detect(color)

参数

描述

color

眼动传感器检测的颜色:

  • BLUE
  • GREEN
  • NONE
  • RED
def main():
    # Stop the robot if the Eye Sensor 
    # detects red
    drivetrain.turn(RIGHT)
    while True:
        if front_eye.detect(RED):
            drivetrain.stop()
        wait(5, MSEC)

# VR threads — Do not delete
vr_thread(main)

brightness#

brightness 返回物体反射的光量,以百分比表示(float)。

百分比越高,意味着反射回眼部传感器的光线越多;百分比越低,意味着反射回的光线越少。

Usage:
eye.brightness(units)

参数

描述

units

亮度单位: PERCENT

打回来#

object_detected#

object_detected 注册一个函数,当眼部传感器检测到新物体时,该函数将被调用。

Usage:
eye.object_detected(callback)

参数

描述

callback

先前定义的 函数,当眼部传感器检测到新物体时执行。

def detected():
    # Display a message
    drivetrain.stop()
    brain.print("object detected")
    wait(0.5,SECONDS)
    brain.clear()

def main():
    # Drive until an object is detected
    drivetrain.drive(FORWARD)
    front_eye.object_detected(detected)

# VR threads — Do not delete
vr_thread(main)

object_lost#

object_lost 注册一个函数,当眼动传感器丢失检测到的对象时,该函数将被调用。

Usage:
eye.object_lost(callback)

参数

描述

callback

先前定义的 函数,当眼部传感器丢失检测到的对象时执行。

def lost():
    # Display a message
    drivetrain.stop()
    brain.print("object lost")
    wait(0.5,SECONDS)
    brain.clear()

def main():
    # Back away from an object
    drivetrain.drive(REVERSE)
    front_eye.object_lost(lost)

# VR threads — Do not delete
vr_thread(main)