Java Code Examples for android.hardware.SensorManager#AXIS_X

The following examples show how to use android.hardware.SensorManager#AXIS_X . 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: StreamActivity.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
protected void update(float[] vectors) {
    int worldAxisX = SensorManager.AXIS_X;
    int worldAxisZ = SensorManager.AXIS_Z;

    float[] rotationMatrix = new float[9];
    float[] adjustedRotationMatrix = new float[9];
    float[] orientation = new float[3];

    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);

    float roll = orientation[2] * FROM_RADS_TO_DEGS;

    if (roll > -45 && roll < 45) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        Log.d(LOG_TAG, "Requesting undefined");
    }
    Log.d(LOG_TAG, "Roll: " + roll);
}
 
Example 2
Source File: StreamActivity.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 6 votes vote down vote up
protected void update(float[] vectors) {
	int worldAxisX = SensorManager.AXIS_X;
	int worldAxisZ = SensorManager.AXIS_Z;

	float[] rotationMatrix = new float[9];
	float[] adjustedRotationMatrix = new float[9];
	float[] orientation = new float[3];

	SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
	SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
	SensorManager.getOrientation(adjustedRotationMatrix, orientation);

	float roll = orientation[2] * FROM_RADS_TO_DEGS;

	if (roll > -45 && roll < 45) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
		Log.d(LOG_TAG, "Requesting undefined");
	}
	Log.d(LOG_TAG, "Roll: " + roll);
}
 
Example 3
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 4
Source File: LocationProvider.java    From open-location-code with Apache License 2.0 6 votes vote down vote up
/**
 * Read the screen rotation so we can correct the device heading.
 */
@SuppressWarnings("SuspiciousNameCombination")
private void readDisplayRotation() {
    mAxisX = SensorManager.AXIS_X;
    mAxisY = SensorManager.AXIS_Y;
    switch (mDisplay.getRotation()) {
        case Surface.ROTATION_0:
            break;
        case Surface.ROTATION_90:
            mAxisX = SensorManager.AXIS_Y;
            mAxisY = SensorManager.AXIS_MINUS_X;
            break;
        case Surface.ROTATION_180:
            mAxisY = SensorManager.AXIS_MINUS_Y;
            break;
        case Surface.ROTATION_270:
            mAxisX = SensorManager.AXIS_MINUS_Y;
            mAxisY = SensorManager.AXIS_X;
            break;
        default:
            break;
    }
}
 
Example 5
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 6
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 7
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void listing16_14() {
  float[] values = new float[3];
  float[] inR = new float[9];
  float[] outR = new float[9];

  SensorManager.getRotationMatrix(inR, null,
    mAccelerometerValues,
    mMagneticFieldValues);

  // Listing 16-14: Remapping the orientation reference frame based on the natural orientation of the device
  // Determine the current orientation relative to the natural orientation
  WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();

  int rotation = display.getRotation();
  int x_axis = SensorManager.AXIS_X;
  int y_axis = SensorManager.AXIS_Y;

  switch (rotation) {
    case (Surface.ROTATION_0): break;
    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(inR, x_axis, y_axis, outR);

  // Obtain the new, remapped, orientation values.
  SensorManager.getOrientation(outR, values);
}
 
Example 8
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 9
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);
}
 
Example 10
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));
	}
}