generated from yeti-robotics/yeti-robot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
vision #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
vision #7
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5319d6
Built vision
akashkulkarni2727 1d93ae7
Finished robot container
akashkulkarni2727 5458882
Merge branch 'main' into vision
akashkulkarni2727 e79da90
resolve merge conflicts
akashkulkarni2727 002368c
Merge branch 'main' of https://github.com/yeti-robotics/akitty into v…
akashkulkarni2727 3cdaa13
took off mega tag 1 and decided on mega tag 2
akashkulkarni2727 6bbda57
made changes to simulate robot and camera pose and logged transforms
akashkulkarni2727 5b0ee3f
Made sone minor fixes to vision constants
akashkulkarni2727 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,156 @@ | ||
| package frc.robot.subsystems.vision; | ||
|
|
||
| import static frc.robot.subsystems.vision.VisionConstants.*; | ||
|
|
||
| import edu.wpi.first.math.Matrix; | ||
| import edu.wpi.first.math.VecBuilder; | ||
| import edu.wpi.first.math.geometry.Pose2d; | ||
| import edu.wpi.first.math.geometry.Pose3d; | ||
| import edu.wpi.first.math.geometry.Rotation2d; | ||
| import edu.wpi.first.math.numbers.N1; | ||
| import edu.wpi.first.math.numbers.N3; | ||
| import edu.wpi.first.wpilibj.Alert; | ||
| import edu.wpi.first.wpilibj.Alert.AlertType; | ||
| import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
| import frc.robot.subsystems.vision.VisionIO.PoseObservationType; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import org.littletonrobotics.junction.Logger; | ||
|
|
||
| public class Vision extends SubsystemBase { | ||
| private final VisionConsumer consumer; | ||
| private final VisionIO[] io; | ||
| private final VisionIOInputsAutoLogged[] inputs; | ||
| private final Alert[] disconnectedAlerts; | ||
|
|
||
| public Vision(VisionConsumer consumer, VisionIO... io) { | ||
| this.consumer = consumer; | ||
| this.io = io; | ||
|
|
||
| this.inputs = new VisionIOInputsAutoLogged[io.length]; | ||
| for (int i = 0; i < inputs.length; i++) { | ||
| inputs[i] = new VisionIOInputsAutoLogged(); | ||
| } | ||
|
|
||
| this.disconnectedAlerts = new Alert[io.length]; | ||
| for (int i = 0; i < inputs.length; i++) { | ||
| disconnectedAlerts[i] = | ||
| new Alert( | ||
| "Vision camera " + Integer.toString(i) + " is disconnected.", | ||
| AlertType.kWarning); | ||
| } | ||
| } | ||
|
|
||
| public Rotation2d getTargetX(int cameraIndex) { | ||
| return inputs[cameraIndex].latestTargetObservation.tx(); | ||
| } | ||
|
|
||
| @Override | ||
| public void periodic() { | ||
| for (int i = 0; i < io.length; i++) { | ||
| io[i].updateInputs(inputs[i]); | ||
| Logger.processInputs("Vision/Camera" + Integer.toString(i), inputs[i]); | ||
| } | ||
|
|
||
| List<Pose3d> allTagPoses = new LinkedList<>(); | ||
| List<Pose3d> allRobotPoses = new LinkedList<>(); | ||
| List<Pose3d> allRobotPosesAccepted = new LinkedList<>(); | ||
| List<Pose3d> allRobotPosesRejected = new LinkedList<>(); | ||
|
|
||
| for (int cameraIndex = 0; cameraIndex < io.length; cameraIndex++) { | ||
| disconnectedAlerts[cameraIndex].set(!inputs[cameraIndex].connected); | ||
|
|
||
| List<Pose3d> tagPoses = new LinkedList<>(); | ||
| List<Pose3d> robotPoses = new LinkedList<>(); | ||
| List<Pose3d> robotPosesAccepted = new LinkedList<>(); | ||
| List<Pose3d> robotPosesRejected = new LinkedList<>(); | ||
|
|
||
| for (int tagId : inputs[cameraIndex].tagIds) { | ||
| var tagPose = aprilTagLayout.getTagPose(tagId); | ||
| if (tagPose.isPresent()) { | ||
| tagPoses.add(tagPose.get()); | ||
| } | ||
| } | ||
|
|
||
| for (var observation : inputs[cameraIndex].poseObservations) { | ||
| boolean rejectPose = | ||
| observation.tagCount() == 0 // Must have at least one tag | ||
| || (observation.tagCount() == 1 | ||
| && observation.ambiguity() | ||
| > maxAmbiguity) // Cannot be high ambiguity | ||
| || Math.abs(observation.pose().getZ()) | ||
| > maxZError // Must have realistic Z coordinate | ||
| || observation.pose().getX() < 0.0 | ||
| || observation.pose().getX() > aprilTagLayout.getFieldLength() | ||
| || observation.pose().getY() < 0.0 | ||
| || observation.pose().getY() > aprilTagLayout.getFieldWidth(); | ||
|
|
||
| robotPoses.add(observation.pose()); | ||
| if (rejectPose) { | ||
| robotPosesRejected.add(observation.pose()); | ||
| } else { | ||
| robotPosesAccepted.add(observation.pose()); | ||
| } | ||
|
|
||
| if (rejectPose) { | ||
| continue; | ||
| } | ||
|
|
||
| double stdDevFactor = | ||
| Math.pow(observation.averageTagDistance(), 2.0) / observation.tagCount(); | ||
| double linearStdDev = linearStdDevBaseline * stdDevFactor; | ||
| double angularStdDev = angularStdDevBaseline * stdDevFactor; | ||
| if (observation.type() == PoseObservationType.MEGATAG_2) { | ||
| linearStdDev *= linearStdDevMegatag2Factor; | ||
| angularStdDev *= angularStdDevMegatag2Factor; | ||
| } | ||
| if (cameraIndex < cameraStdDevFactors.length) { | ||
| linearStdDev *= cameraStdDevFactors[cameraIndex]; | ||
| angularStdDev *= cameraStdDevFactors[cameraIndex]; | ||
| } | ||
|
|
||
| consumer.accept( | ||
| observation.pose().toPose2d(), | ||
| observation.timestamp(), | ||
| VecBuilder.fill(linearStdDev, linearStdDev, angularStdDev)); | ||
| } | ||
|
|
||
| Logger.recordOutput( | ||
| "Vision/Camera" + Integer.toString(cameraIndex) + "/TagPoses", | ||
| tagPoses.toArray(new Pose3d[tagPoses.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPoses", | ||
| robotPoses.toArray(new Pose3d[robotPoses.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPosesAccepted", | ||
| robotPosesAccepted.toArray(new Pose3d[robotPosesAccepted.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPosesRejected", | ||
| robotPosesRejected.toArray(new Pose3d[robotPosesRejected.size()])); | ||
| allTagPoses.addAll(tagPoses); | ||
| allRobotPoses.addAll(robotPoses); | ||
| allRobotPosesAccepted.addAll(robotPosesAccepted); | ||
| allRobotPosesRejected.addAll(robotPosesRejected); | ||
| } | ||
|
|
||
| Logger.recordOutput( | ||
| "Vision/Summary/TagPoses", allTagPoses.toArray(new Pose3d[allTagPoses.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Summary/RobotPoses", | ||
| allRobotPoses.toArray(new Pose3d[allRobotPoses.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Summary/RobotPosesAccepted", | ||
| allRobotPosesAccepted.toArray(new Pose3d[allRobotPosesAccepted.size()])); | ||
| Logger.recordOutput( | ||
| "Vision/Summary/RobotPosesRejected", | ||
| allRobotPosesRejected.toArray(new Pose3d[allRobotPosesRejected.size()])); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface VisionConsumer { | ||
| void accept( | ||
| Pose2d visionRobotPoseMeters, | ||
| double timestampSeconds, | ||
| Matrix<N3, N1> visionMeasurementStdDevs); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/frc/robot/subsystems/vision/VisionConstants.java
This file contains hidden or 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,33 @@ | ||
| package frc.robot.subsystems.vision; | ||
|
|
||
| import edu.wpi.first.apriltag.AprilTagFieldLayout; | ||
| import edu.wpi.first.apriltag.AprilTagFields; | ||
| import edu.wpi.first.math.geometry.Rotation3d; | ||
| import edu.wpi.first.math.geometry.Transform3d; | ||
| import edu.wpi.first.math.geometry.Translation3d; | ||
| import edu.wpi.first.math.util.Units; | ||
|
|
||
| public class VisionConstants { | ||
| public static AprilTagFieldLayout aprilTagLayout = | ||
| AprilTagFieldLayout.loadField(AprilTagFields.k2024Crescendo); | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this if it's not being used |
||
| public static double maxAmbiguity = 0.3; | ||
| public static double maxZError = 0.75; | ||
|
|
||
| public static double linearStdDevBaseline = 0.02; // Meters | ||
| public static double angularStdDevBaseline = 0.06; // Radians | ||
|
|
||
| public static double[] cameraStdDevFactors = new double[] {1.0, 1.0}; | ||
|
|
||
| public static double linearStdDevMegatag2Factor = 0.5; | ||
| public static double angularStdDevMegatag2Factor = Double.POSITIVE_INFINITY; | ||
|
|
||
| public static Transform3d frontCamTrans = | ||
| new Transform3d( | ||
| new Translation3d( | ||
| Units.inchesToMeters(17), | ||
| Units.inchesToMeters(0), | ||
| Units.inchesToMeters(21)), | ||
| new Rotation3d(0, Math.toRadians(180), Math.toRadians(180))); | ||
| } | ||
This file contains hidden or 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,34 @@ | ||
| package frc.robot.subsystems.vision; | ||
|
|
||
| import edu.wpi.first.math.geometry.Pose3d; | ||
| import edu.wpi.first.math.geometry.Rotation2d; | ||
| import org.littletonrobotics.junction.AutoLog; | ||
|
|
||
| public interface VisionIO { | ||
| @AutoLog | ||
| class VisionIOInputs { | ||
| public boolean connected = false; | ||
| public TargetObservation latestTargetObservation = | ||
| new TargetObservation(new Rotation2d(), new Rotation2d()); | ||
| public PoseObservation[] poseObservations = new PoseObservation[0]; | ||
| public int[] tagIds = new int[0]; | ||
| } | ||
|
|
||
| record TargetObservation(Rotation2d tx, Rotation2d ty) {} | ||
|
|
||
| record PoseObservation( | ||
| double timestamp, | ||
| Pose3d pose, | ||
| double ambiguity, | ||
| int tagCount, | ||
| double averageTagDistance, | ||
| PoseObservationType type) {} | ||
|
|
||
| enum PoseObservationType { | ||
| MEGATAG_1, | ||
| MEGATAG_2, | ||
| PHOTONVISION | ||
| } | ||
|
|
||
| default void updateInputs(VisionIOInputs inputs) {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.