Class VisionSim

java.lang.Object
yall.sim.VisionSim

public class VisionSim extends Object
The simulated world every simulated VisionCamera sees, whatever its vendor. It is PhotonVision's VisionSystemSim: each camera projects the field's AprilTags (and any extra targets) through a simulated lens, with noise and latency.

Cameras join getDefault() unless their config names another world. Update the world once per loop with the robot's true pose:

public void simulationPeriodic() {
  VisionSim.getDefault().update(drivetrainSim.getPose());
}
  • Constructor Summary

    Constructors
    Constructor
    Description
    VisionSim(String name, Field field)
    Creates a simulated world.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addCamera(org.photonvision.simulation.PhotonCameraSim cameraSim, Transform3d robotToCamera)
    Adds a simulated camera.
    void
    addVisionTargets(String type, org.photonvision.simulation.VisionTargetSim... targets)
    Adds targets other than the field's AprilTags, such as game pieces for object detection: use VisionTargetSim(Pose3d, org.photonvision.estimation.TargetModel, int, float) with a class ID.
    void
    adjustCamera(org.photonvision.simulation.PhotonCameraSim cameraSim, Transform3d robotToCamera)
    Moves a simulated camera, for a camera on a turret or other mechanism.
    Applies configured Gaussian X/Y field-tag placement jitter to a simulated robot pose.
    void
    Clears any active blackout, stochastic dropout probability, and field-tag jitter.
    static org.photonvision.simulation.PhotonCameraSim
    createCameraSim(String name, VisionCameraConfig config, CameraModel defaultModel, org.photonvision.PhotonCamera camera)
    Makes the PhotonVision simulation of a camera and adds it to the camera's world (the config's, or getDefault()), if the robot program runs in simulation and the config allows it.
    Optional<org.photonvision.simulation.PhotonCameraSim>
    Finds a simulated camera by its PhotonVision name.
    A field drawing of the simulated robot, cameras and the targets they see.
    static VisionSim
    The world cameras join when their config doesn't name one.
    The field whose AprilTags the cameras see.
    org.photonvision.simulation.VisionSystemSim
    The PhotonVision simulation underneath, for settings YALL doesn't cover.
    injectBlackout(double currentTimeSeconds, Time duration)
    Injects a deterministic camera blackout lasting duration starting at currentTimeSeconds.
    boolean
    isBlackedOut(double nowSeconds)
    Whether this simulated world is currently in an injected blackout window at nowSeconds.
    static String
    Name of the private PhotonVision camera made for a camera that isn't a PhotonVision camera.
    void
    removeCamera(org.photonvision.simulation.PhotonCameraSim cameraSim)
    Removes a simulated camera.
    void
    Removes a group of targets added with addVisionTargets(String, VisionTargetSim...).
    void
    Moves the simulated robot without processing frames, for example when resetting the robot pose.
    static void
    Replaces the default world, for example to use another field.
    boolean
    shouldDropFrame(double nowSeconds)
    Evaluates whether a simulated frame arriving at nowSeconds should be dropped due to an active blackout or stochastic dropout probability.
    void
    update(Pose2d robotPose)
    Moves the simulated robot and processes a frame on each camera that is due one.
    void
    update(Pose3d robotPose)
    Moves the simulated robot and processes a frame on each camera that is due one.
    withDropoutProbability(double probability, long seed)
    Configures deterministic per-frame stochastic dropout probability for simulated cameras.
    withFieldTagJitter(Distance stdDev, long seed)
    Configures Gaussian X/Y field-tag placement jitter (for example, Millimeters.of(13) to model Welded vs.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • VisionSim

      public VisionSim(String name, Field field)
      Creates a simulated world.
      Parameters:
      name - Name of the world, used for its NetworkTables debug field.
      field - Field whose AprilTags the cameras see.
  • Method Details

    • getDefault

      public static VisionSim getDefault()
      The world cameras join when their config doesn't name one. It holds the AprilTags of WPILib's Fields.DEFAULT_FIELD.
      Returns:
      The default world.
    • setDefault

      public static void setDefault(VisionSim sim)
      Replaces the default world, for example to use another field. Call it before creating cameras.
      Parameters:
      sim - The new default world.
    • createCameraSim

      public static org.photonvision.simulation.PhotonCameraSim createCameraSim(String name, VisionCameraConfig config, CameraModel defaultModel, org.photonvision.PhotonCamera camera)
      Makes the PhotonVision simulation of a camera and adds it to the camera's world (the config's, or getDefault()), if the robot program runs in simulation and the config allows it. Camera wrappers call this; robot code doesn't need to.
      Parameters:
      name - Name of the camera.
      config - The camera's config.
      defaultModel - Camera to simulate when the config sets no camera properties.
      camera - PhotonVision camera the simulation publishes to, or null to make a private one named YALL-<name>.
      Returns:
      The simulated camera, or null when not simulating.
    • privateCameraName

      public static String privateCameraName(String cameraName)
      Name of the private PhotonVision camera made for a camera that isn't a PhotonVision camera.
      Parameters:
      cameraName - Name of the camera.
      Returns:
      YALL-<cameraName>.
    • addCamera

      public void addCamera(org.photonvision.simulation.PhotonCameraSim cameraSim, Transform3d robotToCamera)
      Adds a simulated camera. VisionCamera calls this; teams don't need to.
      Parameters:
      cameraSim - The simulated camera.
      robotToCamera - Where the camera is on the robot.
    • removeCamera

      public void removeCamera(org.photonvision.simulation.PhotonCameraSim cameraSim)
      Removes a simulated camera.
      Parameters:
      cameraSim - The simulated camera.
    • adjustCamera

      public void adjustCamera(org.photonvision.simulation.PhotonCameraSim cameraSim, Transform3d robotToCamera)
      Moves a simulated camera, for a camera on a turret or other mechanism.
      Parameters:
      cameraSim - The simulated camera.
      robotToCamera - Where the camera is on the robot now.
    • addVisionTargets

      public void addVisionTargets(String type, org.photonvision.simulation.VisionTargetSim... targets)
      Adds targets other than the field's AprilTags, such as game pieces for object detection: use VisionTargetSim(Pose3d, org.photonvision.estimation.TargetModel, int, float) with a class ID.
      Parameters:
      type - Name of the group of targets, for the debug field.
      targets - The targets.
    • removeVisionTargets

      public void removeVisionTargets(String type)
      Removes a group of targets added with addVisionTargets(String, VisionTargetSim...).
      Parameters:
      type - Name of the group.
    • update

      public void update(Pose2d robotPose)
      Moves the simulated robot and processes a frame on each camera that is due one.
      Parameters:
      robotPose - The robot's true pose.
    • update

      public void update(Pose3d robotPose)
      Moves the simulated robot and processes a frame on each camera that is due one.
      Parameters:
      robotPose - The robot's true pose.
    • resetRobotPose

      public void resetRobotPose(Pose2d robotPose)
      Moves the simulated robot without processing frames, for example when resetting the robot pose.
      Parameters:
      robotPose - The robot's new true pose.
    • getDebugField

      public Field2d getDebugField()
      A field drawing of the simulated robot, cameras and the targets they see.
      Returns:
      The debug field.
    • getField

      public Field getField()
      The field whose AprilTags the cameras see.
      Returns:
      The field.
    • getVisionSystemSim

      public org.photonvision.simulation.VisionSystemSim getVisionSystemSim()
      The PhotonVision simulation underneath, for settings YALL doesn't cover.
      Returns:
      PhotonVision's vision system simulation.
    • getCameraSim

      public Optional<org.photonvision.simulation.PhotonCameraSim> getCameraSim(String name)
      Finds a simulated camera by its PhotonVision name.
      Parameters:
      name - The simulated camera's name.
      Returns:
      The camera, if it is in this world.
    • injectBlackout

      public VisionSim injectBlackout(double currentTimeSeconds, Time duration)
      Injects a deterministic camera blackout lasting duration starting at currentTimeSeconds. Useful for testing vision-loss fallback and recovery in simulation.
      Parameters:
      currentTimeSeconds - Current monotonic simulation timestamp in seconds.
      duration - Blackout duration (must be non-negative).
      Returns:
      This simulated world, for chaining.
    • isBlackedOut

      public boolean isBlackedOut(double nowSeconds)
      Whether this simulated world is currently in an injected blackout window at nowSeconds.
      Parameters:
      nowSeconds - Current monotonic timestamp in seconds.
      Returns:
      true if frames should be suppressed at nowSeconds.
    • withDropoutProbability

      public VisionSim withDropoutProbability(double probability, long seed)
      Configures deterministic per-frame stochastic dropout probability for simulated cameras.
      Parameters:
      probability - Per-frame drop probability in [0.0, 1.0].
      seed - Deterministic RNG seed for reproducible CI runs.
      Returns:
      This simulated world, for chaining.
    • withFieldTagJitter

      public VisionSim withFieldTagJitter(Distance stdDev, long seed)
      Configures Gaussian X/Y field-tag placement jitter (for example, Millimeters.of(13) to model Welded vs. AndyMark AprilTag field tolerances) applied to simulated pose estimates.
      Parameters:
      stdDev - Standard deviation of X and Y pose jitter (zero disables jitter).
      seed - Deterministic RNG seed for reproducible CI runs.
      Returns:
      This simulated world, for chaining.
    • clearFaults

      public void clearFaults()
      Clears any active blackout, stochastic dropout probability, and field-tag jitter.
    • shouldDropFrame

      public boolean shouldDropFrame(double nowSeconds)
      Evaluates whether a simulated frame arriving at nowSeconds should be dropped due to an active blackout or stochastic dropout probability.
      Parameters:
      nowSeconds - Current monotonic timestamp in seconds.
      Returns:
      true if the simulated frame should be dropped.
    • applyFieldTagJitter

      public Pose3d applyFieldTagJitter(Pose3d pose)
      Applies configured Gaussian X/Y field-tag placement jitter to a simulated robot pose. Returns pose unchanged when jitter is disabled.
      Parameters:
      pose - Simulated robot pose estimate.
      Returns:
      Pose with optional deterministic X/Y jitter applied.