Need help finding algorithm to fix specified problem

48 Views Asked by At

First I want to say that I am not a mathematician, so asking a question in this area is not easy for me. So I will describe the issue in my words which is not the nice way.

So this is what I do: I have a phone which when moved gives me the orientation angle of the phone. Lets say when I start the application and the phone is turned for $45$ Degrees. I will get the correct result but what I want to do is to calibrate the $45$ degrees and get $0$ degrees and measure the orientation for that calibrated point.

The sensor gives me the rotation angle from $0$ to $2\pi$ and then from $-2\pi$ to $0$ or from $0$ to $180$ and then from $-180$ to $0$ $(0,90,180,-180,-90,0)$.

So what I tried is: take the first position of the sensor lets say it reads $45$ degrees, and make the conjugate function of it (-45) and add it to my current measurements it works until I don't reach $179$ ($179-45 = 139$ which is fine) but then if I turn the phone little more I get

$$-179 - 40 = -219$$

which is not the desired result.

I am sure many people have had this problem before, I hope is a algorithm which solves this issue.

Hope I could explain my issue correctly.

Thank you for taking time to read this.

2

There are 2 best solutions below

1
On BEST ANSWER

The concept you are talking about is called modular arithmetic. http://en.wikipedia.org/wiki/Modular_arithmetic

The modulo function may be useful in your algorithm. http://en.wikipedia.org/wiki/Modulo_operation

You may be able to use mod 180 arithmetic in your algorithm. However, beware that some modulo functions return only positive values, so you might have to use mod 360 arithmetic and subtract 180 to get you into the range of -180 to 180.

4
On

The answer to this problem is to take data often enough that you know when you wrap around. As long as the phone never moves more than $180$ degrees between samples, you can just add or subtract $360$ degrees to get in range. Otherwise, there is basically no hope. If you don't know when you went $+190$ and when you went $-170$ you are lost.