Announcement

Collapse
No announcement yet.

Color Sensor Issues

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

  • Color Sensor Issues

    I'm not sure what's wrong with our color sensor. It isn't able to detect the colors but is able to change the background of the RC phone to the color that it sees (defaults to black). In addition, the LED turns on but doesn't turn off with the code. What's the problem?

    Code:
    package ftc8564opMode;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.View;
    
    import com.qualcomm.ftcrobotcontroller.R;
    import com.qualcomm.robotcore.eventloop.opmode.Disabled;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
    import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
    import com.qualcomm.robotcore.hardware.ColorSensor;
    
    /*
     *
     * This is an example LinearOpMode that shows how to use
     * a Modern Robotics Color Sensor.
     *
     * The op mode assumes that the color sensor
     * is configured with a name of "sensor_color".
     *
     * You can use the X button on gamepad1 to toggle the LED on and off.
     *
     * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
     * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
     */
    @TeleOp(name = "Sensor: MR Color", group = "Sensor")
    public class MRTest extends LinearOpMode {
    
      ColorSensor colorSensor;    // Hardware Device Object
    
    
      @Override
      public void runOpMode() {
    
        // hsvValues is an array that will hold the hue, saturation, and value information.
        float hsvValues[] = {0F,0F,0F};
    
        // values is a reference to the hsvValues array.
        final float values[] = hsvValues;
    
        // get a reference to the RelativeLayout so we can change the background
        // color of the Robot Controller app to match the hue detected by the RGB sensor.
        final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);
    
        // bPrevState and bCurrState represent the previous and current state of the button.
        boolean bPrevState = false;
        boolean bCurrState = false;
    
        // bLedOn represents the state of the LED.
        boolean bLedOn = true;
    
        // get a reference to our ColorSensor object.
        colorSensor = hardwareMap.colorSensor.get("colorSensor");
    
        // Set the LED in the beginning
        colorSensor.enableLed(bLedOn);
    
        // wait for the start button to be pressed.
        waitForStart();
    
        // while the op mode is active, loop and read the RGB data.
        // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
        while (opModeIsActive()) {
    
          // check the status of the x button on either gamepad.
          bCurrState = gamepad1.x;
    
          // check for button state transitions.
          if ((bCurrState == true) && (bCurrState != bPrevState))  {
    
            // button is transitioning to a pressed state. So Toggle LED
            bLedOn = !bLedOn;
            colorSensor.enableLed(bLedOn);
          }
    
          // update previous state variable.
          bPrevState = bCurrState;
    
          // convert the RGB values to HSV values.
          Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);
    
          // send the info back to driver station using telemetry function.
          telemetry.addData("LED", bLedOn ? "On" : "Off");
          telemetry.addData("Clear", colorSensor.alpha());
          telemetry.addData("Red  ", colorSensor.red());
          telemetry.addData("Green", colorSensor.green());
          telemetry.addData("Blue ", colorSensor.blue());
          telemetry.addData("Hue", hsvValues[0]);
    
          // change the background color to match the color detected by the RGB sensor.
          // pass a reference to the hue, saturation, and value array as an argument
          // to the HSVToColor method.
          relativeLayout.post(new Runnable() {
            public void run() {
              relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
            }
          });
    
          telemetry.update();
        }
      }
    }

  • #2
    Team 8564,

    I'm not particularly experienced with the Modern Robotics Color Sensor, but I recently worked with an Adafruit sensor.

    This code is default stuff that's provided as an example, so I wouldn't expect a problem here. At this point, the best thing to do would look for problems hardware-wise. Check to see if everything is nice and snug, and that your color sensor connection into the CDIM is not backwards (it happens to the best of us ) and if all that fails, try stuff like restarting phones, etc.

    With an example program like this, there's little need to try and simplify code to squash room for error. If there was, though, a good program to test this out would be something along these lines:

    Code:
    ColorSensor cs1 = hardwaremap.colorSensor.get("cs1");
    waitForFullHardwareCycle();
    telemety.addData(cs1.red());
    That would be run as a Linear OpMode. Keep in mind that I didn't test that code, and I don't think I struck the right function for the Hardware Cycle.

    Hope this helps!
    The Hermanoid
    PIC (Programmer in Chief) of the Robo Bears, Team 5611

    Comment


    • #3
      Originally posted by FTC8564 View Post
      It isn't able to detect the colors but is able to change the background of the RC phone to the color that it sees (defaults to black).
      Why do you think it isn't able to detect the colors? If they background is changing, then seems like it must be detecting the colors. Are you getting valid telemetry data as you move the sensor around?

      Comment


      • #4
        I've done more testing but I'm thinking that it's not code that's causing our issue but rather hardware. We've used two extension cables and it seems that one of them causes the LED to flicker. Even so, the sensor continues to not be able to detect any other colors when the LED is on (I didn't turn the LED off through joystick).

        Comment


        • #5
          Are you using the color sensor to detect beacon color or for detecting/following the white line on the floor? If you are detecting beacon color, you need to turn the LED off. But for detecting line, you need to turn the LED on. With the LED off, the red/green/blue value may not be very big especially when the environment is bright. We have seen a r/g/b value of (1,0,0) for the red beacon and (0,0,1) for the blue. But if we put a light shield around the sensor, then we are able to read (4,0,0) for red and (0,0,6) for blue.

          Comment


          • #6
            I'm using the color sensor for beacon detection and I'm just running the MR Color Sensor test provided in the FTC SDK to see if it works. I'm not exactly sure how to fix it if it's a hardware problem. I've noticed that the sensor LED flickers while on when I'm fiddling with the wires so I think my problem lies there.

            Comment


            • #7
              Assuming there is a break in the wire, wiggle every inch of the wire independently and see where the break is. If the break is close to the connector then cut off the breaking section of the wire and re-crimp a new heading pin connector to it. That will save the expensive color sensor. If the break is too close to the sensor and not enough left to crimp a new connector, then it may be tough. You may have to get a new color sensor. Can you borrow a color sensor from a team near you to rule out whether it is a code problem.

              Comment


              • #8
                Sometimes, the RC isn't able to detect the DIM and that message comes and goes repeatedly. Is this a possible issue? The "break" happens between two sensor extension cables too.

                Comment


                • #9
                  After more testing, the LED doesn't even come on at all. When we had two sensor extension cables, the LED was flickering when wiggling the connection between the two extension cables. We removed one of the extension cables and it didn't even light up. There have been no disconnections with the DIM. Is our only option to buy a new color sensor at this point?

                  Comment


                  • #10
                    I'm thinking about using the phone camera to track the color of the beacon since I might not get the color sensor by our first meet. Has anyone had success with this so far with Vuforia? I've seen some people use OpenCV but it looks complicated.

                    Comment


                    • #11
                      Our color sensor also malfunctioned on us right before our competition. Fortunately, we have another color sensor so replacing it and we were ready to go. But the interesting thing was how it malfunctioned. We were "improving" the color sensor mounting. We sandwiched the sensor between a piece of plastic and aluminum. I think somebody might have tightened the screws too much. At first, we didn't even know it malfunctioned. What we saw was that when we ran our opmode, our option menu did not come up and it was hanging there. Upon careful examination of the code, we concluded that the reason why the menu did not come up was because the gyro calibration never came back. We called the gyro calibration before putting up the menu. Commenting out the gyro calibration line made the menu came up right away. So it is confirmed that gyro calibration never came back. But we did not touch the gyro, we just messed with the color sensor. A little thinking about it made us realize that since both gyro and the color sensor are I2C devices. If for some reason the color sensor malfunction caused the I2C bus problem, all I2C devices will malfunction. To prove this theory, we unplugged the color sensor, power cycle the power distribution module and voila the gyro calibration is working again. So we just replace the color sensor and everything was good. Note that when you plug and unplug any I2C devices, you need to power cycle the power distribution module. It seems I2C devices are not very plug-and-play. If you hot plug an I2C device, it may not get initialize correctly.
                      Why am I telling you this? You have mentioned on another thread that your dashboard was not coming up. So I am wondering if your gyro calibration is also hanging. It just sounds familiar to our finding. It may not be the same thing, but give it a try and see if unplugging the color sensor and power cycling the power module will fix your dashboard problem.

                      Comment


                      • #12
                        By power cycle, you mean just turn off and on? So if I'm using the sample MR Color Sensor in the FTC SDK, I'd just run that program again?

                        Comment


                        • #13
                          So I think we did end up having the same issue as you did. It worked again but this time we directly connected the MR Color Sensor to the DIM. We'll run further tests with extension cables.

                          Comment


                          • #14
                            Originally posted by mikets View Post
                            We sandwiched the sensor between a piece of plastic and aluminum.
                            Sorry this is off topic for this thread, but what was the purpose of this?

                            Comment


                            • #15
                              Originally posted by FTC8767 View Post
                              Sorry this is off topic for this thread, but what was the purpose of this?
                              We were trying to improve the color value reading. We found that when the environment is too bright, it interferes with the color sensor. So we made a light shield out of a piece of plastic. It sits in front of the sensor. So we figure we can just use longer screws through the light shield and the sensor mounting holes then through the back aluminum mounting plate. Essentially sandwich the sensor in between. But somebody probably tightened the screws too much. We don't know what really caused the malfunction. So it's just a guess.

                              Comment

                              Working...
                              X