Java Code Examples for android.hardware.SensorManager#getQuaternionFromVector()

The following examples show how to use android.hardware.SensorManager#getQuaternionFromVector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ControllerFragment.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
@SuppressWarnings("ConstantConditions")
private void updateOrientation() {
    float[] lastAccelerometer = mSensorData[kSensorType_Accelerometer].values;
    float[] lastMagnetometer = mSensorData[kSensorType_Magnetometer].values;
    if (lastAccelerometer != null && lastMagnetometer != null) {
        SensorManager.getRotationMatrix(mRotation, null, lastAccelerometer, lastMagnetometer);
        SensorManager.getOrientation(mRotation, mOrientation);

        final boolean kUse4Components = true;
        if (kUse4Components) {
            SensorManager.getQuaternionFromVector(mQuaternion, mOrientation);
            // Quaternions in Android are stored as [w, x, y, z], so we change it to [x, y, z, w]
            float w = mQuaternion[0];
            mQuaternion[0] = mQuaternion[1];
            mQuaternion[1] = mQuaternion[2];
            mQuaternion[2] = mQuaternion[3];
            mQuaternion[3] = w;

            mSensorData[kSensorType_Quaternion].values = mQuaternion;
        } else {
            mSensorData[kSensorType_Quaternion].values = mOrientation;
        }
    }
}
 
Example 2
Source File: OrientationPublisher.java    From rosjava_android_template with Apache License 2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
  if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    float[] quaternion = new float[4];
    SensorManager.getQuaternionFromVector(quaternion, event.values);
    PoseStamped pose = publisher.newMessage();
    pose.getHeader().setFrameId("/map");
    // TODO(damonkohler): Should get time from the Node.
    pose.getHeader().setStamp(Time.fromMillis(System.currentTimeMillis()));
    pose.getPose().getOrientation().setW(quaternion[0]);
    pose.getPose().getOrientation().setX(quaternion[1]);
    pose.getPose().getOrientation().setY(quaternion[2]);
    pose.getPose().getOrientation().setZ(quaternion[3]);
    publisher.publish(pose);
  }
}
 
Example 3
Source File: ControllerActivity.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
private void updateOrientation() {
    float[] lastAccelerometer = mSensorData[kSensorType_Accelerometer].values;
    float[] lastMagnetometer = mSensorData[kSensorType_Magnetometer].values;
    if (lastAccelerometer != null && lastMagnetometer != null) {
        SensorManager.getRotationMatrix(mRotation, null, lastAccelerometer, lastMagnetometer);
        SensorManager.getOrientation(mRotation, mOrientation);

        final boolean kUse4Components = true;
        if (kUse4Components) {
            SensorManager.getQuaternionFromVector(mQuaternion, mOrientation);
            // Quaternions in Android are stored as [w, x, y, z], so we change it to [x, y, z, w]
            float w = mQuaternion[0];
            mQuaternion[0] = mQuaternion[1];
            mQuaternion[1] = mQuaternion[2];
            mQuaternion[2] = mQuaternion[3];
            mQuaternion[3] = w;

            mSensorData[kSensorType_Quaternion].values = mQuaternion;
        } else {
            mSensorData[kSensorType_Quaternion].values = mOrientation;
        }
    }
}
 
Example 4
Source File: OrientationDataCollector.java    From DataLogger with MIT License 5 votes vote down vote up
private void logOrientation(SensorEvent event){
    /*// Rotation matrix based on current readings from accelerometer and magnetometer.
    final float[] rotationMatrix = new float[9];
    // Convert the rotation-vector to a 4x4 matrix.
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
    final float[] orientationValues = new float[3];
    SensorManager.getOrientation(rotationMatrix, orientationValues);

    // Optionally convert the result from radians to degrees
    orientationValues[0] = (float) Math.toDegrees(orientationValues[0]);
    orientationValues[1] = (float) Math.toDegrees(orientationValues[1]);
    orientationValues[2] = (float) Math.toDegrees(orientationValues[2]);*/

    final float[] quaternionValue = new float[4];
    SensorManager.getQuaternionFromVector(quaternionValue, event.values);

    // System local time in millis
    long currentMillis = (new Date()).getTime();

    // Sensor timestamp defines uptime in nanos (nanoseconds since boot)
    long eventTimestamp = event.timestamp + mNanosOffset;

    String message = String.format("%s", currentMillis) + ";"
            + String.format("%s", eventTimestamp) + ";"
            + String.format("%s", mNanosOffset);

    for (float v : quaternionValue) {
        message += String.format(";%.7f", v);
    }

    logger.log(message);
    logger.log(System.lineSeparator());
}
 
Example 5
Source File: TiltSensor.java    From WindowView with MIT License 4 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_ROTATION_VECTOR:
            SensorManager.getQuaternionFromVector(latestQuaternion, event.values);
            if (!haveRotVecData) {
                initialiseDefaultFilters(SMOOTHING_FACTOR_HIGH_ACC);
            }
            haveRotVecData = true;
            break;
        case Sensor.TYPE_GRAVITY:
            if (haveRotVecData) {
                // rotation vector sensor data is better
                sensorManager.unregisterListener(this,
                        sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY));
                break;
            }
            System.arraycopy(event.values, 0, latestAccelerations, 0, 3);
            haveGravData = true;
            break;
        case Sensor.TYPE_ACCELEROMETER:
            if (haveGravData || haveRotVecData) {
                // rotation vector / gravity sensor data is better!
                // let's not listen to the accelerometer anymore
                sensorManager.unregisterListener(this,
                        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
                break;
            }
            System.arraycopy(event.values, 0, latestAccelerations, 0, 3);
            haveAccelData = true;
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            if (haveRotVecData) {
                // rotation vector sensor data is better
                sensorManager.unregisterListener(this,
                        sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD));
                break;
            }
            System.arraycopy(event.values, 0, latestMagFields, 0, 3);
            haveMagData = true;
            break;
    }

    if (haveDataNecessaryToComputeOrientation()) {
        computeOrientation();
    }
}