Results 1 to 6 of 6

Thread: Using I2C on the roborio for sensors?

  1. #1
    Join Date
    Oct 2011
    Posts
    4

    Default Using I2C on the roborio for sensors?

    I'm having some issues with getting I2C to work on the Java library. I'm using a triple axis magnetometer -> https://www.sparkfun.com/products/10530

    This is the code i have -> http://pastebin.com/Ad9pbuf0

    I used the same hex commands from the sparkfun tutorial -> https://www.sparkfun.com/tutorials/301

    0x02 Select mode register
    0x00 Continuous measurement mode
    0x03 Select register 3, X MSB register (Most Significant Bit)

    Using the code above i get jumbled numbers back, and it does not update continuously.

    Thanks,

    Matthew Smith

  2. #2
    Join Date
    Oct 2011
    Posts
    550

    Default Re: Using I2C on the roborio for sensors?

    The write method of the I2C class takes 2 parameters, a register address and a value. Your code currently has this:
    I2CBus.write(0x02, 1); //select mode register
    I2CBus.write(0x00, 1); //continuous measurement mode

    The Arduino equivalent of that code is:
    Wire.beginTransmission(address); //open communication with HMC5883
    Wire.send(0x02); //select mode register
    Wire.send(1);
    Wire.send(0x00); //continuous measurement mode
    Wire.send(1);
    Wire.endTransmission();

    What you want is this:
    I2CBus.write(0x02, 0x00);

    Similarly the read method takes the register address as a parameter because it is sending it to the device. You should remove this line:
    I2CBus.write(0x03, 1); //select register 3, X MSB register

    I don't have a device to test the revised code against, but I believe these revisions should make your code match the behavior of the arduino code in the tutorial in terms of what's being written to the device.

    The last question then becomes how to properly re-assemble the data on the Java side. I would recommend taking the approach used in the ADXL345_SPI class rather than the one in the ADXL345_I2C class. This approach uses the java.nio.ByteOrder and java.nio.ByteBuffer classes to pull the data out of the byte array. Here's the relevant code

    Code:
    ByteBuffer rawData = ByteBuffer.wrap(dataBuffer, 1, 6);
    //Sensor is little endian... swap bytes
    rawData.order(ByteOrder.LITTLE_ENDIAN);
    		
    data.XAxis = rawData.getShort() * kGsPerLSB;
    data.YAxis = rawData.getShort() * kGsPerLSB;
    data.ZAxis = rawData.getShort() * kGsPerLSB;
    In your case, you shouldn't need to get rid of the first byte and the datasheet implies it is Big Endian (MSB in lowest data register) so something like this:
    Code:
    ByteBuffer rawData = ByteBuffer.wrap(compBuffer);
    rawData.order(ByteOrder.BIG_ENDIAN);
    cX = rawData.getShort();
    cY = rawData.getShort();
    cZ = rawData.getShort();
    Again, I don't have a device to test against so you'll have to let me know how you fair.
    FIRST
    FRC Robotics Engineer

  3. #3
    Join Date
    Oct 2011
    Posts
    4

    Default Re: Using I2C on the roborio for sensors?

    Okay, ill try this and post the results

    Thanks,

    Matthew Smith

  4. #4
    Join Date
    Oct 2011
    Posts
    4

    Default Re: Using I2C on the roborio for sensors?

    here is my updated code (I know its messy just trying to get it to work)
    Code:
    package org.usfirst.frc.team228.robot;
    
    
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    
    import edu.wpi.first.wpilibj.I2C;
    import edu.wpi.first.wpilibj.SampleRobot;
    import edu.wpi.first.wpilibj.RobotDrive;
    import edu.wpi.first.wpilibj.Joystick;
    import edu.wpi.first.wpilibj.Timer;
    import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
    
    public class Robot extends SampleRobot {
       // RobotDrive Drive;
       // Joystick stick;
        I2C I2CBus;
        
        short cX = 0, cY = 0, cZ = 0;
    
        byte[] dataBuffer = new byte[6];
        ByteBuffer compBuffer = ByteBuffer.wrap(dataBuffer);
        
        
     //   FieldCentricController FCC();
        
        
        public Robot() {
        	
            I2CBus = new I2C(I2C.Port.kOnboard, 0x1E);
           // Drive = new RobotDrive(0, 1);
           // Drive.setExpiration(0.1);
           // stick = new Joystick(0);
           
           
           
       
           
        }
    
        /**
         * Drive left & right motors for 2 seconds then stop
         */
        public void autonomous() {
           
        }
    
        /**
         * Runs the motors with arcade steering.
         */
        public void operatorControl() {
          //  Drive.setSafetyEnabled(true);
        		
        		
        		I2CBus.write(0x02, 0x00);
        	   
        		
        	
        	
            while (isOperatorControl() && isEnabled()) {
            	
            	
        	    
                
            	I2CBus.read(0x03, 6, dataBuffer);
          	
            	compBuffer.order(ByteOrder.BIG_ENDIAN);
            	
            	cX = compBuffer.getShort();
            	cY = compBuffer.getShort();
            	cZ = compBuffer.getShort();
       
            
          
          		
            	
            	SmartDashboard.putNumber("CompX", cX);
            	SmartDashboard.putNumber("CompY", cY);
            	SmartDashboard.putNumber("CompZ", cZ);
            	
           //    Drive.mecanumDrive_Cartesian(stick.getX(), stick.getY(), stick.getTwist(), 0.0);
               
               
               Timer.delay(0.065);		// wait for a motor update time
            }
        }
    
        /**
         * Runs during test mode
         */
        public void test() {
        }
    }

    Getting this error
    Code:
    ERROR Unhandled exception: java.nio.BufferUnderflowException at [java.nio.Buffer.nextGetIndex(Buffer.java:506), java.nio.HeapByteBuffer.getShort(HeapByteBuffer.java:310), org.usfirst.frc.team228.robot.Robot.operatorControl(Robot.java:70), edu.wpi.first.wpilibj.SampleRobot.startCompetition(SampleRobot.java:148), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:234)]

  5. #5
    Join Date
    Oct 2011
    Posts
    550

    Default Re: Using I2C on the roborio for sensors?

    Move this line:
    ByteBuffer compBuffer = ByteBuffer.wrap(dataBuffer);

    To right below this one:
    I2CBus.read(0x03, 6, dataBuffer);
    FIRST
    FRC Robotics Engineer

  6. #6
    Join Date
    Oct 2011
    Posts
    4

    Default Re: Using I2C on the roborio for sensors?

    Oh, I didn't realize that its supposed to be used like that. Thanks for the help I appreciate it. Ill try this out tomorrow.

    Thanks,

    Matthew

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. RoboRIO corn dogs?
    By Khaos in forum Operator Console
    Replies: 1
    Last Post: 01-26-2015, 12:25 PM
  2. RoboRio Port Problems
    By Odekker in forum Java
    Replies: 5
    Last Post: 01-15-2015, 11:50 AM
  3. How to image roboRIO
    By rubix in forum Robot
    Replies: 1
    Last Post: 01-09-2015, 09:39 AM
  4. Cannot connect to roboRIO
    By Team956 in forum Java
    Replies: 2
    Last Post: 01-06-2015, 02:51 PM
  5. roboRIO firmware
    By jross in forum Robot
    Replies: 1
    Last Post: 01-06-2015, 02:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •