|
1 | 1 | package com.chaos131.poses; |
2 | 2 |
|
| 3 | +import static edu.wpi.first.units.Units.Meters; |
| 4 | + |
3 | 5 | import edu.wpi.first.math.geometry.Pose2d; |
4 | 6 | import edu.wpi.first.math.geometry.Pose3d; |
5 | 7 | import edu.wpi.first.math.geometry.Rotation2d; |
6 | 8 | import edu.wpi.first.math.geometry.Translation2d; |
7 | 9 | import edu.wpi.first.units.measure.Distance; |
8 | 10 | import edu.wpi.first.wpilibj.DriverStation; |
9 | 11 | import edu.wpi.first.wpilibj.DriverStation.Alliance; |
10 | | - |
11 | | -import static edu.wpi.first.units.Units.Meters; |
12 | | - |
13 | 12 | import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Comparator; |
14 | 15 | import java.util.HashMap; |
| 16 | +import java.util.List; |
15 | 17 | import java.util.Map; |
16 | 18 |
|
17 | 19 | public abstract class FieldPose { |
@@ -142,4 +144,37 @@ public static Pose2d getClosestKnownPose(Pose2d robotPose) { |
142 | 144 | FieldPoses.forEach((name, pose) -> poses.add(pose.getCurrentAlliancePose())); |
143 | 145 | return robotPose.nearest(poses); |
144 | 146 | } |
| 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 | + } |
145 | 180 | } |
0 commit comments