Example usage for android.hardware SensorManager getOrientation

List of usage examples for android.hardware SensorManager getOrientation

Introduction

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

Prototype

public static float[] getOrientation(float[] R, float[] values) 

Source Link

Document

Computes the device's orientation based on the rotation matrix.

Usage

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 w w. ja  v  a 2s . c o 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 va 2  s  .  com*/
        }
    }

}

From source file:com.bolatu.gezkoncsvlogger.GyroOrientation.GyroscopeOrientation.java

/**
 * The orientation of the device. Euler angles in units of radians.
 * values[0]: azimuth, rotation around the Z axis. values[1]: pitch,
 * rotation around the X axis. values[2]: roll, rotation around the Y axis.
 *//*w  ww . j ava 2  s  .  c o  m*/
public float[] getOrientation() {
    if (isOrientationValidAccelMag) {
        // Now we get a structure we can pass to get a rotation matrix, and
        // then an orientation vector from Android.
        qvOrientation[0] = (float) qGyroscope.getVectorPart()[0];
        qvOrientation[1] = (float) qGyroscope.getVectorPart()[1];
        qvOrientation[2] = (float) qGyroscope.getVectorPart()[2];
        qvOrientation[3] = (float) qGyroscope.getScalarPart();

        // We need a rotation matrix so we can get the orientation vector...
        // Getting Euler
        // angles from a quaternion is not trivial, so this is the easiest
        // way,
        // but perhaps
        // not the fastest way of doing this.
        SensorManager.getRotationMatrixFromVector(rmGyroscope, qvOrientation);

        // Get the fused orienatation
        SensorManager.getOrientation(rmGyroscope, vOrientation);
    }

    return vOrientation;
}

From source file:com.example.johnny.multipong.sensorFilters.Filters.GyroscopeOrientation.java

/**
 * The orientation of the device. Euler angles in units of radians.
 * values[0]: azimuth, rotation around the Z axis. values[1]: pitch,
 * rotation around the X axis. values[2]: roll, rotation around the Y axis.
 *///from w  w w.ja  v a  2  s  .co  m
public float[] getOrientation() {
    //Log.i("Gyroscope", "Getting orientation!");
    if (isOrientationValidAccelMag) {
        // Now we get a structure we can pass to get a rotation matrix, and
        // then an orientation vector from Android.
        qvOrientation[0] = (float) qGyroscope.getVectorPart()[0];
        qvOrientation[1] = (float) qGyroscope.getVectorPart()[1];
        qvOrientation[2] = (float) qGyroscope.getVectorPart()[2];
        qvOrientation[3] = (float) qGyroscope.getScalarPart();

        // We need a rotation matrix so we can get the orientation vector...
        // Getting Euler
        // angles from a quaternion is not trivial, so this is the easiest
        // way,
        // but perhaps
        // not the fastest way of doing this.
        SensorManager.getRotationMatrixFromVector(rmGyroscope, qvOrientation);

        // Get the fused orienatation
        SensorManager.getOrientation(rmGyroscope, vOrientation);
    }

    return vOrientation;
}

From source file:com.adstrosoftware.notificationcompass.CompassService.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        if (previousEventTimeStamp == Long.MIN_VALUE
                || (event.timestamp - previousEventTimeStamp) > DELAY_IN_NS) {
            previousEventTimeStamp = event.timestamp;

            float[] orientation = new float[3];
            float[] rotationMatrix = new float[16];
            float[] remappedRotationMatrix = new float[16];

            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

            if (event.values[0] <= -45) {
                SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        remappedRotationMatrix);
            } else {
                remappedRotationMatrix = rotationMatrix;
            }//from w  w  w .ja v  a  2  s . c o m

            SensorManager.getOrientation(remappedRotationMatrix, orientation);

            Notification notification;

            double azimuth = Math.toDegrees(orientation[0]);

            if (BuildConfig.DEBUG) {
                Log.d(TAG, "Azimuth = " + azimuth);
            }

            if (azimuth <= (NORTH + ANGLE) && azimuth >= (NORTH - ANGLE)) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH");
                notification = buildNotification(R.string.north, R.drawable.ic_stat_north, azimuth);
            } else if (azimuth <= (NORTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_EAST");
                notification = buildNotification(R.string.north_east, R.drawable.ic_stat_north_east, azimuth);
            } else if (azimuth >= (NORTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_WEST");
                notification = buildNotification(R.string.north_west, R.drawable.ic_stat_north_west, azimuth);
            } else if (azimuth <= (EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "EAST");
                notification = buildNotification(R.string.east, R.drawable.ic_stat_east, azimuth);
            } else if (azimuth >= (WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "WEST");
                notification = buildNotification(R.string.west, R.drawable.ic_stat_west, azimuth);
            } else if (azimuth <= (SOUTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_EAST");
                notification = buildNotification(R.string.south_east, R.drawable.ic_stat_south_east, azimuth);
            } else if (azimuth >= (SOUTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_WEST");
                notification = buildNotification(R.string.south_west, R.drawable.ic_stat_south_west, azimuth);
            } else {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH");
                notification = buildNotification(R.string.south, R.drawable.ic_stat_south, azimuth);
            }

            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}

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 w  w w  .ja  v a 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:ch.fhnw.comgr.GLES3Activity.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && GLES3Lib.usesRotation()) {
        // The ROTATION_VECTOR sensor is a virtual fusion sensor
        // The quality strongly depends on the underlying algorithm and on
        // the sensor manufacturer. (See also chapter 7 in the book:
        // "Professional Sensor Programming (WROX Publishing)"

        // Get 3x3 rotation matrix from XYZ-rotation vector (see docs)
        float R[] = new float[9];
        SensorManager.getRotationMatrixFromVector(R, event.values);

        // Get yaw, pitch & roll rotation angles in radians from rotation matrix
        float[] YPR = new float[3];
        SensorManager.getOrientation(R, YPR);

        // Check display orientation (a preset orientation is set in the AndroidManifext.xml)
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics displaymetrics = new DisplayMetrics();
        display.getMetrics(displaymetrics);
        int screenWidth = displaymetrics.widthPixels;
        int screenHeight = displaymetrics.heightPixels;

        if (screenWidth < screenHeight) { // Map pitch, yaw and roll to portrait display orientation
            final float p = YPR[1] * -1.0f - (float) Math.PI * 0.5f;
            final float y = YPR[0] * -1.0f;
            final float r = YPR[2] * -1.0f;
            myView.queueEvent(new Runnable() {
                public void run() {
                    GLES3Lib.onRotationPYR(p, y, r);
                }//from w  ww . ja va2 s.  c o  m
            });
        } else { // Map pitch, yaw and roll to landscape display orientation for Oculus Rift conformance
            final float p = YPR[2] * -1.0f - (float) Math.PI * 0.5f;
            final float y = YPR[0] * -1.0f;
            final float r = YPR[1];
            myView.queueEvent(new Runnable() {
                public void run() {
                    GLES3Lib.onRotationPYR(p, y, r);
                }
            });
        }

        /*
           // Get the rotation quaternion from the XYZ-rotation vector (see docs)
        final float Q[] = new float[4];
        SensorManager.getQuaternionFromVector(Q, event.values);
        myView.queueEvent(new Runnable() {public void run() {GLES3Lib.onRotationQUAT(Q[1],Q[2],Q[3],Q[0]);}});
        */
    }
}

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

@Override
public void onSensorChanged(SensorEvent event) {
    if (collectSensor) {
        Sensor sensor = event.sensor;//  w ww  . j  a v  a  2  s . c o  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   w ww .j a v  a 2s.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  . j  a  v  a  2s .  c  om*/
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;
    }
}