This example demonstrates AprilTag detection using a USB camera, including pose estimation and visualization on the dashboard.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/wpilibsuite/allwpilib/llms.txt
Use this file to discover all available pages before exploring further.
AprilTag Vision Example
- Java
- C++
What This Example Demonstrates
AprilTag Detection
AprilTags are fiducial markers (like QR codes) designed for robot localization. WPILib includes a built-in detector:1: Recommended for RoboRIO 1 (uses 781 KB)2: Feasible on RoboRIO 2 (uses 27.4 MB)3: High error correction (uses 932 MB)
Camera Setup
- CvSink: Receives frames from the camera
- CvSource: Sends processed frames to dashboard
Vision Threading
Important: Vision processing runs in a separate thread to avoid blocking the main robot loop:Image Processing Pipeline
- Grab frame from camera
- Convert to grayscale (required for AprilTag detection)
- Detect tags
- Draw visualization
- Send to dashboard
Pose Estimation
Estimate the 3D position and orientation of detected tags:Publishing to NetworkTables
Memory Management
Mats are expensive! Reuse them instead of creating new ones each frame:Camera Calibration
For accurate pose estimation, calibrate your camera:- Print a checkerboard pattern
- Capture images from various angles
- Use OpenCV’s calibration tools or WPILib calibration utilities
- Extract fx, fy, cx, cy parameters
Performance Considerations
This runs on the RoboRIO! For better performance:- Use a coprocessor (Raspberry Pi, NVIDIA Jetson, etc.)
- Reduce camera resolution (e.g., 320x240)
- Reduce frame rate
- Use lower Hamming distance for error correction
Vision with PhotonVision
For production use, consider PhotonVision:- Runs on coprocessor
- Better performance
- More features (multi-tag localization, etc.)
- Easier calibration
Running in Simulation
- Connect a USB camera
- Deploy the code
- Open Shuffleboard or SmartDashboard
- Add the “Detected” camera stream
- Point the camera at printed AprilTags
- Observe detections in NetworkTables under “apriltags” table
Source Location
- Java:
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/apriltagsvision/Robot.java - C++:
wpilibcExamples/src/main/cpp/examples/AprilTagsVision/cpp/Robot.cpp