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!
Adding a Hue Variable
To determine the final color we will be using the reported hue value from JavaUtil.colorToHue(colors.toColor()). Because we'll be using hue repeatedly, let's establish it as a variable to help keep our code clean.
First we will set up "hue" as a number data type during initialization:
@TeleOp
public class HelloRobot_ColorSensor extends LinearOpMode {
private NormalizedColorSensor test_color;
double hue; // <------ New code
Next, we'll define "hue" while our opModeIsActive using the same method as before:
while (opModeIsActive()) {
telemetry.addData("Light Detected", ((OpticalDistanceSensor) test_color).getLightDetected());
NormalizedRGBA colors = test_color.getNormalizedColors();
hue = JavaUtil.colorToHue(colors.toColor()); // <------ New code
With our variable added, our current code should appear as follows:
@TeleOp
public class HelloRobot_ColorSensor extends LinearOpMode {
private NormalizedColorSensor test_color;
double hue; // <------ New code
@Override
public void runOpMode() {
test_color = hardwareMap.get(NormalizedColorSensor.class, "test_color");
waitForStart();
while (opModeIsActive()) {
telemetry.addData("Light Detected", ((OpticalDistanceSensor) test_color).getLightDetected());
NormalizedRGBA colors = test_color.getNormalizedColors();
hue = JavaUtil.colorToHue(colors.toColor()); // <------ New code
//Determining the amount of red, green, and blue
telemetry.addData("Red", "%.3f", colors.red);
telemetry.addData("Green", "%.3f", colors.green);
telemetry.addData("Blue", "%.3f", colors.blue);
//Determining HSV and alpha
telemetry.addData("Hue", JavaUtil.colorToHue(colors.toColor()));
telemetry.addData("Saturation", "%.3f", JavaUtil.colorToSaturation(colors.toColor()));
telemetry.addData("Value", "%.3f", JavaUtil.colorToValue(colors.toColor()));
telemetry.addData("Alpha", "%.3f", colors.alpha);
telemetry.update();
}
}
}
Detecting Common Colors
Let's first set up the skeleton of our if/else statement for determining different colors:
if(hue < 30){
}
else if (hue < 60) {
}
else if (hue < 90){
}
else if (hue < 150){
}
else if (hue < 225){
}
else if (hue < 350){
}
else{
}
Each check will be for a certain color that is within the specified range. For example, a color that's hue is between 90-149 should appear as green.
Within each statement let's add a telemetry.addData and the color for that range:
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!
Full Color Sensor Program
@TeleOp
public class HelloRobot_ColorSensor extends LinearOpMode {
private NormalizedColorSensor test_color;
double hue;
@Override
public void runOpMode() {
test_color = hardwareMap.get(NormalizedColorSensor.class, "test_color");
waitForStart();
while (opModeIsActive()) {
telemetry.addData("Light Detected", ((OpticalDistanceSensor) test_color).getLightDetected());
NormalizedRGBA colors = test_color.getNormalizedColors();
hue = JavaUtil.colorToHue(colors.toColor());
//Determining the amount of red, green, and blue
telemetry.addData("Red", "%.3f", colors.red);
telemetry.addData("Green", "%.3f", colors.green);
telemetry.addData("Blue", "%.3f", colors.blue);
//Determining HSV and alpha
telemetry.addData("Hue", JavaUtil.colorToHue(colors.toColor()));
telemetry.addData("Saturation", "%.3f", JavaUtil.colorToSaturation(colors.toColor()));
telemetry.addData("Value", "%.3f", JavaUtil.colorToValue(colors.toColor()));
telemetry.addData("Alpha", "%.3f", colors.alpha);
//Using hue to detect color
if(hue < 30){
telemetry.addData("Color", "Red");
}
else if (hue < 60) {
telemetry.addData("Color", "Orange");
}
else if (hue < 90){
telemetry.addData("Color", "Yellow");
}
else if (hue < 150){
telemetry.addData("Color", "Green");
}
else if (hue < 225){
telemetry.addData("Color", "Blue");
}
else if (hue < 350){
telemetry.addData("Color", "Purple");
}
else{
telemetry.addData("Color", "Red");
}
telemetry.update();
}
}
}
Build 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.