Example usage for android.hardware SensorManager getRotationMatrix

List of usage examples for android.hardware SensorManager getRotationMatrix

Introduction

In this page you can find the example usage for android.hardware SensorManager getRotationMatrix.

Prototype


public static boolean getRotationMatrix(float[] R, float[] I, float[] gravity, float[] geomagnetic) 

Source Link

Document

Computes the inclination matrix I as well as the rotation matrix R transforming a vector from the device coordinate system to the world's coordinate system which is defined as a direct orthonormal basis, where:

  • X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
  • Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
  • Z points towards the sky and is perpendicular to the ground.

World coordinate-system diagram.Usage

From source file:Main.java

public static void sensorRotation2Matrix(float[] gravity, float[] geomagnetic, int rotation, float[] output) {
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrix(output, null, gravity, geomagnetic);
        break;//  w w  w  .ja  v  a  2 s .co m
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:MainActivity.java

private void calculateCompassDirection(SensorEvent event) {
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        mAccelerationValues = event.values.clone();
        break;//from  w ww .jav  a 2 s.  co m
    case Sensor.TYPE_MAGNETIC_FIELD:
        mGravityValues = event.values.clone();
        break;
    }
    boolean success = SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelerationValues,
            mGravityValues);
    if (success) {
        float[] orientationValues = new float[3];
        SensorManager.getOrientation(mRotationMatrix, orientationValues);
        float azimuth = (float) Math.toDegrees(-orientationValues[0]);
        RotateAnimation rotateAnimation = new RotateAnimation(mLastDirectionInDegrees, azimuth,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(50);
        rotateAnimation.setFillAfter(true);
        mImageViewCompass.startAnimation(rotateAnimation);
        mLastDirectionInDegrees = azimuth;
    }
}

From source file:com.metinkale.prayerapp.compass._2D.Frag2D.java

@Override
public void onUpdateSensors(float[] rot) {
    if (mCompassView != null && getActivity() != null) {
        // mCompassView.setAngle(rot[0]);
        mGravity = LowPassFilter.filter(((Main) getActivity()).mMagAccel.mAccelVals, mGravity);
        mGeo = LowPassFilter.filter(((Main) getActivity()).mMagAccel.mMagVals, mGeo);

        if ((mGravity != null) && (mGeo != null)) {
            float[] R = new float[9];
            float[] I = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeo);
            if (success) {
                float[] orientation = new float[3];
                SensorManager.getOrientation(R, orientation);

                mCompassView.setAngle((int) Math.toDegrees(orientation[0]));

            }/*from   ww  w.  j a  v  a 2 s  . co  m*/
        }
    }

}

From source file:io.github.data4all.service.OrientationListener.java

@Override
public void onSensorChanged(SensorEvent event) {

    // check sensor type
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // smoothing sensor data
        mGravity = smoothing.filter(event.values.clone(), mGravity);
        System.arraycopy(event.values, 0, mGravity, 0, ARRAYLENGTH);
    } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        // smooting sensor data
        mGeomagnetic = smoothing.filter(event.values.clone(), mGeomagnetic);
        System.arraycopy(event.values, 0, mGeomagnetic, 0, ARRAYLENGTH);
    }/*from   www.j ava  2s. c o  m*/

    // when the 2 sensors data are available
    if (mGravity != null && mGeomagnetic != null) {

        final boolean success = SensorManager.getRotationMatrix(mR, mI, mGravity, mGeomagnetic);

        if (success) {
            SensorManager.getOrientation(mR, orientation);

            if (event.accuracy >= 1) {

                // saving the new model with the orientation in the
                // RingBuffer
                deviceOrientation = new DeviceOrientation(orientation[0], orientation[1],
                        orientation[LAST_INDEX], System.currentTimeMillis());
                Optimizer.putDevOrient(deviceOrientation);

                if (horizonListener != null) {
                    horizonListener.makeHorizon(true);
                }

            }

        }

    }

}

From source file:io.authme.sdk.widget.LockPatternView.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (collectSensor) {
        Sensor sensor = event.sensor;//from   www  .j  a v a 2  s  .co  m
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            mGravity = event.values;
            Accelerometer sensordata = new Accelerometer(mGravity[0], mGravity[1], mGravity[2],
                    event.timestamp);
            accelList.add(sensordata);
        }

        if (sensor.getType() == Sensor.TYPE_GRAVITY) {
            mGravity = event.values;
            Accelerometer sensordata = new Accelerometer(mGravity[0], mGravity[1], mGravity[2],
                    event.timestamp);
            accelList.add(sensordata);
        }

        if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mGeomagnetic = event.values;
            Magnetic magnetic = new Magnetic(mGeomagnetic[0], mGeomagnetic[1], mGeomagnetic[2],
                    event.timestamp);
            magnetics.add(magnetic);
        }

        if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            Gyroscope gyro = new Gyroscope(x, y, z, event.timestamp);
            gyrolist.add(gyro);
        }

        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 orientation[] = new float[3];
                SensorManager.getOrientation(R, orientation);
                Orientation orientationObj = new Orientation(orientation[0], orientation[1], orientation[2],
                        event.timestamp);
                orientationArrayList.add(orientationObj);
            }
        }

    }

}

From source file:com.kircherelectronics.gyroscopeexplorer.activity.filter.Orientation.java

protected void calculateOrientationAccelMag() {
    // To get the orientation vector from the acceleration and magnetic
    // sensors, we let Android do the heavy lifting. This call will
    // automatically compensate for the tilt of the compass and fail if the
    // magnitude of the acceleration is not close to 9.82m/sec^2. You could
    // perform these steps yourself, but in my opinion, this is the best way
    // to do it./*from  ww w. j  a v  a2  s  .  c o  m*/

    if (SensorManager.getRotationMatrix(rmOrientationAccelMag, null, vAcceleration, vMagnetic)) {

        SensorManager.getOrientation(rmOrientationAccelMag, vOrientationAccelMag);

        isOrientationValidAccelMag = true;
    }
}

From source file:com.kircherelectronics.accelerationexplorer.filter.ImuLaCfQuaternion.java

/**
 * Calculates orientation angles from accelerometer and magnetometer output.
 *//*from   w w w  .  ja  va 2 s.c o m*/
private void calculateOrientation() {
    // To get the orientation vector from the acceleration and magnetic
    // sensors, we let Android do the heavy lifting. This call will
    // automatically compensate for the tilt of the compass and fail if the
    // magnitude of the acceleration is not close to 9.82m/sec^2. You could
    // perform these steps yourself, but in my opinion, this is the best way
    // to do it.
    if (SensorManager.getRotationMatrix(rotationMatrix, null, acceleration, magnetic)) {
        SensorManager.getOrientation(rotationMatrix, baseOrientation);

        getRotationVectorFromAccelMag(baseOrientation);

        if (!hasOrientation) {
            quatGyro = new Quaternion(quatAccelMag.getScalarPart(), quatAccelMag.getVectorPart());
        }

        hasOrientation = true;
    }
}

From source file:com.example.android.camera2video.CameraActivity.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        logText("ACCELEROMETER," + event.values[0] + "," + event.values[1] + "," + event.values[2]);
        mAccVal = event.values;//from  w  ww . ja  v  a2  s.  com
    } else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
        logText("GRAVITY," + event.values[0] + "," + event.values[1] + "," + event.values[2]);
    } else if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
        logText("LINEAR_ACCELERATION," + event.values[0] + "," + event.values[1] + "," + event.values[2]);
    } else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        logText("GYROSCOPE," + event.values[0] + "," + event.values[1] + "," + event.values[2]);
    } else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) {
        logText("GYROSCOPE_UNCALIBRATED," + event.values[0] + "," + event.values[1] + "," + event.values[2]
                + "," + event.values[3] + "," + event.values[4] + "," + event.values[5]);
    } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        logText("MAGNETIC_FIELD," + event.values[0] + "," + event.values[1] + "," + event.values[2]);
        mMagVal = event.values;
        if (mAccVal != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mAccVal, mMagVal);
            if (success) {
                float orientation[] = new float[3];
                SensorManager.getOrientation(R, orientation);
                logText("ORIENTATION," + orientation[0] + "," + orientation[1] + "," + orientation[2]); //azimuth, pitch and roll
            }
        }
    }
}

From source file:net.line2soft.preambul.controllers.SlippyMapListener.java

@Override
public void onSensorChanged(SensorEvent event) {
    CompassView cp = (CompassView) activity.findViewById(R.id.compass);
    CompassView cpBig = (CompassView) activity.findViewById(R.id.compassBig);
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        System.arraycopy(event.values, 0, mGravity, 0, 3);
        break;//from   www. jav a 2  s. c  om
    case Sensor.TYPE_MAGNETIC_FIELD:
        System.arraycopy(event.values, 0, mMagnetic, 0, 3);
        break;
    default:
        return;
    }
    if (SensorManager.getRotationMatrix(mRotationM, null, mGravity, mMagnetic)) {
        SensorManager.remapCoordinateSystem(mRotationM, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                mRemapedRotationM);
        SensorManager.getOrientation(mRemapedRotationM, mOrientation);
        int mAzimuth = (int) Math.round((Math.toDegrees(mOrientation[0])) * 2) / 2;
        cp.updateDirection(mAzimuth);
        cpBig.updateDirection(mAzimuth);
    }
}

From source file:com.tritop.androsense2.fragments.GpsFragment.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccel = event.values;//  w w  w . j  av a 2  s.co  m
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mMag = event.values;
    }
    if ((mAccel != null) && (mMag != null)) {
        float R[] = new float[9];
        float I[] = new float[9];
        float Rot[] = new float[9];
        if (SensorManager.getRotationMatrix(R, I, mAccel, mMag)) {
            float orientation[] = new float[3];
            int axisX = 0, axisY = 0;
            switch (display.getRotation()) {
            case Surface.ROTATION_0:
                axisX = SensorManager.AXIS_X;
                axisY = SensorManager.AXIS_Y;
                break;
            case Surface.ROTATION_90:
                axisX = SensorManager.AXIS_Y;
                axisY = SensorManager.AXIS_MINUS_X;
                break;
            case Surface.ROTATION_180:
                axisX = SensorManager.AXIS_MINUS_X;
                axisY = SensorManager.AXIS_MINUS_Y;
                break;
            case Surface.ROTATION_270:
                axisX = SensorManager.AXIS_MINUS_Y;
                axisY = SensorManager.AXIS_X;
                break;
            default:
                break;
            }

            SensorManager.remapCoordinateSystem(R, axisX, axisY, Rot);
            SensorManager.getOrientation(Rot, orientation);
            aRotation = orientation[0];
            aRotation = (int) Math.toDegrees(aRotation);
            satView.setAzimutRotation(-aRotation);
            satView.invalidate();
        }
    }
}