Programming Color Sensors
Last updated
Was this helpful?
Last updated
Was this helpful?
While a touch sensor features a physical switch to gather information, a color sensor makes use of reflected light. By doing so it collects different data to determine how much light it is seeing, the distance to a surface, and of course what color is in front of it.
For our robot we're going to focus on a few key components: hue, saturation, and value. With these we can use something known as the HSV color model to have the robot translate what its seeing into a recognizable color.
HSV is a form of a cylindrical RGB color model used to do things like create color pickers for digital painting programs, to edit photos, and for programming vision code.
Hue, saturation, and value all will play a part in helping our robot tell us what color it detects and allow us to make adjustments for something like a uniquely colored game piece!
Before we tackle colors, let's start with having our robot use the color sensor to tell us how much light is being reflected.
To do this we need to ask our color sensor to act as a light sensor, specifically an .
For this use case, we will use getLightDetected()
to have the sensor report the amount of light detected in a range of 0-1.
Time to test your program to see what your color sensor detects! While testing think about the following questions:
Is the number higher when less or more light is detected?
What happens when the color sensor looks at different color surfaces?
Does the value change when turning the color sensor's LED light on or off?
Does the value change if there is a shadow or if the lighting in the room changes?
However, for this tutorial we want to set our color sensor up as a NormalizedColorSensor.
Color Normalization is another technique within vision programming intended to help compensate for differences caused by lighting and shadows when looking at colors. This also affects shades of a color. For example, there are a ton of different shades of blue, such as cyan, navy, and aquamarine, but to our robot these will all be referenced as blue.
With our color sensor now set for normalized colors, we'll add a call for NormalizedRGBA
colors before we add telemetry for each individually.
Now we're ready to collect more data from our color sensor!
When using the option to "" while creating a new OpMode, the color sensor will be established similar to the following: