1

I want to find the Azimuth angle of device when It is not FLAT to the ground.

Here is an already existing Answer but not complete for me:

Here is the accepted answer on this question, which is almost same. Inconsistent orientation sensor values on Android for azimuth/yaw and roll

Problem:

But in this question, accepted answer gives azimuth value in the range -89 to 89. I want to calculate Azimuth angle value in range 0-359 degree, when moving from Magnetic North. How can I calculate that in Android Sensors?

Community
  • 1
  • 1
Master
  • 2,945
  • 5
  • 34
  • 65

2 Answers2

2

When the device is not flat and if you want your compass direction to be the negative of the direction of the device z-axis (i.e the direction you are looking at) then the azimuth can be calculate by first call

remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);

Then call

getOrientation (float[] R, float[] values)

passing in outR for the parameter R.

This amount to project the z-axis into the World xy plane and then calculate the angle between this projection and the World y-axis which is magnetic north.

The z-axis point in the same direction when the device is rotated around this axis, thus the azimuth should not change. Of course it will vary a little due to acceleration.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
0

Found its answer:

Calculate azimuth angle like this:

mAzimuthAngleNotFlat = (int) Math.toDegrees(Math
                    .atan2((rotationMatrix[1] - rotationMatrix[3]), (rotationMatrix[0] + rotationMatrix[4])));

To convert it to 0 to 359 simply apply this:

    if (mAzimuthAngleNotFlat < 0) {
        mAzimuthAngleNotFlat += 360;
    }

Then you are done. And this angle will be accurate when device is not flat. For more, you can see discussion on accepted answer.

Inconsistent orientation sensor values on Android for azimuth/yaw and roll

Community
  • 1
  • 1
Master
  • 2,945
  • 5
  • 34
  • 65
  • What does this really calculate? I put it in my code, hold the phone vertical and start rotating about the z-axis, the mAzimuthAngleNotFlat value wildly change. For example, initially the angle is 260, rotating clockwise by 90 degree give around 360 and the value either stay pretty much the same when keep rotating or change to value < 90 and suddenly when the phone is upside down it is now about 270. If it is any consistent it should be 90. – Hoan Nguyen Mar 24 '14 at 09:54
  • If the value is in between -89 and 89 how can you get value in the range of 91 - 269 by adding 360? – Hoan Nguyen Mar 24 '14 at 10:07
  • It calculates azimuth when device is not flat. – Master Mar 24 '14 at 11:49
  • azimuth of what? what is the direction with respect to magnetic north that you are interested? – Hoan Nguyen Mar 24 '14 at 17:11
  • Clockwise from North when device is not flat to the ground. – Master Mar 24 '14 at 17:13
  • Of what? The accepted answer you referred to above is wrong. I did not bother to verify at that time. Do you want to obtain the direction of the device y-axis, back camera direction or what? – Hoan Nguyen Mar 24 '14 at 17:20
  • Device will be facing you and you will be moving from North clockwise. The angle will keep on increasing from 0(North) in clockwise direction. The accepted answer is giving accurate value. – Master Mar 24 '14 at 17:38
  • Only if you hold the device in a portrait position. What happen if you rotate your device when moving around? That is from portrait to landscape. Actually a small rotation would change the value considerably. – Hoan Nguyen Mar 24 '14 at 18:31
  • 1
    Is that what you want? The user cannot even rotate the phone slightly? You do not seem to understand what I am at, you should look at my answer at http://stackoverflow.com/questions/22326720/compass-internal-working-in-android-devices/22341936#22341936 – Hoan Nguyen Mar 25 '14 at 04:05
  • So, can I get right azimuth in any Orientation mode, using your answer? – Master Mar 25 '14 at 04:25
  • You have to tell me what direction you want. What is the meaning of azimuth? For example, when the device is flat, the azimuth is the direction of the device y-axis. When the device is not flat what do you want azimuth to be the direction of? The azimuth by itself has no meaning. For example if I do AR, I want the azimuth to be the direction of the back camera, which is the negative direction of the device z-axis. So you have to tell me which direction you are interested in before I can tell you how to calculate the azimuth. – Hoan Nguyen Mar 25 '14 at 08:53
  • If you take a look at Orientation, it gives you Pitch, Roll and Azimuth. This azimuth is what I am calling angle from Magnetic North. Yes, I am only able to get exact angle from North but only when device is flat to the ground and device is kept portrait. In other cases I am getting this wrong. – Master Mar 25 '14 at 18:14
  • You did not get it wrong, you just interpret it wrong. The azimuth in this case calculate the direction of the device y-axis with respect to magnetic north. That is the angle between the World y-axis (magnetic north) and the projection of the device y-axis into the World xy plane. So if you rotate the device, the device y-coordinate change and so is the azimuth. I think if you are interested in the device z-coordinate when the device is not flat. Can you tell me what this is for? – Hoan Nguyen Mar 25 '14 at 19:08
  • Yes, exactly. I am going to use it for Compass. How can I calculate that. – Master Mar 26 '14 at 04:21