WPILibC++ 2024.1.1-beta-4
Rotation2d.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/SymbolExports.h>
8#include <wpi/json_fwd.h>
9
10#include "units/angle.h"
11
12namespace frc {
13
14/**
15 * A rotation in a 2D coordinate frame represented by a point on the unit circle
16 * (cosine and sine).
17 *
18 * The angle is continuous, that is if a Rotation2d is constructed with 361
19 * degrees, it will return 361 degrees. This allows algorithms that wouldn't
20 * want to see a discontinuity in the rotations as it sweeps past from 360 to 0
21 * on the second time around.
22 */
24 public:
25 /**
26 * Constructs a Rotation2d with a default angle of 0 degrees.
27 */
28 constexpr Rotation2d() = default;
29
30 /**
31 * Constructs a Rotation2d with the given radian value.
32 *
33 * @param value The value of the angle in radians.
34 */
35 constexpr Rotation2d(units::radian_t value); // NOLINT
36
37 /**
38 * Constructs a Rotation2d with the given degree value.
39 *
40 * @param value The value of the angle in degrees.
41 */
42 constexpr Rotation2d(units::degree_t value); // NOLINT
43
44 /**
45 * Constructs a Rotation2d with the given x and y (cosine and sine)
46 * components. The x and y don't have to be normalized.
47 *
48 * @param x The x component or cosine of the rotation.
49 * @param y The y component or sine of the rotation.
50 */
51 constexpr Rotation2d(double x, double y);
52
53 /**
54 * Adds two rotations together, with the result being bounded between -pi and
55 * pi.
56 *
57 * For example, <code>Rotation2d{30_deg} + Rotation2d{60_deg}</code> equals
58 * <code>Rotation2d{units::radian_t{std::numbers::pi/2.0}}</code>
59 *
60 * @param other The rotation to add.
61 *
62 * @return The sum of the two rotations.
63 */
64 constexpr Rotation2d operator+(const Rotation2d& other) const;
65
66 /**
67 * Subtracts the new rotation from the current rotation and returns the new
68 * rotation.
69 *
70 * For example, <code>Rotation2d{10_deg} - Rotation2d{100_deg}</code> equals
71 * <code>Rotation2d{units::radian_t{-std::numbers::pi/2.0}}</code>
72 *
73 * @param other The rotation to subtract.
74 *
75 * @return The difference between the two rotations.
76 */
77 constexpr Rotation2d operator-(const Rotation2d& other) const;
78
79 /**
80 * Takes the inverse of the current rotation. This is simply the negative of
81 * the current angular value.
82 *
83 * @return The inverse of the current rotation.
84 */
85 constexpr Rotation2d operator-() const;
86
87 /**
88 * Multiplies the current rotation by a scalar.
89 *
90 * @param scalar The scalar.
91 *
92 * @return The new scaled Rotation2d.
93 */
94 constexpr Rotation2d operator*(double scalar) const;
95
96 /**
97 * Divides the current rotation by a scalar.
98 *
99 * @param scalar The scalar.
100 *
101 * @return The new scaled Rotation2d.
102 */
103 constexpr Rotation2d operator/(double scalar) const;
104
105 /**
106 * Checks equality between this Rotation2d and another object.
107 *
108 * @param other The other object.
109 * @return Whether the two objects are equal.
110 */
111 constexpr bool operator==(const Rotation2d& other) const;
112
113 /**
114 * Adds the new rotation to the current rotation using a rotation matrix.
115 *
116 * <pre>
117 * [cos_new] [other.cos, -other.sin][cos]
118 * [sin_new] = [other.sin, other.cos][sin]
119 * value_new = std::atan2(sin_new, cos_new)
120 * </pre>
121 *
122 * @param other The rotation to rotate by.
123 *
124 * @return The new rotated Rotation2d.
125 */
126 constexpr Rotation2d RotateBy(const Rotation2d& other) const;
127
128 /**
129 * Returns the radian value of the rotation.
130 *
131 * @return The radian value of the rotation.
132 * @see AngleModulus to constrain the angle within (-pi, pi]
133 */
134 constexpr units::radian_t Radians() const { return m_value; }
135
136 /**
137 * Returns the degree value of the rotation.
138 *
139 * @return The degree value of the rotation.
140 * @see InputModulus to constrain the angle within (-180, 180]
141 */
142 constexpr units::degree_t Degrees() const { return m_value; }
143
144 /**
145 * Returns the cosine of the rotation.
146 *
147 * @return The cosine of the rotation.
148 */
149 constexpr double Cos() const { return m_cos; }
150
151 /**
152 * Returns the sine of the rotation.
153 *
154 * @return The sine of the rotation.
155 */
156 constexpr double Sin() const { return m_sin; }
157
158 /**
159 * Returns the tangent of the rotation.
160 *
161 * @return The tangent of the rotation.
162 */
163 constexpr double Tan() const { return Sin() / Cos(); }
164
165 private:
166 units::radian_t m_value = 0_rad;
167 double m_cos = 1;
168 double m_sin = 0;
169};
170
172void to_json(wpi::json& json, const Rotation2d& rotation);
173
175void from_json(const wpi::json& json, Rotation2d& rotation);
176
177} // namespace frc
178
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
namespace for Niels Lohmann
Definition: json.h:96
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:23
constexpr double Tan() const
Returns the tangent of the rotation.
Definition: Rotation2d.h:163
constexpr units::degree_t Degrees() const
Returns the degree value of the rotation.
Definition: Rotation2d.h:142
constexpr double Cos() const
Returns the cosine of the rotation.
Definition: Rotation2d.h:149
constexpr Rotation2d()=default
Constructs a Rotation2d with a default angle of 0 degrees.
constexpr double Sin() const
Returns the sine of the rotation.
Definition: Rotation2d.h:156
constexpr units::radian_t Radians() const
Returns the radian value of the rotation.
Definition: Rotation2d.h:134
Definition: AprilTagPoseEstimator.h:15
WPILIB_DLLEXPORT void from_json(const wpi::json &json, AprilTagFieldLayout &layout)
WPILIB_DLLEXPORT void to_json(wpi::json &json, const AprilTagFieldLayout &layout)
bool operator==(const Value &lhs, const Value &rhs)
unit< std::ratio< 1 >, units::category::scalar_unit > scalar
Definition: base.h:2522
constexpr dimensionless::scalar_t operator/(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs) noexcept
Division for convertible unit_t types with a linear scale.
Definition: base.h:2656
constexpr unit_t< Units, T, NonLinearScale > operator-(const unit_t< Units, T, NonLinearScale > &u) noexcept
Definition: base.h:2364
constexpr unit_t< Units, T, NonLinearScale > operator+(const unit_t< Units, T, NonLinearScale > &u) noexcept
Definition: base.h:2340
constexpr auto operator*(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs) noexcept -> unit_t< compound_unit< squared< typename units::traits::unit_t_traits< UnitTypeLhs >::unit_type > > >
Multiplication type for convertible unit_t types with a linear scale.
Definition: base.h:2600