Announcement

Collapse
No announcement yet.

AMSColorSensorImpl - how to get an object from this class and talk to an Adafruit CS?

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

  • AMSColorSensorImpl - how to get an object from this class and talk to an Adafruit CS?

    If you are using an Adafruit color sensor, there are 2 possible classes in the SDK that can be instantiated into an object. One is AdafruitI2cColorSensor. This class has a comment at the top of it that says it is deprecated and that AMSColorSensorImpl should be used instead. I can see why since the update rate for the sensor in the deprecated class is not configurable and defaults to 600mSec, which is not useable for many applications. In this post
    http://ftcforum.usfirst.org/showthre...-rate-SDK-2-35
    I explored what class actually gets instantiated. It turns out the deprecated class is instantiated in the magic associated with the phone config and the hardware map.

    I have read through the code for AMSColorSensorImpl and I'd really like to use this newer class. I know there are user contributed solutions but I'd prefer to use the class provided in the SDK. For the life of me I can't figure out how to get an object of AMSColorSensorImpl instantiated and associated with an Adafruit color sensor. Has anyone done this? Can you post code to show me how?

    Thanks

  • #2
    Try this:
    Code:
    AMSColorSensor colorSensor = hardwareMap.get(AMSColorSensorImpl.class, "amsColorSensor");

    Comment


    • #3
      Forgot to add one more thing. In your robot config on the RC phone, try configuring "amdColorSensor" as I2C device simple sync or something like that (don't have the phone in front of me). I am not 100% sure about this. If it doesn't find the device, try some other I2c categories.

      Comment


      • #4
        Sorry typo: "amdColorSensor" -> "amsColorSensor"

        Comment


        • #5
          mikets had a good suggestion. Here are snippets from the code:

          Code:
          [COLOR=#000080][B]private [/B][/COLOR]AMSColorSensorImpl [COLOR=#660e7a][B]colorSensor[/B][/COLOR];
          ...
          [COLOR=#660e7a][B]colorSensor [/B][/COLOR]= hardwareMap.get(AMSColorSensorImpl.[COLOR=#000080][B]class[/B][/COLOR], colorSensorName);
          I configured the adafruit color sensor on an I2C port as an "I2C Device" and a "I2C Device (Synchronous)" and a "Adafruit Color Sensor". In each case I got an error message "Unable to find a hardware device with name 'colorSensor' and type AMSColorSensorImpl. I really wonder if anyone is using this updated driver.

          Comment


          • #6
            What is the name in colorSensorName? Make sure it spells exactly as in the robot config including casing and also make sure there are no "spaces" in front or after the name. I did that to myself once that my fat fingers mistakenly put a space in front of the name which is hard to detect. So the code kept complaining device not found. It just happened that I transferred the config file to my laptop so I saw the space.

            Comment


            • #7
              I verified the name in colorSensorName (colorSensor). I did this by changing the config on the phone to Adafruit Color Sensor without changing the name. The code changed to
              Code:
              [COLOR=#000080][B]private [/B][/COLOR]ColorSensor [COLOR=#660e7a][B]colorSensor[/B][/COLOR];color
              Sensor = hardwareMap.[COLOR=#660E7A][B]colorSensor[/B][/COLOR].get(colorSensorName);
              Since this worked, the name has to be correct. Unfortunately this yields an instance of AdafruitI2cColorSensor.

              Comment


              • #8
                Looking at the sample OpMode for the Adafruit Gyro I notice that they use a slightly different technique.

                Code:
                [COLOR=#808080][FONT=&amp][I]// Retrieve and initialize the IMU. We expect the IMU to be attached to an I2C port[/I][/FONT][/COLOR]
                [COLOR=#808080][I]// on a Core Device Interface Module, configured to be a sensor of type "AdaFruit IMU",
                [/I][/COLOR][COLOR=#808080][I]// and named "imu".
                [/I][/COLOR][COLOR=#660E7A][FONT=&amp][B]imu [/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp]= [/FONT][/COLOR][COLOR=#660E7A][FONT=&amp][B]hardwareMap[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp].get(BNO055IMU.[/FONT][/COLOR][COLOR=#000080][FONT=&amp][B]class[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp], [/FONT][/COLOR][COLOR=#008000][FONT=&amp][B]"imu"[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp]);[/FONT][/COLOR]

                In this case BNO055IMU is the interface, not the implementation. AMSColorSensor actually is a ColorSensor so you shouldn't have to declare it as an AMSColorSensorImpl. Can you try again with

                Code:
                [COLOR=#000000][FONT=&amp]ColorSensor colorSensor;
                [/FONT][/COLOR][COLOR=#000000][FONT=&amp]colorSensor = [/FONT][/COLOR][COLOR=#660E7A][FONT=&amp][B]hardwareMap[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp].get(AMSColorSensor.[/FONT][/COLOR][COLOR=#000080][FONT=&amp][B]class[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp], colorSensorName);[/FONT][/COLOR]

                Comment


                • #9
                  Originally posted by mlwilliams View Post
                  Looking at the sample OpMode for the Adafruit Gyro I notice that they use a slightly different technique.

                  Code:
                  [COLOR=#808080][FONT=&amp][I]// Retrieve and initialize the IMU. We expect the IMU to be attached to an I2C port[/I][/FONT][/COLOR]
                  [COLOR=#808080][I]// on a Core Device Interface Module, configured to be a sensor of type "AdaFruit IMU",
                  [/I][/COLOR][COLOR=#808080][I]// and named "imu".
                  [/I][/COLOR][COLOR=#660e7a][FONT=&amp][B]imu [/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp]= [/FONT][/COLOR][COLOR=#660e7a][FONT=&amp][B]hardwareMap[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp].get(BNO055IMU.[/FONT][/COLOR][COLOR=#000080][FONT=&amp][B]class[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp], [/FONT][/COLOR][COLOR=#008000][FONT=&amp][B]"imu"[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp]);[/FONT][/COLOR]

                  In this case BNO055IMU is the interface, not the implementation. AMSColorSensor actually is a ColorSensor so you shouldn't have to declare it as an AMSColorSensorImpl. Can you try again with

                  Code:
                  [COLOR=#000000][FONT=&amp]ColorSensor colorSensor;
                  [/FONT][/COLOR][COLOR=#000000][FONT=&amp]colorSensor = [/FONT][/COLOR][COLOR=#660e7a][FONT=&amp][B]hardwareMap[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp].get(AMSColorSensor.[/FONT][/COLOR][COLOR=#000080][FONT=&amp][B]class[/B][/FONT][/COLOR][COLOR=#000000][FONT=&amp], colorSensorName);[/FONT][/COLOR]
                  That's basically what I suggested. hardwareMap.get() can take a class or an interface as parameter. AMSColorSensor is an interface, AMSColorSensorImpl is a class. So either would work. Regarding the variable type, AMSColorSensor eventually implements ColorSensor. So again, either would work except if you want to access anything specific to AMSColorSensor, then it's more appropriate to declare the variable as AMSColorSensor.

                  Comment


                  • #10
                    Another question would this work with multiplexer as code structure is different?

                    Comment


                    • #11
                      Originally posted by FTC11722 View Post
                      Another question would this work with multiplexer as code structure is different?
                      What would work with multiplexer?

                      Comment


                      • #12
                        I did as mlwilliams suggested, with the config set to Adafruit Color Sensor, Color Sensor and I2C device (Synchronous) and all produced the same exception:
                        Unable to find a hardware device with name 'colorSensor' and type AMSColorSensor.

                        Code:
                        [COLOR=#000080][B]private [/B][/COLOR]ColorSensor [COLOR=#660e7a][B]colorSensor[/B][/COLOR];
                        [COLOR=#660e7a][B]colorSensor [/B][/COLOR]= hardwareMap.get(AMSColorSensor.[COLOR=#000080][B]class[/B][/COLOR], colorSensorName);
                        At this point I am wondering if I stole the AMSColorSensor code and made it into an I2cDeviceSync if I could get it to instantiate that way. Given my skill set that is a bit of a stretch but would it be possible in theory?

                        Comment


                        • #13
                          Originally posted by TullyNYGuy View Post
                          I did as mlwilliams suggested, with the config set to Adafruit Color Sensor, Color Sensor and I2C device (Synchronous) and all produced the same exception:
                          Unable to find a hardware device with name 'colorSensor' and type AMSColorSensor.

                          Code:
                          [COLOR=#000080][B]private [/B][/COLOR]ColorSensor [COLOR=#660e7a][B]colorSensor[/B][/COLOR];
                          [COLOR=#660e7a][B]colorSensor [/B][/COLOR]= hardwareMap.get(AMSColorSensor.[COLOR=#000080][B]class[/B][/COLOR], colorSensorName);
                          At this point I am wondering if I stole the AMSColorSensor code and made it into an I2cDeviceSync if I could get it to instantiate that way. Given my skill set that is a bit of a stretch but would it be possible in theory?
                          Are you saying you are attempting to write your own AMSColorSensor driver using the I2C support in the SDK? Sure, it's possible. We did it for the Modern Robotics Color Sensor.

                          Comment


                          • #14
                            As I thought about writing a driver, the light bulb slowly got brighter :-) I have had tunnel vision. I have been so focused on getting an instance of AMSColorSensor into some form of ColorSensor that I ignored the possibility of just getting it as an instance of I2cDevice. Who cares whether the system sees it as a color sensor? I don't see why I can't treat it as a generic I2cDevice and talk to it that way. Which is where I will head tonight.

                            Comment


                            • #15
                              Originally posted by mikets View Post
                              What would work with multiplexer?
                              I am pretty sure we were using this sample code to use few color sensors with multiplexer https://github.com/Chris857/FTC-Code...s/ftc/teamcode
                              It doesn't use AdafruitI2cColorSensor , but rather I2cDevice

                              I guess better question: is it possible within this code to control refresh rate of color sampling? If not, how to update it to use AMSColorSensor class to control refresh rate of color sampling while using several Adafruit color sensors?

                              Comment

                              Working...
                              X