View Single Post
  #1   Spotlight this post!  
Unread 05-12-2016, 09:16 PM
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,069
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: NavX MXP Continuous Angle to Calculate Derivative

This year on 254 we switched to using a simple Rotation2d class for all angles (because, among other reasons, dealing with angle rollover as you did here is easy to screw up). Internally, this class stores the sine and cosine of an angle explicitly. This has some nice properties:
  • We use static utility methods to create a Rotation2d from a vector (x,y -> cos,sin after normalization), absolute angle in radians, or absolute angle in degrees.
  • We have accessors that return the absolute angle in degrees or radians (wrapped to -Pi to Pi).
  • We have "Inverse()" and "RotateBy(Rotation2d other)" methods for creating a new Rotation2d:
    Code:
    Rotation2d Inverse() {
      return new Rotation2d(cos_angle, -sin_angle);
    }
    
    Rotation2d RotateBy(Rotation2d other) {
      return new Rotation2d(cos_angle * other.cos_angle - sin_angle * other.sin_angle,
                    cos_angle * other.sin_angle + sin_angle * other.cos_angle);
    }
  • If you want to know the shortest rotation from A to B, you can do something like:
    Code:
    double shortest_distance_degrees = A.Inverse().RotateBy(B).ToDegrees();
    This will always give you a solution on [-180, 180].
  • Cheap access to the sine, cosine, and tangent (sin/cos) of the angle because they are already cached.
Reply With Quote