Announcement

Collapse
No announcement yet.

Color Sensor Programming Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Color Sensor Programming Help

    Hi, I'm really new to programming and I honestly don't know how to program the Modern Robotics color sensor. I've seen the example program on github, but I can't seem to figure out a way to put the data received from the color sensor into an if statement. My plan is to read the sensor and move a servo to a certain position depending on what color the sensor reads. Can anyone shed some light on how to write a program that does this?

    Regards,
    Hasan
    Team 10598

  • #2
    We would like for our color sensor to do exactly what you want yours to do, but we can't figure it out either. Did anyone help you? If so, could you please post the code so we would have an example to use? Thanks.

    Comment


    • #3
      You can get the RGB values from the sensor with colorSensor.red() (blue, green, or alpha). Then, these can be converted to values in the HSV color space, using Color.RGBToHSV(sensorRGB.red()*8, sensorRGB.green()*8, sensorRGB.blue()*8, hsvValues);. Here, hsvValues is an array. Different hue values (hsvValues[0] correspond to different colors. You can then check if the returned hue value fits in a range to be considered red or blue. Red is 0/360, and blue is 240.
      Lead programmer for team 6287, Vertigo

      Comment


      • #4
        You don't necessarily need to convert to HSV values to use the sensor. I find it is easier to just use RGB values and to use a simple if or while statement. To figure out what value of RGB to use, this is a good site to play with http://www.rapidtables.com/web/color/RGB_Color.htm. You can also use the sample program to see what values are being received for what you want to sense.

        For instance, if you were trying to move a servo to a certain position after seeing blue:

        if/while (your color sensor.blue() >= 40){

        //If the sensor reads a value of value of blue greater than or equal to 40

        servo.setPosition(the position you want);

        //then set the servo to a certain position

        }

        I just struggled a while to figure this out I hope this helps. I did this for an HiTechnic sensor so the values may be a little bit different for yours (I saw in the Modern Robotics sample program everything was multiplied by eight) but the structure should be the same.
        Lead programmer for Team 71 Teton County Search and Rescue Robotics

        Comment


        • #5
          The exact algorithm may depend on how you mount the color sensor and how close you are to the beacon when reading the color sensor. For our robot, we used the following code. But it may not be suitable for your robot.
          Code:
          int redValue = robot.beaconColorSensor.red();
          int greenValue = robot.beaconColorSensor.green();
          int blueValue = robot.beaconColorSensor.blue();
          boolean isRed = redValue > blueValue && redValue > greenValue;
          boolean isBlue = blueValue > redValue && blueValue > greenValue;
          //
          // Determine which button to push and do it.
          //
          if (alliance == FtcAuto.Alliance.RED_ALLIANCE && isRed ||
              alliance == FtcAuto.Alliance.BLUE_ALLIANCE && isBlue)
          {
              robot.rightButtonPusher.extend();
              rightPusherExtended = true;
          }
          else if (alliance == FtcAuto.Alliance.RED_ALLIANCE && isBlue ||
                   alliance == FtcAuto.Alliance.BLUE_ALLIANCE && isRed)
          {
              robot.leftButtonPusher.extend();
              leftPusherExtended = true;
          }

          Comment


          • #6
            Te issue with using only the RGB colors is that different shades of a color can end up triggering the incorrect if statement. For example, the RGB value 255, 102, 102 would be most likely identified closer to red than blue, but with your test value of 40, the robot would recognize blue instead. Though you could try changing the threshold, something like 255, 204, 204 would be a pale pink, a color that is still closer to red than blue. Thus, a blue threshold greater than 204 is necessary. However, 0, 0, 103 is clearly blue, but would not be recognized as such. Additionally, the color white (255, 255, 255) might be detected depending on your thresholds. Using the HSV color space allows for easy detection of colors, and detection of the amount of grey that a color may contain. Any thresholds will be significantly more accurate, and will work better with different lighting or other uncontrollable factors.
            Lead programmer for team 6287, Vertigo

            Comment


            • #7
              That is a good point Bchay. I had used my program to see a blue line on the floor so I didn't have to worry about it seeing a color like white or red. To see something like whether the button is red or blue it would be useful to ramp up the blue value to 150 or something (You could mistake this for white but you would likely not mistake it for red). Depending on what you are sensing, it may be useful to use HSV values, but for a lot of things I think it is unnecessary. If you need to see more values to make sure it is only blue, you could put more than one requirement for your if statement. For instance

              if/while (your color sensor.blue() >= 40 && your color sensor.red() <= 40 && your color sensor.green() <= 40){

              //If the sensor reads a value of value of blue greater than or equal to 40 and a value of red less than or equal to 40 and a value of green less than or equal to green

              servo.setPosition(the position you want);

              //then set the servo to a certain position

              }

              This isn't exactly elegant but it checks to make sure that it is blue and not red or white or green or some other color. It could still be variations of blueish colors. HSV values would work just as well but RGB values were just more simple and intuitive for me to use. If someone is struggling to grasp the concept of color sensing I think using RGB (because it takes an advanced art class to even figure out what hue, saturation, and value mean) would be easier.
              Lead programmer for Team 71 Teton County Search and Rescue Robotics

              Comment


              • #8
                You present a good argument, and one could certainly use RGB for color identification, but the if statement checks would have to be significantly more complex. A RGB value of 0, 0, 40 is appears black, but would be identified as blue by your system. In HSV, it becomes 240, 100%, 15.7%. The incredibly low value of this color signifies that it is a shade of gray, even though it has a hue of 240. It is entirely possible to adapt your check to incorporate additional cases, but it quickly becomes inordinately complex. As you stated, it "isn't exactly elegant." In my opinion, HSV is a simpler method for determining color. I would also have to respectfully disagree with your point of HSV being difficult to learn; I have not had any art beyond eight grade, which did not focus at all on art/color theory. However, I picked it up with relative ease, and an in depth knowledge of HSV is not required for basic color detection.
                Lead programmer for team 6287, Vertigo

                Comment

                Working...
                X