SPIKE: Advanced color sensor functions detectable_colors() #1752
Replies: 4 comments
-
You can actually modify the from pybricks.hubs import InventorHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = InventorHub()
sensor1 = ColorSensor(Port.B)
Color.NEW = Color(210, 30, 50)
colors = list(sensor1.detectable_colors())
colors.append(Color.NEW)
sensor1.detectable_colors(colors)
while True:
print(sensor1.color())
wait(100)
|
Beta Was this translation helpful? Give feedback.
-
Here's how we do it for our FLL robot: self.colorSensor = ColorSensor(Port.F)
# HSV values were found by testing. Default hsv-values are provided
# in comments. Theoretically, the farther apart the hsv-values are,
# the less likely two colors can get "confused"
# Use the colorTest.py program to get the color sensor values
Color.SENSOR_WHITE = Color(h=0, s=0, v=100) # h=0,s=0,v=100
Color.SENSOR_RED = Color(h=353, s=82, v=92) # h=0,s=100,v=100
Color.SENSOR_YELLOW = Color(h=60, s=60, v=100) # h=60,s=100,v=100
Color.SENSOR_GREEN = Color(h=156, s=66, v=66) # h=120,s=100,v=100
Color.SENSOR_BLUE = Color(h=216, s=84, v=83) # h=240,s=100,v=100
Color.SENSOR_MAGENTA = Color(h=333, s=75, v=78) # h=300,s=100,v=100
Color.SENSOR_ORANGE = Color(h=8, s=75, v=100) # h=30,s=100,v=100
Color.SENSOR_DARKGRAY = Color(h=192, s=21, v=64) # h=0,s=0,v=50
Color.SENSOR_NONE = Color(h=170, s=26, v=15) # h=0,s=0,v=0
Color.SENSOR_LIME = Color(h=92, s=55, v=93) # h=92, s=57, v=93
# Put the custom colors in a list. Best practice is to only use
# colors that we are using for actual missions.
self.sensorColors = [
Color.SENSOR_WHITE,
Color.SENSOR_RED,
Color.SENSOR_YELLOW,
Color.SENSOR_GREEN,
Color.SENSOR_BLUE,
Color.SENSOR_MAGENTA,
Color.SENSOR_ORANGE,
Color.SENSOR_DARKGRAY,
Color.SENSOR_NONE, # must have SENSOR_NONE. Do not comment
Color.SENSOR_LIME,
]
# Set the detectable colors usisng our list
self.colorSensor.detectable_colors(self.sensorColors)
# Translates our costom colors into the default pybricks colors
# Used to set the hub light to the correct color. It dodesn't
# matter if there are extra colors in here that won't be detected
self.myColor2DefaultColorDict = {
Color.SENSOR_GREEN: Color.GREEN,
Color.SENSOR_RED: Color.RED,
Color.SENSOR_YELLOW: Color.YELLOW,
Color.SENSOR_BLUE: Color.BLUE,
Color.SENSOR_MAGENTA: Color.MAGENTA,
Color.SENSOR_WHITE: Color.WHITE,
Color.SENSOR_ORANGE: Color.ORANGE,
Color.SENSOR_DARKGRAY: Color.GRAY,
Color.SENSOR_NONE: Color.NONE,
Color.SENSOR_LIME: Color.CYAN,
} We wanted the translation from sensor colors to default colors because we set the center button light color to whatever color our sensor sees. The button lights really only like fully saturated colors, so having the conversion makes the light colors look right. |
Beta Was this translation helpful? Give feedback.
-
You can find examples at the bottom of the documentation page. If you're going to calibrate one color, it's better to calibrate all of them as in the example. Appending new ones isn't really recommended. This should probably be added to the docs. |
Beta Was this translation helpful? Give feedback.
-
Awesome hints, thanks @FLL-Team-24277 and @laurensvalk for the insights. It is always cool to learn here. |
Beta Was this translation helpful? Give feedback.
-
I was asked about how to add a custom color to SPIKE color sensor detection.
Though the documentation covers the function, it was not easy and straightforward to resolve it as the detectable_colors() returns a tuple.
So I will leave the code for others to find here.
OUPUT:
Beta Was this translation helpful? Give feedback.
All reactions