Hi, I am using pi4j to read minimu9 values. However, I don’t understand why the raw values I am getting with my program is so different from using “minimu9-ahrs --mode raw.” Below is my code using java along with outputs from the program and and minimu9-ahrs.
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;
import com.pi4j.platform.PlatformAlreadyAssignedException;
import com.pi4j.util.Console;
public class pi4j12test {
/** LSM6DS33 (Gyro and Accelerometer Slave Address 1101011b) */
public static final char GYRO_ADDR = 0x6b;
/** LIS3MDL (Magnetometer Slave Address 0011110b) */
public static final char MAGNETOMETER_ADDR = 0x1e;
public static final char CTRL3_C = 0x12;
/** CTRL1_XL Linear acceleration sensor control register */
public static final char CTRL1_XL = 0x10;
/** CTRL2_G Gyro sensor control register activate from power down*/
public static final char CTRL2_G = 0x11;
/** Registers to read Gyro */
public static final char OUTX_L_G = 0x22;
/** Registers to read Accelerometer */
public static final char OUTX_L_XL = 0x28;
private static void readACC(I2CDevice device) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException{
byte data[] = new byte[6];
device.read(OUTX_L_XL, data, 0, 6);
// Format the HIGH/LOW bytes into their respective axis
int axis[] = new int[3];
axis[0] = (int)(data[0] | data[1] << 8); // X
axis[1] = (int)(data[2] | data[3] << 8); // Y
axis[2] = (int)(data[4] | data[5] << 8); // Z
System.out.print("readAccel::axis: " + Arrays.toString(axis) + " ");
}
private static void readGyro(I2CDevice device) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException{
// Read from raw HIGH/LOW bytes gyro registers
byte data[] = new byte[6];
device.read(OUTX_L_G, data, 0, 6);
// Format the HIGH/LOW bytes into their respective axis
int axis[] = new int[3];
axis[0] = (int)(data[0] | data[1] << 8); // X
axis[1] = (int)(data[2] | data[3] << 8); // Y
axis[2] = (int)(data[4] | data[5] << 8); // Z
System.out.println("readGyro::axis: " + Arrays.toString(axis));
}
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException{
final Console console = new Console();
console.title("<--Console Output-->");
console.promptForExit();
try{
int [] ids = I2CFactory.getBusIds();
// console.println("Found following I2C busses: " + Arrays.toString(ids));
} catch (IOException exception) {
console.println("I/O error");
}
I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_1);
I2CDevice device = i2c.getDevice(GYRO_ADDR);
device.write(CTRL1_XL,(byte)0x50);
device.write(CTRL2_G,(byte)0x58);
device.write(CTRL3_C,(byte)0x04);
while(console.isRunning()){
long lStartTime = System.nanoTime();
readACC(device);
readGyro(device);
long lEndTime = System.nanoTime();
while((lEndTime - lStartTime)/1000000 < 2000){
Thread.sleep(100);
lEndTime = System.nanoTime();
}
}
}
}
PRESS CTRL-C TO EXIT |
---|
readAccel::axis: [-672, -156, -15043] readGyro::axis: [-117, -127, 11385]
readAccel::axis: [-86, -65, -16091] readGyro::axis: [-120, -161, -123]
readAccel::axis: [-72, -68, -16106] readGyro::axis: [-122, -160, -123]
readAccel::axis: [-63, -65, -16119] readGyro::axis: [-121, -160, -121]
readAccel::axis: [-79, -76, -16117] readGyro::axis: [-123, -159, -120]
readAccel::axis: [-69, -95, -16111] readGyro::axis: [-124, -160, -124]
readAccel::axis: [-58, -75, -16103] readGyro::axis: [-121, -161, -124]
readAccel::axis: [-62, -75, -16110] readGyro::axis: [-120, -160, -126]
readAccel::axis: [-79, -75, -16098] readGyro::axis: [-125, -160, -125]
^C
GOODBYE
pi@raspberrypi:~/java_programs $ minimu9-ahrs --mode raw
-3604 1629 4322 -133 9 -4022 63 -82 -59
-3617 1593 4342 -76 48 -4099 43 -84 -62
-3617 1593 4342 -107 17 -4029 83 -106 -64
-3617 1593 4342 -137 27 -4043 69 -89 -63
-3617 1593 4342 -117 25 -4078 105 -95 -65
-3597 1598 4402 -103 29 -4062 73 -85 -65
-3597 1598 4402 -150 7 -4064 66 -72 -59
-3597 1598 4402 -113 16 -4081 73 -88 -63
-3597 1598 4402 -151 247 -4059 55 -65 -61
-3589 1570 4340 -78 -10 -4040 52 -96 -70
-3589 1570 4340 -161 5 -4059 70 -88 -57
-3589 1570 4340 -100 24 -4020 89 -107 -68
-3589 1570 4340 -79 17 -4029 66 -90 -62
-3589 1570 4340 -132 20 -4113 69 -74 -59
-3600 1584 4328 -137 15 -4079 79 -98 -69