4

I need to get exact angle of my android device from each axis.

First I should say I searched through web and also stackoverflow and I saw pages like these:

How to measure the tilt of the phone in XY plane using accelerometer in Android

Get Android rotation angle in x-axis

But they were not helpful. I used some of them but they give wrong values.

If the axis in device will be like this:

enter image description here

I need to get these rotation angle:

enter image description here

I don't want to use any hardware sensor except accelerometer and I work with API 17. I think this is possible based on racing games, but I don't know how to do that.

I will appreciate if you help me. Thanks

Community
  • 1
  • 1
Taher
  • 1,045
  • 2
  • 16
  • 33
  • You need to refer this. http://developer.android.com/reference/android/hardware/SensorEvent.html – Murtaza Khursheed Hussain Dec 02 '14 at 07:24
  • As I said previously, I just want to get orientation, using accelerometer. I don't have any other hardware sensor. Also please consider that TYPE_ORIENTATION is deprecated in API level 8 and also I don't have access to sensors which belong to API 20 like STRING_TYPE_LINEAR_ACCELERATION. – Taher Dec 02 '14 at 12:40

1 Answers1

1

There is no way you can get all the rotations using TYPE_ACCELEROMETER alone. You also need TYPE_MAGNETIC_FIELD. If you do not care about how sensitive the values changed with respect to quick movement then you need TYPE_GRAVITY or TYPE_ACCELEROMETER together with TYPE_MAGNETIC_FIELD.
For rotation with respect to the z-axis you can follow the first link in your question.
For rotation with respect to the x and y axes you can use pitch and roll and screen facing.

Hoan Nguyen
  • 17,479
  • 3
  • 47
  • 51
  • Thanks. Do I need to have another hardware sensor to access TYPE_MAGNETIC_FIELD? – Taher Dec 03 '14 at 06:29
  • Normally most phone has TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD. If there is TYPE_GRAVITY then use it instead of TYPE_ACCELEROMETER. If you use type TYPE_ACCELEROMETER then you have to low pass filter to filter out the x and y accelerations. – Hoan Nguyen Dec 03 '14 at 06:32
  • I checked them and unfortunately, I don't have access to TYPE_MAGNETIC_FIELD and TYPE_GRAVITY. – Taher Dec 05 '14 at 08:42
  • What kind of device do you have? It is very rare that an android device does not have TYPE_MAGNETIC_FIELD. – Hoan Nguyen Dec 05 '14 at 08:56
  • HTC Desire 310 with android 4.2.2. I wrote simple code to check it: Sensor mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); and onSensorChanged method I used this code: if (event.sensor == mMagnetometer) { Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show(); } I used same code to check my accelerometer and it worked. – Taher Dec 05 '14 at 14:05
  • Without TYPE_MAGNATIC_FIELD you will not be able to obtain the information you wish as state in my answer. For example if the device is lying flat and start rotating. So in this case it is rotation around z-axis. To find the rotation you need to find the direction of the y-axis with respect to magnetic north. The calculation requires TYPE_MAGNETIC_FIELD and TYPE_ACCELEROMETER. – Hoan Nguyen Dec 05 '14 at 22:59