Skip to content

Commit

Permalink
starting 6328-style state estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonwatson committed Oct 15, 2024
1 parent 7c03b2d commit e62e16b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 89 deletions.
33 changes: 6 additions & 27 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import frc.lib.util.FieldConstants;
import frc.lib.util.photon.PhotonCameraWrapper;
import frc.robot.Robot.RobotRunType;
import frc.robot.autos.JustShoot1;
import frc.robot.autos.P123;
Expand All @@ -45,6 +44,7 @@
import frc.robot.subsystems.swerve.Swerve;
import frc.robot.subsystems.swerve.SwerveIO;
import frc.robot.subsystems.swerve.SwerveReal;
import frc.robot.subsystems.vision.Vision;


/**
Expand Down Expand Up @@ -106,9 +106,9 @@ public class RobotContainer {
private Swerve s_Swerve;
private Shooter shooter;
private Intake intake;
private PhotonCameraWrapper[] cameras;
private ElevatorWrist elevatorWrist;
private LEDs leds = new LEDs(Constants.LEDConstants.LED_COUNT, Constants.LEDConstants.PWM_PORT);
private Vision vision;
// private PhotonCamera backLeftCamera = new PhotonCamera("back-left");


Expand All @@ -124,43 +124,26 @@ public RobotContainer(RobotRunType runtimeType) {
for (int i = 0; i < 7; i++) {
numNoteChooser.addOption(String.valueOf(i), i);
}
cameras =
/*
* Camera Order: 0 - Front Left 1 - Front RIght 2 - Back Left 3 - Back Right
*/
new PhotonCameraWrapper[] {
// new PhotonCameraWrapper(
// new PhotonReal(Constants.CameraConstants.FrontLeftFacingCamera.CAMERA_NAME,
// Constants.CameraConstants.FrontLeftFacingCamera.CAMERA_IP),
// Constants.CameraConstants.FrontLeftFacingCamera.KCAMERA_TO_ROBOT),
new PhotonCameraWrapper(
Constants.CameraConstants.FrontRightFacingCamera.CAMERA_NAME,
Constants.CameraConstants.FrontRightFacingCamera.CAMERA_IP,
Constants.CameraConstants.FrontRightFacingCamera.KCAMERA_TO_ROBOT),
// new PhotonCameraWrapper(
// new PhotonReal(Constants.CameraConstants.BackLeftFacingCamera.CAMERA_NAME,
// Constants.CameraConstants.BackLeftFacingCamera.CAMERA_IP),
// Constants.CameraConstants.BackLeftFacingCamera.KCAMERA_TO_ROBOT)
};
// new PhotonCameraWrapper(
// new PhotonReal(Constants.CameraConstants.BackRightFacingCamera.CAMERA_NAME),
// Constants.CameraConstants.BackRightFacingCamera.KCAMERA_TO_ROBOT)};

vision = new Vision(runtimeType == Robot.RobotRunType.kSimulation);
switch (runtimeType) {
case kReal:
shooter = new Shooter(new ShooterVortex());
intake = new Intake(new IntakeIOFalcon());
s_Swerve = new Swerve(new SwerveReal(), cameras);
s_Swerve = new Swerve(new SwerveReal());
elevatorWrist = new ElevatorWrist(new ElevatorWristReal(), operator);
break;
case kSimulation:
s_Swerve = new Swerve(new SwerveIO() {}, cameras);
s_Swerve = new Swerve(new SwerveIO() {});
shooter = new Shooter(new ShooterIO() {});
intake = new Intake(new IntakeIO() {});
elevatorWrist = new ElevatorWrist(new ElevatorWristIO() {}, operator);
break;
default:
s_Swerve = new Swerve(new SwerveIO() {}, cameras);
s_Swerve = new Swerve(new SwerveIO() {});
shooter = new Shooter(new ShooterIO() {});
intake = new Intake(new IntakeIO() {});
elevatorWrist = new ElevatorWrist(new ElevatorWristIO() {}, operator);
Expand Down Expand Up @@ -200,8 +183,6 @@ private void configureButtonBindings() {

/* Driver Buttons */
driver.y().onTrue(new InstantCommand(() -> s_Swerve.resetFieldRelativeOffset()));
driver.start().onTrue(
new InstantCommand(() -> s_Swerve.resetPvInitialization()).ignoringDisable(true));
// intake forward
driver.rightTrigger().whileTrue(CommandFactory.newIntakeCommand(intake, elevatorWrist));
// intake backward
Expand All @@ -211,8 +192,6 @@ private void configureButtonBindings() {
/* Operator Buttons */
// spit note currently in robot through shooter
operator.x().whileTrue(CommandFactory.spit(shooter, intake));
// reset apriltag vision
operator.b().onTrue(new InstantCommand(() -> s_Swerve.resetPvInitialization()));
// spin up shooter
operator.leftTrigger().whileTrue(shooter.shootSpeaker());
// operator.leftTrigger()
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/frc/robot/RobotState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package frc.robot;

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.Rotation2d;
import edu.wpi.first.math.geometry.Twist2d;
import edu.wpi.first.math.interpolation.TimeInterpolatableBuffer;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.math.kinematics.SwerveDriveWheelPositions;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;

public class RobotState {

Check warning on line 15 in src/main/java/frc/robot/RobotState.java

View workflow job for this annotation

GitHub Actions / Linting

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/frc/robot/RobotState.java:15:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

public record OdometryObservation(SwerveDriveWheelPositions wheelPositions,

Check warning on line 17 in src/main/java/frc/robot/RobotState.java

View workflow job for this annotation

GitHub Actions / Linting

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/frc/robot/RobotState.java:17:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
Rotation2d gyroAngle, double timestamp) {
}

public record VisionObservation(Pose2d visionPose, double timestamp, Matrix<N3, N1> stdDevs) {

Check warning on line 21 in src/main/java/frc/robot/RobotState.java

View workflow job for this annotation

GitHub Actions / Linting

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/frc/robot/RobotState.java:21:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
}

private static final double poseBufferSizeSeconds = 2.0;

private static RobotState instance = new RobotState();

public static RobotState getInstance() {
return instance;
}

private Pose2d odometryPose = new Pose2d();
private Pose2d estimatedPose = new Pose2d();
private final TimeInterpolatableBuffer<Pose2d> poseBuffer =
TimeInterpolatableBuffer.createBuffer(poseBufferSizeSeconds);
private final Matrix<N3, N1> qStdDevs =
new Matrix<>(VecBuilder.fill(Math.pow(0.003, 2), Math.pow(0.003, 2), Math.pow(0.0002, 2)));
// Odometry
private final SwerveDriveKinematics kinematics;
private SwerveDriveWheelPositions lastWheelPositions =
new SwerveDriveWheelPositions(new SwerveModulePosition[] {new SwerveModulePosition(),
new SwerveModulePosition(), new SwerveModulePosition(), new SwerveModulePosition()});
private Rotation2d lastGyroAngle = new Rotation2d();
private Twist2d robotVelocity = new Twist2d();
private Twist2d trajectoryVelocity = new Twist2d();

private RobotState() {
this.kinematics = Constants.Swerve.swerveKinematics;
}

public void addOdometryObservation(OdometryObservation observation) {
// TODO
// https://github.com/Mechanical-Advantage/RobotCode2024/blob/a025615a52193b7709db7cf14c51c57be17826f2/src/main/java/org/littletonrobotics/frc2024/RobotState.java#L160
}

public void addVisionObservation(VisionObservation observation) {
// TODO
// https://github.com/Mechanical-Advantage/RobotCode2024/blob/a025615a52193b7709db7cf14c51c57be17826f2/src/main/java/org/littletonrobotics/frc2024/RobotState.java#L181
}

}
63 changes: 1 addition & 62 deletions src/main/java/frc/robot/subsystems/swerve/Swerve.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package frc.robot.subsystems.swerve;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import org.littletonrobotics.junction.AutoLogOutput;
import org.littletonrobotics.junction.Logger;
Expand All @@ -15,16 +13,13 @@
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.networktables.GenericEntry;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.shuffleboard.BuiltInWidgets;
import edu.wpi.first.wpilibj.smartdashboard.Field2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.lib.util.FieldConstants;
import frc.lib.util.photon.PhotonCameraWrapper;
import frc.lib.util.swerve.SwerveModule;
import frc.robot.Constants;
import frc.robot.Robot;
Expand All @@ -40,21 +35,12 @@ public class Swerve extends SubsystemBase {
private double fieldOffset;
private SwerveInputsAutoLogged inputs = new SwerveInputsAutoLogged();
private SwerveIO swerveIO;
private boolean hasInitialized = false;
private PhotonCameraWrapper[] cameras;
private Boolean[] cameraSeesTarget = {false, false, false, false};

private GenericEntry aprilTagTarget = RobotContainer.mainDriverTab.add("See April Tag", false)
.withWidget(BuiltInWidgets.kBooleanBox)
.withProperties(Map.of("Color when true", "green", "Color when false", "red"))
.withPosition(11, 0).withSize(2, 2).getEntry();

/**
* Swerve Subsystem
*/
public Swerve(SwerveIO swerveIO, PhotonCameraWrapper[] cameras) {
public Swerve(SwerveIO swerveIO) {
this.swerveIO = swerveIO;
this.cameras = cameras;
fieldOffset = getGyroYaw().getDegrees();
swerveMods = new SwerveModule[] {
swerveIO.createSwerveModule(0, Constants.Swerve.Mod0.driveMotorID,
Expand Down Expand Up @@ -241,10 +227,6 @@ public void resetModulesToAbsolute() {
}
}

public void resetPvInitialization() {
hasInitialized = false;
}

@Override
public void periodic() {
Robot.profiler.push("swerve_periodic");
Expand All @@ -258,57 +240,14 @@ public void periodic() {
swerveOdometry.update(getGyroYaw(), getModulePositions());
Robot.profiler.swap("process_inputs");
Logger.processInputs("Swerve", inputs);
Robot.profiler.swap("process_cameras");
for (int i = 0; i < cameras.length; i++) {
cameras[i].periodic();
cameraSeesTarget[i] = cameras[i].seesTarget();
}
Robot.profiler.swap("do_camera_stuff");
Logger.recordOutput("/Swerve/hasInitialized", hasInitialized);
if (!hasInitialized && !DriverStation.isAutonomous()) {
Robot.profiler.push("init");
for (int i = 0; i < cameras.length; i++) {
var robotPose = cameras[i].getInitialPose();
Logger.recordOutput("/Swerve/hasInitialPose[" + i + "]", robotPose.isPresent());

if (robotPose.isPresent()) {
swerveOdometry.resetPosition(getGyroYaw(), getModulePositions(),
robotPose.get().robotPose);
hasInitialized = true;
break;
}
}
Robot.profiler.pop();
} else {
Robot.profiler.push("update");
for (int i = 0; i < cameras.length; i++) {
var result = cameras[i].getEstimatedGlobalPose(getPose());
if (result.isPresent()) {
if (DriverStation.isAutonomous() && result.get().targetsUsed.size() < 2) {
continue;
} else if (result.get().targetsUsed.size() == 1
&& result.get().targetsUsed.get(0).getPoseAmbiguity() > 0.1) {
continue;
}
swerveOdometry.addVisionMeasurement(result.get().estimatedPose.toPose2d(),
Timer.getFPGATimestamp() - cameras[i].latency());
}
}
Robot.profiler.pop();
}
Robot.profiler.swap("update_shuffleboard");
Robot.profiler.push("field");
field.setRobotPose(getPose());
Robot.profiler.swap("apriltag");
aprilTagTarget
.setBoolean(Arrays.asList(cameraSeesTarget).stream().anyMatch(val -> val == true));

Robot.profiler.swap("dist-to-speaker");
SmartDashboard.putNumber("Distance to Speaker",
FieldConstants.allianceFlip(FieldConstants.Speaker.centerSpeakerOpening)
.getTranslation().minus(getPose().getTranslation()).getNorm());
Robot.profiler.swap("simple");
SmartDashboard.putBoolean("Has Initialized", hasInitialized);
SmartDashboard.putNumber("Gyro Yaw", getGyroYaw().getDegrees());
Robot.profiler.pop();
Robot.profiler.pop();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/frc/robot/subsystems/vision/Vision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package frc.robot.subsystems.vision;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.lib.util.photon.PhotonCameraWrapper;
import frc.robot.Constants;

public class Vision extends SubsystemBase {

Check warning on line 7 in src/main/java/frc/robot/subsystems/vision/Vision.java

View workflow job for this annotation

GitHub Actions / Linting

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/frc/robot/subsystems/vision/Vision.java:7:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

private final PhotonCameraWrapper[] cameras;

public Vision(boolean isSim) {

Check warning on line 11 in src/main/java/frc/robot/subsystems/vision/Vision.java

View workflow job for this annotation

GitHub Actions / Linting

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/frc/robot/subsystems/vision/Vision.java:11:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
super("Vision");
cameras = new PhotonCameraWrapper[] {
new PhotonCameraWrapper(Constants.CameraConstants.FrontRightFacingCamera.CAMERA_NAME,
Constants.CameraConstants.FrontRightFacingCamera.CAMERA_IP,
Constants.CameraConstants.FrontRightFacingCamera.KCAMERA_TO_ROBOT)};
}

@Override
public void periodic() {
for (var cam : cameras) {
cam.periodic();
}
// TODO
// https://github.com/Mechanical-Advantage/RobotCode2024/blob/a025615a52193b7709db7cf14c51c57be17826f2/src/main/java/org/littletonrobotics/frc2024/subsystems/apriltagvision/AprilTagVision.java#L59
}

}

0 comments on commit e62e16b

Please sign in to comment.