/** Returns the rotation matrix describing the devices rotation as per <a href= * "http://developer.android.com/reference/android/hardware/SensorManager.html#getRotationMatrix(float[], float[], float[], float[])" * >SensorManager#getRotationMatrix(float[], float[], float[], float[])</a>. Does not manipulate the matrix if the platform * does not have an accelerometer and compass, or a rotation vector sensor. * @param matrix */ public void getRotationMatrix (float[] matrix) { if (rotationVectorAvailable) SensorManager.getRotationMatrixFromVector(matrix, rotationVectorValues); else // compass + accelerometer SensorManager.getRotationMatrix(matrix, null, accelerometerValues, magneticFieldValues); }
boolean success = SensorManager.getRotationMatrix(rotationMatrix, null, mGravity, mGeomagnetic);
private void updateSensorValues(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { setAcclValues(event); } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { setmagValues(event); } SensorManager.getRotationMatrix(rotation, null, grav, mag); SensorManager.getOrientation(rotation, orientation); double floatBearing = orientation[0]; floatBearing = Math.toDegrees(floatBearing); if (magField != null) floatBearing += magField.getDeclination(); if (floatBearing < 0) floatBearing += 360; setBearing(floatBearing); setRotationInDegrees(floatBearing); }
/** Returns the rotation matrix describing the devices rotation as per <a href= * "http://developer.android.com/reference/android/hardware/SensorManager.html#getRotationMatrix(float[], float[], float[], float[])" * >SensorManager#getRotationMatrix(float[], float[], float[], float[])</a>. Does not manipulate the matrix if the platform * does not have an accelerometer and compass, or a rotation vector sensor. * @param matrix */ public void getRotationMatrix (float[] matrix) { if (rotationVectorAvailable) SensorManager.getRotationMatrixFromVector(matrix, rotationVectorValues); else // compass + accelerometer SensorManager.getRotationMatrix(matrix, null, accelerometerValues, magneticFieldValues); }
if (SensorManager.getRotationMatrix(curRotationMat, curInclinationMat, accValues, magValues)) { final float [] orientValues = new float[3]; if (remapCoordinates(curRotationMat, rotatedRotationMat)) {
private void updateOrientation () { if (rotationVectorAvailable){ SensorManager.getRotationMatrixFromVector(R, rotationVectorValues); } else if (!SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues)) { return; // compass + accelerometer in free fall } SensorManager.getOrientation(R, orientation); azimuth = (float)Math.toDegrees(orientation[0]); pitch = (float)Math.toDegrees(orientation[1]); roll = (float)Math.toDegrees(orientation[2]); }
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];
private void updateOrientation () { if (rotationVectorAvailable){ SensorManager.getRotationMatrixFromVector(R, rotationVectorValues); } else if (!SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues)) { return; // compass + accelerometer in free fall } SensorManager.getOrientation(R, orientation); azimuth = (float)Math.toDegrees(orientation[0]); pitch = (float)Math.toDegrees(orientation[1]); roll = (float)Math.toDegrees(orientation[2]); }
@Override public void onSensorChanged(SensorEvent event) { if (listener == null) return; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { geomagnetic = event.values.clone(); } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { gravity = event.values.clone(); } if (gravity != null && geomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic); if (success) { R = remapToDisplayRotation(R); float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); float azimut = orientation[0] - declination; float pitch = orientation[1]; float roll = orientation[2]; rotation = azimut; tilt = pitch; } } }
public void calculateAccMagOrientation() { if (SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) { SensorManager.getOrientation(rotationMatrix, accMagOrientation); } }
private void listing16_12() { // Listing 16-12: Finding the current orientation using the accelerometer and magnetometer float[] values = new float[3]; float[] R = new float[9]; SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticFieldValues); SensorManager.getOrientation(R, values); // Convert from radians to degrees if preferred. values[0] = (float) Math.toDegrees(values[0]); // Azimuth values[1] = (float) Math.toDegrees(values[1]); // Pitch values[2] = (float) Math.toDegrees(values[2]); // Roll }
private void updateOrientationAngles() { SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading); SensorManager.getOrientation(rotationMatrix, orientationAngles); beaconRadar.startDeviceAngleAnimation((float) Math.toDegrees(orientationAngles[0])); }
private void emitAzimuth() { if (gravity != null && geomagnetic != null) { boolean success = SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomagnetic); if (success) { SensorManager.getOrientation(rotationMatrix, rotationMatrixResult); emitData(rotationMatrixResult[0]); } } } }
@Override public void onSensorChanged(SensorEvent event) { // we received a sensor event. it is a good practice to check // that we received the proper event if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { System.arraycopy(event.values, 0, magnitudeValues, 0, magnitudeValues.length); } else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) { System.arraycopy(event.values, 0, gravityValues, 0, gravityValues.length); } if (magnitudeValues != null && gravityValues != null) { // Fuse gravity-sensor (virtual sensor) with compass SensorManager.getRotationMatrix(currentOrientationRotationMatrix.matrix, inclinationValues, gravityValues, magnitudeValues); // Transform rotation matrix to quaternion currentOrientationQuaternion.setRowMajor(currentOrientationRotationMatrix.matrix); } } }
@Override public void onSensorChanged(SensorEvent event) { // we received a sensor event. it is a good practice to check // that we received the proper event if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { System.arraycopy(event.values, 0, magnitudeValues, 0, magnitudeValues.length); } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { System.arraycopy(event.values, 0, accelerometerValues, 0, accelerometerValues.length); } if (magnitudeValues != null && accelerometerValues != null) { // Fuse accelerometer with compass SensorManager.getRotationMatrix(currentOrientationRotationMatrix.matrix, inclinationValues, accelerometerValues, magnitudeValues); // Transform rotation matrix to quaternion currentOrientationQuaternion.setRowMajor(currentOrientationRotationMatrix.matrix); } } }
public void onSensorChanged(SensorEvent event) { if (event.accuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW) return; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { arrayCopy(event.values, geomagnetic); } if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { arrayCopy(event.values, gravity); } if (SensorManager.getRotationMatrix(R, I, gravity, geomagnetic)) { SensorManager.getOrientation(R, orientation); azimuth += easing * (orientation[0] - azimuth); pitch += easing * (orientation[1] - pitch); roll += easing * (orientation[2] - roll); } }
@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) { boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { SensorManager.getOrientation(R, orientation); } } setHeading(restrictAngle((int) Math.toDegrees((double) orientation[0]))); setPitch(restrictAngle((int) Math.toDegrees((double) orientation[2]))); setRoll(restrictAngle((int) Math.toDegrees((double) orientation[1]))); EventBus.getDefault().post(new PhoneSensorsData(heading, pitch, roll)); }
public void calculateAzimuth() { SensorManager.getRotationMatrix(RMatrix, iMatrix, gravity, magn); SensorManager.remapCoordinateSystem(RMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Y, RMatrixRemapped); SensorManager.getOrientation(RMatrixRemapped, orientation); Matrix.transposeM(RMatrixTranspose, 0, RMatrix, 0); Matrix.multiplyMV(linearRemapped, 0, RMatrixTranspose, 0, linear, 0); //If Gyroscope exists, use ImprovedOrientationProvider, else use accelerometer and magentic field if (gyroExists) { azimuth = mOrientationProvider.getAzimuth(decl); } else { if (orientation[0] >= 0) { // Azimuth-Calculation (rad in degree) azimuth = (orientation[0] * 57.29577951f + decl); } else { // Azimuth-Calculation (rad in degree) +360 azimuth = (orientation[0] * 57.29577951f + 360 + decl); } if (azimuth >= 360) { azimuth -= 360; } } }
/** * SensorEventListener method called when sensor values are updated. Reads gravitational and * magnetic field information, and when both are available computes the orientation values * and calls the delegate with them. */ @Override public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()) { case Sensor.TYPE_MAGNETIC_FIELD: mags = event.values.clone(); break; case Sensor.TYPE_ACCELEROMETER: accels = event.values.clone(); break; } if (mags!=null && accels!=null) { SensorManager.getRotationMatrix(R, I, accels, mags); SensorManager.getOrientation(R, orientationValues); delegate.receivedOrientationValues( orientationValues[0], orientationValues[1], orientationValues[2]); } }
/** * Calculates orientation vector from accelerometer and magnetometer output. * * @param acceleration the acceleration measurement. * @param magnetic the magnetic measurement. * @return */ public static Quaternion getOrientationVectorFromAccelerationMagnetic(float[] acceleration, float[] magnetic) { float[] rotationMatrix = new float[9]; if (SensorManager.getRotationMatrix(rotationMatrix, null, acceleration, magnetic)) { float[] rv = new float[3]; SensorManager.getOrientation(rotationMatrix,rv); // SensorManager.getOrientation() returns an orientation in Earth frame and that needs to be rotated into device frame so the reported angles // are indexed with the orientation of the sensors Rotation rotation = new Rotation(RotationOrder.XYZ, RotationConvention.VECTOR_OPERATOR, rv[1], -rv[2], rv[0]); return new Quaternion(rotation.getQ0(), rotation.getQ1(),rotation.getQ2(),rotation.getQ3()); } return null; }