Class VisionCamera

java.lang.Object
yall.VisionCamera
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
LimelightWrapper, PhotonVisionWrapper, QuestNavWrapper, SynapseWrapper

public abstract class VisionCamera extends Object implements AutoCloseable
A camera from any supported vendor, with one API for targets and robot pose estimates. On a real robot it reads the vendor's library; in simulation it reads a PhotonVision camera simulation in a VisionSim world, so the same robot code runs in both.

Create one with the wrapper for the camera's vendor:

VisionCamera front = new LimelightWrapper(new Limelight("limelight-front"), frontConfig);
VisionCamera back = new PhotonVisionWrapper(new PhotonCamera("back"), backConfig);
VisionCamera side = new SynapseWrapper(new SynapseCamera("side"), sideConfig);
VisionCamera quest = new QuestNavWrapper(new QuestNav(), questConfig);

A QuestNav headset isn't a camera, but it gives robot poses the same way, so it is one here too. See QuestNavWrapper.

Call update() once per loop (or VisionSystem.update() for several cameras), then read getLatestResult(), getEstimatedPose() and the other getters. They all report what the last update() read.

  • Field Details

    • name

      protected final String name
      Name of the camera.
    • config

      protected final VisionCameraConfig config
      The camera's config.
  • Constructor Details

  • Method Details

    • readFrames

      protected abstract List<VisionFrame> readFrames()
      Reads the frames the real camera produced since the last call. Call once per loop.
      Returns:
      The frames, oldest first.
    • applyPipelineIndex

      protected abstract void applyPipelineIndex(int index)
      Switches the real camera's pipeline.
      Parameters:
      index - Pipeline index.
    • readPipelineIndex

      protected abstract int readPipelineIndex()
      Reads the real camera's pipeline.
      Returns:
      Pipeline index.
    • readConnected

      protected abstract boolean readConnected()
      Whether the real camera is sending data.
      Returns:
      true if connected.
    • applyRobotOrientation

      protected void applyRobotOrientation(double timestampSeconds, Rotation3d orientation, double yawRateDegreesPerSecond)
      Passes the robot orientation to the real camera, for cameras that use it themselves.
      Parameters:
      timestampSeconds - When the orientation was measured.
      orientation - Robot orientation on the field.
      yawRateDegreesPerSecond - Yaw rate, or NaN if unknown.
    • applyRobotToCamera

      protected void applyRobotToCamera(Transform3d robotToCamera)
      Passes a new camera position to the real camera, for cameras that use it themselves.
      Parameters:
      robotToCamera - Transform from the robot origin to the camera.
    • cacheRobotToCamera

      protected void cacheRobotToCamera(Transform3d robotToCamera)
      Updates the cached robot-to-camera transform without re-sending it to hardware.
    • getCamera

      public abstract Object getCamera()
      The vendor's camera object, for settings YALL doesn't cover. Each wrapper narrows the return type to its vendor's class.
      Returns:
      The vendor's camera object.
    • update

      public void update()
      Reads new frames from the camera (or its simulation), filters their pose estimates, counts the filter's decisions and publishes telemetry. Call once per loop, before the getters.
    • checkEstimate

      protected Optional<String> checkEstimate(VisionPoseEstimate estimate, VisionPoseFilter filter, Field field, double nowSeconds, double averageTagAreaPercent)
      Runs the pose filter on an estimate. AprilTag cameras use VisionPoseFilter.check(VisionPoseEstimate, Field, double, double); a pose source that doesn't use tags overrides this, for example with VisionPoseFilter.checkWithoutTags(VisionPoseEstimate, Field, double).
      Parameters:
      estimate - The estimate.
      filter - The config's pose filter.
      field - The config's field.
      nowSeconds - Current Timer.getMonotonicTimestamp().
      averageTagAreaPercent - Average image area of the frame's AprilTags, or NaN if unknown.
      Returns:
      Why the estimate was rejected, or empty to accept it.
    • setRobotOrientation

      public void setRobotOrientation(Rotation3d orientation, AngularVelocity yawRate)
      Gives the camera the robot's orientation on the field. PoseEstimationMode.HEADING_ASSISTED needs it every loop, and the pose filter's yaw rate check uses the yaw rate.

      The yaw must be the robot's heading on the field, your pose estimator's rotation, not the gyro's own reading: resetting the pose estimator turns its heading without turning the gyro. The roll and pitch come from the gyro.

      Parameters:
      orientation - Robot orientation on the field (blue-origin, counterclockwise positive yaw).
      yawRate - Robot yaw rate.
    • setRobotOrientation

      public void setRobotOrientation(Rotation2d heading, AngularVelocity yawRate)
      Gives the camera the robot's heading on the field and its yaw rate, with no roll or pitch.
      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 the camera the robot's heading on the field, without a yaw rate. The pose filter's yaw rate check is skipped.
      Parameters:
      heading - Robot heading on the field, your pose estimator's rotation.
    • setRobotToCamera

      public void setRobotToCamera(Transform3d robotToCamera)
      Moves the camera on the robot, for a camera on a turret or other moving mechanism.
      Parameters:
      robotToCamera - Transform from the robot origin to the camera.
    • setRobotToCamera

      public void setRobotToCamera(Distance forward, Distance left, Distance up, Angle roll, Angle pitch, Angle yaw)
      Moves the camera on the robot, for a camera on a turret or other moving mechanism. Takes the same measurements as VisionCameraConfig.withRobotToCamera(Distance, Distance, Distance, Angle, Angle, Angle).
      Parameters:
      forward - Distance forward of the robot center.
      left - Distance left of the robot center.
      up - Height above the floor.
      roll - Rotation about the camera's forward axis.
      pitch - Rotation about the left axis. Negative tilts the camera up.
      yaw - Rotation about the up axis, counterclockwise positive.
    • getRobotToCamera

      public Transform3d getRobotToCamera()
      Where the camera is on the robot.
      Returns:
      Transform from the robot origin to the camera.
    • getCameraFieldPose

      public Optional<Pose3d> getCameraFieldPose()
      Where the camera itself is on the field, worked out from the AprilTags in the newest frame alone. It doesn't use the robot pose or the camera offset, so it can measure them: for example a turret's angle, with getCameraYawOnRobot(Rotation2d).

      Each AprilTag the camera solved gives a camera pose: the tag's place on the field, moved back along the camera-to-tag transform. The closest tag's is returned, because it is the most accurate.

      Returns:
      The camera's pose on the field, or empty if the newest frame has no solved AprilTag on the field.
    • getCameraYawOnRobot

      public Optional<Rotation2d> getCameraYawOnRobot(Rotation2d robotHeading)
      Which way the camera points relative to the robot, from what it sees: the camera's heading on the field (getCameraFieldPose()) minus the robot's heading. For a camera on a turret, this is the turret's angle (plus any angle the camera is mounted at on the turret), so it can check or zero the turret's encoder.
      Parameters:
      robotHeading - The robot's heading on the field, your pose estimator's rotation.
      Returns:
      The camera's yaw on the robot, counterclockwise positive, or empty if the newest frame has no solved AprilTag on the field.
    • setPipelineIndex

      public void setPipelineIndex(int index)
      Switches the camera's pipeline.
      Parameters:
      index - Pipeline index.
    • getPipelineIndex

      public int getPipelineIndex()
      The camera's current pipeline.
      Returns:
      Pipeline index.
    • isConnected

      public boolean isConnected()
      Whether the camera is sending data. Simulated cameras are always connected.
      Returns:
      true if connected.
    • isSimulated

      public boolean isSimulated()
      Whether this camera is giving simulated results.
      Returns:
      true in simulation with simulation enabled in the config.
    • getCameraSim

      public Optional<org.photonvision.simulation.PhotonCameraSim> getCameraSim()
      The camera's PhotonVision simulation, for settings YALL doesn't cover.
      Returns:
      The simulated camera, or empty when not simulating.
    • getVisionSim

      public Optional<VisionSim> getVisionSim()
      The simulated world this camera is in.
      Returns:
      The world, or empty when not simulating.
    • getSimulatedPoseEstimator

      public Optional<org.photonvision.PhotonPoseEstimator> getSimulatedPoseEstimator()
      PhotonVision's pose estimator for this camera's simulated results, for settings YALL doesn't cover.
      Returns:
      The pose estimator, or empty when not simulating.
    • getName

      public String getName()
      Name of the camera.
      Returns:
      The name.
    • getConfig

      public VisionCameraConfig getConfig()
      The camera's config.
      Returns:
      The config.
    • getUnreadResults

      public List<VisionResult> getUnreadResults()
      The frames read by the last update().
      Returns:
      The frames, oldest first.
    • getLatestResult

      public Optional<VisionResult> getLatestResult()
      The newest frame the camera has produced, even if the last update() read none.
      Returns:
      The newest frame, or empty if there hasn't been one.
    • hasTargets

      public boolean hasTargets()
      Whether the newest frame has targets.
      Returns:
      true if it has targets.
    • getTargets

      public List<VisionTarget> getTargets()
      The targets in the newest frame.
      Returns:
      The targets, best first.
    • getBestTarget

      public Optional<VisionTarget> getBestTarget()
      The best target in the newest frame.
      Returns:
      The best target, if any.
    • getEstimatedPose

      public Optional<VisionPoseEstimate> getEstimatedPose()
      The newest robot pose estimate from the last update() that passed the pose filter.
      Returns:
      The estimate with its timestamp and standard deviations, or empty if none passed.
    • getEstimatedPoses

      public List<VisionPoseEstimate> getEstimatedPoses()
      Every robot pose estimate from the last update() that passed the pose filter.
      Returns:
      The estimates, oldest first.
    • getRejectedPoses

      public List<VisionPoseEstimate> getRejectedPoses()
      The robot pose estimates from the last update() that the pose filter rejected.
      Returns:
      The rejected estimates.
    • getLastRejectionReason

      public String getLastRejectionReason()
      Why the pose filter last rejected an estimate.
      Returns:
      The reason, or an empty string if it hasn't rejected any.
    • getFilterStats

      public PoseFilterStats getFilterStats()
      What the pose filter has accepted and rejected since the stats were last reset, and how spread out the accepted poses are.
      Returns:
      The stats.
    • resetFilterStats

      public void resetFilterStats()
      Starts the pose filter stats counting again.
    • forEachEstimatedPose

      public void forEachEstimatedPose(Consumer<VisionPoseEstimate> consumer)
      Passes every accepted estimate from the last update() to a consumer, oldest first.
      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.
      Parameters:
      poseEstimator - The drivetrain's pose estimator.
    • close

      public void close()
      Removes the camera from simulation and closes its telemetry.
      Specified by:
      close in interface AutoCloseable