Skip to content

Commit 115a012

Browse files
Log known object poses and some random stuff
1 parent 9d8915c commit 115a012

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/main/java/frc/trigon/robot/misc/objectdetectioncamera/ObjectPoseEstimator.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import edu.wpi.first.wpilibj2.command.SubsystemBase;
88
import frc.trigon.robot.RobotContainer;
99
import frc.trigon.robot.misc.simulatedfield.SimulatedGamePieceConstants;
10+
import org.littletonrobotics.junction.Logger;
1011

1112
import java.util.ArrayList;
1213
import java.util.HashMap;
@@ -41,6 +42,7 @@ public ObjectPoseEstimator(double deletionThresholdSeconds, DistanceCalculationM
4142
public void periodic() {
4243
updateObjectPositions();
4344
removeOldObjects();
45+
Logger.recordOutput("ObjectPoseEstimator/knownObjectPositions", knownObjectPositions.keySet().toArray(Translation2d[]::new));
4446
}
4547

4648
/**
@@ -57,7 +59,7 @@ public ArrayList<Translation2d> getObjectsOnField() {
5759
*/
5860
public void removeClosestObjectToRobot() {
5961
final Translation2d closestObject = getClosestObjectToRobot();
60-
knownObjectPositions.remove(closestObject);
62+
removeObject(closestObject);
6163
}
6264

6365
/**
@@ -67,7 +69,7 @@ public void removeClosestObjectToRobot() {
6769
*/
6870
public void removeClosestObjectToIntake(Transform2d intakeTransform) {
6971
final Pose2d robotPose = RobotContainer.ROBOT_POSE_ESTIMATOR.getEstimatedRobotPose();
70-
knownObjectPositions.remove(getClosestObjectToPose(robotPose.transformBy(intakeTransform)));
72+
removeObject(getClosestObjectToPosition(robotPose.transformBy(intakeTransform).getTranslation()));
7173
}
7274

7375
/**
@@ -76,8 +78,13 @@ public void removeClosestObjectToIntake(Transform2d intakeTransform) {
7678
* @param fieldRelativePose the pose to which the removed object is closest
7779
*/
7880
public void removeClosestObjectToPose(Pose2d fieldRelativePose) {
79-
final Translation2d closestObject = getClosestObjectToPose(fieldRelativePose);
80-
knownObjectPositions.remove(closestObject);
81+
final Translation2d closestObject = getClosestObjectToPosition(fieldRelativePose.getTranslation());
82+
removeObject(closestObject);
83+
}
84+
85+
public void removeClosestObjectToPosition(Translation2d position) {
86+
final Translation2d closestObject = getClosestObjectToPosition(position);
87+
removeObject(closestObject);
8188
}
8289

8390
/**
@@ -98,27 +105,30 @@ public void removeObject(Translation2d objectPosition) {
98105
* @return the best object's 2D position on the field (z is assumed to be 0)
99106
*/
100107
public Translation2d getClosestObjectToRobot() {
101-
return getClosestObjectToPose(RobotContainer.ROBOT_POSE_ESTIMATOR.getEstimatedRobotPose());
108+
return getClosestObjectToPosition(RobotContainer.ROBOT_POSE_ESTIMATOR.getEstimatedRobotPose().getTranslation());
102109
}
103110

111+
104112
/**
105-
* Gets the position of the closest object to a given pose on the field.
113+
* Gets the position of the closest object to a given position on the field.
106114
*
107-
* @param pose the pose to which the returned object is closest
115+
* @param position the position to which the returned object is closest
108116
* @return the closest object's position on the field, or null if no objects are known
109117
*/
110-
public Translation2d getClosestObjectToPose(Pose2d pose) {
118+
public Translation2d getClosestObjectToPosition(Translation2d position) {
111119
final Translation2d[] objectsTranslations = knownObjectPositions.keySet().toArray(Translation2d[]::new);
112120
if (knownObjectPositions.isEmpty())
113121
return null;
114122
Translation2d bestObjectTranslation = objectsTranslations[0];
123+
double closestObjectDistance = position.getDistance(bestObjectTranslation);
115124

116125
for (int i = 1; i < objectsTranslations.length; i++) {
117126
final Translation2d currentObjectTranslation = objectsTranslations[i];
118-
final double bestObjectRating = calculateObjectDistanceRating(bestObjectTranslation, pose);
119-
final double currentObjectRating = calculateObjectDistanceRating(currentObjectTranslation, pose);
120-
if (currentObjectRating < bestObjectRating)
127+
final double currentObjectDistance = position.getDistance(currentObjectTranslation);
128+
if (currentObjectDistance < closestObjectDistance) {
129+
closestObjectDistance = currentObjectDistance;
121130
bestObjectTranslation = currentObjectTranslation;
131+
}
122132
}
123133
return bestObjectTranslation;
124134
}
@@ -156,7 +166,7 @@ private void updateObjectPositions() {
156166
}
157167

158168
if (closestObjectToVisibleObjectDistanceMeters < ObjectDetectionCameraConstants.TRACKED_OBJECT_TOLERANCE_METERS && knownObjectPositions.get(closestObjectToVisibleObject) != currentTimestamp)
159-
knownObjectPositions.remove(closestObjectToVisibleObject);
169+
removeObject(closestObjectToVisibleObject);
160170
knownObjectPositions.put(visibleObject, currentTimestamp);
161171
}
162172
}

0 commit comments

Comments
 (0)