RGB Sensors
Last updated
Was this helpful?
Last updated
Was this helpful?
Download the colorblocks.pdf worksheet and print it in color to use along with this exercise.
RGB reading refers to the measurement of the intensity of red, green, and blue light components captured by a color sensor or obtained from an RGB LED. The RGB values typically range from 0 to 255, representing the intensity or brightness of each color channel.
Transforming RGB values to the x, y space involves converting the RGB color space to a different color space, such as the CIE XYZ color space. These color spaces are designed to represent colors perceptually, allowing for more accurate color manipulation and analysis.
The Scout contains two RGB sensors. It’s values are available from the SCT:RGB block. The block also contains the transformed X,Y values for each sensor.
Converting RGB values to the x, y space allows for a more perceptually meaningful representation of colors, enabling accurate color manipulation and comparison. It facilitates color-based calculations, such as color matching, color correction, and color gamut mapping, in various applications such as imaging, color science, and color management systems.
Now that this background is available, we can use this to help write an algorithm that can calibrate our sensor to look for a given color and later report back if that color is found.
Our algorithm needs to do the following steps
Collect several readings when looking at a color of interest.
Calculate the Average X and Y values for the sampled readings.
Use the average values during operation to determine if the observed color is similar
The blocks below are used to collect the samples. Notice that in this case, 20 samples are added to a list every .5 seconds.
Upon completion, the “calculateavg” message is broadcasted, to trigger both the x and y averages to be computed.
For each value, the stored values in the list are summed, and divided by the total number of records. Upon completion, the averages are stored in Avgx and AvgY respectively. The value are a good representation of the expected sensor output when this color is observed.
Y Average
Y Average
Now that there is a value that represents a color, we need a way to check if the current sensor readings are representative of that color. The best way to do this is to us the Pythagorean theorem.
The Pythagorean theorem is a fundamental principle in mathematics that relates to the lengths of the sides of a right triangle. It states that in a right triangle, the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides.
Mathematically, the Pythagorean theorem can be expressed as:
The Pythagorean theorem is a powerful tool for calculating distances in a two-dimensional space. By treating the coordinates of two points as the lengths of the sides of a right triangle, we can apply the Pythagorean theorem to find the distance between the two points.
The distance between two points can be calculated by using the Distance Formula which is derived from the Pythagorean Theorem,
The following code shows how to represent this in scratch blocks.
And the example below implements this using the variables defined earlier. D represents the resulting distance calculation.
When running this program, the smaller the distance value, the closer you are to the projected color.
The full program is below. Follow these steps.
Place the robot on a color of interest.
Press C to calibrate the sensor for the selected color.
After calibration, move the robot around to other colors and observe the distance value printed to the screen.
Observe the distance value to determine what tolerance you can use to determine that the color has been detected. The value should be low and typically less than 0.05.
Open this code sample using this link,