Beispiel #1
0
# Initialize sensor to make sure we're in the expected mode.
color_sensor.color()
wait(500)

# Color angle targets
angles = {
    "GREEN": 20,
    "BLUE": 110,
    "RED": 200,
    "YELLOW": 290,
    "BLACK": 250,
    "WHITE": 75,
    "NONE": 162,
}

# Go to blue and assert that we really do see blue.
motor.run_target(SPEED, angles["BLUE"])
assert color_sensor.color() == Color["BLUE"]

# Read ambient light, causing a mode change.
color_sensor.ambient()
wait(3000)

# Go to red and read the color right away.
motor.run_target(SPEED, angles["RED"])
detected = color_sensor.color()

# With the built-in delay, the stale blue value should be gone, now giving red.
assert detected == Color["RED"], "Expected {0} but got {1}".format(Color["RED"], detected)
Beispiel #2
0
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize the sensor.
sensor = ColorSensor(Port.A)

# Repeat forever.
while True:

    # Get the ambient color values. Instead of scanning the color of a surface,
    # this lets you scan the color of light sources like lamps or screens.
    hsv = sensor.hsv(surface=False)
    color = sensor.color(surface=False)

    # Get the ambient light intensity.
    ambient = sensor.ambient()

    # Print the measurements.
    print(hsv, color, ambient)

    # Point the sensor at a computer screen or colored light. Watch the color.
    # Also, cover the sensor with your hands and watch the ambient value.

    # Wait so we can read the printed line
    wait(100)
# SPDX-License-Identifier: MIT
# Copyright (c) 2021 The Pybricks Authors
"""
Hardware Module: 1

Description: Verify that ambient() and reflection() refuse an argument.
"""

from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port

# Initialize device.
color_sensor = ColorSensor(Port.B)

# Get the ambient light intensity to verify that the default works.
ambient_light_default = color_sensor.ambient()

# verify an argument passed to ambient is correctly refused.
expected = "function doesn't take keyword arguments"
try:
    ambient_light_surface_true = color_sensor.ambient(surface=True)
except Exception as e:
    assert str(e) == expected, "Expected '{0}' == '{1}'".format(e, expected)

# Get the reflected light intensity to verify that the default works.
reflection_default = color_sensor.reflection()

# verify an argument passed to reflection is correctly refused.
expected = "function doesn't take keyword arguments"
try:
    reflection_surface_true = color_sensor.reflection(surface=True)