Example usage for android.hardware SensorManager getRotationMatrixFromVector

List of usage examples for android.hardware SensorManager getRotationMatrixFromVector

Introduction

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

Prototype

public static void getRotationMatrixFromVector(float[] R, float[] rotationVector) 

Source Link

Document

Helper function to convert a rotation vector to a rotation matrix.

Usage

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;/*  w w w.java  2  s  .c o m*/
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        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:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {/* ww  w .  ja va 2  s . com*/
            SensorManager.getRotationMatrixFromVector(sUIThreadTmp, event.values);
        } catch (Exception e) {
            // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception
            // if the rotation vector has more than 4 elements. Since only the four first elements are used,
            // we can truncate the vector without losing precision.
            Log.e(TAG, "maybe Samsung bug, will truncate vector");
            sIsTruncated = true;
        }
    }

    if (sIsTruncated) {
        System.arraycopy(event.values, 0, sTruncatedVector, 0, 4);
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, sTruncatedVector);
    }

    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static float[] getRotationFromGyro(float[] values, float timestamp, float nowTimeStamp,
        float[] currentRotMatrix, boolean swapX, boolean swapY, boolean swapZ) {
    float[] deltaRotationVector = new float[4];
    if (timestamp != 0) {
        final float dT = (nowTimeStamp - timestamp) * NS2S;
        float axisX = swapX ? -values[0] : values[0];
        float axisY = swapY ? -values[1] : values[1];
        float axisZ = swapZ ? -values[2] : values[2];

        float omegaMagnitude = (float) Math.sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
        if (omegaMagnitude > 0.1f) {
            axisX /= omegaMagnitude;/* ww  w .  ja va2  s  .  co m*/
            axisY /= omegaMagnitude;
            axisZ /= omegaMagnitude;
        }

        float thetaOverTwo = omegaMagnitude * dT / 2.0f;
        float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
        float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
        deltaRotationVector[0] = sinThetaOverTwo * axisX;
        deltaRotationVector[1] = sinThetaOverTwo * axisY;
        deltaRotationVector[2] = sinThetaOverTwo * axisZ;
        deltaRotationVector[3] = cosThetaOverTwo;
    }
    float[] deltaRotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
    return naivMatrixMultiply(currentRotMatrix, deltaRotationMatrix);
}

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.
 *//*from  www . j ava2 s. co 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  ww.j  a  v  a 2s.com*/
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  .j  a v  a 2 s  .  c  om*/

            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: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 w  w  .  jav  a2  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:com.example.casthelloworld.MainActivity.java

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;// w  w  w.j a v  a2  s .com
    }

    SensorManager.getRotationMatrixFromVector(mRotationMatrix, sensorEvent.values);

    SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            mRotationMatrix);

    SensorManager.getOrientation(mRotationMatrix, orientationVals);

    orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

    float Roll = orientationVals[2];

    if (Roll < -85 && Roll > -95 && lastVal != 0.0f) {
        // Log.d("Middle: ", "" + Roll);
        lastVal = 0.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -95 && Roll > -105 && lastVal != -0.2f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -85 && Roll < -75 && lastVal != 0.2f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -105 && Roll > -115 && lastVal != -0.4f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -75 && Roll < -65 && lastVal != 0.4f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -115 && Roll > -125 && lastVal != -0.6f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -65 && Roll < -55 && lastVal != 0.6f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -125 && Roll > -135 && lastVal != -0.8f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -55 && Roll < -45 && lastVal != 0.8f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -135 && lastVal != -1.0f && lastVal != 1.0f) {
        // Log.d("Left", "" + Roll);
        lastVal = -1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -45 && lastVal != 1.0f && lastVal != -1.0f) {
        // Log.d("Right", "" + Roll);
        lastVal = 1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    }

}

From source file:jp.co.recruit_lifestyle.android.widget.BeerSwipeRefreshLayout.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() != Sensor.TYPE_ROTATION_VECTOR || !mBeerView.isMax()) {
        return;/*from w  w  w .jav a  2s  .c om*/
    }

    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            adjustedRotationMatrix);

    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);

    float roll = orientation[2] * -57;

    if (roll < ROLL_LIMIT && roll > -ROLL_LIMIT) {
        mBeerView.drawGlass(-roll);
        mBeerView.drawGlassFroth(-roll, 1);
        mOldRoll = -roll;
    } else {
        mBeerView.drawGlass(mOldRoll);
        mBeerView.drawGlassFroth(mOldRoll, 1);
    }
}

From source file:com.example.basensortracker.SensorFusionActivity.java

public void gyroFunction(SensorEvent event) {
    // don't start until first accelerometer/magnetometer orientation has
    // been acquired
    if (accMagOrientation == null)
        return;/* www.ja v a2 s  .  co m*/

    // initialisation of the gyroscope based rotation matrix
    if (initState) {
        float[] initMatrix = new float[9];
        initMatrix = getRotationMatrixFromOrientation(accMagOrientation);
        float[] test = new float[3];
        SensorManager.getOrientation(initMatrix, test);
        gyroMatrix = matrixMultiplication(gyroMatrix, initMatrix);
        initState = false;
    }

    // copy the new gyro values into the gyro array
    // convert the raw gyro data into a rotation vector
    float[] deltaVector = new float[4];
    if (timestamp != 0) {
        final float dT = (event.timestamp - timestamp) * NS2S;
        System.arraycopy(event.values, 0, gyro, 0, 3);
        getRotationVectorFromGyro(gyro, deltaVector, dT / 2.0f);
    }

    // measurement done, save current time for next interval
    timestamp = event.timestamp;

    // convert rotation vector into rotation matrix
    float[] deltaMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(deltaMatrix, deltaVector);

    // apply the new rotation interval on the gyroscope based rotation
    // matrix
    gyroMatrix = matrixMultiplication(gyroMatrix, deltaMatrix);

    // get the gyroscope based orientation from the rotation matrix
    SensorManager.getOrientation(gyroMatrix, gyroOrientation);
}