-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeomUtil.java
191 lines (170 loc) · 6.76 KB
/
GeomUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package frc.robot.utils;
// Copyright (c) 2023 FRC 6328
// http://github.com/Mechanical-Advantage
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file at
// the root directory of this project.
import edu.wpi.first.math.Vector;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Transform2d;
import edu.wpi.first.math.geometry.Transform3d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.geometry.Twist2d;
import edu.wpi.first.math.numbers.N2;
/** Geometry utilities for working with translations, rotations, transforms, and poses. */
public class GeomUtil {
/**
* Creates a pure translating transform
*
* @param translation The translation to create the transform with
* @return The resulting transform
*/
public static Transform2d translationToTransform(Translation2d translation) {
return new Transform2d(translation, new Rotation2d());
}
/**
* Creates a pure translating transform
*
* @param x The x componenet of the translation
* @param y The y componenet of the translation
* @return The resulting transform
*/
public static Transform2d translationToTransform(double x, double y) {
return new Transform2d(new Translation2d(x, y), new Rotation2d());
}
/**
* Creates a pure rotating transform
*
* @param rotation The rotation to create the transform with
* @return The resulting transform
*/
public static Transform2d rotationToTransform(Rotation2d rotation) {
return new Transform2d(new Translation2d(), rotation);
}
/**
* Converts a Pose2d to a Transform2d to be used in a kinematic chain
*
* @param pose The pose that will represent the transform
* @return The resulting transform
*/
public static Transform2d poseToTransform(Pose2d pose) {
return new Transform2d(pose.getTranslation(), pose.getRotation());
}
/**
* Converts a Transform2d to a Pose2d to be used as a position or as the start of a kinematic
* chain
*
* @param transform The transform that will represent the pose
* @return The resulting pose
*/
public static Pose2d transformToPose(Transform2d transform) {
return new Pose2d(transform.getTranslation(), transform.getRotation());
}
/**
* Creates a pure translated pose
*
* @param translation The translation to create the pose with
* @return The resulting pose
*/
public static Pose2d translationToPose(Translation2d translation) {
return new Pose2d(translation, new Rotation2d());
}
/**
* Creates a translation from the x and y of a pose, ignoring rotation
*
* @param pose The pose to create the translation with
* @return The resulting translation
*/
public static Translation2d poseToTranslation(Pose2d pose) {
return new Translation2d(pose.getX(), pose.getY());
}
/**
* Creates a pure rotated pose
*
* @param rotation The rotation to create the pose with
* @return The resulting pose
*/
public static Pose2d rotationToPose(Rotation2d rotation) {
return new Pose2d(new Translation2d(), rotation);
}
/**
* Multiplies a twist by a scaling factor
*
* @param twist The twist to multiply
* @param factor The scaling factor for the twist components
* @return The new twist
*/
public static Twist2d multiplyTwist(Twist2d twist, double factor) {
return new Twist2d(twist.dx * factor, twist.dy * factor, twist.dtheta * factor);
}
/**
* Converts a Pose3d to a Transform3d to be used in a kinematic chain
*
* @param pose The pose that will represent the transform
* @return The resulting transform
*/
public static Transform3d pose3dToTransform3d(Pose3d pose) {
return new Transform3d(pose.getTranslation(), pose.getRotation());
}
/**
* Converts a Transform3d to a Pose3d to be used as a position or as the start of a kinematic
* chain
*
* @param transform The transform that will represent the pose
* @return The resulting pose
*/
public static Pose3d transform3dToPose3d(Transform3d transform) {
return new Pose3d(transform.getTranslation(), transform.getRotation());
}
/**
* Converts a Translation3d to a Translation2d by extracting two dimensions (X and Y). chain
*
* @param transform The original translation
* @return The resulting translation
*/
public static Translation2d translation3dTo2dXY(Translation3d translation) {
return new Translation2d(translation.getX(), translation.getY());
}
/**
* Converts a Translation3d to a Translation2d by extracting two dimensions (X and Z). chain
*
* @param transform The original translation
* @return The resulting translation
*/
public static Translation2d translation3dTo2dXZ(Translation3d translation) {
return new Translation2d(translation.getX(), translation.getZ());
}
/**
* Find the velocity of an object toward a target point
*
* @param velocity The velocity of the object
* @param currentTranslation The current position of the object
* @param targetTranslation The position of the target point
* @return The velocity toward the target point (a negative number if object is moving away)
*/
public static double findVelocityTowardPoint(
Vector<N2> velocity,
Translation2d currentTranslation,
Translation2d targetTranslation) {
// Calculate the velocity to the goal based on this formula
// https://web.ma.utexas.edu/users/m408m/Display12-3-4.shtml
// Vector from current to target (translate target so that current is now 0, 0)
Translation2d currentToTarget = targetTranslation.minus(currentTranslation);
// Get the angle of the vector from current to target
Rotation2d targetAngle =
Rotation2d.fromRadians(Math.atan2(currentToTarget.getY(), currentToTarget.getX()));
// Get the angle of the velocity vector
Rotation2d velocityAngle =
Rotation2d.fromRadians(Math.atan2(velocity.get(1, 0), velocity.get(0, 0)));
// Calculate theta, the angle between the speakerAngle and the current velocity angle
Rotation2d theta = velocityAngle.minus(targetAngle);
// Project the velocity vector onto the speaker vector with ||u|| * cos(theta)
// Where u is the velocity vector
double velocityToGoal = velocity.norm() * theta.getCos();
return velocityToGoal;
}
}