javaandroidsensormanager

Rotation sensor not reacting if phone is moved slowly


I'm trying to read the azimuth, pitch & roll from the sensor manager. For some reason if I move the phone very slowly, the azimuth, pitch & roll values do not get updated, they only update if I move the phone strong enough. In other words, if I move the phone very slowly, I can do a full 90 degrees rotation without getting any new values.

I see that it gets 'stuck' on this line and if phone is getting moved too slowly, this line will return false:

SensorManager.getRotationMatrix( R, I, mGravity, mGeomagnetic );

It almost seems like if there is no significant force for accelerator, then the rotation won't update as well.

Here's my code:

mSensorManager = (SensorManager)activityContext.getSystemService(Context.SENSOR_SERVICE);;
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
mMagnetometer  = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mMagnetometer,  SensorManager.SENSOR_DELAY_GAME); 

And here's the onSensorChanged event:

@Override
public void onSensorChanged(SensorEvent event) 
{
    if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION)
        mGravity     = event.values;
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
        mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) 
    {
        // The code is triggering to this point fine

        float R[] = new float[9];
        float I[] = new float[9];

        float forceX = Float.valueOf(((float)((float)mGravity[0] ) ) ) / (float)4.0;
        float forceY = Float.valueOf(((float)((float)mGravity[1] ) ) ) / (float)4.0;
        float forceZ = Float.valueOf(((float)((float)mGravity[2] ) ) ) / (float)4.0;

        boolean success = SensorManager.getRotationMatrix( R, I, mGravity, mGeomagnetic );

        if (success) // <-- this is only triggering if phone is moved fast enough 
        {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            rotation_degrees_x = ((float)Math.toDegrees( orientation[0] ) + (float)180.0 ) / (float)360.0;
            rotation_degrees_y = ((float)Math.toDegrees( orientation[1] ) + (float)180.0 ) / (float)360.0;
            rotation_degrees_z = ((float)Math.toDegrees( orientation[2] ) + (float)180.0 ) / (float)360.0;

        }

        mGeomagnetic = null;
        mGravity     = null;
    }
}

I also see that the following condition is triggering constantly:

if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE)

So I added the following line within the condition:

Log.d( "ROTATIONDEBUG", "TYPE_MAGNETIC_FIELD: " + mGeomagnetic[0] + "\t" + mGeomagnetic[1] + "\t" + mGeomagnetic[1] );

And got some values which reflect the slightest changes in the rotation:

TYPE_MAGNETIC_FIELD: 23.5      20.7    20.7
TYPE_MAGNETIC_FIELD: 23.2      22.2    22.2
TYPE_MAGNETIC_FIELD: 23.0      24.9    24.9
TYPE_MAGNETIC_FIELD: 22.4      27.5    27.5
TYPE_MAGNETIC_FIELD: 21.5      29.2    29.2
TYPE_MAGNETIC_FIELD: 21.6      30.2    30.2
TYPE_MAGNETIC_FIELD: 21.1      32.1    32.10
TYPE_MAGNETIC_FIELD: 20.7      34.8    34.8
TYPE_MAGNETIC_FIELD: 20.7      34.7    34.7
TYPE_MAGNETIC_FIELD: 20.9      35.0    35.0
TYPE_MAGNETIC_FIELD: 20.6      36.0    36.0

But can't understand why they're not working together and the SensorManager.getRotationMatrix line returns false..

I downloaded a few compass and sensor testing apps and all of them could pick up this light movement, so I'm guessing there's a problem with my code.

EDIT: I tried a different code example, but it's giving me exactly the same results:

SensorEventListener sensorEventListener = new SensorEventListener() {
    float[] mGravity;
    float[] mGeomagnetic;

    @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) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
            if (success) {
                float orientationData[] = new float[3];
                SensorManager.getOrientation(R, orientationData);
                azimuth = orientationData[0];
                pitch = orientationData[1];
                roll = orientationData[2];
            }
        }
    }
};

So far I see this pattern:

If I read the values directly from the condition below, I can get 'detailed/sensitive' changes:

 if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) 
    mGeomagnetic = event.values; 

BUT if I try to use getRotationMatrix, it will only return true, if enought force is detected with accelerometer:

 boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);

How can I get the SensorManager.getRotationMatrix to trigger regardless of the accelerometer value? or how can I convert these RAW values to degrees?

Any hints please? Thank you!


Solution

  • It turns out by changing the SENSOR_DELAY_GAME to SENSOR_DELAY_NORMAL and using just regular TYPE_GYROSCOPE, solved the problem and it's triggering even on the slightest movement!