Simulating a Limelight
This tutorial adds a simulated Limelight to your robot code so that vision dependent behavior (aiming, MegaTag pose fusion, object reactions) can be exercised in desktop simulation without physical hardware.
LimelightSimprojects AprilTags from the current season's field onto a simulated camera.It publishes the same NetworkTables keys real Limelight hardware would.
Nothing downstream (
LimelightData,LimelightTargetData,LimelightPoseEstimator) needs to know it isn't talking to a real camera.

withField2d drawing a raycast to every AprilTag currently in view.1. Create the LimelightSim
Construct one
LimelightSimperLimelight, right next to where you construct theLimelightitself:
No further configuration is required. By default
LimelightSim:Loads the current season's official AprilTag field.
Assumes the camera sits at the robot's origin.
Uses Limelight 3 like resolution, field of view, latency, and noise.
2. Match the camera offset you already configured
If you called
withCameraOffseton the realLimelight, give the simulation the same transform so the simulated camera looks out from the same point:
The translation is the camera's offset from the center of the robot, in meters, not from any other reference point on the chassis.
3. Update it from simulationPeriodic
Feed it the robot's ground truth pose (from your drivetrain simulation) every simulation loop:
That's it. From the simulated pose,
LimelightSimupdates:tx/ty/ta/tv/tidevery
botpose*variantrawfiducialst2dthe target/camera relative poses
the
jsonresults blobthe same as they would from a real Limelight watching the same field.
Finding a ground truth pose from a YAMS SwerveDrive
If your drivetrain is a YAMS
SwerveDrive,getPose()is the same kind of thingDifferentialDrivePoseEstimator.getEstimatedPosition()is for a tank drive: the fused odometry estimate, not ground truth.Feeding it back into
LimelightSim.update(...)would make the simulated camera and the pose estimator each assume the other is correct, so vision correction would never actually show up as a change.
YAMS now exposes
SwerveDrive#getSimPose(), which already tracks this same ground truth pose for you. Prefer it over the manual integration below when it's available.The manual approach below is still worth knowing for tank drives or older YAMS versions without
getSimPose(): integrateSwerveDrive#getDesiredChassisSpeeds()into a separate pose every simulation loop:
getDesiredChassisSpeeds()returns the last commanded robot-relative speed.It's a setpoint rather than a measurement, which is exactly what makes it convenient here: you get a usable ground truth pose without needing per-module
DCMotorSimwiring first.
Pose2d#exptreats aTwist2das robot-relative, so this composes correctly whether the drive is moving straight, strafing, or rotating in place.If you've already wired up module-level simulation and want the simulated pose to track actual rather than commanded motion, integrate
getRobotRelativeSpeed()the same way instead.This is the same pattern the example project's tank-drive
DrivebaseSubsystemuses with its wheel speeds: keep a ground truth pose separate from odometry, and only feed that separate pose intoLimelightSim.

LimelightSim publishes, viewed in the WPILib Sim GUI's NetworkTables panel.
tv, tx, ty, tid, and tdist updating from the simulated pose shown in the Field2d panel on the left.Visualizing raycasts on a Field2d
Pass a
Field2dtowithField2dandLimelightSimdraws a line from the robot to every AprilTag it currently sees, so you can watch detections come and go as the robot moves instead of reasoning about it purely from NT values:
This creates a single
FieldObject2dnamedLimelight Raycastson theField2d.It does not set the
Field2d's own robot pose, so keep doing that yourself (field2d.setRobotPose(...)) alongside it.
Pass
nullto turn visualization off again and clear the existing raycasts.


Tuning the simulated camera
LimelightSimSettingscovers resolution, field of view, tag size, detection range, latency, and noise, each with awithXXXmethod and a Limelight 3 like default:
Use
LimelightSimSettings.perfect()when writing a unit test that needs deterministic, noise free output.
Scope
Only the AprilTag/fiducial pipeline is simulated. Retroreflective, neural classifier/detector, and barcode pipelines are not.
Robot pose and tag geometry are known exactly by the simulator, so instead of re-deriving a pose estimate the way the real firmware does with solvePnP, every
botpose*entry is the ground truth robot pose with configurable Gaussian noise applied.Treat it as a reasonable approximation for exercising downstream code, not a source of physically accurate MegaTag uncertainty.
Next steps
Basic Setup: if you haven't created a
Limelightyet.AprilTag Pose Estimation: the fusion code this simulation lets you exercise without hardware.
Last updated
Was this helpful?