examples: Update detectable colors API.

This commit is contained in:
Laurens Valk
2021-04-10 15:31:28 +02:00
parent 68f626f1ef
commit ca96ee2fce
4 changed files with 24 additions and 32 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ Changing the detectable colors
******************************
By default, the sensor is configured to detect red, yellow, green,
blue, white, or ``None``, which suits many applications.
blue, white, or no color, which suits many applications.
For better results in your application, you can measure your desired
colors in advance, and tell the sensor to look only for those colors.
+1 -1
View File
@@ -58,7 +58,7 @@ Changing the detectable colors
******************************
By default, the sensor is configured to detect red, yellow, green,
blue, white, or ``None``, which suits many applications.
blue, white, or no color, which suits many applications.
For better results in your application, you can measure your desired
colors in advance, and tell the sensor to look only for those colors.
+11 -15
View File
@@ -5,26 +5,22 @@ from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorSensor(Port.A)
# First, decide which objects you want to detect.
# Then measure their color with the hsv() method,
# as shown in the previous example. Write them down
# as shown below. The name is optional, but it is
# useful when you print the color value.
green = Color(h=132, s=94, v=26, name='GREEN_BRICK')
magenta = Color(h=348, s=96, v=40, name='MAGENTA_BRICK')
brown = Color(h=17, s=78, v=15, name='BROWN_BRICK')
red = Color(h=359, s=97, v=39, name='RED_BRICK')
# First, decide which objects you want to detect, and measure their HSV values.
# You can do that with the hsv() method as shown in the previous example.
#
# Use your measurements to override the default colors, or add new colors:
Color.GREEN = Color(h=132, s=94, v=26)
Color.MAGENTA = Color(h=348, s=96, v=40)
Color.BROWN = Color(h=17, s=78, v=15)
Color.RED = Color(h=359, s=97, v=39)
# Put your colors in a list or tuple.
# Including None is optional. Just omit it if
# you always want to get one of your colors.
my_colors = (green, magenta, brown, red, None)
my_colors = (Color.GREEN, Color.MAGENTA, Color.BROWN, Color.RED, Color.NONE)
# Save your colors.
sensor.detectable_colors(my_colors)
# color() works as usual, but it only
# returns one of your specified colors.
# color() works as usual, but now it returns one of your specified colors.
while True:
color = sensor.color()
@@ -32,7 +28,7 @@ while True:
print(color)
# Check which one it is.
if color == magenta:
if color == Color.MAGENTA:
print("It works!")
# Wait so we can read it.
@@ -5,26 +5,22 @@ from pybricks.tools import wait
# Initialize the sensor.
sensor = ColorDistanceSensor(Port.A)
# First, decide which objects you want to detect.
# Then measure their color with the hsv() method,
# as shown in the previous example. Write them down
# as shown below. The name is optional, but it is
# useful when you print the color value.
green = Color(h=132, s=94, v=26, name='GREEN_BRICK')
magenta = Color(h=348, s=96, v=40, name='MAGENTA_BRICK')
brown = Color(h=17, s=78, v=15, name='BROWN_BRICK')
red = Color(h=359, s=97, v=39, name='RED_BRICK')
# First, decide which objects you want to detect, and measure their HSV values.
# You can do that with the hsv() method as shown in the previous example.
#
# Use your measurements to override the default colors, or add new colors:
Color.GREEN = Color(h=132, s=94, v=26)
Color.MAGENTA = Color(h=348, s=96, v=40)
Color.BROWN = Color(h=17, s=78, v=15)
Color.RED = Color(h=359, s=97, v=39)
# Put your colors in a list or tuple.
# Including None is optional. Just omit it if
# you always want to get one of your colors.
my_colors = (green, magenta, brown, red, None)
my_colors = (Color.GREEN, Color.MAGENTA, Color.BROWN, Color.RED, Color.NONE)
# Save your colors.
sensor.detectable_colors(my_colors)
# color() works as usual, but it only
# returns one of your specified colors.
# color() works as usual, but now it returns one of your specified colors.
while True:
color = sensor.color()
@@ -32,7 +28,7 @@ while True:
print(color)
# Check which one it is.
if color == magenta:
if color == Color.MAGENTA:
print("It works!")
# Wait so we can read it.