snippets/pup/sensor_color: add 6 samples

These are nearly the same as for the ColorDistanceSensor.
This commit is contained in:
Laurens Valk
2020-07-13 14:04:59 +02:00
parent 36a4bdd2a9
commit caa4012b2a
6 changed files with 145 additions and 0 deletions
@@ -0,0 +1,26 @@
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorSensor(Port.A)
# Get the current color settings.
hues, saturation, values = sensor.color_map()
# Let's say we have used the hsv() method to determine
# that the orange hue is 10. We can add it like this.
hues[Color.ORANGE] = 10
# Also, let's say that we are not interested in blue.
# So, we remove it from the hues dictionary.
hues.pop(Color.BLUE)
# Now save the new settings
sensor.color_map(hues, saturation, values)
# Now use the color() method as usual. Now it can also detect ORANGE
# and it will not report BLUE, because we removed it from the hues.
while True:
print(sensor.color())
wait(100)
@@ -0,0 +1,24 @@
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorSensor(Port.A)
# Here we choose only red, yellow, green, and blue, and provide the hues.
hues = {
Color.RED: 350,
Color.YELLOW: 30,
Color.GREEN: 110,
Color.BLUE: 210
}
# Save the updated settings. The values dictionary is empty because we don't
# need to detect black/white in this demo application.
sensor.color_map(hues, 30, {})
# color() works as usual, but it only returns red/yellow/green/blue, or None.
# This avoids accidentally detecting black on the dark gray train tracks.
while True:
print(sensor.color())
wait(100)
+19
View File
@@ -0,0 +1,19 @@
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorSensor(Port.A)
while True:
# Read the color.
color = sensor.color()
# Print the measured color.
print(color)
# Move the sensor around and see how
# well you can detect colors.
# Wait so we can read the value.
wait(100)
+22
View File
@@ -0,0 +1,22 @@
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)
+27
View File
@@ -0,0 +1,27 @@
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:
# Turn on one light at a time, at half the brightness.
# Do this for all 3 lights and repeat that 5 times.
for i in range(5):
sensor.lights.on(50, 0, 0)
wait(100)
sensor.lights.on(0, 50, 0)
wait(100)
sensor.lights.on(0, 0, 50)
wait(100)
# Turn all lights on at maximum brightness.
sensor.lights.on(100)
wait(500)
# Turn all lights off.
sensor.lights.off()
wait(500)
@@ -0,0 +1,27 @@
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorSensor(Port.A)
# This is a function that waits for a desired color.
def wait_for_color(desired_color):
# While the color is not the desired color, we keep waiting.
while sensor.color() != desired_color:
wait(20)
# Now we use the function we just created above.
while True:
# Here you can make your train/vehicle go forward.
print("Waiting for red ...")
wait_for_color(Color.RED)
# Here you can make your train/vehicle go backward.
print("Waiting for blue ...")
wait_for_color(Color.BLUE)