RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Rotation2d.java
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
5package org.wpilib.math.geometry;
6
7import static org.wpilib.math.util.UnitConversions.radiansToRotations;
8import static org.wpilib.math.util.UnitConversions.rotationsToRadians;
9import static org.wpilib.units.Units.Radians;
10
11import io.avaje.jsonb.Json;
12import java.util.Objects;
13import org.wpilib.math.geometry.proto.Rotation2dProto;
14import org.wpilib.math.geometry.struct.Rotation2dStruct;
15import org.wpilib.math.interpolation.Interpolatable;
16import org.wpilib.math.linalg.MatBuilder;
17import org.wpilib.math.linalg.Matrix;
18import org.wpilib.math.numbers.N2;
19import org.wpilib.math.util.MathSharedStore;
20import org.wpilib.math.util.Nat;
21import org.wpilib.units.measure.Angle;
22import org.wpilib.util.protobuf.ProtobufSerializable;
23import org.wpilib.util.struct.StructSerializable;
24
28@Json
29public final class Rotation2d
36 public static final Rotation2d ZERO = new Rotation2d();
37
43 public static final Rotation2d CW_PI_2 = new Rotation2d(-Math.PI / 2);
44
50 public static final Rotation2d CW_90DEG = CW_PI_2;
51
57 public static final Rotation2d CCW_PI_2 = new Rotation2d(Math.PI / 2);
58
64 public static final Rotation2d CCW_90DEG = CCW_PI_2;
65
71 public static final Rotation2d PI = new Rotation2d(Math.PI);
72
78 public static final Rotation2d k180deg = PI;
79
80 @Json.Ignore private final double m_cos;
81 @Json.Ignore private final double m_sin;
82
84 public Rotation2d() {
85 m_cos = 1.0;
86 m_sin = 0.0;
87 }
88
94 @Json.Creator
95 public Rotation2d(@Json.Alias(value = "radians") double value) {
96 m_cos = Math.cos(value);
97 m_sin = Math.sin(value);
98 }
99
106 public Rotation2d(double x, double y) {
107 double magnitude = Math.hypot(x, y);
108 if (magnitude > 1e-6) {
109 m_cos = x / magnitude;
110 m_sin = y / magnitude;
111 } else {
112 m_cos = 1.0;
113 m_sin = 0.0;
115 "x and y components of Rotation2d are zero\n", Thread.currentThread().getStackTrace());
116 }
117 }
118
124 public Rotation2d(Angle angle) {
125 this(angle.in(Radians));
126 }
127
134 public Rotation2d(Matrix<N2, N2> rotationMatrix) {
135 final var R = rotationMatrix;
136
137 // Require that the rotation matrix is special orthogonal. This is true if
138 // the matrix is orthogonal (RRᵀ = I) and normalized (determinant is 1).
139 if (R.times(R.transpose()).minus(Matrix.eye(Nat.N2())).normF() > 1e-9) {
140 var msg = "Rotation matrix isn't orthogonal\n\nR =\n" + R.getStorage().toString() + '\n';
141 MathSharedStore.reportError(msg, Thread.currentThread().getStackTrace());
142 throw new IllegalArgumentException(msg);
143 }
144 if (Math.abs(R.det() - 1.0) > 1e-9) {
145 var msg =
146 "Rotation matrix is orthogonal but not special orthogonal\n\nR =\n"
147 + R.getStorage().toString()
148 + '\n';
149 MathSharedStore.reportError(msg, Thread.currentThread().getStackTrace());
150 throw new IllegalArgumentException(msg);
151 }
152
153 // R = [cosθ −sinθ]
154 // [sinθ cosθ]
155 m_cos = R.get(0, 0);
156 m_sin = R.get(1, 0);
157 }
158
165 public static Rotation2d fromRadians(double radians) {
166 return new Rotation2d(radians);
167 }
168
175 public static Rotation2d fromDegrees(double degrees) {
176 return new Rotation2d(Math.toRadians(degrees));
177 }
178
185 public static Rotation2d fromRotations(double rotations) {
186 return new Rotation2d(rotationsToRadians(rotations));
187 }
188
198 public Rotation2d plus(Rotation2d other) {
199 return rotateBy(other);
200 }
201
212 return rotateBy(other.unaryMinus());
213 }
214
222 return new Rotation2d(m_cos, -m_sin);
223 }
224
231 public Rotation2d times(double scalar) {
232 return new Rotation2d(getRadians() * scalar);
233 }
234
241 public Rotation2d div(double scalar) {
242 return times(1.0 / scalar);
243 }
244
260 return new Rotation2d(
261 m_cos * other.m_cos - m_sin * other.m_sin, m_cos * other.m_sin + m_sin * other.m_cos);
262 }
263
272 return rotateBy(other.unaryMinus());
273 }
274
281 // R = [cosθ −sinθ]
282 // [sinθ cosθ]
283 return MatBuilder.fill(Nat.N2(), Nat.N2(), m_cos, -m_sin, m_sin, m_cos);
284 }
285
291 public Angle getMeasure() {
292 return Radians.of(getRadians());
293 }
294
300 @Json.Property("radians")
301 public double getRadians() {
302 return Math.atan2(m_sin, m_cos);
303 }
304
310 public double getDegrees() {
311 return Math.toDegrees(getRadians());
312 }
313
319 public double getRotations() {
320 return radiansToRotations(getRadians());
321 }
322
328 public double getCos() {
329 return m_cos;
330 }
331
337 public double getSin() {
338 return m_sin;
339 }
340
346 public double getTan() {
347 return m_sin / m_cos;
348 }
349
350 @Override
351 public String toString() {
352 return String.format("Rotation2d(Rads: %.2f, Deg: %.2f)", getRadians(), getDegrees());
353 }
354
361 @Override
362 public boolean equals(Object obj) {
363 return obj instanceof Rotation2d other
364 && Math.hypot(m_cos - other.m_cos, m_sin - other.m_sin) < 1E-9;
365 }
366
367 @Override
368 public int hashCode() {
369 return Objects.hash(getRadians());
370 }
371
372 @Override
373 public Rotation2d interpolate(Rotation2d endValue, double t) {
374 return plus(endValue.minus(this).times(t));
375 }
376
378 public static final Rotation2dProto proto = new Rotation2dProto();
379
381 public static final Rotation2dStruct struct = new Rotation2dStruct();
382}
static Rotation2d fromDegrees(double degrees)
Rotation2d relativeTo(Rotation2d other)
Rotation2d minus(Rotation2d other)
Rotation2d plus(Rotation2d other)
static final Rotation2d CW_90DEG
Rotation2d rotateBy(Rotation2d other)
static final Rotation2dProto proto
static final Rotation2d CW_PI_2
Rotation2d times(double scalar)
static final Rotation2d CCW_90DEG
Rotation2d div(double scalar)
Rotation2d(Matrix< N2, N2 > rotationMatrix)
static Rotation2d fromRadians(double radians)
Rotation2d(@Json.Alias(value="radians") double value)
static final Rotation2d k180deg
static final Rotation2d ZERO
static final Rotation2d CCW_PI_2
static Rotation2d fromRotations(double rotations)
static< R extends Num, C extends Num > Matrix< R, C > fill(Nat< R > rows, Nat< C > cols, double... data)
static< D extends Num > Matrix< D, D > eye(Nat< D > dim)
Definition Matrix.java:613
final double get(int row, int col)
Definition Matrix.java:125
static void reportError(String error, StackTraceElement[] stackTrace)
static Nat< N2 > N2()
Definition Nat.java:72