Programming Color Sensors
Color Sensor Basics:
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.
But what makes up a color?
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!
Detecting Light vs. Dark
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 OpticalDistanceSensor.
while (opModeIsActive()) {
telemetry.addData("Light Detected", ((OpticalDistanceSensor) test_color).getLightDetected());
telemetry.update();
}
For this use case, we will use getLightDetected()
to have the sensor report the amount of light detected in a range of 0-1.
Quick Check!
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?
Normalized Colors
When using the option to "Setup Code for Configured Hardware" while creating a new OpMode, the color sensor will be established similar to the following:
public class HelloRobot_ColorSensor extends LinearOpMode {
private ColorSensor test_color;
@Override
public void runOpMode() {
test_color = hardwareMap.get(ColorSensor.class, "test_color");
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.
public class HelloRobot_ColorSensor extends LinearOpMode {
private NormalizedColorSensor test_color;
@Override
public void runOpMode() {
test_color = hardwareMap.get(NormalizedColorSensor.class, "test_color");
With our color sensor now set for normalized colors, we'll add a call for NormalizedRGBA
colors before we add telemetry for each individually.
while (opModeIsActive()) {
telemetry.addData("Light Detected", ((OpticalDistanceSensor) test_color).getLightDetected());
NormalizedRGBA colors = test_color.getNormalizedColors();
telemetry.update();
}
Now we're ready to collect more data from our color sensor!
Last updated
Was this helpful?