|
| 1 | +package com.team2813.lib2813.vision; |
| 2 | +; |
| 3 | +import edu.wpi.first.math.geometry.Pose2d; |
| 4 | +import edu.wpi.first.math.geometry.Rotation3d; |
| 5 | +import edu.wpi.first.wpilibj.Timer; |
| 6 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 7 | +import org.photonvision.EstimatedRobotPose; |
| 8 | + |
| 9 | +/** |
| 10 | + * Defines a vision subsystem. |
| 11 | + * |
| 12 | + * @param <C> the type for the camera |
| 13 | + */ |
| 14 | +public abstract class VisionSubsystem<C extends Camera> extends SubsystemBase { |
| 15 | + protected final MultiPhotonPoseEstimator<C> poseEstimator; |
| 16 | + |
| 17 | + /** |
| 18 | + * Constructor. |
| 19 | + * |
| 20 | + * @param poseEstimator The pose estimator to use. |
| 21 | + */ |
| 22 | + protected VisionSubsystem(MultiPhotonPoseEstimator<C> poseEstimator) { |
| 23 | + this.poseEstimator = poseEstimator; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor. |
| 28 | + * |
| 29 | + * @param poseEstimator The pose estimator to use. |
| 30 | + * @param name Name of the subsystem for telemetry and logging. |
| 31 | + */ |
| 32 | + protected VisionSubsystem(MultiPhotonPoseEstimator<C> poseEstimator, String name) { |
| 33 | + super(name); |
| 34 | + this.poseEstimator = poseEstimator; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void periodic() { |
| 39 | + if (poseEstimator.poseStrategyRequiresHeadingData()) { |
| 40 | + poseEstimator.addHeadingData(Timer.getTimestamp(), getRotation3d()); |
| 41 | + } |
| 42 | + poseEstimator.processAllUnreadResults(this::handleEstimatedPose, this::handleRejectedPose); |
| 43 | + poseEstimator.publishCameraPosesRelativeTo(getDrivetrainPose()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the current orientation of the robot as a {@link Rotation3d} from |
| 48 | + * the Pigeon 2 quaternion values. |
| 49 | + * |
| 50 | + * @return The robot orientation. |
| 51 | + */ |
| 52 | + protected abstract Rotation3d getRotation3d(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the current pose of the robot as a {@link Pose2d} from the drivetrain. |
| 56 | + * |
| 57 | + * @return The robot position. |
| 58 | + */ |
| 59 | + protected abstract Pose2d getDrivetrainPose(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Called when new estimated robot positions are available. |
| 63 | + * |
| 64 | + * @param estimatedPose The estimated robot position. |
| 65 | + * @param camera The camera. |
| 66 | + */ |
| 67 | + protected abstract void handleEstimatedPose(EstimatedRobotPose estimatedPose, C camera); |
| 68 | + |
| 69 | + /** |
| 70 | + * Called when invalid robot positions were computed by the estimator. |
| 71 | + * |
| 72 | + * @param rejectedPose The rejected estimated robot position. |
| 73 | + */ |
| 74 | + protected void handleRejectedPose(EstimatedRobotPose rejectedPose) {} |
| 75 | +} |
0 commit comments