Class VisionSystem
java.lang.Object
yall.VisionSystem
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final TimeDefault debounce duration before a disconnected camera triggers alerts or failover callbacks. -
Constructor Summary
Constructors -
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.voidclose()Closes every camera and any active disconnect alerts.voidforEachEstimatedPose(Consumer<VisionPoseEstimate> consumer) Passes every accepted estimate from the lastupdate()to a consumer, oldest first.Looks up a camera in this system by its configured name.The cameras in this system.intNumber of cameras currently reporting connected.Cameras that are currently connected.Cameras that are currently disconnected.Every accepted estimate from every camera in the lastupdate().booleanWhether at least one camera in this system is currently connected.onCameraDisconnected(Consumer<VisionCamera> callback) Registers an edge-triggered callback invoked once when a camera transitions into the disconnected state (after any configuredwithDisconnectDebounce(Time)).onCameraReconnected(Consumer<VisionCamera> callback) Registers an edge-triggered callback invoked once when a previously disconnected camera reconnects.booleansetPipelineIndex(String cameraName, int pipelineIndex) Switches the pipeline on a named camera in this system.voidsetRobotOrientation(Rotation2d heading) Gives every camera the robot's heading on the field, without a yaw rate.voidsetRobotOrientation(Rotation2d heading, AngularVelocity yawRate) Gives every camera the robot's heading on the field and its yaw rate.voidsetRobotOrientation(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.voidupdate()Updates every camera and evaluates connection health.voidupdate(double nowSeconds) Updates every camera and evaluates connection health using an explicit monotonic timestamp (useful for deterministic unit tests and log replay).voidupdateSimulation(Pose2d robotPose) Updates the simulated worlds the cameras are in.withDisconnectAlerts(boolean enabled) Enables or disables automatic WPILibAlerts when a camera disconnects.withDisconnectDebounce(Time debounce) Sets how long a camera must remain disconnected beforeonCameraDisconnected(Consumer)andwithDisconnectAlerts(boolean)activate, preventing brief NetworkTables hiccups from flapping failover logic.
-
Field Details
-
DEFAULT_DISCONNECT_DEBOUNCE
Default debounce duration before a disconnected camera triggers alerts or failover callbacks.
-
-
Constructor Details
-
VisionSystem
Creates a vision system.- Parameters:
cameras- The cameras.
-
-
Method Details
-
withDisconnectDebounce
Sets how long a camera must remain disconnected beforeonCameraDisconnected(Consumer)andwithDisconnectAlerts(boolean)activate, preventing brief NetworkTables hiccups from flapping failover logic.- Parameters:
debounce- Non-negative debounce duration (e.g.DEFAULT_DISCONNECT_DEBOUNCE).nulldisables debouncing (immediate reporting).- Returns:
- This vision system, for chaining.
-
withDisconnectAlerts
-
onCameraDisconnected
Registers an edge-triggered callback invoked once when a camera transitions into the disconnected state (after any configuredwithDisconnectDebounce(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, ornullto clear.- Returns:
- This vision system, for chaining.
-
onCameraReconnected
Registers an edge-triggered callback invoked once when a previously disconnected camera reconnects.- Parameters:
callback- Callback receiving the reconnected camera, ornullto 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
Updates the simulated worlds the cameras are in. Call once per loop fromsimulationPeriodic.- Parameters:
robotPose- The robot's true pose.
-
setRobotOrientation
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
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
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
-
getCamera
Looks up a camera in this system by its configured name.- Parameters:
name- Camera name.- Returns:
- Matching camera, or empty if not found.
-
setPipelineIndex
Switches the pipeline on a named camera in this system.- Parameters:
cameraName- Name of the camera to switch.pipelineIndex- Target pipeline index.- Returns:
trueif the camera was found and switched,falseotherwise.
-
getConnectedCameras
Cameras that are currently connected.- Returns:
- Connected cameras in registration order.
-
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:
trueif one or more cameras are connected.
-
getEstimatedPoses
Every accepted estimate from every camera in the lastupdate().- Returns:
- The estimates, oldest first.
-
forEachEstimatedPose
Passes every accepted estimate from the lastupdate()to a consumer, oldest first. Use it for drivetrains with their own vision measurement method, passing each estimate'spose,timestampandstandard deviations.- Parameters:
consumer- Receives each estimate.
-
addVisionMeasurements
-
close
public void close()Closes every camera and any active disconnect alerts.- Specified by:
closein interfaceAutoCloseable
-