-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package frc.robot; | ||
|
||
import edu.wpi.first.math.VecBuilder; | ||
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator; | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import frc.robot.swerve.SwerveDrive; | ||
import frc.robot.vision.Vision; | ||
import java.util.function.DoubleSupplier; | ||
import org.littletonrobotics.junction.AutoLogOutput; | ||
|
||
public class PoseEstimation { | ||
private static PoseEstimation INSTANCE = null; | ||
private final SwerveDrivePoseEstimator estimator; | ||
private final Vision vision = Vision.getInstance(); | ||
private final SwerveDrive swerveDrive = SwerveDrive.getInstance(); | ||
|
||
public PoseEstimation() { | ||
estimator = | ||
new SwerveDrivePoseEstimator( | ||
swerveDrive.getKinematics(), | ||
swerveDrive.getYaw(), | ||
swerveDrive.getModulePositions(), | ||
swerveDrive.getBotPose()); | ||
} | ||
|
||
public static PoseEstimation getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new PoseEstimation(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
public void addVisionMeasurement( | ||
DoubleSupplier std1, DoubleSupplier std2, DoubleSupplier std3) { | ||
var results = vision.getResults(); | ||
for (org.photonvision.EstimatedRobotPose result : results) { | ||
estimator.addVisionMeasurement( | ||
result.estimatedPose.toPose2d(), | ||
result.timestampSeconds, | ||
VecBuilder.fill(std1.getAsDouble(), std2.getAsDouble(), std3.getAsDouble())); | ||
} | ||
} | ||
|
||
public void updatePose() { | ||
for (int i = 0; i < swerveDrive.getHighFreqModulePositions().size(); i++) { | ||
estimator.updateWithTime( | ||
swerveDrive.getHighFreqTimeStamps()[i], | ||
swerveDrive.getYaw(), | ||
swerveDrive.getHighFreqModulePositions().get(i)); | ||
} | ||
} | ||
|
||
public void resetPose(Pose2d pose) { | ||
estimator.resetPosition( | ||
swerveDrive.getRawYaw(), | ||
swerveDrive | ||
.getHighFreqModulePositions() | ||
.get(swerveDrive.getHighFreqModulePositions().size() - 1), | ||
pose); | ||
} | ||
|
||
@AutoLogOutput(key = "EstimatedRobotPose") | ||
public Pose2d getEstimatedPose() { | ||
return estimator.getEstimatedPosition(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters