Programming Color Sensors
Last updated
Last updated
It is recommended to create a new OpMode while following this tutorial. Ours is named HelloRobot_ColorSensor!
The color and light sensor menus are found under the "Sensors" dropdown as seen below: Additional blocks to set or call colors are within the "Color" menu under Utilities:
While a touch sensor features a physical switch to gather information, a color sensor makes use of reflected light. By doing so it collect 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 start, let's grab a block to add to our loop. Our "key" should be set to "Light detected":
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?
Let's start by establishing a few variables in our program.
We'll be going over what a variable is in more detail during Part 2: Robot Control, but for this example we are using them to help our robot translate the data it records more clearly. Our variables will be called "color", "hue", "saturation", "value", and "normalizedColors".
We've discussed how most of these are related to the HSV color model, but what about normalizedColors?
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.
Now that we've named our variables, we need to set them to different values.
Next, let's go ahead and add set blocks for all our variables:
To each we can connect their corresponding block from the Color menu under Utilities:
Next we need to change our variable name from the default of "myColor".
Notice that "color" is matched with NormalizedColors using the matching variable while the rest have the variable set to "color".
From here we can add our telemetry blocks to see what values the color sensor detects!
To the "number" place we will pull a block from the color sensor menu:
From our variable menu we need a block. From the dropdown menu, we can change it to "normalizedColors". Next we will snap it in place with a block from the Color Sensor menu below our light detecting telemetry: