Reply To: Reading gyro values

Home Forums Forums Technical Support for BerryIMU Reading gyro values Reply To: Reading gyro values

#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) 😀

Blip, blop, bloop…