Skip to content
This repository was archived by the owner on May 19, 2024. It is now read-only.

Commit ae4bfb3

Browse files
committed
refactor(odometry): Improve Limelight.
1 parent cfea267 commit ae4bfb3

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package frc.robot.odometry;
2+
3+
import java.util.Optional;
4+
5+
import edu.wpi.first.math.geometry.Rotation2d;
6+
import frc.lib.LimelightHelpers;
7+
import frc.lib.LimelightHelpers.PoseEstimate;
8+
9+
public class Limelight {
10+
11+
private final String name;
12+
13+
public Limelight(String name) {
14+
this.name = name;
15+
}
16+
17+
public void setTagFilter(int[] tags) {
18+
LimelightHelpers.SetFiducialIDFiltersOverride(this.name, tags);
19+
}
20+
21+
public void setYaw(Rotation2d yaw) {
22+
LimelightHelpers.SetRobotOrientation(this.name, yaw.getDegrees(), 0, 0, 0, 0, 0);
23+
}
24+
25+
public Optional<PoseEstimate> getPoseEstimate() {
26+
LimelightHelpers.PoseEstimate megaTag2 = LimelightHelpers.getBotPoseEstimate_wpiBlue_MegaTag2(this.name);
27+
28+
if (megaTag2.tagCount < 1) return Optional.empty();
29+
30+
return Optional.of(megaTag2);
31+
}
32+
33+
}

src/main/java/frc/robot/odometry/Odometry.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import edu.wpi.first.wpilibj2.command.Command;
1515
import edu.wpi.first.wpilibj2.command.Commands;
1616
import frc.lib.AllianceFlipHelper;
17-
import frc.lib.LimelightHelpers;
1817
import frc.lib.Subsystem;
1918
import frc.lib.Telemetry;
2019
import frc.robot.odometry.GyroscopeIO.GyroscopeIOValues;
@@ -51,6 +50,8 @@ public class Odometry extends Subsystem {
5150
/** List of functions to be called when pose is manually updated. */
5251
private final List<Consumer<Rotation2d>> yawUpdateConsumers;
5352

53+
private final Limelight limelight;
54+
5455
/** Creates a new instance of the odometry subsystem. */
5556
private Odometry() {
5657
gyroscope = OdometryFactory.createGyroscope(this);
@@ -72,12 +73,15 @@ private Odometry() {
7273
initialGyroRotation,
7374
swerveModulePositionsSupplier.get(),
7475
initialPose);
76+
swervePoseEstimator.setVisionMeasurementStdDevs(VecBuilder.fill(.7,.7,9999999));
7577

7678
field = new Field2d();
7779

7880
yawUpdateConsumers = new ArrayList<Consumer<Rotation2d>>();
7981

80-
LimelightHelpers.SetFiducialIDFiltersOverride(OdometryConstants.LIMELIGHT_NAME, OdometryConstants.VALID_TAGS);
82+
limelight = new Limelight("limelight");
83+
84+
limelight.setTagFilter(OdometryConstants.VALID_TAGS);
8185
}
8286

8387
/**
@@ -97,19 +101,18 @@ public static Odometry getInstance() {
97101
public void periodic() {
98102
gyroscope.update(gyroscopeValues);
99103

100-
LimelightHelpers.SetRobotOrientation(OdometryConstants.LIMELIGHT_NAME, getFieldRelativeHeading().getDegrees(), 0, 0, 0, 0, 0);
101-
102-
LimelightHelpers.PoseEstimate megaTag2 = LimelightHelpers.getBotPoseEstimate_wpiBlue_MegaTag2(OdometryConstants.LIMELIGHT_NAME);
104+
limelight.setYaw(getFieldRelativeHeading());
103105

104-
final boolean spinningFast = Math.abs(gyroscopeValues.yawVelocityRotations) > 1.0;
106+
final boolean stationary = Math.abs(gyroscopeValues.yawVelocityRotations) > 1.0;
105107

106-
final boolean hasTag = megaTag2.tagCount != 0;
108+
if (stationary) {
109+
var pe = limelight.getPoseEstimate();
107110

108-
if(hasTag && !spinningFast) {
109-
swervePoseEstimator.setVisionMeasurementStdDevs(VecBuilder.fill(.7,.7,9999999));
110-
swervePoseEstimator.addVisionMeasurement(
111-
megaTag2.pose,
112-
megaTag2.timestampSeconds);
111+
if (pe.isPresent()) {
112+
swervePoseEstimator.addVisionMeasurement(
113+
pe.get().pose,
114+
pe.get().timestampSeconds);
115+
}
113116
}
114117

115118
swervePoseEstimator.update(

0 commit comments

Comments
 (0)