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:
We are going to add several telemetry blocks within our program, but let's start by having our robot tell us how much red, green, or blue it sees when looking at an object with our Color Sensor.
For this we will be using the block from the Telemetry menu. We will need three in total, one for each color, which will be entered in the "key".
The "precision" on the block will be changed to 3. Precision sets the number of decimal places!
To add telemetry for hue, saturation, and value we will repeat most of the previous process. However, for "number" we will snap in the matching variable block.
Save your OpMode and give it a try! How do the values change depending on what color object the sensor is looking at?
This feature requires the Color Sensor's LED to be switched on!
While working on your code, you may have noticed something called "alpha". The alpha value of a surface tells how transparent or opaque it may be.
Using a similar method as before, we can add a telemetry call for the alpha value to see on our Driver Hub.
To "number" we will add the appropriate block from the "Color" menu:
We've asked our robot to gather a lot of data with the color sensor. Now let's have it use that information to output an actual color name rather than just a value!
Recall when we learned about using if/else
statements while working with the touch sensor.
Let's first set up the skeleton of our if/else statement for determining different colors:
Last we can add our telemetry for our robot to read out information to the Driver Hub based on what it detects:
Each check will be for a certain color that is within the specified range. "Key" can be changed to "Color" on the telemetry blocks. We can add the colors in first:
Next we will add our values for the hue ranges. For example, a color that's hue is between 90-149 should appear as green.
The exact hue values may need to be adjusted slightly, but those used above are based on the default conversion of HSV to RGB when using hue to identify color.
You'll notice that "red" is detected for values under 30 and above 350. This is intentional as red is the beginning and end of the RGB spectrum!
Let's snap our If/Else statement into our loop below the "Alpha" telemetry call.
Save your OpMode and give it a try! You can adjust the values as you need to better reflect the colors available or changes due to lighting in the room.
Once we've added our block we'll click the gear to add the needed "else if" pieces to have enough for all our colors. To each we will add a block from the Logic menu. Next we can add our variable and a block to each side of this logic statement. We want for each if/else our robot to check if the read hue is LESS THAN a set number!