Search code examples
androidorientationsensorseuler-angles

Get phone angle (pitch)


I really hope someone is able to help me: I am working on an Android App that displays the angle the phone is held

I am using this code:

protected void onCreate(Bundle savedInstanceState) 
{
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer,100000);
    mSensorManager.registerListener(this, mMagnetometer, 100000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

public void onSensorChanged(SensorEvent event) {
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = lowPass(event.values.clone(), mGravity);
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = lowPass(event.values.clone(), mGeomagnetic);
    if (mGravity != null && mGeomagnetic != null) {


        boolean success = SensorManager.getRotationMatrix(Ra, Ia, mGravity, mGeomagnetic);
       if (success) {
           SensorManager.getOrientation(Ra, orientation);

            float tempxGy =  (orientation[0]);// az
            float tempyGy =  (orientation[1]);// pitch
            float tempzGy  =  (orientation[2]); //roll
}

Im further processing the values that I get here but thats not the problem here.

When the phone is flat on the table the values look like that:

roll -0,018

pitch 0,024

yaw 2,51

that looks fine to me (also if I convert the values to euler angles)

now imagine that I take the phone from the table - the bottom of the phone (where the micro usb port is) stays on the table and I lift the side with the camera

the values of the pitch are getting more and more negative - till the pitch reaches -1.53 (-90 in euler) as soon as this value is reached the values are increasing again and if the phone is on the table with the display facing the table the pitch is back to 0 again. The problem is, that I have to differenciate between the two values (for example -1 and -1

How can I do that? The roll isnt a problem as the roll has a 180 degree maximum and not this 90 degree problem..

I really hope you can help me!


Solution

  • So

    if(mGravity[2]<0) orientation[1] = (float) (Math.PI + orientation[1]);
    

    is enough to get values that are like I need them --- but now I have a new problem - I would like to remap the coordinate system, so I get 0 degrees (euler) when the phone is on the table and +90 if the phone is standing on the micro usb port

    I tried

    SensorManager.remapCoordinateSystem(Ra, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, Ri);
    

    but now the roll and pitch are swapped --- what would I have to do to simply get positive pitch values as long as the display is facing up and negative values if the display is facing down?

    Is the remap the wrong idea and I could do it simpler by doing something else?