It is commonly known that colors can be broken down into three basic colors: red, green, and blue. The color sensor scans what is in front of it and returns the basic color values of the scene. By comparing these returned values to predefined threshold values, we can easily determine what color an object in front of the robot is.
The main difference between the Rev V3 and other color sensors is the getDistance() function, meaning the color sensor can return the distance of an object within a certain range, from 1 cm - 10 cm.
colorSensorV3.getDistance(DistanceUnit.CM);
It is recommended that you do not use the V3 Color Sensor to actually determine the distance between your robot and an object, but instead use it to determine if an object is in front of the sensor, maybe to see if there is an object in a specific robot module.
Color Thresholding
It is commonly known that colors can be broken down into three basic colors: red, green, and blue. The color sensor scans what is in front of it and returns the basic color values of the scene. By comparing these returned values to predefined threshold values, we can easily determine what color an object in front of the robot is.
Because the resources listed above provide a sufficient explanation of how the code below works, we will just provide you with an implementation.
packageorg.firstinspires.ftc.teamcode.Sensors;importcom.qualcomm.robotcore.hardware.ColorSensor;importcom.qualcomm.robotcore.hardware.DistanceSensor;importcom.qualcomm.robotcore.hardware.HardwareMap;importorg.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;publicclassRevColor {publicColorSensor internalColorSensor;publicDistanceSensor internalDistanceSensor;// Base Color Thresholdpublicfinaldouble RED_THRESHOLD_LEFT =700, RED_THRESHOLD_RIGHT =700;publicfinaldouble BLUE_THRESHOLD_LEFT =700, BLUE_THRESHOLD_RIGHT =700;publicfinaldouble RED_THRESHOLD =550;publicfinaldouble BLUE_THRESHOLD =550;// Complex Color Thresholds (not red, green, or blue)publicfinaldouble YELLOW_RED_VALUE =0.419;publicfinaldouble YELLOW_GREEN_VALUE =0.3675;publicfinaldouble YELLOW_BLUE_VALUE =0.2116;publicfinaldouble[] YELLOW_CONSTANTS = {YELLOW_RED_VALUE, YELLOW_GREEN_VALUE, YELLOW_BLUE_VALUE};publicfinaldouble YELLOW_THRESHOLD =0.12;publicfinaldouble WHITE_RED_VALUE =0.30125;publicfinaldouble WHITE_GREEN_VALUE =0.37125;publicfinaldouble WHITE_BLUE_VALUE =0.325;publicfinaldouble[] WHITE_CONSTANTS = {WHITE_RED_VALUE, WHITE_GREEN_VALUE, WHITE_BLUE_VALUE};publicfinaldouble WHITE_THRESHOLD =0.01;publicfinaldouble WHITE_TOTAL_COUNT =800;// check for black with the alpha valuepublicfinaldouble BLACK_ALPHA_VALUE =325; //Test valuepublicRevColor(HardwareMap hardwareMap,String name){this.internalColorSensor=hardwareMap.get(ColorSensor.class, name);this.internalDistanceSensor=hardwareMap.get(DistanceSensor.class, name); }publicdoubledistance(){returninternalDistanceSensor.getDistance(DistanceUnit.CM); }// utility method for easily getting the color value. publicintred(){ returninternalColorSensor.red(); }publicintgreen(){ returninternalColorSensor.green(); }publicintblue(){ returninternalColorSensor.blue(); }publicdoubletotal(){ returnred()+green()+blue(); }// convert to arraypublicdouble[] rgb(){double[] arr =newdouble[3]; arr[0] =red(); arr[1] =green(); arr[2] =blue();return arr; }// Scale the rgb values (0 to 1)publicdouble[] normalizedRGB(){double[] arr =newdouble[3];double[] originalArr =rgb();double total =0;for(double i : originalArr) total+= i;for(int i =0; i <3; i++){ arr[i] = originalArr[i] / total; }return arr; }publicdoublearrayError(double[] arr1,double[] arr2){double total =0;for(int i =0; i <arr1.length; i++){ total +=Math.pow(arr1[i] - arr2[i],2); }returnMath.sqrt(total); }publicbooleanisBlack(){return (internalColorSensor.alpha() < BLACK_ALPHA_VALUE) ?true:false; }publicintalphaValue(){returninternalColorSensor.alpha(); }publicbooleanisRed(){returninternalColorSensor.red() > RED_THRESHOLD; }publicbooleanisBlue(){returninternalColorSensor.blue() > BLUE_THRESHOLD; }publicdoubleyellowError(){ returnarrayError(normalizedRGB(), YELLOW_CONSTANTS); }publicdoublewhiteError(){ returnarrayError(normalizedRGB(), WHITE_CONSTANTS); }publicbooleanisYellow(){returnyellowError()< YELLOW_THRESHOLD || (internalDistanceSensor.getDistance(DistanceUnit.CM)>6&&whiteError()>0.02); }publicbooleanisWhite(){returnwhiteError()< WHITE_THRESHOLD &&total()> WHITE_TOTAL_COUNT; }publicStringnormalizedValues() {double red =internalColorSensor.red();double green =internalColorSensor.green();double blue =internalColorSensor.blue();double total = red + green + blue;returnString.format("RGB: %.2f %.2f %.2f", red / total, green / total, blue / total); }// turn on the lightspublicvoidenableLED(boolean LEDMode){internalColorSensor.enableLed(LEDMode); }publicbooleanwithinColorRange(){returnisYellow()||isWhite(); }}