Android Question compass heading for the vertical phone

peacemaker

Expert
Licensed User
Longtime User
How to calculate the heading, if the phone is vertically placed ? I guess from magnetical sensor ?
 

peacemaker

Expert
Licensed User
Longtime User
Thanks, but what are the final digits ? Degree angles ?
I need the compass heading, but not by ORIENTATION sensor at horizontal phone position - but the same compass heading if phne is vertically placed.
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Ideally, one would use accellerometer sensors to determine device rotation, and then use that to compensate the compass. Probably not that hard, but I'm too tired to do the math at the moment.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Yes, i'm getting phone orientation already, so question is only how to compensate the compass values
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
It seems to stupid me, that just the Z value of ORIENTATION sensor is all i need :)
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Well, it all depends. If you are only using the phone in a perfectly upright position, that's enough. However, users frequently tilt the device, so it mihgt be prudent to use a full orientation.

What you need to do is to use accelerometers to find down. From down, it's easy to find forward, it's 90 degrees off down in the forward/up direction.

Then, you get the three orientation values to get a 3D vector. Project that vector on the forward direction, and you'll have a heading that works regardless of device orientation.

However, be aware that the magnetic sensor is usually extremely fickle, don't rely on it to be accurate, even under the best of circumstances. If the device is moving, you'll get a much more accurate reading from the GPS, which simply calculates the direction between current and previous positions.
 
Upvote 0
Top