Class VisionPoseFilter
Each check is set with its withX method. Pass null to turn the check off:
new VisionPoseFilter()
.withMaxAmbiguity(0.15) // tighter than the default
.withMaxTilt(Degrees.of(10)) // on; off by default
.withMaxYawRate(null); // off
A new filter starts with these checks:
| Check | Default | Rejects an estimate when |
|---|---|---|
Field bounds | On, 0.5 m margin | the robot would be off the field |
Height | On, 0.5 m | the robot would be floating above or sunk below the floor |
Tilt | Off | the robot would be rolled or pitched further than the limit |
Ambiguity | On, 0.2 | a single-tag estimate is too ambiguous |
Tag count | On, 1 tag | too few tags were used |
Tag distance |
On, 4 m for one tag, 8 m for several | the tags are too far away |
Yaw rate | On, 720 °/s | the robot was spinning too fast, which blurs the image (needs VisionCamera.setRobotOrientation(Rotation3d, AngularVelocity)) |
Age | On, 0.5 s | the image is too old, or claims to be from the future |
Pose jump | Off | the estimate is too far from the robot's current pose |
Add your own checks with withCustomCheck(String, Predicate). Each has a name, which is
the rejection reason and the name its rejections are counted under in PoseFilterStats.
Estimates that used no tags are always rejected by check(VisionPoseEstimate, Field, double, double). Pose sources that don't use
tags, such as a QuestNav headset, are checked with checkWithoutTags(VisionPoseEstimate, Field, double) instead, which runs
only the checks that don't need tags.
Accepted estimates get standard deviations that grow with the square of the tag distance:
base * (1 + distance² / distanceDivisor), with a smaller base for multi-tag estimates. See
withStdDevs(Matrix, Matrix, double).
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a filter with the default checks described in the class documentation. -
Method Summary
Modifier and TypeMethodDescriptionstatic VisionPoseFilterCreates a filter that accepts every estimate that used a tag.applyStdDevs(VisionPoseEstimate estimate) Computes the standard deviations for an estimate.booleanapplySuggestedBaseStdDevs(PoseFilterStats stats, long minSamples, Matrix<N3, N1> minBase, Matrix<N3, N1> maxBase) AppliesPoseFilterStats.getSuggestedBaseStdDevs(double)as the multi-tag base standard deviations once at leastminSamplesaccepted poses have been collected, clamping each axis into[minBase, maxBase]so noisy calibration runs cannot produce extreme weights.check(VisionPoseEstimate estimate, Field field, double nowSeconds, double yawRateDegreesPerSecond) Checks an estimate.check(VisionPoseEstimate estimate, Field field, double nowSeconds, double yawRateDegreesPerSecond, double averageTagAreaPercent) Checks an estimate with optional average AprilTag image area.checkWithoutTags(VisionPoseEstimate estimate, Field field, double nowSeconds) Checks an estimate that doesn't come from AprilTags, such as a QuestNav headset's tracked pose.The custom checks, by name.doubleThe divisor for the squared tag distance in the standard deviation formula.The field bounds margin.The minimum tag count required to trigger the high-tag-count trust boost.The oldest accepted image.The largest accepted single-tag ambiguity.The largest accepted distance from the current pose.The largest accepted height error.The largest accepted average tag distance for a multi-tag estimate.The largest accepted average tag distance for a single-tag estimate.The largest accepted roll or pitch.The fastest accepted spin.The smallest accepted single-tag image area percentage.The fewest tags accepted.The yaw-rate window half-width.booleanWhether pre-enable alignment mode is currently active (configured and robot disabled).voidrecordYawRate(double timestampSeconds, double yawRateDegreesPerSecond) Records a timestamped robot yaw-rate sample forwithYawRateWindow(Time).withCustomCheck(String name, Predicate<VisionPoseEstimate> accepts) Adds a check of your own, run after the built-in ones.withFieldBounds(Distance margin) Rejects estimates that put the robot off the field.withHeadingAssistedThetaStdDev(Angle thetaStdDev) Sets the heading standard deviation for heading-assisted estimates (Limelight MegaTag2, PhotonVision's trig solve).withHighTagCountBoost(Integer minTags, Double xyScale) Applies an extra X/Y standard deviation multiplier when an estimate sees at leastminTagsAprilTags (for example, trusting a 3+ tag shooter-camera solve more tightly).withMaxAge(Time maxAge) Rejects estimates from images older than the limit, and from images timestamped in the future.withMaxAmbiguity(Double ambiguity) Rejects single-tag estimates whose pose ambiguity is above the limit.withMaxDistanceFromPose(Supplier<Pose2d> currentPose, Distance maxDistance) Rejects estimates further than the limit from the robot's current pose.withMaxHeightError(Distance maxHeightError) Rejects estimates that put the robot above or below the floor.withMaxTagDistance(Distance maxSingleTagDistance, Distance maxMultiTagDistance) Rejects estimates whose tags are, on average, further away than the limit.withMaxTilt(Angle maxTilt) Rejects estimates that roll or pitch the robot further than the limit.withMaxYawRate(AngularVelocity maxYawRate) Rejects estimates taken while the robot spun faster than the limit.withMinSingleTagArea(Double minAreaPercent) Rejects single-tag estimates whose tag covers less than the given percentage of the image.withMinTagCount(Integer tagCount) Rejects estimates that used fewer tags than the limit.withPreEnableAlignment(BooleanSupplier isEnabled, Integer minPreEnableTags, Angle preEnableThetaStdDev) Enables pre-enable (disabled) field alignment: whileisEnabledreturnsfalse, the pose-jump gate is bypassed so vision can snap to the field from an unknown starting pose, at leastminPreEnableTagstags are required, and heading usespreEnableThetaStdDevso multi-tag solves can align the gyro before the match starts.withStdDevCalculator(Function<VisionPoseEstimate, Matrix<N3, N1>> calculator) Supplies a custom standard-deviation calculator that replaces the built-inbase * (1 + d² / distanceDivisor)model for accepted estimates (for example,d^1.5 / tagCountor ambiguity-weighted covariance).Sets how standard deviations are computed for accepted estimates:base * (1 + d² / distanceDivisor), where d is the average tag distance in meters.withStdDevScale(double xyScale) Applies a constant multiplier to accepted X/Y standard deviations (for example, tightening trust during alignment or right after traversing a bump).withStdDevScale(DoubleSupplier xyScaleSupplier) Applies a dynamic multiplier to accepted X/Y standard deviations (for example, switching between normal driving, close-in alignment, and post-bump relocalization snap modes).withYawRateWindow(Time halfWindow) Checks the peak yaw rate over a symmetric window (timestamp +/- halfWindow) rather than only the instantaneous yaw rate when the frame arrived.
-
Constructor Details
-
VisionPoseFilter
public VisionPoseFilter()Creates a filter with the default checks described in the class documentation.
-
-
Method Details
-
acceptAll
Creates a filter that accepts every estimate that used a tag. Turn checks on with thewithXmethods.- Returns:
- A filter with every check off.
-
withFieldBounds
Rejects estimates that put the robot off the field.- Parameters:
margin- How far past the field edge to still accept, for measurement error. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxHeightError
Rejects estimates that put the robot above or below the floor.- Parameters:
maxHeightError- Largest accepted distance between the estimated robot origin and the floor. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxTilt
Rejects estimates that roll or pitch the robot further than the limit. Leave this off if the robot drives over ramps or bumps.- Parameters:
maxTilt- Largest accepted roll or pitch. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxAmbiguity
Rejects single-tag estimates whose pose ambiguity is above the limit. Multi-tag estimates and cameras that don't report ambiguity aren't checked.- Parameters:
ambiguity- Largest accepted ambiguity, 0 to 1. null turns the check off.- Returns:
- This filter, for chaining.
-
withMinTagCount
Rejects estimates that used fewer tags than the limit. Estimates that used no tags are always rejected.- Parameters:
tagCount- Fewest tags accepted. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxTagDistance
public VisionPoseFilter withMaxTagDistance(Distance maxSingleTagDistance, Distance maxMultiTagDistance) Rejects estimates whose tags are, on average, further away than the limit.- Parameters:
maxSingleTagDistance- Limit for estimates from one tag. null turns this limit off.maxMultiTagDistance- Limit for estimates from several tags. null turns this limit off.- Returns:
- This filter, for chaining.
-
withMaxYawRate
Rejects estimates taken while the robot spun faster than the limit. Needs the yaw rate fromVisionCamera.setRobotOrientation(Rotation3d, AngularVelocity); without it, estimates aren't checked.- Parameters:
maxYawRate- Fastest accepted spin. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxAge
Rejects estimates from images older than the limit, and from images timestamped in the future.- Parameters:
maxAge- Oldest accepted image. null turns the check off.- Returns:
- This filter, for chaining.
-
withMaxDistanceFromPose
Rejects estimates further than the limit from the robot's current pose. Turn this on only once the robot knows where it is, or vision can never correct a bad starting pose.- Parameters:
currentPose- Supplies the robot's current pose, usually from the drivetrain's pose estimator. null turns the check off.maxDistance- Largest accepted distance from the current pose. null turns the check off.- Returns:
- This filter, for chaining.
-
withCustomCheck
Adds a check of your own, run after the built-in ones. Its name is the rejection reason, andPoseFilterStatscounts its rejections under that name.filter.withCustomCheck("not in our half", e -> e.pose().getX() < 8.27);- Parameters:
name- Name of the check, shown as the rejection reason. Must not be empty. A check with the same name is replaced.accepts- Returns true to accept an estimate. null removes the check.- Returns:
- This filter, for chaining.
- Throws:
IllegalArgumentException- if the name is null or blank.
-
getCustomChecks
The custom checks, by name.- Returns:
- The checks, in the order they were added.
-
getDistanceDivisor
public double getDistanceDivisor()The divisor for the squared tag distance in the standard deviation formula.- Returns:
- The divisor.
-
withStdDevs
public VisionPoseFilter withStdDevs(Matrix<N3, N1> singleTagBase, Matrix<N3, N1> multiTagBase, double distanceDivisor) Sets how standard deviations are computed for accepted estimates:base * (1 + d² / distanceDivisor), where d is the average tag distance in meters. A smaller divisor makes distant tags count for less.- Parameters:
singleTagBase- Base standard deviations of x (m), y (m) and heading (rad) for one tag.multiTagBase- Base standard deviations for several tags.distanceDivisor- Divisor for the squared tag distance.- Returns:
- This filter, for chaining.
-
withHeadingAssistedThetaStdDev
Sets the heading standard deviation for heading-assisted estimates (Limelight MegaTag2, PhotonVision's trig solve). Their heading is the one given tosetRobotOrientation, so it should carry almost no weight.- Parameters:
thetaStdDev- Heading standard deviation.- Returns:
- This filter, for chaining.
-
getFieldBoundsMargin
-
getMaxHeightError
-
getMaxTilt
-
getMaxAmbiguity
-
getMinTagCount
-
getMaxSingleTagDistance
-
getMaxMultiTagDistance
-
getMaxYawRate
The fastest accepted spin.- Returns:
- The limit, or empty if the check is off.
-
getMaxAge
-
getMaxDistanceFromPose
-
withMinSingleTagArea
Rejects single-tag estimates whose tag covers less than the given percentage of the image. Multi-tag estimates and cameras that don't report target area aren't checked.- Parameters:
minAreaPercent- Smallest accepted image area percentage (0 to 100). null turns the check off.- Returns:
- This filter, for chaining.
-
withYawRateWindow
Checks the peak yaw rate over a symmetric window (timestamp +/- halfWindow) rather than only the instantaneous yaw rate when the frame arrived.- Parameters:
halfWindow- Half-width of the yaw-rate window around each image timestamp. null turns windowed checking off.- Returns:
- This filter, for chaining.
-
recordYawRate
public void recordYawRate(double timestampSeconds, double yawRateDegreesPerSecond) Records a timestamped robot yaw-rate sample forwithYawRateWindow(Time). Called automatically byVisionCamera.setRobotOrientation(Rotation3d, AngularVelocity).- Parameters:
timestampSeconds- Monotonic timestamp in seconds.yawRateDegreesPerSecond- Robot yaw rate in degrees per second.
-
withStdDevScale
Applies a constant multiplier to accepted X/Y standard deviations (for example, tightening trust during alignment or right after traversing a bump).- Parameters:
xyScale- Positive multiplier applied to X and Y standard deviations.- Returns:
- This filter, for chaining.
-
withStdDevScale
Applies a dynamic multiplier to accepted X/Y standard deviations (for example, switching between normal driving, close-in alignment, and post-bump relocalization snap modes).- Parameters:
xyScaleSupplier- Supplies a positive multiplier each time standard deviations are computed. null resets the scale to 1.0.- Returns:
- This filter, for chaining.
-
withStdDevCalculator
public VisionPoseFilter withStdDevCalculator(Function<VisionPoseEstimate, Matrix<N3, N1>> calculator) Supplies a custom standard-deviation calculator that replaces the built-inbase * (1 + d² / distanceDivisor)model for accepted estimates (for example,d^1.5 / tagCountor ambiguity-weighted covariance). If the calculator returnsnullor any non-finite / non-positive entry, the filter safely falls back to its built-in formula.- Parameters:
calculator- Function computing[x, y, theta]standard deviations for an accepted estimate, ornullto restore the built-in model.- Returns:
- This filter, for chaining.
-
applySuggestedBaseStdDevs
public boolean applySuggestedBaseStdDevs(PoseFilterStats stats, long minSamples, Matrix<N3, N1> minBase, Matrix<N3, N1> maxBase) AppliesPoseFilterStats.getSuggestedBaseStdDevs(double)as the multi-tag base standard deviations once at leastminSamplesaccepted poses have been collected, clamping each axis into[minBase, maxBase]so noisy calibration runs cannot produce extreme weights.- Parameters:
stats- Collected pose-filter statistics from a stationary calibration run.minSamples- Minimum accepted sample count required before applying (must be>= 2).minBase- Per-axis lower clamp[x, y, theta](all finite and positive).maxBase- Per-axis upper clamp[x, y, theta](all finite and>= minBase).- Returns:
trueif the base standard deviations were updated,falseif not enough valid samples were available.
-
withHighTagCountBoost
Applies an extra X/Y standard deviation multiplier when an estimate sees at leastminTagsAprilTags (for example, trusting a 3+ tag shooter-camera solve more tightly).- Parameters:
minTags- Fewest tags required for the boost (must be>= 2). null turns the boost off.xyScale- Positive multiplier applied to X and Y standard deviations (e.g.0.35). null turns the boost off.- Returns:
- This filter, for chaining.
-
withPreEnableAlignment
public VisionPoseFilter withPreEnableAlignment(BooleanSupplier isEnabled, Integer minPreEnableTags, Angle preEnableThetaStdDev) Enables pre-enable (disabled) field alignment: whileisEnabledreturnsfalse, the pose-jump gate is bypassed so vision can snap to the field from an unknown starting pose, at leastminPreEnableTagstags are required, and heading usespreEnableThetaStdDevso multi-tag solves can align the gyro before the match starts.- Parameters:
isEnabled- Supplies whether the robot is currently enabled (e.g.DriverStation::isEnabled). null turns pre-enable alignment off.minPreEnableTags- Fewest tags required while disabled (must be>= 1). null defaults to 2.preEnableThetaStdDev- Heading standard deviation while disabled. null defaults to 2 deg.- Returns:
- This filter, for chaining.
-
getMinSingleTagArea
-
getYawRateWindow
-
getHighTagCountBoostMinTags
-
isPreEnableAlignmentActive
public boolean isPreEnableAlignmentActive()Whether pre-enable alignment mode is currently active (configured and robot disabled).- Returns:
- true if pre-enable alignment is active right now.
-
check
public Optional<String> check(VisionPoseEstimate estimate, Field field, double nowSeconds, double yawRateDegreesPerSecond) Checks an estimate.- Parameters:
estimate- Estimate to check.field- Field the robot is on, for the bounds check. null skips that check.nowSeconds- CurrentTimer.getMonotonicTimestamp().yawRateDegreesPerSecond- Robot yaw rate when the image was taken, orDouble.NaNif unknown.- Returns:
- Why the estimate was rejected, or empty if it passes every enabled check.
-
check
public Optional<String> check(VisionPoseEstimate estimate, Field field, double nowSeconds, double yawRateDegreesPerSecond, double averageTagAreaPercent) Checks an estimate with optional average AprilTag image area.- Parameters:
estimate- Estimate to check.field- Field the robot is on, for the bounds check. null skips that check.nowSeconds- CurrentTimer.getMonotonicTimestamp().yawRateDegreesPerSecond- Robot yaw rate when the image was taken, orDouble.NaNif unknown.averageTagAreaPercent- Average image area percentage of the AprilTags in the frame, orDouble.NaNif unknown.- Returns:
- Why the estimate was rejected, or empty if it passes every enabled check.
-
checkWithoutTags
public Optional<String> checkWithoutTags(VisionPoseEstimate estimate, Field field, double nowSeconds) Checks an estimate that doesn't come from AprilTags, such as a QuestNav headset's tracked pose. Only the checks that don't need tags run: non-finite pose, age, field bounds, height, tilt, pose jump and the custom checks. The tag count, ambiguity, tag area, tag distance and yaw rate checks are skipped, and an estimate with no tags isn't rejected for it.- Parameters:
estimate- Estimate to check.field- Field the robot is on, for the bounds check. null skips that check.nowSeconds- CurrentTimer.getMonotonicTimestamp().- Returns:
- Why the estimate was rejected, or empty if it passes every enabled check.
-
applyStdDevs
Computes the standard deviations for an estimate.- Parameters:
estimate- Estimate to weigh.- Returns:
- The estimate with
VisionPoseEstimate.stdDevs()filled in.
-