0

I'm trying to retrieve my current heading from an android device using Delphi Rad 10.1 Berlin.

There is a function in the OrientationSensor that is True Heading, however, this is only enabled on Windows, according to the Embarcadero knowledgebase.

So, I think to do it, I need to convert the following Variables into one heading.

OrientationSensor1.Sensor.HeadingX
OrientationSensor1.Sensor.HeadingY
OrientationSensor1.Sensor.HeadingZ

As I only need heading(and don't care about altitude), I believe I can disregard Z.

In return I need to retrieve current heading which should be from 0-360.

I used a formula I found online which is :-

angle = atan2(Y, X);

Which seemed to help, but was wildly inaccurate at some positions, and was negative at others.

Any help or advice would be appreciated.

Some details that may help are :

It's a Multi-Device Application in Delphi.

It's only going on Android Devices(and also only being tested on them).

Thanks in advance.

M J
  • 13
  • 3
  • FWIW, atan2 is expected to be negative for some inputs. And what's wrong with that? If you think that is unreasonable then you need to step back and get a better grasp of basic trig before you proceed. Don't try to solve the problem in state of ignorance. Understand any potential solution. This copy paste without thought culture won't do you any good. – David Heffernan Mar 10 '17 at 07:46
  • Hi David, As i require heading in degrees, it should be positive for what i need it for, I appreciate what you mean, and I will be the first to put my hands up and admit i'm terrible at trig and therefore I have turned to the internet for help, that doesn't mean there wasn't 3 or 4 days of pain before resorting to it. – M J Mar 10 '17 at 17:29
  • -30 is the same as 330, is the same as -390. Heading is periodic. Think what this means. – David Heffernan Mar 10 '17 at 17:41

1 Answers1

0

Don't discard HeadingZ.

These tree headings are not relative to world surface but instead relative to your device orientation and tilt.

So in order to get true heading you will have to take into account heading for all three axis and also tilt information for all three axis.

You can read more information about calculating heading here: https://stackoverflow.com/a/1055051/3636228

Yes linked answer is for Objective C but math behind is same for every programming language.

Community
  • 1
  • 1
SilverWarior
  • 7,372
  • 2
  • 16
  • 22