Using WPILib Pose Estimation, Simulation, and PhotonVision Together

Knowledge and Equipment Needed

Background

Full code may be found in the PhotonLib repository (Java).

This example builds upon WPILib’s Differential Drive Pose Estimator. It adds a PhotonCamera to gather estimates of the robot’s position on the field. This in turn can be used for aligning with vision targets, and increasing accuracy of autonomous routines.

To support simulation, a SimVisionSystem is used to drive data into the PhotonCamera. The far high goal target from 2020 is modeled.

Walkthrough

WPILib’s Pose2d class is used to represent robot positions on the field.

Three different Pose2d positions are relevant for this example:

  1. Desired Pose: The location the some autonomous routine wants the robot to be in.

  2. Estimated Pose: The location the software believes the robot to be in, based on physics models and sensor feedback.

  3. Actual Pose: The locations he robot is actually at. The physics simulation generates this in simulation, but it cannot be directly measured on the real robot.

Estimating Pose

The DrivetrainPoseEstimator class is responsible for generating an estimated robot pose using sensor readings (including PhotonVision).

Please reference the WPILib documentation on using the DifferentialDrivePoseEstimator class.

Specifically, to incorporate Photon Vision, we need to create a PhotonCamera:

41    private PhotonCamera cam = new PhotonCamera(Constants.kCamName);

//Coming soon!

Then, during periodic execution, we read back results. If we see a target in the image, we pass the camera-measured pose of the robot to the DifferentialDrivePoseEstimator.

67    /**
68     * Perform all periodic pose estimation tasks.
69     *
70     * @param actWheelSpeeds Current Speeds (in m/s) of the drivetrain wheels
71     * @param leftDist Distance (in m) the left wheel has traveled
72     * @param rightDist Distance (in m) the right wheel has traveled
73     */
74    public void update(
75            DifferentialDriveWheelSpeeds actWheelSpeeds, double leftDist, double rightDist) {
76        m_poseEstimator.update(gyro.getRotation2d(), actWheelSpeeds, leftDist, rightDist);
77
78        var res = cam.getLatestResult();
79        if (res.hasTargets()) {
80            double imageCaptureTime = Timer.getFPGATimestamp() - res.getLatencyMillis();
81            Transform2d camToTargetTrans = res.getBestTarget().getCameraToTarget();
82            Pose2d camPose = Constants.kFarTargetPose.transformBy(camToTargetTrans.inverse());
83            m_poseEstimator.addVisionMeasurement(
84                    camPose.transformBy(Constants.kCameraToRobot), imageCaptureTime);
85        }
86    }

//Coming soon!

That’s it!

Simulating the Camera

First, we create a new SimVisionSystem to represent our camera and coprocessor running PhotonVision.

69    // Simulated Vision System.
70    // Configure these to match your PhotonVision Camera,
71    // pipeline, and LED setup.
72    double camDiagFOV = 75.0; // degrees
73    double camPitch = 15.0; // degrees
74    double camHeightOffGround = 0.85; // meters
75    double maxLEDRange = 20; // meters
76    int camResolutionWidth = 640; // pixels
77    int camResolutionHeight = 480; // pixels
78    double minTargetArea = 10; // square pixels
79
80    SimVisionSystem simVision =
81            new SimVisionSystem(
82                    Constants.kCamName,
83                    camDiagFOV,
84                    camPitch,
85                    Constants.kCameraToRobot,
86                    camHeightOffGround,
87                    maxLEDRange,
88                    camResolutionWidth,
89                    camResolutionHeight,
90                    minTargetArea);

//Coming soon!

Next, we create objects to represent the physical location and size of the vision targets we are calibrated to detect. This example models the down-field high goal vision target from the 2020 and 2021 games.

71    // See
72    // https://firstfrc.blob.core.windows.net/frc2020/PlayingField/2020FieldDrawing-SeasonSpecific.pdf
73    // page 208
74    public static final double targetWidth =
75            Units.inchesToMeters(41.30) - Units.inchesToMeters(6.70); // meters
76
77    // See
78    // https://firstfrc.blob.core.windows.net/frc2020/PlayingField/2020FieldDrawing-SeasonSpecific.pdf
79    // page 197
80    public static final double targetHeight =
81            Units.inchesToMeters(98.19) - Units.inchesToMeters(81.19); // meters
82    public static final double targetHeightAboveGround = Units.inchesToMeters(81.19); // meters
83
84    // See https://firstfrc.blob.core.windows.net/frc2020/PlayingField/LayoutandMarkingDiagram.pdf
85    // pages 4 and 5
86    public static final double kFarTgtXPos = Units.feetToMeters(54);
87    public static final double kFarTgtYPos =
88            Units.feetToMeters(27 / 2) - Units.inchesToMeters(43.75) - Units.inchesToMeters(48.0 / 2.0);
89    public static final Pose2d kFarTargetPose =
90            new Pose2d(new Translation2d(kFarTgtXPos, kFarTgtYPos), new Rotation2d(0.0));
91
92    public static final SimVisionTarget kFarTarget =
93            new SimVisionTarget(kFarTargetPose, targetHeightAboveGround, targetWidth, targetHeight);

//Coming soon!

Finally, we add our target to the simulated vision system.

92    public DrivetrainSim() {
93        simVision.addSimVisionTarget(Constants.kFarTarget);
94    }

//Coming soon!

If you have additional targets you want to detect, you can add them in the same way as the first one.

Updating the Simulated Vision System

Once we have all the properties of our simulated vision system defined, the work to do at runtime becomes very minimal. Simply pass in the robot’s pose periodically to the simulated vision system.

133        // Update PhotonVision based on our new robot position.
134        simVision.processFrame(drivetrainSimulator.getPose());

//Coming soon!

The rest is done behind the scenes.