眼睛#
介绍#
VR眼部传感器可以检测物体并识别其颜色、亮度和色调。它还允许您调整检测范围和照明设置,以获得更准确的读数。
以下是所有可用方法的列表:
Getters – Read object presence, color, brightness, and hue.
near_object– Returns whether the Eye Sensor detects an object within range.detect– Returns whether the Eye Sensor detects a specific color.brightness– Returns the amount of light reflected from the object as a percent.
回调函数——当检测到物体或物体丢失时运行的代码。
object_detected– Registers a function to be called whenever the Eye Sensor detects an object.object_lost– Registers a function to be called whenever the Eye Sensor loses an object.
获取器#
near_object#
near_object returns a Boolean that reports whether or not the Eye Sensor detects an object within range.
True– The sensor is near an object.False– The sensor is not near an object.
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 returns a Boolean that reports whether the Eye Sensor detects a specified color, based on the detected hue value.
True— The Eye Sensor detects the selected color.False— The Eye Sensor does not detect the selected color.
Usage:
eye.detect(color)
参数 |
描述 |
|---|---|
|
The color for the Eye Sensor to check for:
|
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 returns the amount of light reflected from the object as a decimal (float) representing a percent.
A higher percentage means more light is reflected back to the Eye Sensor. A lower percentage means less light is reflected back.
Usage:
eye.brightness(units)
参数 |
描述 |
|---|---|
|
The unit that represents the brightness: |
打回来#
object_detected#
object_detected registers a function to be called whenever the Eye Sensor detects a new object.
Usage:
eye.object_detected(callback)
参数 |
描述 |
|---|---|
|
A previously defined function that executes when the Eye Sensor detects a new object. |
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 registers a function to be called whenever the Eye Sensor loses a detected object.
Usage:
eye.object_lost(callback)
参数 |
描述 |
|---|---|
|
A previously defined function that executes when the Eye Sensor loses a detected object. |
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)