Home › Forums › Forums › Technical Support for BerryIMU › Reading gyro values
- This topic has 8 replies, 3 voices, and was last updated 6 years, 11 months ago by luizmendesalmeida.
- AuthorPosts
- January 3, 2016 at 9:38 am #4393HesherParticipant
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!
January 9, 2016 at 12:44 pm #4400Mark WilliamsKeymasterHi, are you still having the issue?
does the IMU work with the code from our GIT repo?
https://github.com/mwilliams03/BerryIMUMark --OzzMaker.com --
January 10, 2016 at 11:21 pm #4405HesherParticipantHey 🙂
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()January 11, 2016 at 1:57 am #4407Mark WilliamsKeymasterthanks 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 --
January 11, 2016 at 2:06 am #4408HesherParticipantnot 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 notJanuary 24, 2016 at 9:59 pm #4525HesherParticipantHi again,
ive tried to read values from BMP180 – which works well with this code
https://code.google.com/p/raspberry-pi4j-samples/source/browse/AdafruitI2C/src/adafruiti2c/AdafruitBMP180.java?spec=svn74c33e4a5cc3048678061c52c6df0119dbca14a2&r=74c33e4a5cc3048678061c52c6df0119dbca14a2I´ll try accelerometer next.
January 25, 2016 at 11:47 am #4526Mark WilliamsKeymastercool, 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 --
February 2, 2016 at 8:33 am #4536HesherParticipantHi,
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/sensorNote: 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) 😀June 15, 2016 at 5:58 pm #4972luizmendesalmeidaParticipantHi @Hesher.
Did you put BerryIMU working with Pi4J?
If yes, could you share your code?
Thank you in advance. - AuthorPosts
- You must be logged in to reply to this topic.