Reading gyro values

Home Forums Forums Technical Support for BerryIMU Reading gyro values

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #4393
    Hesher
    Participant

    Hello guys,

    i am working with Pi4J and trying to get some values. Well – so far i get some value and also a reaction if i move the gyro around, but, the value ist the same for all axes x,y,z.
    Whats wrong – this output(see code) makes no sense?

    //LSM9DS0 Gyro
    private static final int GYR_ADDRESS = 0x6A;

    //LSM9DS0 Gyro Registers
    private static final int CTRL_REG1_G = 0x20;
    private static final int CTRL_REG4_G = 0x23;
    private static final int OUT_X_L_G = 0x28;
    private static final int OUT_X_H_G = 0x29;

    private static byte[] bytes = new byte[6];

    bus = I2CFactory.getInstance(I2CBus.BUS_1);
    device = bus.getDevice(GYR_ADDRESS);
    Thread.sleep(250); //Small delay before starting
    device.write(CTRL_REG1_G, (byte) 0b00001111); // Normal power mode, all axes enabled
    device.write(CTRL_REG4_G, (byte) 0b00110000); // Continuos update, 2000 dps full scale
    Thread.sleep(250); //Small delay before starting
    while (true) {
    int xlg = device.read(OUT_X_L_G, bytes, 0, 6);
    if(xlg < 6) {
    System.out.println(“Error reading data, < ” + bytes.length + ” bytes”);
    }
    System.out.println(“X:” + bytes[0] +”~”+ bytes[1] + ” ## Y:” + bytes[2] +”~”+ bytes[3] + ” ## Z:” + bytes[4] +”~”+ bytes[5]);
    //output e.g: X:68~68 ## Y:68~68 ## Z:68~68
    try {
    Thread.sleep(20);
    } catch (InterruptedException ex) {
    Logger.getLogger(GyroSensor.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    Thanks a lot for your help!

    #4400
    Mark Williams
    Keymaster

    Hi, are you still having the issue?

    does the IMU work with the code from our GIT repo?
    https://github.com/mwilliams03/BerryIMU

    Mark --OzzMaker.com --

    #4405
    Hesher
    Participant

    Hey 🙂

    thank you for reply, yes i still have the issue.

    The gyro works with C and Phyton code.

    PS: i guess i´ve found a bug in python-LSM9DS0-gryo-accel-compass 😉

    Line 154 – 156
    GYRx = readGYRx()
    GYRy = readGYRx()
    GYRz = readGYRx()

    #4407
    Mark Williams
    Keymaster

    thanks for pointing the typo out.

    I am no Java expert… but your code looks correct to me.

    Have you tried reading the values from the compass or accelerometer?

    Mark --OzzMaker.com --

    #4408
    Hesher
    Participant

    not yet, but i will, just need to fix some other issues with my pi 🙂
    i´ll give you an update in some days – if this works / or not

    #4525
    Hesher
    Participant
    #4526
    Mark Williams
    Keymaster

    cool, once you have it up and running, would you be open to sharing the code with us and I can add it to our GIT repo? under JAVA

    Mark --OzzMaker.com --

    #4536
    Hesher
    Participant

    Hi,

    so here are my findings:

    The best lib for the Gyro (L3GD20) i´ve found is:
    https://github.com/OlivierLD/raspberry-pi4j-samples/tree/75f8ec4583477a5ee1cf8707e64d9df75976986c/I2C.SPI/src/i2c/sensor

    Note: You have to change the ADDRESS in:
    L3GD20.java
    Line 17: public final static int L3GD20ADDRESS = 0x6b;
    to public final static int L3GD20ADDRESS = 0x6a;

    And thats the code which works for me – but bugy – if i move the gyro around, it looses accuracy over time

    
    L3GD20 sensor = new L3GD20();
    sensor.setPowerMode(L3GD20Dictionaries.NORMAL);
    sensor.setFullScaleValue(L3GD20Dictionaries._250_DPS);
    sensor.setAxisXEnabled(true);
    sensor.setAxisYEnabled(true);
    sensor.setAxisZEnabled(true);
    sensor.init();
    sensor.calibrate();
    Thread t = new Thread(){
    	
    	public void run()
    	{
    		while (true)
    		{      
    			try {
    				data = sensor.getCalOutValue();
    				//data = sensor.getRawOutValues();
    			} catch (Exception ex) {
    				Logger.getLogger(Raspi.class.getName()).log(Level.SEVERE, null, ex);
    			}
    			
    			now = System.nanoTime();
    			timeDelta = (now - lastRead)*0.000000001;
    			lastRead = now;
    			
    			xAngle += (data[0]*timeDelta);
    			yAngle += (data[1]*timeDelta);
    			zAngle += (data[2]*timeDelta);
    			
    			
    			try { Thread.sleep(1); } catch (InterruptedException ex) {}
    		}
    	}
    };
    t.start();
    Thread t2 = new Thread(){
    	
    	public void run()
    	{
    		while (true)
    		{      
    			try {
    				System.out.println("X:" + Math.round(xAngle) + " ## Y:" + Math.round(yAngle) + " ## Z:" + Math.round(zAngle));
    				//System.out.println(timeDelta);
    			} catch (Exception ex) {
    				Logger.getLogger(Raspi.class.getName()).log(Level.SEVERE, null, ex);
    			}
    			try { Thread.sleep(500); } catch (InterruptedException ex) {}
    		}
    	}
    };
    t2.start();
    

    PS:
    Btw: the error in my code above (in my question) is: it reads X-axis only (not x,y,z) 😀

    #4972
    luizmendesalmeida
    Participant

    Hi @Hesher.

    Did you put BerryIMU working with Pi4J?
    If yes, could you share your code?
    Thank you in advance.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Blip, blop, bloop…