Skip to content
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

AprilTag pose estimation #18

Merged
merged 21 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ff8b891
feat: use SwerveDrivePoseEstimator for odometry tracking
mimizh2418 Jan 24, 2024
2c65005
Merge branch 'zhangal/phoenixpro' into zhangal/poseEstimation
mimizh2418 Jan 28, 2024
cc0abdf
feat: AprilTag vision IO and implementation for Limelight
mimizh2418 Jan 29, 2024
255352c
Merge branch 'zhangal/phoenixPro' into zhangal/poseEstimation
mimizh2418 Jan 29, 2024
e786243
feat: AprilTagVision subsystem
mimizh2418 Jan 29, 2024
921462c
feat: fuse vision poses with drivetrain poses
mimizh2418 Jan 30, 2024
6e3b8cb
fix: null vision poses no longer causing issues
mimizh2418 Jan 30, 2024
5dfb4ef
fix: actually fix null vision poses
mimizh2418 Jan 30, 2024
9f51d51
Merge branch 'zhangal/phoenixPro' into zhangal/poseEstimation
mimizh2418 Jan 30, 2024
5867759
feat: limelight positioning adjusts based on elevator height
mimizh2418 Jan 30, 2024
3c90cda
fix: properly update rear camera tag poses
mimizh2418 Jan 30, 2024
45acad7
feat: block vision updates with moving elevator
mimizh2418 Jan 30, 2024
138a18e
fix: correctly pass in suppliers
mimizh2418 Jan 30, 2024
4e307cb
fix: use optionals for getting vision pose
mimizh2418 Jan 30, 2024
32b504b
feat: change how vision pose accepter is used
mimizh2418 Jan 31, 2024
5a356c0
feat: untested vision sim io with photonvision sim support
mimizh2418 Feb 1, 2024
1bf8e01
fix: use vision pose in sim
mimizh2418 Feb 1, 2024
695f5c3
fix: sim values as constants
mimizh2418 Feb 1, 2024
79b12f5
refactor: remove unnecessary IO input
mimizh2418 Feb 1, 2024
577b050
refactor: april tag layout declared in sim io instead of constants
mimizh2418 Feb 2, 2024
0647ed1
fix: correctly check robot rotational velocity
mimizh2418 Feb 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/org/team1540/robot2024/Constants.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.team1540.robot2024;

import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.apriltag.AprilTagFields;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.util.Units;

import java.io.IOException;

/**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
* constants. This class should not be used for any other purpose. All constants should be declared
Expand Down Expand Up @@ -78,6 +82,15 @@ public static class Vision {
public static final double MIN_ACCEPTED_NUM_TAGS = 1;
public static final double MAX_ACCEPTED_AVG_TAG_DIST_METERS = 8.0;
public static final double MAX_ACCEPTED_ELEVATOR_SPEED_MPS = 0.05;

public static final AprilTagFieldLayout SIM_APRILTAG_LAYOUT;
rutmanz marked this conversation as resolved.
Show resolved Hide resolved
static {
try {
SIM_APRILTAG_LAYOUT = AprilTagFieldLayout.loadFromResource(AprilTagFields.k2024Crescendo.m_resourceFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public static class Elevator {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/team1540/robot2024/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.team1540.robot2024.subsystems.vision.AprilTagVision;
import org.team1540.robot2024.subsystems.vision.AprilTagVisionIO;
import org.team1540.robot2024.subsystems.vision.AprilTagVisionIOLimelight;
import org.team1540.robot2024.subsystems.vision.AprilTagVisionIOSim;
import org.team1540.robot2024.util.PhoenixTimeSyncSignalRefresher;
import org.team1540.robot2024.util.swerve.SwerveFactory;
import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;
Expand Down Expand Up @@ -84,11 +85,11 @@ public RobotContainer() {
shooter = new Shooter(new ShooterPivotIOSim(), new FlywheelsIOSim());
aprilTagVision =
new AprilTagVision(
new AprilTagVisionIO() {},
new AprilTagVisionIO() {},
(ignored) -> {},
() -> 0.0,
new VisionPoseAcceptor(drivetrain::getChassisSpeeds, () -> 0.0));
new AprilTagVisionIOSim(Constants.Vision.FRONT_CAMERA_NAME, Constants.Vision.FRONT_CAMERA_POSE, drivetrain::getPose),
new AprilTagVisionIOSim(Constants.Vision.REAR_CAMERA_NAME, Constants.Vision.REAR_CAMERA_POSE, drivetrain::getPose),
ignored -> {},
() -> 0.0, // TODO: ACTUALLY GET ELEVATOR HEIGHT HERE
new VisionPoseAcceptor(drivetrain::getChassisSpeeds, () -> 0.0)); // TODO: ACTUALLY GET ELEVATOR VELOCITY HERE
break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.team1540.robot2024.subsystems.vision;

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.geometry.Transform3d;
import org.photonvision.EstimatedRobotPose;
import org.photonvision.PhotonCamera;
import org.photonvision.PhotonPoseEstimator;
import org.photonvision.simulation.PhotonCameraSim;
import org.photonvision.simulation.SimCameraProperties;
import org.photonvision.simulation.VisionSystemSim;
import org.photonvision.targeting.PhotonTrackedTarget;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static org.team1540.robot2024.Constants.Vision.*;

public class AprilTagVisionIOSim implements AprilTagVisionIO {
private final VisionSystemSim visionSystemSim;
private final PhotonCameraSim cameraSim;
private final PhotonCamera camera;
private final PhotonPoseEstimator photonEstimator;

private final Supplier<Pose2d> poseSupplier;

private Transform3d cameraTransform;
private Pose2d lastEstimatedPose;

public AprilTagVisionIOSim(String name, Pose3d cameraOffsetMeters, Supplier<Pose2d> poseSupplier) {
this.visionSystemSim = new VisionSystemSim(name);
this.poseSupplier = poseSupplier;
visionSystemSim.addAprilTags(SIM_APRILTAG_LAYOUT);

SimCameraProperties cameraProps = new SimCameraProperties();
cameraProps.setCalibration(1280, 960, Rotation2d.fromDegrees(100));
cameraProps.setFPS(14.5);
cameraProps.setAvgLatencyMs(67.0);
camera = new PhotonCamera(name);
cameraSim = new PhotonCameraSim(camera, cameraProps);

cameraTransform = new Transform3d(cameraOffsetMeters.getTranslation(), cameraOffsetMeters.getRotation());
visionSystemSim.addCamera(cameraSim, cameraTransform);

photonEstimator = new PhotonPoseEstimator(
SIM_APRILTAG_LAYOUT,
PhotonPoseEstimator.PoseStrategy.MULTI_TAG_PNP_ON_COPROCESSOR,
camera,
cameraTransform);
photonEstimator.setMultiTagFallbackStrategy(PhotonPoseEstimator.PoseStrategy.CLOSEST_TO_LAST_POSE);
lastEstimatedPose = poseSupplier.get();
}

@Override
public void updateInputs(AprilTagVisionIOInputs inputs) {
photonEstimator.setReferencePose(lastEstimatedPose);
visionSystemSim.update(poseSupplier.get());

Optional<EstimatedRobotPose> estimatedPose = photonEstimator.update();
List<PhotonTrackedTarget> trackedTargets = camera.getLatestResult().getTargets();

if (estimatedPose.isPresent()) {
lastEstimatedPose = estimatedPose.get().estimatedPose.toPose2d();
inputs.estimatedPoseMeters = lastEstimatedPose;
inputs.lastMeasurementTimestampSecs = estimatedPose.get().timestampSeconds;
}
inputs.hasTarget = !trackedTargets.isEmpty();
inputs.seenTagIDs = new int[trackedTargets.size()];
inputs.tagPosesMeters = new Pose2d[trackedTargets.size()];
for (int i = 0; i < trackedTargets.size(); i++) {
PhotonTrackedTarget target = trackedTargets.get(i);
inputs.seenTagIDs[i] = target.getFiducialId();
inputs.tagPosesMeters[i] =
new Pose3d().plus(target.getBestCameraToTarget().plus(cameraTransform.inverse())).toPose2d();
}
}

@Override
public void setPoseOffset(Pose3d newPose) {
cameraTransform = new Transform3d(newPose.getTranslation(), newPose.getRotation());
visionSystemSim.adjustCamera(cameraSim, cameraTransform);
}

@Override
public String getName() {
return camera.getName();
}
}
57 changes: 57 additions & 0 deletions vendordeps/photonlib-json-1.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"fileName": "photonlib.json",
"name": "photonlib",
"version": "v2024.2.2",
"uuid": "515fe07e-bfc6-11fa-b3de-0242ac130004",
"frcYear": "2024",
"mavenUrls": [
"https://maven.photonvision.org/repository/internal",
"https://maven.photonvision.org/repository/snapshots"
],
"jsonUrl": "https://maven.photonvision.org/repository/internal/org/photonvision/photonlib-json/1.0/photonlib-json-1.0.json",
"jniDependencies": [],
"cppDependencies": [
{
"groupId": "org.photonvision",
"artifactId": "photonlib-cpp",
"version": "v2024.2.2",
"libName": "photonlib",
"headerClassifier": "headers",
"sharedLibrary": true,
"skipInvalidPlatforms": true,
"binaryPlatforms": [
"windowsx86-64",
"linuxathena",
"linuxx86-64",
"osxuniversal"
]
},
{
"groupId": "org.photonvision",
"artifactId": "photontargeting-cpp",
"version": "v2024.2.2",
"libName": "photontargeting",
"headerClassifier": "headers",
"sharedLibrary": true,
"skipInvalidPlatforms": true,
"binaryPlatforms": [
"windowsx86-64",
"linuxathena",
"linuxx86-64",
"osxuniversal"
]
}
],
"javaDependencies": [
{
"groupId": "org.photonvision",
"artifactId": "photonlib-java",
"version": "v2024.2.2"
},
{
"groupId": "org.photonvision",
"artifactId": "photontargeting-java",
"version": "v2024.2.2"
}
]
}
Loading