Announcement

Collapse
No announcement yet.

i2c programs from last year don't work

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

  • i2c programs from last year don't work

    The Rev IMU inaccuracy thread made me wonder if i2c device code from last year was more accurate.
    It requires the configuration file to be set to i2c device. That option is no longer available, so I haven't been able to get this to work. The closest is i2csynch, which doesn't work. I have also tried using an Adafruit IMU (external to REV). (I checked, it still works with 2.62 and a MR DIM.)


    Is this code obsolete?


    Code:
     
     package org.firstinspires.ftc.teamcode;  import com.qualcomm.robotcore.eventloop.opmode.Autonomous; import com.qualcomm.robotcore.eventloop.opmode.Disabled; import com.qualcomm.robotcore.eventloop.opmode.OpMode; import com.qualcomm.robotcore.hardware.I2cAddr; import com.qualcomm.robotcore.hardware.I2cDevice; import com.qualcomm.robotcore.hardware.I2cDeviceSynch; import com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl;  // Config file // I2C Device // imu  /**  * This is an example OpMode that shows how to read the Modern Robotics gyro sensor  * using I2cDeviceSynch calls in the new FTC app beta.  */ @Autonomous(name = "Sensor: AdafruitIMU", group = "Sensor") //@Disabled public class  ExampleNewI2cADAIMUOpMode extends OpMode {     int heading = 0; //Variable for Heading data     int intZValue = 0; //Variable for the integrated Z value      byte[] gyro1Cache; //The read will return an array of bytes. They are stored in this variable      public static final byte GYRO1ADDRESS = 0x50; //Default I2C address for MR gyro     public static final int GYRO1_REG_START = 0x04; //Register to start reading     public static final int GYRO1_READ_LENGTH = 14; //Number of byte to read     public static final int GYRO_COMMAND_REGISTER = 0x03; //Register to issue commands to the gyro     public static final byte GYRO_CALIBRATE_COMMAND = 0x4E; //Command to calibrate the gyro I2cAddr gyroADAAddr;      public static final byte BNO055_PAGE_ID_ADDR = 0x07;     public static final byte BNO055_SYS_TRIGGER_ADDR = 0x3F;     public static final byte OPERATION_MODE_IMU  = 0X08;     public static final byte  BNO055_OPR_MODE_ADDR = 0x3D;     public static final byte EUL_DATA_X_LSB = 0x1A;     public static final byte QUA_DATA_X_LSB = 0x22;       public I2cDevice gyroADA;     public I2cDeviceSynch gyro1Reader;      private void snooze(long milliSecs){//Simple utility for sleeping (thereby releasing the CPU to         // threads other than this one)         try {             Thread.sleep(milliSecs);         } catch (InterruptedException e){}     }      public ExampleNewI2cADAIMUOpMode() {     }     @Override     public void init() {         gyroADAAddr = I2cAddr.create8bit(GYRO1ADDRESS);         gyroADA = hardwareMap.i2cDevice.get("imu");         gyro1Reader = new I2cDeviceSynchImpl(gyroADA, gyroADAAddr, false);         gyro1Reader.engage();         //Sets the PAGE_ID bit for page 0 (Table 4-2)         gyro1Reader.write8(BNO055_PAGE_ID_ADDR, 0x00);         //The "E" sets the RST_SYS and RST_INT bits, and sets the CLK_SEL bit         //, to select the external IMU clock mounted on the Adafruit board (Table 4-2, and p.70). In         // the lower 4 bits, a "1" sets the commanded Self Test bit, which causes self-test to run (p. 46)         gyro1Reader.write8(BNO055_SYS_TRIGGER_ADDR, 0xE1);          snooze(1000);          gyro1Reader.write8( BNO055_OPR_MODE_ADDR, OPERATION_MODE_IMU);        }      @Override     public void init_loop() {         gyro1Cache = gyro1Reader.read(BNO055_OPR_MODE_ADDR, 1);         telemetry.addData("1", "Mode " + gyro1Cache[0]);      }       @Override     public void loop() {          gyro1Cache = gyro1Reader.read(EUL_DATA_X_LSB, 2);  //One byte only goes to 256 values. Two bytes must be combined to get 360 degrees         heading = 0X000000FF & (int)gyro1Cache[1];         heading = (heading << 8 | (0X000000FF & (int)gyro1Cache[0]));         heading = heading /16;          //intZValue = 0X000000FF & (int)gyro1Cache[3];         //intZValue = (intZValue << 8 | (0X000000FF & (int)gyro1Cache[2]));  // send the info back to driver station using telemetry function.          telemetry.addData("Gyro Heading LSB", gyro1Cache[0]);         telemetry.addData("Gyro Heading MSB", gyro1Cache[1]);         telemetry.addData("Gyro HeadingTot", heading );         //telemetry.addData("Gyro Int Z1", gyro1Cache[2]);         //telemetry.addData("Gyro Int Z2", gyro1Cache[3]);         //telemetry.addData("Gyro Int ZTot", (short) intZValue);      }       @Override     public void stop() {      } /* I2C Registers Address Function 0x00 Sensor Firmware Revision 0x01 Manufacturer Code 0x02 Sensor ID Code 0x03 Command 0x04/0x05 Heading Data (lsb:msb) 0x06/0x07 Integrated Z Value (lsb:msb) 0x08/0x09 Raw X Value (lsb:msb) 0x0A/0x0B Raw Y Value (lsb:msb) 0x0C/0x0D Raw Z Value (lsb:msb) 0x0E/0x0F Z Axis Offset (lsb:msb) 0x10/0x11 Z Axis Scaling Coefficient (lsb:msb)  Commands Command    Operation 0x00   Normal measurement mode 0x4E   Null gyro offset and reset Z axis integrator 0x52   Reset Z axis integrator 0x57   Write EEPROM data */ }

  • #2
    Can someone post a sticky how we can post stuff that's pasted in without messing it up?

    Comment


    • #3
      OK I'll see if the Notepad trick works for posting code. (This time my example is for the color sensor instead of the IMU).

      I like the method below for accessing some I2C devices because you can do things like read the color number off of the Modern Robotics Color Sensor, or change the resolution/integration on the Adafruit color sensor so it can update at a reasonable speed, or select parameters from the well documented Adafruit IMU datasheet.

      The code below works with 3.5 on the MR Interface module, but I can't get it to work on the REV Hub. It requires that you choose I2C Device as the type in the configuration file. This appears on the MR Interface Module, but not on the REV. I've tried I2CSynch Device and Modern Robotics Color Sensor, but the alternatives don't work.

      Is there a way for this to work on REV?
      Are there drawbacks, like slows everything down?

      Code:
      package org.firstinspires.ftc.teamcode;
      
      import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
      import com.qualcomm.robotcore.eventloop.opmode.OpMode;
      import com.qualcomm.robotcore.hardware.I2cAddr;
      import com.qualcomm.robotcore.hardware.I2cDevice;
      import com.qualcomm.robotcore.hardware.I2cDeviceSynch;
      import com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl;
      @Autonomous (name = "MR synchI2c")
      public class ExampleNewI2cColorOpMode extends OpMode {
          int color = 0; //Variable for Heading data
      
      
          byte[] color1Cache; //The read will return an array of bytes. They are stored in this variable
      
          public static final byte COLOR1ADDRESS = 0x3C; //Default I2C address for MR gyro
          public static final int COLOR1_REG_START = 0x04; //Register to start reading
          public static final int COLOR1_READ_LENGTH = 5; //Number of byte to read
          public static final int COLOR_COMMAND_REGISTER = 0x03; //Register to issue commands to the gyro
          public static final byte COLOR_ACTIVE_COMMAND = 0x00;
          public static final byte COLOR_PASSIVE_COMMAND =0x01;
      //public static final byte GYRO_CALIBRATE_COMMAND = 0x4E; //Command to calibrate the gyro
      
          public I2cDevice color1;
          public I2cDeviceSynch color1Reader;
          I2cAddr Color1AddrAddress;
      
          public ExampleNewI2cColorOpMode() {
          }
      
          @Override
          public void init() {
              Color1AddrAddress = I2cAddr.create8bit(COLOR1ADDRESS);
              color1 = hardwareMap.i2cDevice.get("color1MR");
              color1Reader = new I2cDeviceSynchImpl(color1, Color1AddrAddress, false);
              color1Reader.engage();
      
          }
      
          @Override
          public void init_loop() {
      //Calibrate the gyro if it is not reading 0
              color1Reader.write8(COLOR_COMMAND_REGISTER, COLOR_PASSIVE_COMMAND);
      
      /*
      if (gyro1Reader.read8(4) != 0) {
      gyro1Reader.write8(GYRO_COMMAND_REGISTER, GYRO_CALIBRATE_COMMAND);
      }
      */
          }
      
      
          @Override
          public void loop() {
              color1Reader.write8(COLOR_COMMAND_REGISTER, COLOR_ACTIVE_COMMAND);
              color1Cache = color1Reader.read(COLOR1_REG_START, COLOR1_READ_LENGTH);
      
              color = (int)color1Cache[0];
      //heading = (heading << 8 | (0X000000FF & (int)gyro1Cache[0]));
      
      // send the info back to driver station using telemetry function.
      
              telemetry.addData("color number byte", color1Cache[0]);
              telemetry.addData("red byte", color1Cache[1]);
              telemetry.addData("color number int", color );
              telemetry.addData("green", color1Cache[2]);
              telemetry.addData("blue", color1Cache[3]);
      
      
          }
      
      
          @Override
          public void stop() {
      
          }
      /*
      I2C Registers
      Address Function
      0x00 Sensor Firmware Revision
      0x01 Manufacturer Code
      0x02 Sensor ID Code
      0x03 Command
      0x04/0x05 Heading Data (lsb:msb)
      0x06/0x07 Integrated Z Value (lsb:msb)
      0x08/0x09 Raw X Value (lsb:msb)
      0x0A/0x0B Raw Y Value (lsb:msb)
      0x0C/0x0D Raw Z Value (lsb:msb)
      0x0E/0x0F Z Axis Offset (lsb:msb)
      0x10/0x11 Z Axis Scaling Coefficient (lsb:msb)
      
      Commands
      Command    Operation
      0x00    Normal measurement mode
      0x4E    Null gyro offset and reset Z axis integrator
      0x52    Reset Z axis integrator
      0x57    Write EEPROM data
      */
      }

      Comment


      • #4
        Found the solution. While I was looking for something else I came across this link -

        Tutorial: Writing an I2C Driver
        https://github.com/ftctechnh/ftc_app...-an-I2C-Driver

        I have as a trial read color numbers from the MR color sensor on the REV hub, and have been able to select the integrations/resolution on the AdaFruit color sensor on the REV hub.

        Comment

        Working...
        X