-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* vision config + localization * Adjust vision config * Changes * vision configs + localization --------- Co-authored-by: Jordan <[email protected]>
- Loading branch information
1 parent
86e5b6a
commit 1f2a20d
Showing
5 changed files
with
86 additions
and
4 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
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
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,5 @@ | ||
package frc.robot.localization; | ||
|
||
public enum LocalizationState { | ||
DEFAULT_STATE; | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/frc/robot/localization/LocalizationSubsystem.java
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,67 @@ | ||
package frc.robot.localization; | ||
|
||
import edu.wpi.first.math.VecBuilder; | ||
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator; | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.interpolation.TimeInterpolatableBuffer; | ||
import edu.wpi.first.wpilibj.Timer; | ||
import frc.robot.config.RobotConfig; | ||
import frc.robot.imu.ImuSubsystem; | ||
import frc.robot.util.scheduling.SubsystemPriority; | ||
import frc.robot.util.state_machines.StateMachine; | ||
import frc.robot.vision.VisionResult; | ||
import frc.robot.vision.VisionSubsystem; | ||
import java.util.Optional; | ||
|
||
public class LocalizationSubsystem extends StateMachine<LocalizationState> { | ||
private final ImuSubsystem imu; | ||
private final VisionSubsystem vision; | ||
private final SwerveDrivePoseEstimator poseEstimator; | ||
private final TimeInterpolatableBuffer<Pose2d> poseHistory = | ||
TimeInterpolatableBuffer.createBuffer(1.5); | ||
private double lastAddedVisionTimestamp = 0; | ||
private Optional<VisionResult> latestResult = Optional.empty(); | ||
|
||
public LocalizationSubsystem(ImuSubsystem imu, VisionSubsystem vision) { | ||
super(SubsystemPriority.LOCALIZATION, LocalizationState.DEFAULT_STATE); | ||
this.imu = imu; | ||
this.vision = vision; | ||
poseEstimator = new SwerveDrivePoseEstimator(null, imu.getRobotHeading(), null, new Pose2d()); | ||
} | ||
|
||
@Override | ||
protected void collectInputs() { | ||
latestResult = vision.getVisionResult(); | ||
} | ||
|
||
public Pose2d getPose() { | ||
return poseEstimator.getEstimatedPosition(); | ||
} | ||
|
||
@Override | ||
public void robotPeriodic() { | ||
super.robotPeriodic(); | ||
|
||
if (latestResult.isPresent()) { | ||
var results = latestResult.get(); | ||
Pose2d visionPose = results.pose(); | ||
|
||
double visionTimestamp = results.timestamp(); | ||
|
||
if (visionTimestamp == lastAddedVisionTimestamp) { | ||
// Don't add the same vision pose over and over | ||
} else { | ||
poseEstimator.addVisionMeasurement( | ||
visionPose, | ||
visionTimestamp, | ||
VecBuilder.fill( | ||
RobotConfig.get().vision().xyStdDev(), | ||
RobotConfig.get().vision().xyStdDev(), | ||
RobotConfig.get().vision().thetaStdDev())); | ||
lastAddedVisionTimestamp = visionTimestamp; | ||
} | ||
} | ||
|
||
poseHistory.addSample(Timer.getFPGATimestamp(), poseEstimator.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