Optimize vision pipeline: reduce PhotonVision CPU load#104
Open
dmeglan wants to merge 1 commit into
Open
Conversation
Key changes: - Skip getAllUnreadResults() when camera is disconnected (avoids expensive _versionCheck traceback spam that costs significant CPU per warning) - Reduce result cap from 10 to 2 per cycle (camera runs at 30-90 FPS vs 50 Hz robot loop, older results have stale timestamps anyway) - Cache tag poses and field bounds at init instead of looking them up every cycle through the AprilTagFieldLayout - Fix single-tag fiducial ID bitmask encoding (was passing raw ID instead of 1 << (id-1), breaking the hub estimator tag filtering) - Limit bitmask iteration to 22 tags instead of 32 - Only log vision data when observations exist (skip 14+ Logger.recordOutput calls per camera on empty frames) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduces CPU time spent in the vision subsystem periodic, addressing intermittent loop overruns caused by PhotonVision processing.
Changes
1. Skip
getAllUnreadResults()when camera is disconnected (biggest win)Before: Called every cycle regardless of connection state. When disconnected, photonlibpy's
_versionCheck()fireswpilib.reportWarning()every 5 seconds, which constructs a full Python stack trace each time. On the roboRIO's ARM CPU, generating stack traces is expensive (~5-10ms per warning).After: Early return when
isConnected()is false — zero CAN/NT traffic, zero Transform3d math, zero warning overhead.2. Reduce result processing cap from 10 to 2
Before:
allResults[-10:]— processes up to 10 queued results per cycle.After:
allResults[-2:]— processes at most 2 results per cycle.PhotonVision's pipeline runs at 30-90 FPS while the robot loop runs at 50 Hz. Results accumulate between cycles, especially during vision processing spikes. Each result requires Transform3d inversions, Pose3d construction, bitmask encoding, and observation object creation. At 10 results with multi-tag solving, this can spike to >15ms of vision processing alone.
The most recent 2 results are sufficient — older results have increasingly stale timestamps and the pose estimator already handles latency compensation.
3. Cache tag poses and field bounds at init
Before:
kApriltagFieldLayout.getTagPose(tagId)called per tag per cycle (in both IO and subsystem),getFieldLength()/getFieldWidth()called per observation for bounds checking.After: Tag poses cached in a dict at
__init__, field bounds cached as floats. Lookups become dict.get()calls instead of method calls through the WPILib AprilTagFieldLayout.4. Fix single-tag fiducial ID bitmask encoding (bug fix)
Before: Single-tag observations passed
target.fiducialIddirectly astagsList(e.g., tag 5 → value5).After: Correctly encodes as
1 << (target.fiducialId - 1)(e.g., tag 5 → value16, bit 4 set).This bug meant the
issubsetcheck inrobotstate.pythat gates hub estimator updates could not work correctly for single-tag observations. The hub estimator may have been receiving non-hub tag data, corrupting hub-relative pose estimates. (Same fix as PR #101 but for both turreted and non-turreted paths.)5. Limit bitmask iteration to 22 tags
Before:
for tagId in range(32)— iterates all 32 bits.After:
for tagId in range(22)— only 22 tags exist on the field. Saves 10 iterations × 2 (regular + turreted) × per-observation.6. Conditional logging — skip empty frames
Before: 14
Logger.recordOutput()calls per camera every cycle, even when no tags were seen (serializing empty Pose3d lists).After: Only logs when there are actual observations. On frames with no visible tags (which is most frames when driving away from tags), this eliminates all 14+ serialization calls.
Files Changed
src/subsystems/vision/visioniophoton.pysrc/subsystems/vision/visionsubsystem.pyExpected Impact
On cycles with no visible tags (common during driving):
On cycles with visible tags:
Test Plan
Timing/RobotPeriodic/SchedulerUpdateMSin AdvantageScope — vision-heavy cycles should be shorter