Monday, May 14, 2012

Android Pitch and Roll Issue

I am working on a tilt app for Android. I am having an issue with Portrait & landscape mode. When the pitch = 90 degrees (phone on end) and even slightly before the roll value goes crazy when there has been no physical change in roll. I have not been able to find a solution to this problem. If anyone can point me in the right direction, it would be appreciated.

Here's a short code dump, so you know it is not an accelerometer error.

final SensorEventListener mEventListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
setListners(sensorManager, mEventListener);

SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);

synchronized (this) {

switch (event.sensor.getType()){

System.arraycopy(event.values, 0, mValuesAccel, 0, 3);

long actualTime = System.currentTimeMillis();

//Sensitivity delay
if (actualTime - lastUpdate < 250) {
else {
sysAzimuth = (int)Math.toDegrees(mValuesOrientation[0]);
sysPitch = (int)Math.toDegrees(mValuesOrientation[1]);
sysRoll = (int)Math.toDegrees(mValuesOrientation[2]);

//invert direction with -1
pitch = (sysPitch - pitchCal)*-1;
roll = (sysRoll - rollCal);
azimuth = sysAzimuth;

lastUpdate = actualTime;

No comments:

Post a Comment