Esempio n. 1
0
    "GREEN": 20,
    "BLUE": 110,
    "RED": 200,
    "YELLOW": 290,
    "BLACK": 250,
    "WHITE": 75,
    "NONE": 162,
}

# Verify saturated colors without calibration.
for name in ("GREEN", "BLUE", "RED", "YELLOW"):
    motor.run_target(SPEED, angles[name])
    detected = color_sensor.color()
    assert detected == Color[name], "Expected {0} but got {1}".format(
        Color[name], detected)

# Update all colors.
for name in angles.keys():
    motor.run_target(SPEED, angles[name])
    Color[name] = color_sensor.hsv()

# Set new colors as detectable colors.
color_sensor.detectable_colors([Color[key] for key in angles.keys()])

# Test all newly calibrated colors.
for name in angles.keys():
    motor.run_target(SPEED, angles[name])
    detected = color_sensor.color()
    assert detected == Color[name], "Expected {0} but got {1}".format(
        Color[name], detected)
Esempio n. 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)
Esempio n. 3
0
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait

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

while True:
    # The standard color() method always "rounds" the
    # measurement to the nearest "whole" color.
    # That's useful for most applications.

    # But you can get the original hue, saturation,
    # and value without "rounding", as follows:
    color = sensor.hsv()

    # Print the results.
    print(color)

    # Wait so we can read the value.
    wait(500)
Esempio n. 4
0
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait

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

# Show the default color map.
print(sensor.color_map())

while True:
    # Read the HSV values.
    h, s, v = sensor.hsv()

    # Read the corresponding color based on the existing settings.
    color = sensor.color()

    # Print the measured values.
    print("Hue:", h, "Sat:", s, "Val:", v, "Col:", color)

    # Wait so we can read the value.
    wait(100)
Esempio n. 5
0
"""
Hardware Module: 1

Description: Verify color sensor arguments,
    by asserting that the same color is reported by:
    - using the default argument, e.g. surface=True
    - and using NO argument.
"""

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

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

# Verify that the default and NO-argument calls report the same color.
# Assume that the lighting will not change a lot during measuring.
hsv_default = color_sensor.hsv()
hsv_surface_true = color_sensor.hsv(surface=True)  # the default is surface=True.
# do not use surface=False in this test, that causes a mode switch and that would need more time.
assert hsv_default == hsv_surface_true, "Expected default '{0}' == '{1}'".format(
    hsv_default, hsv_surface_true
)

color_default = color_sensor.color()
color_surface_true = color_sensor.color(surface=True)  # the default is surface=True.
assert color_default == color_surface_true, "Expected default '{0}' == '{1}'".format(
    color_default, color_surface_true
)
Esempio n. 6
0
class ExplorationRover():
    """Control the Mars Exploration Rover."""

    # In this robot, we want to detect red and cyan/teal "mars rocks".
    ROCK_COLORS = (Color.RED, Color.CYAN)

    # These are the gears between the motor and the rear wheels.
    REAR_GEARS = (8, 24, 12, 20)

    # An animation of heart icons of varying brightness, giving a heart beat.
    HEART_BEAT = [
        Icon.HEART * i / 100
        for i in list(range(0, 100, 4)) + list(range(100, 0, -4))
    ]

    def __init__(self):
        # Initialize each motor with the correct direction and gearing.
        self.left_rear_wheels = Motor(Port.E, Direction.CLOCKWISE,
                                      self.REAR_GEARS)
        self.right_rear_wheels = Motor(Port.F, Direction.COUNTERCLOCKWISE,
                                       self.REAR_GEARS)
        self.left_front_wheel = Motor(Port.C, Direction.COUNTERCLOCKWISE, None)
        self.right_front_wheel = Motor(Port.D, Direction.CLOCKWISE, None)

        # Initialize sensors, named after the Perseverance Mars Rover instruments.
        self.mast_cam = UltrasonicSensor(Port.B)
        self.sherloc = ColorSensor(Port.A)

        # Initialize the hub and start the animation
        self.hub = InventorHub()
        self.hub.display.animate(self.HEART_BEAT, 30)

    def calibrate(self):
        # First, measure the color of the floor. This makes it easy to explicitly
        # ignore the floor later. This is useful if your floor has a wood color,
        # which can appear red or yellow to the sensor.
        self.floor_color = self.sherloc.hsv()

        # Set up all the colors we want to distinguish, including no color at all.
        self.sherloc.detectable_colors(self.ROCK_COLORS +
                                       (self.floor_color, None))

    def drive(self, speed, steering, time=None):
        # Drive the robot at a given speed and steering for a given amount of time.
        # Speed and steering is expressed as degrees per second of the wheels.
        self.left_rear_wheels.run(speed + steering)
        self.left_front_wheel.run(speed + steering)
        self.right_rear_wheels.run(speed - steering)
        self.right_front_wheel.run(speed - steering)

        # If the user specified a time, wait for this duration and then stop.
        if time is not None:
            wait(time)
            self.stop()

    def stop(self):
        # Stops all the wheels.
        self.left_rear_wheels.stop()
        self.left_front_wheel.stop()
        self.right_rear_wheels.stop()
        self.right_front_wheel.stop()

    def scan_rock(self, time):
        # Stops the robot and moves the scan arm.
        self.stop()
        self.left_front_wheel.run(100)

        # During the given duration, scan for rocks.
        watch = StopWatch()
        while watch.time() < time:

            # If a rock color is detected, display it and make a sound.
            if self.sherloc.color() in self.ROCK_COLORS:
                self.hub.display.image(Icon.CIRCLE)
                self.hub.speaker.beep()
            else:
                self.hub.display.off()

            wait(10)

        # Turn the arm motor and restore the heartbeat animation again.
        self.hub.display.animate(self.HEART_BEAT, 30)
        self.left_front_wheel.stop()