Announcement

Collapse
No announcement yet.

Using the Adafruit RGB Sensor With REV Expansion Hub

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

  • Using the Adafruit RGB Sensor With REV Expansion Hub

    Hi,
    We are trying to use an Adafruit color sensor with the REV expansion hub using Android Studio but the sample code for the Adafruit color sensor is for the Modern Robotics CDIM. How can we adapt the code to use the Adafruit RGB color sensor with the REV expansion hub? Any sample code or other resources would be appreciated.
    Emma
    Captain of Team 7593 TigerBots, NorCal Region

  • #2
    I think that sample only really references the CDIM because it also turns the CDIM blue or red LED on depending on which color it sees and because it uses a hack to turn the sensor LED on. You can safely remove the CDIM references. The color sensor itself should work the same way when you're using the REV hub.

    Comment


    • #3
      We are successfully using the Adafruit color sensor on the REV hub.

      Comment


      • #4
        Originally posted by mlwilliams View Post
        I think that sample only really references the CDIM because it also turns the CDIM blue or red LED on depending on which color it sees and because it uses a hack to turn the sensor LED on. You can safely remove the CDIM references. The color sensor itself should work the same way when you're using the REV hub.
        We tried commenting out the references to the CDIM in the example code, but the color sensor still did not work. The LED on the color sensor is on but it does not return any values when we run our program.

        This is our current code for the Adafruit RGB color sensor:

        Code:
        @TeleOp (name = "Sensor: AdafruitRGB", group = "Sensor") // Comment this out to add to the opmode list 
         public class SensorAdafruitRGB extends LinearOpMode {    ColorSensor sensorRGB;    @Override   public void runOpMode() {     // bLedOn represents the state of the LED.     boolean bLedOn = true;      // get a reference to our ColorSensor object.     sensorRGB = hardwareMap.colorSensor.get("color");     sensorRGB.enableLed(bLedOn);      // wait for the start button to be pressed.     waitForStart();      // loop and read the RGB data.     // Note we use opModeIsActive() as our loop condition because it is an interruptible method.     while (opModeIsActive())  {       // send the info back to driver station using telemetry function.       telemetry.addData("Clear", sensorRGB.alpha());       telemetry.addData("Red  ", sensorRGB.red());       telemetry.addData("Green", sensorRGB.green());       telemetry.addData("Blue ", sensorRGB.blue());        telemetry.update();     }   } }
        Originally posted by 5294-jjkd View Post
        We are successfully using the Adafruit color sensor on the REV hub.
        Can you provide a code snippet and details of how your team has the sensor wired to the REV hub?
        Emma
        Captain of Team 7593 TigerBots, NorCal Region

        Comment


        • #5
          Some code, I had to delete a lot of surrounding code, hopefully I've left all the good bits:

          Code:
          From robotinit:
          
          ...
          import com.qualcomm.robotcore.hardware.ColorSensor;
          ...
          
          public class robotconfig {
          
          ...
              static ColorSensor ada;
              static DigitalChannel adaLed;
          ...
          
          ...
              static void jewelStuffauto() {
          
                  ada = hwMap.colorSensor.get("ada");
                  adaLed = hwMap.digitalChannel.get("adaLed");
                  adaLed.setMode(DigitalChannel.Mode.OUTPUT);
                  adaLed.setState(false);
              }
          
          ...
              /***
               * code that detects the color of the right jewel
               *
               * @return 1 for red, -1 for blue, 0 if can't tell
               */
              static int detectColor() {
          
               // add 20 to blue to remove natural bias towards sensing red light then determine which value is higher and return that color
                  if (ada.red() > ada.blue() + 20) {
                      return 1;
                  } else if (ada.red() < ada.blue() + 20) {
                      return -1;
                  } else if (ada.red() > ada.blue() + 20) {
                      return 1;
                  } else if (ada.red() < ada.blue() + 20) {
                      return -1;
                  } else {
                      return 0;
                  }
              }
          ...
          
          From autonomous:
          
          ...
          import static org.firstinspires.ftc.teamcode.robotconfig.adaLed;
          import static org.firstinspires.ftc.teamcode.robotconfig.detectColor;
          ...
          
          
                      // scan and remember color
                      adaLed.setState(true);    // LED on
                      jewelColor = detectColor();
                      adaLed.setState(false);    // LED off
          Those bits need to get inserted in the proper places, hopefully it is fairly clear where they need to go. The code to address the bias in the color sensor is pretty crude, but has been sufficient so far. Basically, they tested the values as returned by the sensor from a white sheet of paper, a red jewel and a blue jewel, at various distances from the sensor and under different lighting conditions, and came up with that fairly simple adjustment for the bias in the sensor. You may need to fudge that value for your particular sample of the sensor.

          The wiring is straightforward. The Adafruit sensor is 5V/3.3V compatible, so no level shifter is needed. The power, ground and I2C clock/data pins are wired directly to the matching pins on a REV I2C port (by function, not by number). The LED pin we wired to the D7 pin on a REV digital port. Update the robot controller phone config for both ports and you should be ready to go.

          Note that we built a custom JST cable, with two JST connectors at the REV end and a DUPONT connector at the Adafruit end. Worked fine for one cable, but needed a second cable and didn't want to fiddle with crimping the JST ends again (those are a real pain), so just cut up the appropriate matching pre-fab cables and spliced everything together.

          Comment


          • #6
            We found out that our problem was a broken sensor, not the code or wiring. After swapping out the sensor, it worked! Our next step will be to put an adjustment in our code for the bias in the color sensor as suggested. Thanks for your help!
            Emma
            Captain of Team 7593 TigerBots, NorCal Region

            Comment

            Working...
            X