Class VisionSystem

java.lang.Object
yall.VisionSystem
All Implemented Interfaces:
AutoCloseable

public class VisionSystem extends Object implements AutoCloseable
Several VisionCameras working together, from any mix of vendors.
VisionSystem vision =
    new VisionSystem(frontLimelight, backPhoton, sideSynapse)
        .withDisconnectDebounce(VisionSystem.DEFAULT_DISCONNECT_DEBOUNCE)
        .withDisconnectAlerts(true)
        .onCameraDisconnected(cam -> sideSynapse.setPipelineIndex(APRILTAG_PIPELINE));

public void periodic() {
  vision.setRobotOrientation(poseEstimator.getEstimatedPosition().getRotation(), gyro.getYawRate());
  vision.update();
  vision.addVisionMeasurements(poseEstimator);
}

public void simulationPeriodic() {
  vision.updateSimulation(drivetrainSim.getPose());
}

Add every estimate to your drivetrain's pose estimator with addVisionMeasurements(PoseEstimator), or with forEachEstimatedPose(Consumer) for a drivetrain with its own addVisionMeasurement method. Each estimate goes in on its own, with the time its image was taken and its own standard deviations, so the pose estimator applies it against the odometry from that moment and trusts each camera as much as it should. Don't combine the cameras' estimates first: that loses their timestamps and standard deviations.

  • Field Details

    • DEFAULT_DISCONNECT_DEBOUNCE

      public static final Time DEFAULT_DISCONNECT_DEBOUNCE
      Default debounce duration before a disconnected camera triggers alerts or failover callbacks.
  • Constructor Details

    • VisionSystem

      public VisionSystem(VisionCamera... cameras)
      Creates a vision system.
      Parameters:
      cameras - The cameras.
  • Method Details

    • withDisconnectDebounce

      public VisionSystem withDisconnectDebounce(Time debounce)
      Sets how long a camera must remain disconnected before onCameraDisconnected(Consumer) and withDisconnectAlerts(boolean) activate, preventing brief NetworkTables hiccups from flapping failover logic.
      Parameters:
      debounce - Non-negative debounce duration (e.g. DEFAULT_DISCONNECT_DEBOUNCE). null disables debouncing (immediate reporting).
      Returns:
      This vision system, for chaining.
    • withDisconnectAlerts

      public VisionSystem withDisconnectAlerts(boolean enabled)
      Enables or disables automatic WPILib Alerts when a camera disconnects.
      Parameters:
      enabled - true to publish a high-priority Alert per disconnected camera.
      Returns:
      This vision system, for chaining.
    • onCameraDisconnected

      public VisionSystem onCameraDisconnected(Consumer<VisionCamera> callback)
      Registers an edge-triggered callback invoked once when a camera transitions into the disconnected state (after any configured withDisconnectDebounce(Time)). Useful for active multi-camera failover, such as switching a surviving game-piece camera to an AprilTag pipeline when the primary localization camera drops mid-match.
      Parameters:
      callback - Callback receiving the disconnected camera, or null to clear.
      Returns:
      This vision system, for chaining.
    • onCameraReconnected

      public VisionSystem onCameraReconnected(Consumer<VisionCamera> callback)
      Registers an edge-triggered callback invoked once when a previously disconnected camera reconnects.
      Parameters:
      callback - Callback receiving the reconnected camera, or null to clear.
      Returns:
      This vision system, for chaining.
    • update

      public void update()
      Updates every camera and evaluates connection health. Call once per loop.
    • update

      public void update(double nowSeconds)
      Updates every camera and evaluates connection health using an explicit monotonic timestamp (useful for deterministic unit tests and log replay).
      Parameters:
      nowSeconds - Current monotonic timestamp in seconds.
    • updateSimulation

      public void updateSimulation(Pose2d robotPose)
      Updates the simulated worlds the cameras are in. Call once per loop from simulationPeriodic.
      Parameters:
      robotPose - The robot's true pose.
    • setRobotOrientation

      public void setRobotOrientation(Rotation3d orientation, AngularVelocity yawRate)
      Gives every camera the robot's orientation on the field: the yaw from your pose estimator, the roll and pitch from the gyro.
      Parameters:
      orientation - Robot orientation on the field.
      yawRate - Robot yaw rate.
      See Also:
    • setRobotOrientation

      public void setRobotOrientation(Rotation2d heading, AngularVelocity yawRate)
      Gives every camera the robot's heading on the field and its yaw rate.
      Parameters:
      heading - Robot heading on the field, your pose estimator's rotation.
      yawRate - Robot yaw rate.
      See Also:
    • setRobotOrientation

      public void setRobotOrientation(Rotation2d heading)
      Gives every camera the robot's heading on the field, without a yaw rate.
      Parameters:
      heading - Robot heading on the field, your pose estimator's rotation.
      See Also:
    • getCameras

      public List<VisionCamera> getCameras()
      The cameras in this system.
      Returns:
      The cameras.
    • getCamera

      public Optional<VisionCamera> getCamera(String name)
      Looks up a camera in this system by its configured name.
      Parameters:
      name - Camera name.
      Returns:
      Matching camera, or empty if not found.
    • setPipelineIndex

      public boolean setPipelineIndex(String cameraName, int pipelineIndex)
      Switches the pipeline on a named camera in this system.
      Parameters:
      cameraName - Name of the camera to switch.
      pipelineIndex - Target pipeline index.
      Returns:
      true if the camera was found and switched, false otherwise.
    • getConnectedCameras

      public List<VisionCamera> getConnectedCameras()
      Cameras that are currently connected.
      Returns:
      Connected cameras in registration order.
    • getDisconnectedCameras

      public List<VisionCamera> getDisconnectedCameras()
      Cameras that are currently disconnected.
      Returns:
      Disconnected cameras in registration order.
    • getConnectedCameraCount

      public int getConnectedCameraCount()
      Number of cameras currently reporting connected.
      Returns:
      Count of connected cameras.
    • hasConnectedCamera

      public boolean hasConnectedCamera()
      Whether at least one camera in this system is currently connected.
      Returns:
      true if one or more cameras are connected.
    • getEstimatedPoses

      public List<VisionPoseEstimate> getEstimatedPoses()
      Every accepted estimate from every camera in the last update().
      Returns:
      The estimates, oldest first.
    • forEachEstimatedPose

      public void forEachEstimatedPose(Consumer<VisionPoseEstimate> consumer)
      Passes every accepted estimate from the last update() to a consumer, oldest first. Use it for drivetrains with their own vision measurement method, passing each estimate's pose, timestamp and standard deviations.
      Parameters:
      consumer - Receives each estimate.
    • addVisionMeasurements

      public void addVisionMeasurements(PoseEstimator<?> poseEstimator)
      Adds every accepted estimate from the last update() to a WPILib pose estimator, oldest first, with its timestamp and standard deviations. Call it once per loop, after update().
      Parameters:
      poseEstimator - The drivetrain's pose estimator.
    • close

      public void close()
      Closes every camera and any active disconnect alerts.
      Specified by:
      close in interface AutoCloseable