1"use strict";(self.webpackChunktechtoolbox=self.webpackChunktechtoolbox||[]).push([[2859],{3905:(e,n,r)=>{r.d(n,{Zo:()=>c,kt:()=>E});var o=r(7294);function t(e,n,r){return n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r,e}function l(e,n){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);n&&(o=o.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.push.apply(r,o)}return r}function a(e){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?l(Object(r),!0).forEach((function(n){t(e,n,r[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(r,n))}))}return e}function i(e,n){if(null==e)return{};var r,o,t=function(e,n){if(null==e)return{};var r,o,t={},l=Object.keys(e);for(o=0;o<l.length;o++)r=l[o],n.indexOf(r)>=0||(t[r]=e[r]);return t}(e,n);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(o=0;o<l.length;o++)r=l[o],n.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(t[r]=e[r])}return t}var u=o.createContext({}),s=function(e){var n=o.useContext(u),r=n;return e&&(r="function"==typeof e?e(n):a(a({},n),e)),r},c=function(e){var n=s(e.components);return o.createElement(u.Provider,{value:n},e.children)},b="mdxType",d={inlineCode:"code",wrapper:function(e){var n=e.children;return o.createElement(o.Fragment,{},n)}},p=o.forwardRef((function(e,n){var r=e.components,t=e.mdxType,l=e.originalType,u=e.parentName,c=i(e,["components","mdxType","originalType","parentName"]),b=s(r),p=t,E=b["".concat(u,".").concat(p)]||b[p]||d[p]||l;return r?o.createElement(E,a(a({ref:n},c),{},{components:r})):o.createElement(E,a({ref:n},c))}));function E(e,n){var r=arguments,t=n&&n.mdxType;if("string"==typeof e||t){var l=r.length,a=new Array(l);a[0]=p;var i={};for(var u in n)hasOwnProperty.call(n,u)&&(i[u]=n[u]);i.originalType=e,i[b]="string"==typeof e?e:t,a[1]=i;for(var s=2;s<l;s++)a[s]=r[s];return o.createElement.apply(null,a)}return o.createElement.apply(null,r)}p.displayName="MDXCreateElement"},9262:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>u,contentTitle:()=>a,default:()=>d,frontMatter:()=>l,metadata:()=>i,toc:()=>s});var o=r(7462),t=(r(7294),r(3905));const l={},a="Color Sensors",i={unversionedId:"Sensors/b",id:"Sensors/b",title:"Color Sensors",description:"Official FTC Video Docs - Must Watch!*",source:"@site/docs/Sensors/b.md",sourceDirName:"Sensors",slug:"/Sensors/b",permalink:"/docs/Sensors/b",draft:!1,tags:[],version:"current",frontMatter:{},sidebar:"tutorialSidebar",previous:{title:"IMU",permalink:"/docs/Sensors/a"},next:{title:"Distance Sensors",permalink:"/docs/Sensors/c"}},u={},s=[{value:"Initialization",id:"initialization",level:2},{value:"Implementation",id:"implementation",level:2},{value:"Rev Color Sensor V3",id:"rev-color-sensor-v3",level:3},{value:"Initialization",id:"initialization-1",level:4},{value:"Color Thresholding",id:"color-thresholding",level:2},{value:"Advanced Version",id:"advanced-version",level:3}],c={toc:s},b="wrapper";function d(e){let{components:n,...r}=e;return(0,t.kt)(b,(0,o.Z)({},c,r,{components:n,mdxType:"MDXLayout"}),(0,t.kt)("h1",{id:"color-sensors"},"Color Sensors"),(0,t.kt)("admonition",{title:"Resources",type:"note"},(0,t.kt)("ul",{parentName:"admonition"},(0,t.kt)("li",{parentName:"ul"},(0,t.kt)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=iQufRF1HFRc"},"Official FTC Video Docs")," - ",(0,t.kt)("strong",{parentName:"li"},"Must Watch!")),(0,t.kt)("li",{parentName:"ul"}
1,(0,t.kt)("a",{parentName:"li",href:"https://ftc-tricks.com/overview-color-sensor/"},"FTC Tricks"),"\nIt 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. "))),(0,t.kt)("h2",{id:"initialization"},"Initialization"),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},'ColorSensor colorSensor;\ncolorSensor = hardwareMap.get(ColorSensor.class, "color_sensor");\nTest Edit - dont merge\n')),(0,t.kt)("h2",{id:"implementation"},"Implementation"),(0,t.kt)("admonition",{type:"caution"},(0,t.kt)("p",{parentName:"admonition"},"You should always be calling the ",(0,t.kt)("inlineCode",{parentName:"p"},"enableLED()")," function before reading the color sensor as it helps to eliminate inaccuracies caused by lighting. ")),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},"colorSensor.enableLed(true);\n")),(0,t.kt)("p",null,"Find red, green, blue, or alpha values from 0-255"),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},"int red = colorSensor.red();\nint blue = colorSensor.blue();\nint green = colorSensor.green();\nint alpha = colorSensor.alpha(); //0 - Transparent, 255 - Opaque\n")),(0,t.kt)("p",null,"Return a combined argb color value by using the argb() command."),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},"int combined = colorSensor.argb();\n")),(0,t.kt)("h3",{id:"rev-color-sensor-v3"},"Rev Color Sensor V3"),(0,t.kt)("p",null,"The newest color sensor by Rev Robotics, version 3, has some extra capabilities, and therefore a different class to use for its functions."),(0,t.kt)("h4",{id:"initialization-1"},"Initialization"),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},'RevColorSensorV3 colorSensorV3 = (RevColorSensorV3) hardwareMap.colorSensor.get("colorV3");\n')),(0,t.kt)("admonition",{type:"info"},(0,t.kt)("p",{parentName:"admonition"},"The main difference between the Rev V3 and other color sensors is the ",(0,t.kt)("inlineCode",{parentName:"p"},"getDistance()")," function, meaning the color sensor can return the distance of an object within a certain range, from 1 cm - 10 cm.")),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},"colorSensorV3.getDistance(DistanceUnit.CM);\n")),(0,t.kt)("admonition",{type:"info"},(0,t.kt)("p",{parentName:"admonition"},"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.")),(0,t.kt)("h2",{id:"color-thresholding"},"Color Thresholding"),(0,t.kt)("p",null,"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."),(0,t.kt)("p",null,"Because the resources listed above provide a sufficient explanation of how the code below works, we will just provide you with an implementation. "),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},'package org.firstinspires.ftc.teamcode.Sensors;\n\nimport com.qualcomm.robotcore.hardware.ColorSensor;\nimport com.qualcomm.robotcore.hardware.DistanceSensor;\nimport com.qualcomm.robotcore.hardware.HardwareMap;\n\nimport org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;\n\npublic class RevColor {\n\n public ColorSensor internalColorSensor;\n public DistanceSensor internalDistanceSensor;\n // Base Color Threshold\n public final double RED_THRESHOLD_LEFT = 700, RED_THRESHOLD_RIGHT = 700;\n public final double BLUE_THRESHOLD_LEFT = 700, BLUE_THRESHOLD_RIGHT = 700;\n public final double RED_THRESHOLD = 550;\n public final double BLUE_THRESHOLD = 550;\n // Complex Color Thresholds (not red, green, or blue)\n public final double YELLOW_RED_VALUE = 0.419;\n public final double YELLOW_GREEN_VALUE = 0.3675;\n public final double YELLOW_BLUE_VALUE = 0.2116;\n public final double[] YELLOW_CONSTANTS = {YELLOW_RED_VALUE, YELLOW_GREEN_VALUE, YELLOW_BLUE_VALUE};\n public final double YELLOW_THRESHOLD = 0.12;\n\n public final double WHITE_RED_VALUE = 0.30125;\n public final double WHITE_GREEN_VALUE = 0.37125;\n public final double WHITE_BLUE_VALUE = 0.325;\n public final double[] WHITE_CONSTANTS = {WHITE_RED_VALUE, WHITE_GREEN_VALUE, WHITE_BLUE_VALUE};\n public final double WHITE_THRESHOLD = 0.01;\n public final double WHITE_TOTAL_COUNT = 800;\n \n // check for black with the alpha value\n public final double BLACK_ALPHA_VALUE = 325; //Test value\n\n public RevColor(HardwareMap hardwareMap, String name){\n this.internalColorSensor = hardwareMap.get(ColorSensor.class, name);\n this.internalDistanceSensor = hardwareMap.get(DistanceSensor.class, name);\n }\n\n public double distance(){\n return internalDistanceSensor.getDistance(DistanceUnit.CM);\n }\n \n \n // utility method for easily getting the color value. \n public int red(){ return internalColorSensor.red(); }\n public int green(){ return internalColorSensor.green(); }\n public int blue(){ return internalColorSensor.blue(); }\n \n public double total(){ return red() + green() + blue(); }\n \n // convert to array\n public double[] rgb(){\n double[] arr = new double[3];\n arr[0] = red();\n arr[1] = green();\n arr[2] = blue();\n\n return arr;\n }\n \n // Scale the rgb values (0 to 1)\n public double[] normalizedRGB(){\n double[] arr = new double[3];\n double[] originalArr = rgb();\n\n double total = 0;\n for(double i : originalArr)\n total+= i;\n\n for(int i = 0; i < 3; i++){\n arr[i] = originalArr[i] / total;\n }\n\n return arr;\n }\n\n public double arrayError(double[] arr1, double[] arr2){\n double total = 0;\n\n for(int i = 0; i < arr1.length; i++){\n total += Math.pow(arr1[i] - arr2[i], 2);\n }\n\n return Math.sqrt(total);\n }\n\n public boolean isBlack(){\n return (internalColorSensor.alpha() < BLACK_ALPHA_VALUE) ? true : false;\n }\n\n public int alphaValue(){\n return internalColorSensor.alpha();\n }\n\n public boolean isRed(){\n return internalColorSensor.red() > RED_THRESHOLD;\n }\n\n public boolean isBlue(){\n return internalColorSensor.blue() > BLUE_THRESHOLD;\n }\n\n public double yellowError(){ return arrayError(normalizedRGB(), YELLOW_CONSTANTS); }\n public double whiteError(){ return arrayError(normalizedRGB(), WHITE_CONSTANTS); }\n\n public boolean isYellow(){\n return yellowError() < YELLOW_THRESHOLD || (internalDistanceSensor.getDistance(DistanceUnit.CM)>6 && whiteError() > 0.02);\n }\n\n public boolean isWhite(){\n return whiteError() < WHITE_THRESHOLD && total() > WHITE_TOTAL_COUNT;\n }\n \n public String normalizedValues() {\n double red = internalColorSensor.red();\n double green = internalColorSensor.green();\n double blue = internalColorSensor.blue();\n\n double total = red + green + blue;\n return String.format("RGB: %.2f %.2f %.2f", red / total, green / total, blue / total);\n }\n \n // turn on the lights\n public void enableLED(boolean LEDMode){\n internalColorSensor.enableLed(LEDMode);\n }\n\n public boolean withinColorRange(){\n return isYellow() || isWhite();\n }\n}\n')),(0,t.kt)("h3",{id:"advanced-version"},"Advanced Version"),(0,t.kt)("pre",null,(0,t.kt)("code",{parentName:"pre",className:"language-java"},'public ColorSensor internalColorSensor;\npublic DistanceSensor internalDistanceSensor;\n// Base Color Threshold\npublic final double RED_THRESHOLD_LEFT = 700, RED_THRESHOLD_RIGHT = 700;\npublic final double BLUE_THRESHOLD_LEFT = 700, BLUE_THRESHOLD_RIGHT = 700;\npublic final double RED_THRESHOLD = 550;\npublic final double BLUE_THRESHOLD = 550;\n// Complex Color Thresholds (not red, green, or blue)\npublic final double YELLOW_RED_VALUE = 0.419;\npublic final double YELLOW_GREEN_VALUE = 0.3675;\npublic final double YELLOW_BLUE_VALUE = 0.2116;\npublic final double[] YELLOW_CONSTANTS = {YELLOW_RED_VALUE, YELLOW_GREEN_VALUE, YELLOW_BLUE_VALUE};\npublic final double YELLOW_THRESHOLD = 0.12;\n\npublic final double WHITE_RED_VALUE = 0.30125;\npublic final double WHITE_GREEN_VALUE = 0.37125;\npublic final double WHITE_BLUE_VALUE = 0.325;\npublic final double[] WHITE_CONSTANTS = {WHITE_RED_VALUE, WHITE_GREEN_VALUE, WHITE_BLUE_VALUE};\npublic final double WHITE_THRESHOLD = 0.01;\npublic final double WHITE_TOTAL_COUNT = 800;\n\n// check for black with the alpha value\npublic final double BLACK_ALPHA_VALUE = 325; //Test value\n\npublic RevColor(HardwareMap hardwareMap, String name){\n this.internalColorSensor = hardwareMap.get(ColorSensor.class, name);\n this.internalDistanceSensor = hardwareMap.get(DistanceSensor.class, name);\n}\n\npublic double distance(){\n return internalDistanceSensor.getDistance(DistanceUnit.CM);\n}\n\n\n// utility method for easily getting the color value. \npublic int red(){ return internalColorSensor.red(); }\npublic int green(){ return internalColorSensor.green(); }\npublic int blue(){ return internalColorSensor.blue(); }\n \npublic double total(){ return red() + green() + blue(); }\n\n// convert to array\npublic double[] rgb(){\n double[] arr = new double[3];\n arr[0] = red();\n arr[1] = green();\n arr[2] = blue();\n\n return arr;\n}\n\n// Scale the rgb values (0 to 1)\npublic double[] normalizedRGB(){\n double[] arr = new double[3];\n double[] originalArr = rgb();\n\n double total = 0;\n for(double i : originalArr)\n total+= i;\n\n for(int i = 0; i < 3; i++){\n arr[i] = originalArr[i] / total;\n }\n\n return arr;\n}\n\npublic double arrayError(double[] arr1, double[] arr2){\n double total = 0;\n\n for(int i = 0; i < arr1.length; i++){\n total += Math.pow(arr1[i] - arr2[i], 2);\n }\n\n return Math.sqrt(total);\n}\n\npublic boolean isBlack(){\n return (internalColorSensor.alpha() < BLACK_ALPHA_VALUE) ? true : false;\n}\n\npublic int alphaValue(){\n return internalColorSensor.alpha();\n}\n\npublic boolean isRed(){\n return internalColorSensor.red() > RED_THRESHOLD;\n}\n\npublic boolean isBlue(){\n return internalColorSensor.blue() > BLUE_THRESHOLD;\n}\n\npublic double yellowError(){ return arrayError(normalizedRGB(), YELLOW_CONSTANTS); }\npublic double whiteError(){ return arrayError(normalizedRGB(), WHITE_CONSTANTS); }\n\npublic boolean isYellow(){\n return yellowError() < YELLOW_THRESHOLD || (internalDistanceSensor.getDistance(DistanceUnit.CM)>6 && whiteError() > 0.02);\n}\n\npublic boolean isWhite(){\n return whiteError() < WHITE_THRESHOLD && total() > WHITE_TOTAL_COUNT;\n}\n\npublic String normalizedValues() {\n double red = internalColorSensor.red();\n double green = internalColorSensor.green();\n double blue = internalColorSensor.blue();\n\n double total = red + green + blue;\n return String.format("RGB: %.2f %.2f %.2f", red / total, green / total, blue / total);\n}\n\n// turn on the lights\npublic void enableLED(boolean LEDMode){\n internalColorSensor.enableLed(LEDMode);\n}\n\npublic boolean withinColorRange(){\n return isYellow() || isWhite();\n}\n')))}d.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.