眼睛#

介绍#

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

For the examples below, the configured Eye Sensor will be named eye. This name is used in all subsequent examples throughout this API documentation when referring to Eye class methods.

以下是所有方法的列表:

Mutators — Change the settings of the Eye Sensor.

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

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

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

Getters — Return data from detected objects.

  • 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

The state of the light to set:

  • 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 sets the range at which the Eye Sensor can detect objects. By default, the range is set to FAR.

用法:
eye.set_range(distance)

参数

描述

distance

The detection range of the sensor:

  • FAR — Up to 40 millimeters
  • NEAR — Up to 18 millimeters

# 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.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 度)。

A circular color wheel displaying a full spectrum of hues labeled with degree values around the perimeter, increasing in 30-degree increments from 0° at the top to 360°.

用法:
eye.get_hue()

参数

描述

该方法没有参数。

# Build Used: Code Base 2.0 - Eye Forward
def main():
    # Display the hue of a pink GO piece
    while True:
        console.clear()
        if 10 < eye.get_hue() < 40:
            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():
    # Monitor the brightness until red is detected
    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 — The Eye Sensor detects an object.

  • False — The Eye Sensor does not detect an object.

用法:
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 — The Eye Sensor detects the color.

  • False — The Eye Sensor does not detect the color.

用法:
eye.is_color_detected(color)

参数

描述

color

The color the Eye Sensor will check:

  • 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)