Android : How to apply certain Offset to getOrientation of Sensors?

on Thursday, March 19, 2015


I'm struggeling with a problem for a long time now and still don't know how to solve it.


I want to get the device orientation, but in a special way. I'm using sensor combination of Accelerometer and Magnetic Field, my outcome values are azimut, pitch and roll (see Android Documentation).


What I try to achieve is to set a certain offset to these values.


Example: Lets say you put your device on your office chair and put a little box under the top of the device.


Orientation will be:


azimut: [0 - 360°] depends on direction of chair


pitch: 45° (caused by the box)


roll: 0° (this axis is still balanced)


Now I want this orientation to be the new basic orientation. Lets say I hit a calibration button and the current orientation will be stored. Now I will have:


azimut: [0 - 360°]


pitch: 0° (45° of box is new 0°)


roll: 0°


This isn't a big problem, I solved it using getAngleChange(float[] angleChange, float[] R, float[] prevR), but now the new values don't match my expectations. I was hoping that when I will rotate the chair, the values for pitch and roll will stay almost 0°, but they are changing while rotating (only azimut should change).


I'm storing the rotationMatrix when hitting the calibration button (thats where caliRot comes from).


Anyone did something similar and can help me out?


Thanks in Advance!



@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
Rot = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(Rot, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(Rot, orientation);
//azimut = orientation[0]; // orientation contains: azimut, pitch and roll
x.setText(formatter.format((Math.toDegrees(orientation[0]) + 360) % 360));
y.setText(formatter.format(Math.toDegrees(orientation[1])));
z.setText(formatter.format(Math.toDegrees(orientation[2])));

if (caliRot != null) {
float offsetAngles[] = new float[3];
SensorManager.getAngleChange(offsetAngles, Rot, caliRot);

offX.setText(formatter.format(Math.toDegrees(offsetAngles[0])));
offY.setText(formatter.format(Math.toDegrees(offsetAngles[1])));
offZ.setText(formatter.format(Math.toDegrees(offsetAngles[2])));
}
}
}
}

0 comments:

Post a Comment