Skip to content

Commit

Permalink
added photon camera disconnected alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
catr1xLiu committed Aug 7, 2024
1 parent 85149ce commit a23175c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/main/java/frc/robot/subsystems/drive/SwerveDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class SwerveDrive extends MapleSubsystem implements HolonomicDriveSubsyst
private final SwerveDrivePoseEstimator poseEstimator;

private final OdometryThread odometryThread;
private final Alert gyroDisconnected = new Alert("Gyro Hardware Fault", Alert.AlertType.ERROR);
private final Alert gyroDisconnectedAlert = new Alert("Gyro Hardware Fault", Alert.AlertType.ERROR);
public SwerveDrive(GyroIO gyroIO, ModuleIO frontLeftModuleIO, ModuleIO frontRightModuleIO, ModuleIO backLeftModuleIO, ModuleIO backRightModuleIO, MapleConfigFile.ConfigBlock generalConfigBlock) {
super("Drive");
this.gyroIO = gyroIO;
Expand Down Expand Up @@ -82,7 +82,7 @@ kinematics, rawGyroRotation, lastModulePositions, new Pose2d(),
this.odometryThreadInputs = new OdometryThreadInputsAutoLogged();
this.odometryThread.start();

gyroDisconnected.setActivated(false);
gyroDisconnectedAlert.setActivated(false);
}

@Override
Expand Down Expand Up @@ -111,7 +111,7 @@ private void fetchOdometryInputs() {

gyroIO.updateInputs(gyroInputs);
Logger.processInputs("Drive/Gyro", gyroInputs);
gyroDisconnected.setActivated(!gyroInputs.connected);
gyroDisconnectedAlert.setActivated(!gyroInputs.connected);

odometryThread.unlockOdometry();
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/frc/robot/subsystems/drive/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class SwerveModule extends MapleSubsystem {
private SwerveModuleState setPoint;
private SwerveModulePosition[] odometryPositions = new SwerveModulePosition[]{};

private final Alert alert;
private final Alert hardwareFaultAlert;

public SwerveModule(ModuleIO io, String name) {
super("Module-" + name);
this.io = io;
this.name = name;
this.alert = new Alert(
this.hardwareFaultAlert = new Alert(
"Module-" + name + " Hardware Fault",
Alert.AlertType.ERROR
);
this.alert.setActivated(false);
this.hardwareFaultAlert.setActivated(false);

driveOpenLoop = InterpolatedMotorFeedForward.fromDeployedDirectory("DrivingMotorOpenLoop");
turnCloseLoop = new MaplePIDController(Constants.SwerveModuleConfigs.steerHeadingCloseLoopConfig);
Expand All @@ -61,7 +61,7 @@ public void onReset() {
public void updateOdometryInputs() {
io.updateInputs(inputs);
Logger.processInputs("Drive/Module-" + name, inputs);
this.alert.setActivated(!inputs.hardwareConnected);
this.hardwareFaultAlert.setActivated(!inputs.hardwareConnected);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package frc.robot.subsystems.vision.apriltags;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import frc.robot.subsystems.MapleSubsystem;
import frc.robot.subsystems.drive.HolonomicDriveSubsystem;
import frc.robot.utils.Alert;
import frc.robot.utils.Config.PhotonCameraProperties;
import org.littletonrobotics.junction.Logger;

Expand All @@ -21,10 +21,19 @@ public class AprilTagVision extends MapleSubsystem {

private final MapleMultiTagPoseEstimator multiTagPoseEstimator;
private final HolonomicDriveSubsystem driveSubsystem;
private final Alert[] camerasDisconnectedAlerts;
public AprilTagVision(AprilTagVisionIO io, List<PhotonCameraProperties> camerasProperties, HolonomicDriveSubsystem driveSubsystem) {
super("Vision");
this.io = io;
this.inputs = new AprilTagVisionIO.VisionInputs(camerasProperties.size());
this.camerasDisconnectedAlerts = new Alert[camerasProperties.size()];
for (int i = 0; i < camerasProperties.size(); i++) {
this.camerasDisconnectedAlerts[i] = new Alert(
"Photon Camera " + i + " '" + camerasProperties.get(i).name + "' disconnected",
Alert.AlertType.WARNING
);
this.camerasDisconnectedAlerts[i].setActivated(false);
}

this.multiTagPoseEstimator = new MapleMultiTagPoseEstimator(
fieldLayout,
Expand All @@ -44,6 +53,9 @@ public void periodic(double dt, boolean enabled) {
io.updateInputs(inputs);
Logger.processInputs(APRIL_TAGS_VISION_PATH + "Inputs", inputs);

for (int i = 0; i < inputs.camerasInputs.length; i++)
this.camerasDisconnectedAlerts[i].setActivated(!inputs.camerasInputs[i].cameraConnected);

Optional<RobotPoseEstimationResult> result = multiTagPoseEstimator.estimateRobotPose(inputs.camerasInputs, driveSubsystem.getPose());
result = discardResultIfOverThreshold(result);
result.ifPresent(robotPoseEstimationResult -> driveSubsystem.addVisionMeasurement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.photonvision.targeting.PhotonPipelineResult;
import org.photonvision.targeting.PhotonTrackedTarget;

import java.util.Arrays;

public interface AprilTagVisionIO {
int MAX_SUPPORTED_CAMERA_AMOUNT = 10;
class CameraInputs {
public boolean cameraConnected;
public double resultsDelaySeconds;
Expand Down Expand Up @@ -67,6 +64,7 @@ class VisionInputs implements LoggableInputs {
public final int camerasAmount;
public final CameraInputs[] camerasInputs;
public double inputsFetchedRealTimeStampSeconds = 0;
public boolean coprocessorConnected = false;

public VisionInputs(int camerasAmount) {
this.camerasAmount = camerasAmount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public class AprilTagVisionIOReal implements AprilTagVisionIO {
protected final PhotonCamera[] cameras;
public AprilTagVisionIOReal(List<PhotonCameraProperties> cameraProperties) {
if (cameraProperties.size() > MAX_SUPPORTED_CAMERA_AMOUNT)
throw new IllegalArgumentException("max supported camera count is 10");
if (cameraProperties.size() > 16)
throw new IllegalArgumentException("max supported camera count is 16");
cameras = new PhotonCamera[cameraProperties.size()];

for (int i = 0; i < cameraProperties.size(); i++)
Expand Down

0 comments on commit a23175c

Please sign in to comment.