眼睛#

介绍#

GO 眼传感器使机器人能够检测物体并返回与其外观相关的信息,例如颜色和亮度。

在以下示例中,配置的 Eye Sensor 将被命名为 eye。在本 API 文档的所有后续示例中,当引用 Eye 类方法时,都将使用它们。

以下是所有方法的列表:

变异器——改变眼睛传感器的设置。

  • set_light – Turn the Eye Sensor’s light on or off.

  • set_range – Set the Eye’s Sensor detection range.

  • set_light_power – Set the Eye’s light power level.

Getters – 从检测到的对象返回数据。

  • get_color – Returns the color detected by the Eye Sensor.

  • get_hue – Returns the hue detected by the Eye Sensor.

  • get_brightness – Returns the brightness detected by the Eye Sensor.

  • is_object_detected – Returns whether or not an object is detected.

  • is_color_detected – Returns whether or not a certain color is detected.

修改器#

set_light#

set_light 打开或关闭眼睛传感器的灯。

用法:
eye.set_light(state)

参数

描述

state

要设置的灯光状态:

  • OFF
  • ON
# Build Used: Super Code Base 2.0
def main():
    # Turn the light on and off
    while True:
        eye.set_light(ON)
        wait(2, SECONDS)
        eye.set_light(OFF)
        wait(2, SECONDS)

# Start threads — Do not delete
start_thread(main)

set_range#

set_range 设置眼睛传感器可以检测物体的范围。

用法:
eye.set_range(distance)

参数

描述

distance

The detection range of the sensor:

  • FAR
  • NEAR
# Build Used: Code Base 2.0 - Eye Forward
def main():
    # Drive to an object with different ranges
    eye.set_range(FAR)
    drivetrain.drive(FORWARD)
    wait(0.2, SECONDS)
    while not eye.is_object_detected():
        wait(0.2, SECONDS)
    drivetrain.stop()
    drivetrain.drive_for(REVERSE, 100, MM)
    # Closer detection range
    eye.set_range(NEAR)
    drivetrain.drive(FORWARD)
    wait(0.2, SECONDS)
    while not eye.is_object_detected():
        wait(0.2, SECONDS)
    drivetrain.stop()


# Start threads — Do not delete
start_thread(main)

set_light_power#

set_light_power 设置 Eye Sensor 灯光的亮度。如果使用此方法时灯光未亮,则将以指定的亮度打开灯光。

用法:
eye.set_light_power(value)

参数

描述

value

灯光亮度以百分比表示,范围从 0% 到 100%。

# Build Used: Super Code Base 2.0
def main():
    # Turn on the light at different brightnesses
    eye.set_light_power(25)
    wait(2, SECONDS)
    eye.set_light_power(50)
    wait(2, SECONDS)
    eye.set_light_power(100)

# Start threads — Do not delete
start_thread(main)

吸气剂#

get_color#

get_color 返回眼睛传感器检测到的颜色。

用法:
eye.get_color()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Stop when red is detected
    drivetrain.drive(FORWARD)
    while True:
        if eye.get_color() == RED:
            break
        wait(0.2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

get_hue#

get_hue 返回眼睛传感器检测到的颜色的色调(0 到 359.99 度)。

用法:
eye.get_hue()

参数

描述

该方法没有参数。

# Build Used: Code Base 2.0 - Eye Forward
def main():
    # Check the hue of a pink GO piece
    while True:
        console.clear()
        if 290 < eye.get_hue() < 350:
            console.print("Pink!")
            wait(0.1,SECONDS)
        else:
            console.print("Not pink.")
            wait(0.1,SECONDS)

# Start threads — Do not delete
start_thread(main)

get_brightness#

get_brightness 返回眼睛传感器检测到的亮度,以 0% 到 100% 的百分比表示。

用法:
eye.get_brightness()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Continuously monitor the detected brightness
    # until detecting red
    monitor_sensor("eye.get_brightness")
    drivetrain.drive(FORWARD)
    while not eye.get_color() == RED:
        wait(0.2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

is_object_detected#

is_object_detected 返回一个布尔值,指示眼睛传感器是否检测到物体。

  • True – 眼睛传感器检测到物体。

  • False – 眼睛传感器未检测到物体。

用法:
eye.is_object_detected()

参数

描述

该方法没有参数。

# Build Used: Code Base 2.0 - Eye Forward
def main():
    # Drive forward until an object is detected
    drivetrain.drive(FORWARD)
    wait(0.2, SECONDS)
    while not eye.is_object_detected():
        wait(0.5, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

is_color_detected#

is_color_detected 返回一个布尔值,指示眼睛传感器是否检测到特定颜色。

  • True – 眼睛传感器检测到颜色。

  • False – 眼睛传感器未检测到颜色。

用法:
eye.is_color_detected(color)

参数

描述

color

眼睛传感器将检查的颜色:

  • BLUE
  • GREEN
  • NONE
  • RED
# Build Used: Super Code Base 2.0
def main():
    # Drive forward until a green object is detected
    drivetrain.drive(FORWARD)
    wait(0.2, SECONDS)
    while not eye.is_color_detected(GREEN):
        wait(0.5, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)