WPILibC++  2020.3.2
Rotation2d.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <units/units.h>
11 
12 namespace wpi {
13 class json;
14 } // namespace wpi
15 
16 namespace frc {
17 
22 class Rotation2d {
23  public:
27  constexpr Rotation2d() = default;
28 
34  Rotation2d(units::radian_t value); // NOLINT(runtime/explicit)
35 
43  Rotation2d(double x, double y);
44 
56  Rotation2d operator+(const Rotation2d& other) const;
57 
68  Rotation2d& operator+=(const Rotation2d& other);
69 
81  Rotation2d operator-(const Rotation2d& other) const;
82 
93  Rotation2d& operator-=(const Rotation2d& other);
94 
101  Rotation2d operator-() const;
102 
109  Rotation2d operator*(double scalar) const;
110 
117  bool operator==(const Rotation2d& other) const;
118 
125  bool operator!=(const Rotation2d& other) const;
126 
139  Rotation2d RotateBy(const Rotation2d& other) const;
140 
146  units::radian_t Radians() const { return m_value; }
147 
153  units::degree_t Degrees() const { return m_value; }
154 
160  double Cos() const { return m_cos; }
161 
167  double Sin() const { return m_sin; }
168 
174  double Tan() const { return m_sin / m_cos; }
175 
176  private:
177  units::radian_t m_value = 0_deg;
178  double m_cos = 1;
179  double m_sin = 0;
180 };
181 
182 void to_json(wpi::json& json, const Rotation2d& rotation);
183 
184 void from_json(const wpi::json& json, Rotation2d& rotation);
185 
186 } // namespace frc
wpi::json
a class to store JSON values
Definition: json.h:2655
frc::Rotation2d::operator+
Rotation2d operator+(const Rotation2d &other) const
Adds two rotations together, with the result being bounded between -pi and pi.
frc::Rotation2d::operator*
Rotation2d operator*(double scalar) const
Multiplies the current rotation by a scalar.
frc::Rotation2d::operator==
bool operator==(const Rotation2d &other) const
Checks equality between this Rotation2d and another object.
frc::Rotation2d::operator-=
Rotation2d & operator-=(const Rotation2d &other)
Subtracts the new rotation from the current rotation.
frc::Rotation2d::Degrees
units::degree_t Degrees() const
Returns the degree value of the rotation.
Definition: Rotation2d.h:153
frc::Rotation2d::Tan
double Tan() const
Returns the tangent of the rotation.
Definition: Rotation2d.h:174
frc::Rotation2d::Cos
double Cos() const
Returns the cosine of the rotation.
Definition: Rotation2d.h:160
frc::Rotation2d::operator!=
bool operator!=(const Rotation2d &other) const
Checks inequality between this Rotation2d and another object.
frc::Rotation2d
A rotation in a 2d coordinate frame represented a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:22
frc::Rotation2d::Rotation2d
constexpr Rotation2d()=default
Constructs a Rotation2d with a default angle of 0 degrees.
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
frc::Rotation2d::RotateBy
Rotation2d RotateBy(const Rotation2d &other) const
Adds the new rotation to the current rotation using a rotation matrix.
frc::Rotation2d::Sin
double Sin() const
Returns the sine of the rotation.
Definition: Rotation2d.h:167
frc::Rotation2d::Radians
units::radian_t Radians() const
Returns the radian value of the rotation.
Definition: Rotation2d.h:146
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::Rotation2d::operator-
Rotation2d operator-() const
Takes the inverse of the current rotation.
frc::Rotation2d::operator+=
Rotation2d & operator+=(const Rotation2d &other)
Adds a rotation to the current rotation.