-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing out the vision into a separate subsystem
modified: src/main/java/frc/robot/Constants.java modified: src/main/java/frc/robot/RobotContainer.java modified: src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java renamed: src/main/java/frc/robot/subsystems/swervedrive/Vision.java -> src/main/java/frc/robot/subsystems/vision/Vision.java new file: src/main/java/frc/robot/subsystems/vision/VisionIO.java new file: src/main/java/frc/robot/subsystems/vision/VisionIOPhoton.java
- Loading branch information
Showing
6 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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,54 @@ | ||
// Copyright (c) 2024 Az-FIRST | ||
// http://github.com/AZ-First | ||
// | ||
// This program is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU General Public License | ||
// version 3 as published by the Free Software Foundation or | ||
// available in the root directory of this project. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
package frc.robot.subsystems.vision; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.littletonrobotics.junction.LogTable; | ||
import org.littletonrobotics.junction.inputs.LoggableInputs; | ||
import org.photonvision.targeting.PhotonTrackedTarget; | ||
|
||
public interface VisionIO { | ||
|
||
class AprilTagVisionIOInputs implements LoggableInputs { | ||
public String camname = ""; | ||
public double latency = 0.0; | ||
public double timestamp = 0.0; | ||
public List<PhotonTrackedTarget> targets = new ArrayList<PhotonTrackedTarget>() {}; | ||
|
||
public long fps = 0; | ||
|
||
@Override | ||
public void toLog(LogTable table) { | ||
table.put("Latency", latency); | ||
table.put("Timestamp", timestamp); | ||
table.put("TargetCount", targets.size()); | ||
for (int i = 0; i < targets.size(); i++) { | ||
table.put("Target/" + i, targets.get(i)); | ||
} | ||
table.put("Fps", fps); | ||
} | ||
|
||
@Override | ||
public void fromLog(LogTable table) { | ||
latency = table.get("Latency", 0.0); | ||
timestamp = table.get("Timestamp", 0.0); | ||
int targetCount = table.get("TargetCount", 0); | ||
targets = new ArrayList<PhotonTrackedTarget>(targetCount); | ||
fps = table.get("Fps", 0); | ||
} | ||
} | ||
|
||
default void updateInputs(AprilTagVisionIOInputs inputs) {} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/frc/robot/subsystems/vision/VisionIOPhoton.java
This file contains 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,77 @@ | ||
// Copyright (c) 2024 Az-FIRST | ||
// http://github.com/AZ-First | ||
// | ||
// This program is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU General Public License | ||
// version 3 as published by the Free Software Foundation or | ||
// available in the root directory of this project. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
package frc.robot.subsystems.vision; | ||
|
||
import edu.wpi.first.math.geometry.Transform3d; | ||
import edu.wpi.first.wpilibj.Timer; | ||
import frc.robot.Constants.AprilTagLayoutType; | ||
import frc.robot.util.Alert; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import org.littletonrobotics.junction.Logger; | ||
import org.photonvision.PhotonCamera; | ||
import org.photonvision.targeting.PhotonTrackedTarget; | ||
|
||
public class VisionIOPhoton implements VisionIO { | ||
|
||
private final Supplier<AprilTagLayoutType> aprilTagTypeSupplier; | ||
private AprilTagLayoutType lastAprilTagType = null; | ||
|
||
private static final double disconnectedTimeout = 0.5; | ||
private final Alert disconnectedAlert; | ||
private final Timer disconnectedTimer = new Timer(); | ||
public final PhotonCamera camera; | ||
public final String camname; | ||
|
||
public VisionIOPhoton(Supplier<AprilTagLayoutType> aprilTagTypeSupplier, String camname) { | ||
this.aprilTagTypeSupplier = aprilTagTypeSupplier; | ||
this.camname = camname; | ||
this.camera = new PhotonCamera(camname); | ||
|
||
disconnectedAlert = new Alert("No data from \"" + camname + "\"", Alert.AlertType.ERROR); | ||
disconnectedTimer.start(); | ||
} | ||
|
||
public void updateInputs(AprilTagVisionIOInputs inputs) { | ||
// Get observations | ||
var result = camera.getLatestResult(); | ||
|
||
// Log the entire result for each camera to AdvantageKit | ||
Logger.recordOutput("PhotonVision/" + camname, result); // result.getLatencyMillis()); | ||
|
||
// Put the relevant information into the `inputs` | ||
inputs.camname = camname; | ||
inputs.latency = result.getLatencyMillis(); | ||
inputs.timestamp = result.getTimestampSeconds(); | ||
|
||
if (result.hasTargets()) { | ||
List<PhotonTrackedTarget> targets = result.getTargets(); | ||
inputs.targets = targets; | ||
for (PhotonTrackedTarget target : targets) { | ||
// Get information from target. | ||
int targetID = target.getFiducialId(); | ||
double poseAmbiguity = target.getPoseAmbiguity(); | ||
Transform3d bestCameraToTarget = target.getBestCameraToTarget(); | ||
Transform3d alternateCameraToTarget = target.getAlternateCameraToTarget(); | ||
|
||
String logTag = "PhotonVision/Tag" + Integer.toString(targetID); | ||
Logger.recordOutput(logTag + "/PoseAmbiguity", poseAmbiguity); | ||
Logger.recordOutput(logTag + "/BestCameraToTarget", bestCameraToTarget); | ||
Logger.recordOutput(logTag + "/AlternateCameraToTarget", alternateCameraToTarget); | ||
} | ||
} else { | ||
inputs.targets = null; | ||
} | ||
} | ||
} |