Thread: PID crossing 0
View Single Post
  #23   Spotlight this post!  
Unread 27-05-2011, 15:50
Strants Strants is offline
Registered User
AKA: Gavin Stewart
FRC #1977
Team Role: College Student
 
Join Date: Dec 2010
Rookie Year: 2010
Location: Colorado
Posts: 11
Strants is an unknown quantity at this point
Re: PID crossing 0

Quote:
Originally Posted by ratdude747 View Post
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.
Reply With Quote