Skip to content

Commit

Permalink
Add vision pose estimates to drivetrain
Browse files Browse the repository at this point in the history
  • Loading branch information
BrownGenius committed Mar 28, 2024
1 parent 20b8e20 commit 3191307
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/Drive.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package frc.robot.subsystems.drive;

import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.wpilibj2.command.Command;

public interface Drive {
Expand Down Expand Up @@ -66,4 +69,7 @@ public default double getAngle() {

public default void lockPose() {}
;

public default void addVisionMeasurement(
Pose2d robotPose, double timestamp, Matrix<N3, N1> visionMeasurementStdDevs) {}
}
10 changes: 10 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/DriveSwerveYAGSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.pathplanner.lib.util.HolonomicPathFollowerConfig;
import com.pathplanner.lib.util.PIDConstants;
import com.pathplanner.lib.util.ReplanningConfig;
import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine.Config;
Expand Down Expand Up @@ -155,4 +158,11 @@ public void periodic() {
io.updateInputs(inputs, swerveDrive);
Logger.processInputs("Drive", inputs);
}

@Override
public void addVisionMeasurement(
Pose2d robotPose, double timestamp, Matrix<N3, N1> visionMeasurementStdDevs) {

swerveDrive.addVisionMeasurement(robotPose, timestamp, visionMeasurementStdDevs);
}
}
9 changes: 0 additions & 9 deletions src/main/java/frc/robot/subsystems/vision/Vision.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import edu.wpi.first.math.geometry.Pose2d;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

Expand Down Expand Up @@ -83,13 +82,5 @@ public default boolean setPrimaryCamera(String name) {
*/
public Optional<Double> getYawToAprilTag(int id);

/**
* Returns a list of vision-based estimated robot poses
*
* @return list of estimated poses. list length may be zero if updated pose estimation data is not
* available.
*/
public List<VisionPose> getEstimatedRobotPoses();

public default void enableSimulation(Supplier<Pose2d> poseSupplier, boolean enableWireFrame) {}
}
29 changes: 15 additions & 14 deletions src/main/java/frc/robot/subsystems/vision/VisionSubsystem.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package frc.robot.subsystems.vision;

import edu.wpi.first.apriltag.AprilTagFieldLayout;
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.geometry.Transform3d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.config.RobotConfig;
import frc.robot.util.DevilBotState;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -90,8 +92,6 @@ private Transform3d getRobotToCamera() {
private VisionCameraImpl primaryCamera = null;
private final AprilTagFieldLayout fieldLayout;

private List<VisionPose> estimatedPoses = new ArrayList<VisionPose>();

/* Debug Info */
@AutoLogOutput private int debugTargetsVisible;
@AutoLogOutput private int debugCurrentTargetId = DevilBotState.getActiveTargetId();
Expand All @@ -112,7 +112,6 @@ private Transform3d getRobotToCamera() {
public VisionSubsystem(List<VisionCamera> cameras, AprilTagFieldLayout fieldLayout) {
for (VisionCamera camera : cameras) {
this.cameras.add(new VisionCameraImpl(camera, fieldLayout));
estimatedPoses.add(new VisionPose());
}
this.fieldLayout = fieldLayout;
if (0 != cameras.size()) {
Expand Down Expand Up @@ -166,14 +165,21 @@ public void periodic() {

Optional<EstimatedRobotPose> currentEstimatedRobotPose = camera.getEstimatedRobotPose();
if (currentEstimatedRobotPose.isPresent()) {
estimatedPoses.get(camera.getIndex()).robotPose =
currentEstimatedRobotPose.get().estimatedPose.toPose2d();
estimatedPoses.get(camera.getIndex()).timestamp =
currentEstimatedRobotPose.get().timestampSeconds;
estimatedPoses.get(camera.getIndex()).cameraName = camera.getName();
Optional<Double> distanceToBestTarget =
getDistanceToAprilTag(camera.getBestTarget().getFiducialId());
if (distanceToBestTarget.isPresent()) {
double distance = distanceToBestTarget.get();

// Add vision measurement to the drivetrain.
// TODO: clean up this abstraction
RobotConfig.drive.addVisionMeasurement(
currentEstimatedRobotPose.get().estimatedPose.toPose2d(),
currentEstimatedRobotPose.get().timestampSeconds,
VecBuilder.fill(distance / 2, distance / 2, 100));
}

if (camera == primaryCamera) {
debugEstimatedPose = estimatedPoses.get(camera.getIndex()).robotPose;
debugEstimatedPose = currentEstimatedRobotPose.get().estimatedPose.toPose2d();
}
}
}
Expand Down Expand Up @@ -257,11 +263,6 @@ public Optional<Double> getYawToAprilTag(int id) {
return Optional.empty();
}

@Override
public List<VisionPose> getEstimatedRobotPoses() {
return estimatedPoses;
}

@Override
public boolean setPrimaryCamera(String name) {
boolean foundCamera = false;
Expand Down

0 comments on commit 3191307

Please sign in to comment.