Class VisionCamera
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
LimelightWrapper, PhotonVisionWrapper, QuestNavWrapper, SynapseWrapper
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 Summary
FieldsModifier and TypeFieldDescriptionprotected final VisionCameraConfigThe camera's config.protected final StringName of the camera. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedVisionCamera(String name, VisionCameraConfig config, org.photonvision.simulation.PhotonCameraSim cameraSim) Sets up the parts every camera shares. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddVisionMeasurements(PoseEstimator<?> poseEstimator) Adds every accepted estimate from the lastupdate()to a WPILib pose estimator, oldest first, with its timestamp and standard deviations.protected abstract voidapplyPipelineIndex(int index) Switches the real camera's pipeline.protected voidapplyRobotOrientation(double timestampSeconds, Rotation3d orientation, double yawRateDegreesPerSecond) Passes the robot orientation to the real camera, for cameras that use it themselves.protected voidapplyRobotToCamera(Transform3d robotToCamera) Passes a new camera position to the real camera, for cameras that use it themselves.protected voidcacheRobotToCamera(Transform3d robotToCamera) Updates the cached robot-to-camera transform without re-sending it to hardware.checkEstimate(VisionPoseEstimate estimate, VisionPoseFilter filter, Field field, double nowSeconds, double averageTagAreaPercent) Runs the pose filter on an estimate.voidclose()Removes the camera from simulation and closes its telemetry.voidforEachEstimatedPose(Consumer<VisionPoseEstimate> consumer) Passes every accepted estimate from the lastupdate()to a consumer, oldest first.The best target in the newest frame.abstract ObjectThe vendor's camera object, for settings YALL doesn't cover.Where the camera itself is on the field, worked out from the AprilTags in the newest frame alone.Optional<org.photonvision.simulation.PhotonCameraSim> The camera's PhotonVision simulation, for settings YALL doesn't cover.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.The camera's config.The newest robot pose estimate from the lastupdate()that passed the pose filter.Every robot pose estimate from the lastupdate()that passed the pose filter.What the pose filter has accepted and rejected since the stats were last reset, and how spread out the accepted poses are.Why the pose filter last rejected an estimate.The newest frame the camera has produced, even if the lastupdate()read none.getName()Name of the camera.intThe camera's current pipeline.The robot pose estimates from the lastupdate()that the pose filter rejected.Where the camera is on the robot.Optional<org.photonvision.PhotonPoseEstimator> PhotonVision's pose estimator for this camera's simulated results, for settings YALL doesn't cover.The targets in the newest frame.The frames read by the lastupdate().The simulated world this camera is in.booleanWhether the newest frame has targets.booleanWhether the camera is sending data.booleanWhether this camera is giving simulated results.protected abstract booleanWhether the real camera is sending data.protected abstract List<VisionFrame> Reads the frames the real camera produced since the last call.protected abstract intReads the real camera's pipeline.voidStarts the pose filter stats counting again.voidsetPipelineIndex(int index) Switches the camera's pipeline.voidsetRobotOrientation(Rotation2d heading) Gives the camera the robot's heading on the field, without a yaw rate.voidsetRobotOrientation(Rotation2d heading, AngularVelocity yawRate) Gives the camera the robot's heading on the field and its yaw rate, with no roll or pitch.voidsetRobotOrientation(Rotation3d orientation, AngularVelocity yawRate) Gives the camera the robot's orientation on the field.voidsetRobotToCamera(Transform3d robotToCamera) Moves the camera on the robot, for a camera on a turret or other moving mechanism.voidMoves the camera on the robot, for a camera on a turret or other moving mechanism.voidupdate()Reads new frames from the camera (or its simulation), filters their pose estimates, counts the filter's decisions and publishes telemetry.
-
Field Details
-
name
Name of the camera. -
config
The camera's config.
-
-
Constructor Details
-
VisionCamera
protected VisionCamera(String name, VisionCameraConfig config, org.photonvision.simulation.PhotonCameraSim cameraSim) Sets up the parts every camera shares.- Parameters:
name- Name of the camera.config- The camera's config.cameraSim- The camera's PhotonVision simulation, fromVisionSim.createCameraSim(String, VisionCameraConfig, CameraModel, PhotonCamera), or null when not simulating.
-
-
Method Details
-
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
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
Updates the cached robot-to-camera transform without re-sending it to hardware. -
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 useVisionPoseFilter.check(VisionPoseEstimate, Field, double, double); a pose source that doesn't use tags overrides this, for example withVisionPoseFilter.checkWithoutTags(VisionPoseEstimate, Field, double).- Parameters:
estimate- The estimate.filter- The config's pose filter.field- The config's field.nowSeconds- CurrentTimer.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
Gives the camera the robot's orientation on the field.PoseEstimationMode.HEADING_ASSISTEDneeds 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
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
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
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 asVisionCameraConfig.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
Where the camera is on the robot.- Returns:
- Transform from the robot origin to the camera.
-
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, withgetCameraYawOnRobot(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
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
The camera's PhotonVision simulation, for settings YALL doesn't cover.- Returns:
- The simulated camera, or empty when not simulating.
-
getVisionSim
-
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
-
getConfig
-
getUnreadResults
The frames read by the lastupdate().- Returns:
- The frames, oldest first.
-
getLatestResult
The newest frame the camera has produced, even if the lastupdate()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
The targets in the newest frame.- Returns:
- The targets, best first.
-
getBestTarget
The best target in the newest frame.- Returns:
- The best target, if any.
-
getEstimatedPose
The newest robot pose estimate from the lastupdate()that passed the pose filter.- Returns:
- The estimate with its timestamp and standard deviations, or empty if none passed.
-
getEstimatedPoses
Every robot pose estimate from the lastupdate()that passed the pose filter.- Returns:
- The estimates, oldest first.
-
getRejectedPoses
The robot pose estimates from the lastupdate()that the pose filter rejected.- Returns:
- The rejected estimates.
-
getLastRejectionReason
Why the pose filter last rejected an estimate.- Returns:
- The reason, or an empty string if it hasn't rejected any.
-
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
Passes every accepted estimate from the lastupdate()to a consumer, oldest first.- Parameters:
consumer- Receives each estimate.
-
addVisionMeasurements
Adds every accepted estimate from the lastupdate()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:
closein interfaceAutoCloseable
-