I am trying to get the device tilted (turning the device on the Y axis, but unfortunately I canโt achieve my goal. I tried many things using TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD as a combined sensor (merging sensors). After Motion sensors
What I want?
I want to get the tilt of a device (cell phone) attached to a vehicle. Say I attached a device in a car and the car is stationary. Thus, the slope is 0 degrees. When the vehicle is switched on through walkways or overpasses, the slope should be appropriate. I tried to calculate this, here is my code:
... ... private static float ALPHA = 0.005f TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); tv = (TextView) findViewById(R.id.tv); edtAlpha = (EditText) findViewById(R.id.alpha); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accelerometer = mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetometer = mSensorManager .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); if (accelerometer == null) { Toast.makeText(this, "Oh not found", Toast.LENGTH_SHORT).show(); } if (magnetometer == null) { Toast.makeText(this, "Oh magnetometer not found", Toast.LENGTH_SHORT).show(); } } protected float[] lowPass(float[] input, float[] output) { if (output == null) return input; String s = edtAlpha.getText().toString(); if (s != null && s.length() > 0) { try { ALPHA = Float.valueOf(s); } catch (NumberFormatException e) { ALPHA = 0.005f; } } else { ALPHA = 0.005f; } for (int i = 0; i < input.length; i++) { output[i] = output[i] + ALPHA * (input[i] - output[i]); } return output; } public int getRotation(final Activity activity) { int result = 1; Method mDefaultDisplay_getRotation; try { mDefaultDisplay_getRotation = Display.class.getMethod( "getRotation", new Class[] {}); Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay(); Object retObj = mDefaultDisplay_getRotation.invoke(display); if (retObj != null) { result = (Integer) retObj; } } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } @Override public void onSensorChanged(SensorEvent event) { Log.d(tag, "onSensorChanged"); if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { gravSensorVals = lowPass(event.values.clone(), gravSensorVals); } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { magSensorVals = lowPass(event.values.clone(), magSensorVals); } if (gravSensorVals != null && magSensorVals != null) { SensorManager.getRotationMatrix(RTmp, I, gravSensorVals, magSensorVals); int rotation = getRotation(this); if (rotation == 1) { SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, Rot); } else { SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_Z, Rot); } SensorManager.getOrientation(Rot, results); float azimuth = (float) (((results[0] * 180) / Math.PI) + 180); float pitch = (float) (((results[1] * 180 / Math.PI)) + 90); float roll = (float) (((results[2] * 180 / Math.PI))); tv.setText("Azimuth : " + df.format(azimuth) + "\nPitch : " + df.format(pitch) + "\nRoll : " + df.format(roll)); } Log.d(tag, "Sensor type : " + event.sensor.getType()); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI); } ... ...
What is the problem with my code?
When I quickly accelerate / de-sphere my car, the angle quickly increases / decreases, but this should not. In other words, when I accelerate / decaf a car, there should be no effect on the angle. I also tried following these tutorials: Link 1 Link 2 , etc.