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

The following examples show how to use android.hardware.SensorManager#remapCoordinateSystem() . 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: OrientationManager.java    From SpeedHud with Apache License 2.0 8 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        // Get the current heading from the sensor, then notify the listeners of the
        // change.
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
                SensorManager.AXIS_Z, mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, mOrientation);

        // Store the pitch (used to display a message indicating that the user's head
        // angle is too steep to produce reliable results.
        mPitch = (float) Math.toDegrees(mOrientation[1]);

        // Convert the heading (which is relative to magnetic north) to one that is
        // relative to true north, using the user's current location to compute this.
        float magneticHeading = (float) Math.toDegrees(mOrientation[0]);
        mHeading = MathUtils.mod(computeTrueNorth(magneticHeading), 360.0f)
                - ARM_DISPLACEMENT_DEGREES;

        notifyOrientationChanged();
    }
}
 
Example 2
Source File: CompassView.java    From android with Apache License 2.0 6 votes vote down vote up
private void configureDeviceAngle() {
    switch (mDeviceOrientation) {
        case Surface.ROTATION_0: // Portrait
            SensorManager.remapCoordinateSystem(mRotation, SensorManager.AXIS_X,
                    SensorManager.AXIS_Y, mRotationMapped);
            break;
        case Surface.ROTATION_90: // Landscape
            SensorManager.remapCoordinateSystem(mRotation, SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_Z, mRotationMapped);
            break;
        case Surface.ROTATION_180: // Portrait
            SensorManager.remapCoordinateSystem(mRotation, SensorManager.AXIS_MINUS_X,
                    SensorManager.AXIS_MINUS_Y, mRotationMapped);
            break;
        case Surface.ROTATION_270: // Landscape
            SensorManager.remapCoordinateSystem(mRotation, SensorManager.AXIS_MINUS_Y,
                    SensorManager.AXIS_Z, mRotationMapped);
            break;
    }
}
 
Example 3
Source File: OrientationData.java    From 30-android-libraries-in-30-days with Apache License 2.0 6 votes vote down vote up
private void updateOrientation() {
		SensorManager.getRotationMatrix(this.mRotationMatrix, null, this.mAccelerationValues, this.mMagneticFieldValues);

		// TODO Use dont't use identical matrixes in remapCoordinateSystem, due to performance reasons.
		switch(this.mDisplayRotation) {
			case Surface.ROTATION_0:
				/* Nothing. */
				break;
			case Surface.ROTATION_90:
				SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, this.mRotationMatrix);
				break;
//			case Surface.ROTATION_180:
//				SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_?, SensorManager.AXIS_?, this.mRotationMatrix);
//				break;
//			case Surface.ROTATION_270:
//				SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_?, SensorManager.AXIS_?, this.mRotationMatrix);
//				break;
		}

		final float[] values = this.mValues;
		SensorManager.getOrientation(this.mRotationMatrix, values);

		for(int i = values.length - 1; i >= 0; i--) {
			values[i] = values[i] * MathConstants.RAD_TO_DEG;
		}
	}
 
Example 4
Source File: RotationAngleDetector.java    From sensey with Apache License 2.0 6 votes vote down vote up
@Override
protected void onSensorEvent(SensorEvent sensorEvent) {
    // Get rotation matrix
    float[] rotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values);

    // Remap coordinate system
    float[] remappedRotationMatrix = new float[16];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            remappedRotationMatrix);

    // Convert to orientations
    float[] orientations = new float[3];
    SensorManager.getOrientation(remappedRotationMatrix, orientations);

    // Convert values in radian to degrees
    for (int i = 0; i < 3; i++) {
        orientations[i] = (float) (Math.toDegrees(orientations[i]));
    }

    rotationAngleListener.onRotation(orientations[0], orientations[1], orientations[2]);
}
 
Example 5
Source File: BeerSwipeRefreshLayout.java    From BeerSwipeRefresh 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 || !mBeerView.isMax()) {
    return;
  }

  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);
  }
}
 
Example 6
Source File: OrientationManager.java    From PTVGlass with MIT License 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        // Get the current heading from the sensor, then notify the listeners of the
        // change.
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
                SensorManager.AXIS_Z, mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, mOrientation);

        // Store the pitch (used to display a message indicating that the user's head
        // angle is too steep to produce reliable results.
        mPitch = (float) Math.toDegrees(mOrientation[1]);

        // Convert the heading (which is relative to magnetic north) to one that is
        // relative to true north, using the user's current location to compute this.
        float magneticHeading = (float) Math.toDegrees(mOrientation[0]);
        mHeading = MathUtils.mod(computeTrueNorth(magneticHeading), 360.0f)
                - ARM_DISPLACEMENT_DEGREES;

        notifyOrientationChanged();
    }
}
 
Example 7
Source File: GyroscopeController.java    From crazyflie-android-client with GNU General Public License v2.0 6 votes vote down vote up
private void update(float[] vectors) {
    int AMP_MAX = 50;
    int AMPLIFICATION = AMP_MAX / mControls.getGyroAmplification();
    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
    int worldAxisX = SensorManager.AXIS_X;
    int worldAxisY = SensorManager.AXIS_Y;
    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisY, adjustedRotationMatrix);
    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
    float pitch = (orientation[2] * FROM_RADS_TO_DEGS * -1) / AMPLIFICATION;
    float roll = (orientation[1] * FROM_RADS_TO_DEGS) / AMPLIFICATION;
    mSensorRoll = roll;
    mSensorPitch = pitch;
    updateFlightData();
}
 
Example 8
Source File: LocationProvider.java    From open-location-code with Apache License 2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    float rotationMatrix[] = new float[16];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
    float[] orientationValues = new float[3];
    readDisplayRotation();
    SensorManager.remapCoordinateSystem(rotationMatrix, mAxisX, mAxisY, rotationMatrix);
    SensorManager.getOrientation(rotationMatrix, orientationValues);
    double azimuth = Math.toDegrees(orientationValues[0]);
    // Azimuth values are now -180-180 (N=0), but once added to the location object
    // they become 0-360 (N=0).
    @SuppressLint("UseValueOf") Float newBearing = new Float(azimuth);
    if (mBearing == null || Math.abs(mBearing - newBearing) > MIN_BEARING_DIFF) {
        mBearing = newBearing;
        if (mCurrentBestLocation != null) {
            mCurrentBestLocation.setBearing(mBearing);
        }
        mLocationCallback.handleNewBearing(mBearing);
    }
}
 
Example 9
Source File: CompassActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private float[] calculateOrientation(float[] values) {
  float[] rotationMatrix = new float[9];
  float[] remappedMatrix = new float[9];
  float[] orientation = new float[3];

  // Determine the rotation matrix
  SensorManager.getRotationMatrixFromVector(rotationMatrix, values);

  // Remap the coordinates based on the natural device orientation.
  int x_axis = SensorManager.AXIS_X;
  int y_axis = SensorManager.AXIS_Y;
  switch (mScreenRotation) {
    case (Surface.ROTATION_90):
      x_axis = SensorManager.AXIS_Y;
      y_axis = SensorManager.AXIS_MINUS_X;
      break;
    case (Surface.ROTATION_180):
      y_axis = SensorManager.AXIS_MINUS_Y;
      break;
    case (Surface.ROTATION_270):
      x_axis = SensorManager.AXIS_MINUS_Y;
      y_axis = SensorManager.AXIS_X;
      break;
    default: break;
  }

  SensorManager.remapCoordinateSystem(rotationMatrix,
    x_axis, y_axis,
    remappedMatrix);

  // Obtain the current, corrected orientation.
  SensorManager.getOrientation(remappedMatrix, orientation);

  // Convert from Radians to Degrees.
  values[0] = (float) Math.toDegrees(orientation[0]);
  values[1] = (float) Math.toDegrees(orientation[1]);
  values[2] = (float) Math.toDegrees(orientation[2]);
  return values;
}
 
Example 10
Source File: AndroidSensorJoyInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean remapCoordinates(float[] inR, float[] outR) {
        int xDir = SensorManager.AXIS_X;
        int yDir = SensorManager.AXIS_Y;
        int curRotation = getScreenRotation();
        if (lastRotation != curRotation) {
            logger.log(Level.FINE, "Device Rotation changed to: {0}", curRotation);
        }
        lastRotation = curRotation;

//        logger.log(Level.FINE, "Screen Rotation: {0}", getScreenRotation());
        switch (getScreenRotation()) {
            // device natural position
            case Surface.ROTATION_0:
                xDir = SensorManager.AXIS_X;
                yDir = SensorManager.AXIS_Y;
                break;
            // device rotated 90 deg counterclockwise
            case Surface.ROTATION_90:
                xDir = SensorManager.AXIS_Y;
                yDir = SensorManager.AXIS_MINUS_X;
                break;
            // device rotated 180 deg counterclockwise
            case Surface.ROTATION_180:
                xDir = SensorManager.AXIS_MINUS_X;
                yDir = SensorManager.AXIS_MINUS_Y;
                break;
            // device rotated 270 deg counterclockwise
            case Surface.ROTATION_270:
                xDir = SensorManager.AXIS_MINUS_Y;
                yDir = SensorManager.AXIS_X;
                break;
            default:
                break;
        }
        return SensorManager.remapCoordinateSystem(inR, xDir, yDir, outR);
    }
 
Example 11
Source File: DeviceOrientation.java    From ARCore-Location with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    // Get the device heading
    float degree = Math.round( event.values[0] );
    currentDegree = -degree;

    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) {
        gravity = new float[9];
        magnetic = new float[9];
        SensorManager.getRotationMatrix(gravity, magnetic, accels, mags);
        float[] outGravity = new float[9];
        SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X,SensorManager.AXIS_Z, outGravity);
        SensorManager.getOrientation(outGravity, values);

        azimuth = values[0] * 57.2957795f;
        pitch = values[1] * 57.2957795f;
        roll = values[2] * 57.2957795f;
        mags = null;
        accels = null;
    }
}
 
Example 12
Source File: Drawer.java    From meter with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mAccelerometer) {
        System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
        mLastAccelerometerSet = true;
    } else if (event.sensor == mMagnetometer) {
        System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
        mLastMagnetometerSet = true;
    }
    if (mLastAccelerometerSet && mLastMagnetometerSet) {
        SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);

        try {
            mDisplay = ((WindowManager) ((WallpaperService) context).getApplication().getSystemService(Service.WINDOW_SERVICE))
                    .getDefaultDisplay();
        } catch (Exception ignored){}


        int rotation = Surface.ROTATION_0;
        if(mDisplay != null) {
            rotation = mDisplay.getRotation();
        }

        float[] mRremap = mR.clone();
        if(rotation == Surface.ROTATION_90){
            SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mRremap);
        }
        if(rotation == Surface.ROTATION_270){
            SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, mRremap);
        }
        if(rotation == Surface.ROTATION_180){
            SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, mRremap);
        }

        SensorManager.getOrientation(mRremap, mOrientation);
    }
}
 
Example 13
Source File: DeviceOrientation.java    From ARCore-Location with MIT License 5 votes vote down vote up
@SuppressWarnings("SuspiciousNameCombination")
private void processSensorOrientation(float[] rotation) {
    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, rotation);
    final int worldAxisX;
    final int worldAxisY;

    switch (windowManager.getDefaultDisplay().getRotation()) {
        case Surface.ROTATION_90:
            worldAxisX = SensorManager.AXIS_Z;
            worldAxisY = SensorManager.AXIS_MINUS_X;
            break;
        case Surface.ROTATION_180:
            worldAxisX = SensorManager.AXIS_MINUS_X;
            worldAxisY = SensorManager.AXIS_MINUS_Z;
            break;
        case Surface.ROTATION_270:
            worldAxisX = SensorManager.AXIS_MINUS_Z;
            worldAxisY = SensorManager.AXIS_X;
            break;
        case Surface.ROTATION_0:
        default:
            worldAxisX = SensorManager.AXIS_X;
            worldAxisY = SensorManager.AXIS_Z;
            break;
    }
    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX,
            worldAxisY, adjustedRotationMatrix);

    // azimuth/pitch/roll
    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);

    this.orientation = ((float) Math.toDegrees(orientation[0]) + 360f) % 360f;
}
 
Example 14
Source File: HeadScrollView.java    From glass_snippets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
	float[] mat = new float[9],
			orientation = new float[3];

	if (mLastAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
		return;

	SensorManager.getRotationMatrixFromVector(mat, event.values);
	SensorManager.remapCoordinateSystem(mat, SensorManager.AXIS_X, SensorManager.AXIS_Z, mat);
	SensorManager.getOrientation(mat, orientation);

	float z = orientation[0], // see https://developers.google.com/glass/develop/gdk/location-sensors/index
	      x = orientation[1],
		  y = orientation[2];

	if (mStartX == 10)
		mStartX = x;

	float mEndX = mStartX - (getChildAt(0).getHeight() - getHeight() * 0.5F) / VELOCITY;

	int prior = getScrollY(),
	      pos = (int) ((mStartX - x) * VELOCITY);

	if (x < mStartX) mStartX = x;
	else if (x > mEndX) mStartX += x - mEndX;

	smoothScrollTo(0, pos);
}
 
Example 15
Source File: ARSurfaceView.java    From geoar-app with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the Transformation from device to world coordinates
 */
private void computeRotationMatrix() {
	synchronized (rotMatrix) {
		if (magnetValues.hasValues() && accelValues.hasValues()) {
			SensorManager.getRotationMatrix(rotMatrixSensor, null,
					accelValues.get(), magnetValues.get());
			// transforms from sensor to world

			switch (display.getOrientation()) {
			case Surface.ROTATION_0:
				// No adjustment
				SensorManager.remapCoordinateSystem(rotMatrixSensor,
						SensorManager.AXIS_X, SensorManager.AXIS_Y,
						rotMatrix);
				break;
			case Surface.ROTATION_90:
				SensorManager.remapCoordinateSystem(rotMatrixSensor,
						SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
						rotMatrix);
				break;
			case Surface.ROTATION_180:
				SensorManager.remapCoordinateSystem(rotMatrixSensor,
						SensorManager.AXIS_MINUS_X,
						SensorManager.AXIS_MINUS_Y, rotMatrix);
				break;
			case Surface.ROTATION_270:
				SensorManager.remapCoordinateSystem(rotMatrixSensor,
						SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
						rotMatrix);
				break;
			}
			// transforms from device to world

			Matrix.rotateM(rotMatrix, 0, 90, 1, 0, 0);
			// Account for the upward usage of this app
		}
	}
}
 
Example 16
Source File: CameraFocusActivity.java    From MeasureCam with MIT License 4 votes vote down vote up
private float getDirection() 
{    	
    float[] temp = new float[9];
    float[] R = new float[9];
            
    //Load rotation matrix into R
    SensorManager.getRotationMatrix(temp, null, mGravity, mMagnetic);
   
    //Remap to camera's point-of-view
    SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_X, SensorManager.AXIS_Z, R);
   
    //Return the orientation values
    
    SensorManager.getOrientation(R, value);
            
    //value[0] - Z, value[1]-X, value[2]-Y in radians
          
    return value[1];       //return x
}
 
Example 17
Source File: SensorInterpreter.java    From motion with Apache License 2.0 4 votes vote down vote up
/**
 * Converts sensor data in a {@link SensorEvent} to yaw, pitch, and roll.
 *
 * @param context the context of the
 * @param event   the event to interpret
 * @return an interpreted vector of yaw, pitch, and roll delta values
 */
@SuppressWarnings("SuspiciousNameCombination")
public float[] interpretSensorEvent(@NonNull Context context, @Nullable SensorEvent event) {
    if (event == null) {
        return null;
    }

    // Retrieves the RotationVector from SensorEvent
    float[] rotationVector = getRotationVectorFromSensorEvent(event);

    // Set target rotation if none has been set
    if (!mTargeted) {
        setTargetVector(rotationVector);
        return null;
    }

    // Get rotation matrix from event's values
    SensorManager.getRotationMatrixFromVector(mRotationMatrix, rotationVector);

    // Acquire rotation of screen
    final int rotation = ((WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay()
            .getRotation();

    // Calculate angle differential between target and current orientation
    if (rotation == Surface.ROTATION_0) {
        SensorManager.getAngleChange(mTiltVector, mRotationMatrix, mTargetMatrix);
    } else {
        // Adjust axes on screen orientation by remapping coordinates
        switch (rotation) {
            case Surface.ROTATION_90:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_Y, AXIS_MINUS_X, mOrientedRotationMatrix);
                break;

            case Surface.ROTATION_180:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_MINUS_X, AXIS_MINUS_Y, mOrientedRotationMatrix);
                break;

            case Surface.ROTATION_270:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_MINUS_Y, AXIS_X, mOrientedRotationMatrix);
                break;
        }

        SensorManager.getAngleChange(mTiltVector, mOrientedRotationMatrix, mTargetMatrix);
    }

    // Perform value scaling and clamping on value array
    for (int i = 0; i < mTiltVector.length; i++) {
        // Map domain of tilt vector from radian (-PI, PI) to fraction (-1, 1)
        mTiltVector[i] /= Math.PI;

        // Adjust for tilt sensitivity
        mTiltVector[i] *= mTiltSensitivity;

        // Clamp values to image bounds
        if (mTiltVector[i] > 1) {
            mTiltVector[i] = 1f;
        } else if (mTiltVector[i] < -1) {
            mTiltVector[i] = -1f;
        }
    }

    return mTiltVector;
}
 
Example 18
Source File: ShaderRenderer.java    From ShaderEditor with MIT License 4 votes vote down vote up
private void setRotationMatrix() {
	boolean haveInclination = false;
	if (gravityListener != null && magneticFieldListener != null &&
			SensorManager.getRotationMatrix(
					rotationMatrix,
					inclinationMatrix,
					gravityValues,
					magneticFieldListener.filtered)) {
		haveInclination = true;
	} else if (rotationVectorListener != null) {
		SensorManager.getRotationMatrixFromVector(
				rotationMatrix,
				rotationVectorListener.values);
	} else {
		return;
	}
	if (deviceRotation != 0) {
		int x = SensorManager.AXIS_Y;
		int y = SensorManager.AXIS_MINUS_X;
		switch (deviceRotation) {
			default:
				break;
			case 270:
				x = SensorManager.AXIS_MINUS_Y;
				y = SensorManager.AXIS_X;
				break;
		}
		SensorManager.remapCoordinateSystem(
				rotationMatrix,
				x,
				y,
				rotationMatrix);
	}
	if (rotationMatrixLoc > -1) {
		GLES20.glUniformMatrix3fv(rotationMatrixLoc, 1, true,
				rotationMatrix, 0);
	}
	if (orientationLoc > -1) {
		SensorManager.getOrientation(rotationMatrix, orientation);
		GLES20.glUniform3fv(orientationLoc, 1, orientation, 0);
	}
	if (inclinationMatrixLoc > -1 && haveInclination) {
		GLES20.glUniformMatrix3fv(inclinationMatrixLoc, 1, true,
				inclinationMatrix, 0);
	}
	if (inclinationLoc > -1 && haveInclination) {
		GLES20.glUniform1f(inclinationLoc,
				SensorManager.getInclination(inclinationMatrix));
	}
}
 
Example 19
Source File: SensorInterpreter.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("SuspiciousNameCombination")
public float[] interpretSensorEvent(@NonNull Context context, @Nullable SensorEvent event) {
    if (event == null) {
        return null;
    }

    float[] rotationVector = getRotationVectorFromSensorEvent(event);

    if (!mTargeted) {
        setTargetVector(rotationVector);
        return null;
    }

    SensorManager.getRotationMatrixFromVector(mRotationMatrix, rotationVector);

     int rotation = ((WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay()
            .getRotation();

    if (rotation == Surface.ROTATION_0) {
        SensorManager.getAngleChange(mTiltVector, mRotationMatrix, mTargetMatrix);
    } else {
        switch (rotation) {
            case Surface.ROTATION_90:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_Y, AXIS_MINUS_X, mOrientedRotationMatrix);
                break;

            case Surface.ROTATION_180:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_MINUS_X, AXIS_MINUS_Y, mOrientedRotationMatrix);
                break;

            case Surface.ROTATION_270:
                SensorManager.remapCoordinateSystem(mRotationMatrix, AXIS_MINUS_Y, AXIS_X, mOrientedRotationMatrix);
                break;
        }

        SensorManager.getAngleChange(mTiltVector, mOrientedRotationMatrix, mTargetMatrix);
    }

    for (int i = 0; i < mTiltVector.length; i++) {
        mTiltVector[i] /= Math.PI;

        mTiltVector[i] *= mTiltSensitivity;

        if (mTiltVector[i] > 1) {
            mTiltVector[i] = 1f;
        } else if (mTiltVector[i] < -1) {
            mTiltVector[i] = -1f;
        }
    }

    return mTiltVector;
}
 
Example 20
Source File: AeyriumSensorPlugin.java    From aeyrium-sensor with MIT License 4 votes vote down vote up
private void updateOrientation(float[] rotationVector, EventChannel.EventSink events) {
  float[] rotationMatrix = new float[9];
  SensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector);

  final int worldAxisForDeviceAxisX;
  final int worldAxisForDeviceAxisY;

  // Remap the axes as if the device screen was the instrument panel,
  // and adjust the rotation matrix for the device orientation.
  switch (mWindowManager.getDefaultDisplay().getRotation()) {
    case Surface.ROTATION_0:
    default:
      worldAxisForDeviceAxisX = SensorManager.AXIS_X;
      worldAxisForDeviceAxisY = SensorManager.AXIS_Z;
      break;
    case Surface.ROTATION_90:
      worldAxisForDeviceAxisX = SensorManager.AXIS_Z;
      worldAxisForDeviceAxisY = SensorManager.AXIS_MINUS_X;
      break;
    case Surface.ROTATION_180:
      worldAxisForDeviceAxisX = SensorManager.AXIS_MINUS_X;
      worldAxisForDeviceAxisY = SensorManager.AXIS_MINUS_Z;
      break;
    case Surface.ROTATION_270:
      worldAxisForDeviceAxisX = SensorManager.AXIS_MINUS_Z;
      worldAxisForDeviceAxisY = SensorManager.AXIS_X;
      break;
  }

  
  float[] adjustedRotationMatrix = new float[9];
  SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisForDeviceAxisX,
          worldAxisForDeviceAxisY, adjustedRotationMatrix);

  // Transform rotation matrix into azimuth/pitch/roll
  float[] orientation = new float[3];
  SensorManager.getOrientation(adjustedRotationMatrix, orientation);

  double pitch = - orientation[1];
  double roll = - orientation[2];
  double[] sensorValues = new double[2];
  sensorValues[0] = pitch;
  sensorValues[1] = roll;
  events.success(sensorValues);
}