Quote:
Originally Posted by ratdude747
I used sin and asin since it always puts it in the correct range. you could also use cos and acos.
|
Actually, asin will always return a value between -pi/2 and pi/2, while acos will always return a value between 0 and pi. If you want an angle between 0 and 2pi, you have to consider both acos
and asin, as alone neither gives enough information to determine the angle*.
If you want to use trig, this method could work:
Let U be the raw angle given by the encoder, and Q be that same angle put in the range [0, 2pi].
If sin(U) >= 0, then Q = acos(cos(U))
Otherwise, Q = 2pi - acos(cos(U))
* In fact, for sin(x) = k, 0 <= x < 2pi, there are two possible values for x (barring x = pi/2 or 3pi/2). Likewise, cos(x) = m also has two values (barring x = 0 or x = pi). The possible value represented in both equations is the true value of x.