Skip to content

Commit 52ff316

Browse files
authored
Merge pull request #56 from Manchester-Central/nearest-field-point
Adds getNearestPoint to FieldPose
2 parents 26ff414 + 3e44ad5 commit 52ff316

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/main/java/com/chaos131/poses/FieldPose.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.chaos131.poses;
22

3+
import static edu.wpi.first.units.Units.Meters;
4+
35
import edu.wpi.first.math.geometry.Pose2d;
46
import edu.wpi.first.math.geometry.Pose3d;
57
import edu.wpi.first.math.geometry.Rotation2d;
68
import edu.wpi.first.math.geometry.Translation2d;
79
import edu.wpi.first.units.measure.Distance;
810
import edu.wpi.first.wpilibj.DriverStation;
911
import edu.wpi.first.wpilibj.DriverStation.Alliance;
10-
11-
import static edu.wpi.first.units.Units.Meters;
12-
1312
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.Comparator;
1415
import java.util.HashMap;
16+
import java.util.List;
1517
import java.util.Map;
1618

1719
public abstract class FieldPose {
@@ -142,4 +144,37 @@ public static Pose2d getClosestKnownPose(Pose2d robotPose) {
142144
FieldPoses.forEach((name, pose) -> poses.add(pose.getCurrentAlliancePose()));
143145
return robotPose.nearest(poses);
144146
}
147+
148+
/**
149+
* Finds the closest FieldPose to the sourcePose given the current alliance. Will throw a runtime
150+
* error if no FieldPoses are added.
151+
*
152+
* @param sourcePose the pose to compare to
153+
* @param fieldPoses the poses to search through
154+
*/
155+
public static FieldPose getClosestPose(Pose2d sourcePose, FieldPose... fieldPoses) {
156+
return getClosestPose(sourcePose, Arrays.asList(fieldPoses));
157+
}
158+
159+
/**
160+
* Finds the closest FieldPose to the sourcePose given the current alliance. Will throw a runtime
161+
* error if no FieldPoses are added.
162+
*
163+
* @param sourcePose the pose to compare to
164+
* @param fieldPoses the poses to search through
165+
*/
166+
public static FieldPose getClosestPose(Pose2d sourcePose, List<FieldPose> fieldPoses) {
167+
// spotless:off
168+
return fieldPoses
169+
.stream()
170+
.min(Comparator.comparingDouble(pose -> getDistanceFromLocations(sourcePose, pose.getCurrentAlliancePose()).in(Meters)))
171+
.get();
172+
// spotless:on
173+
}
174+
175+
@Override
176+
public String toString() {
177+
return String.format(
178+
"%s(name: %s, bluePose: %s)", getClass().getSimpleName(), m_name, m_bluePose);
179+
}
145180
}

src/test/java/com/chaos131/poses/MirroredDrivePoseTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testMirroringAngle() {
7676
}
7777

7878
@Test
79-
public void testGetClosestPose() {
79+
public void testGetClosestKnownPose() {
8080
var pose1 = new DrivePoseImpl("pose1", new Pose2d(0, 0, Rotation2d.fromDegrees(0)));
8181
var pose2 = new DrivePoseImpl("pose2", new Pose2d(6, 6, Rotation2d.fromDegrees(0)));
8282
var pose3 = new DrivePoseImpl("pose3", new Pose2d(9, 0, Rotation2d.fromDegrees(0)));
@@ -92,6 +92,21 @@ public void testGetClosestPose() {
9292
pose3.getBluePose());
9393
}
9494

95+
@Test
96+
public void testGetClosestPose() {
97+
var pose1 = new DrivePoseImpl("pose1", new Pose2d(0, 0, Rotation2d.fromDegrees(0)));
98+
var pose2 = new DrivePoseImpl("pose2", new Pose2d(6, 6, Rotation2d.fromDegrees(0)));
99+
100+
var drivePose1 = new Pose2d(0, 0, Rotation2d.fromDegrees(180));
101+
assertEquals(DrivePoseImpl.getClosestPose(drivePose1, pose1, pose2), pose1);
102+
103+
var drivePose2 = new Pose2d(1, 0, Rotation2d.fromDegrees(0));
104+
assertEquals(DrivePoseImpl.getClosestPose(drivePose2, pose1, pose2), pose1);
105+
106+
var drivePose3 = new Pose2d(5, 5, Rotation2d.fromDegrees(180));
107+
assertEquals(DrivePoseImpl.getClosestPose(drivePose3, pose1, pose2), pose2);
108+
}
109+
95110
// @Test
96111
// public void testAngleFrom() {
97112
// var pose1 = new DrivePoseImpl(new Pose2d());

0 commit comments

Comments
 (0)