|
| 1 | +AprilTags and RobotPoseEstimator |
| 2 | +================================ |
| 3 | + |
| 4 | +.. note:: For more information on how to methods to get AprilTag data, look :ref:`here <docs/programming/photonlib/getting-target-data:Getting AprilTag Data From A Target>`. |
| 5 | + |
| 6 | +PhotonLib includes a ``RobotPoseEstimator`` class, which allows you to combine the pose data from all tags in view in order to get one final pose using different strategies. |
| 7 | + |
| 8 | +Creating an ``AprilTagFieldLayout`` |
| 9 | +----------------------------------- |
| 10 | +``AprilTagFieldLayout`` is used to represent a layout of AprilTags within a space (field, shop at home, classroom, etc.). WPILib provides a JSON that describes the layout of AprilTags on the field which you can then use in the AprilTagFieldLayout constructor. You can also specify a custom layout. |
| 11 | + |
| 12 | +The API documentation can be found in here: `Java <https://github.wpilib.org/allwpilib/docs/beta/java/edu/wpi/first/apriltag/AprilTagFieldLayout.html>`_ and `C++ <https://github.wpilib.org/allwpilib/docs/beta/cpp/classfrc_1_1_april_tag_field_layout.html>`_. |
| 13 | + |
| 14 | +.. tab-set-code:: |
| 15 | + .. code-block:: java |
| 16 | +
|
| 17 | + // The parameter for loadFromResource() will be different depending on the game. |
| 18 | + AprilTagFieldLayout aprilTagFieldLayout = new ApriltagFieldLayout(AprilTagFieldLayout.loadFromResource(AprilTagFields.k2022RapidReact.m_resourceFile)); |
| 19 | +
|
| 20 | + .. code-block:: c++ |
| 21 | + |
| 22 | + // Two example tags in our layout -- ID 0 at (3, 3) and 0 rotation, and |
| 23 | + // id 1 and (5, 5) and 0 rotation. |
| 24 | + std::vector<frc::AprilTag> tags = { |
| 25 | + {0, frc::Pose3d(units::meter_t(3), units::meter_t(3), units::meter_t(3), |
| 26 | + frc::Rotation3d())}, |
| 27 | + {1, frc::Pose3d(units::meter_t(5), units::meter_t(5), units::meter_t(5), |
| 28 | + frc::Rotation3d())}}; |
| 29 | + std::shared_ptr<frc::AprilTagFieldLayout> aprilTags = |
| 30 | + std::make_shared<frc::AprilTagFieldLayout>(tags, 54_ft, 27_ft); |
| 31 | + |
| 32 | +Creating a ``RobotPoseEstimator`` |
| 33 | +--------------------------------- |
| 34 | +The RobotPoseEstimator has a constructor that takes an ``AprilTagFieldLayout`` (see above), ``PoseStrategy``, and ``ArrayList<Pair<PhotonCamera, Transform3d>>``. ``PoseStrategy`` has five possible values: |
| 35 | + |
| 36 | +* LOWEST_AMBIGUITY |
| 37 | + * Choose the Pose with the lowest ambiguity |
| 38 | +* CLOSEST_TO_CAMERA_HEIGHT |
| 39 | + * Choose the Pose which is closest to the camera height |
| 40 | +* CLOSEST_TO_REFERENCE_POSE |
| 41 | + * Choose the Pose which is closest to the camera height |
| 42 | +* CLOSEST_TO_LAST_POSE |
| 43 | + * Choose the Pose which is closest to the last pose calculated |
| 44 | +* AVERAGE_BEST_TARGETS |
| 45 | + * Choose the Pose which is the average of all the poses from each tag |
| 46 | + |
| 47 | +.. tab-set-code:: |
| 48 | + .. code-block:: java |
| 49 | +
|
| 50 | + //Forward Camera |
| 51 | + cam = new PhotonCamera("testCamera"); |
| 52 | + Transform3d robotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5), new Rotation3d(0,0,0)); //Cam mounted facing forward, half a meter forward of center, half a meter up from center. |
| 53 | +
|
| 54 | + // ... Add other cameras here |
| 55 | +
|
| 56 | + // Assemble the list of cameras & mount locations |
| 57 | + var camList = new ArrayList<Pair<PhotonCamera, Transform3d>>(); |
| 58 | + camList.add(new Pair<PhotonCamera, Transform3d>(cam, robotToCam)); |
| 59 | + RobotPoseEstimator robotPoseEstimator = new RobotPoseEstimator(aprilTagFieldLayout, PoseStrategy.CLOSEST_TO_REFERENCE_POSE, camList); |
| 60 | +
|
| 61 | + .. code-block:: c++ |
| 62 | + |
| 63 | + // Forward Camera |
| 64 | + std::shared_ptr<photonlib::PhotonCamera> cameraOne = |
| 65 | + std::make_shared<photonlib::PhotonCamera>("testCamera"); |
| 66 | + // Camera is mounted facing forward, half a meter forward of center, half a |
| 67 | + // meter up from center. |
| 68 | + frc::Transform3d robotToCam = |
| 69 | + frc::Transform3d(frc::Translation3d(0.5_m, 0_m, 0.5_m), |
| 70 | + frc::Rotation3d(0_rad, 0_rad, 0_rad)); |
| 71 | + |
| 72 | + // ... Add other cameras here |
| 73 | + |
| 74 | + // Assemble the list of cameras & mount locations |
| 75 | + std::vector< |
| 76 | + std::pair<std::shared_ptr<photonlib::PhotonCamera>, frc::Transform3d>> |
| 77 | + cameras; |
| 78 | + cameras.push_back(std::make_pair(cameraOne, robotToCam)); |
| 79 | + |
| 80 | + photonlib::RobotPoseEstimator estimator( |
| 81 | + aprilTags, photonlib::CLOSEST_TO_REFERENCE_POSE, cameras); |
| 82 | + |
| 83 | +Using a ``RobotPoseEstimator`` |
| 84 | +------------------------------ |
| 85 | +Calling ``update()`` on your ``RobotPoseEstimator`` will return a ``Pair<Pose3d, Double>``, which includes a ``Pose3d`` of the latest estimated pose (using the selected strategy) along with a ``Double`` of the latency in milliseconds. You should be updating your `drivetrain pose estimator <https://docs.wpilib.org/en/latest/docs/software/advanced-controls/state-space/state-space-pose-estimators.html>`_ with the result from the ``RobotPoseEstimator`` every loop using ``addVisionMeasurement()``. See our `code example <https://www.google.com/>`_ for more. |
| 86 | + |
| 87 | +.. tab-set-code:: |
| 88 | + .. code-block:: java |
| 89 | +
|
| 90 | + public Pair<Pose2d, Double> getEstimatedGlobalPose(Pose3d prevEstimatedRobotPose) { |
| 91 | + robotPoseEstimator.setReferencePose(prevEstimatedRobotPose); |
| 92 | + var currentTime = Timer.getFPGATimestamp(); |
| 93 | + var result = robotPoseEstimator.update(); |
| 94 | + if(result.getFirst() != null){ |
| 95 | + return new Pair<Pose2d, Double>(result.getFirst().toPose2d(), currentTime - result.getSecond()); |
| 96 | + } else { |
| 97 | + return new Pair<Pose2d, Double>(null, 0.0); |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + .. code-block:: c++ |
| 102 | + |
| 103 | + std::pair<frc::Pose2d, units::millisecond_t> getEstimatedGlobalPose( |
| 104 | + frc::Pose3d prevEstimatedRobotPose) { |
| 105 | + robotPoseEstimator.SetReferencePose(prevEstimatedRobotPose); |
| 106 | + units::millisecond_t currentTime = frc::Timer::GetFPGATimestamp(); |
| 107 | + auto result = robotPoseEstimator.Update(); |
| 108 | + if (result.second) { |
| 109 | + return std::make_pair<>(result.first.ToPose2d(), |
| 110 | + currentTime - result.second); |
| 111 | + } else { |
| 112 | + return std::make_pair(frc::Pose2d(), 0_ms); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +Additional ``RobotPoseEstimator`` Methods |
| 117 | +----------------------------------------- |
| 118 | + |
| 119 | +``setRefrencePose(Pose3d referencePose)`` |
| 120 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 121 | + |
| 122 | +Updates the stored reference pose when using the CLOSEST_TO_REFERENCE_POSE strategy. |
| 123 | + |
| 124 | +``setLastPose(Pose3d lastPose)`` |
| 125 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 126 | + |
| 127 | +Update the stored last pose. Useful for setting the initial estimate when using the CLOSEST_TO_LAST_POSE strategy. |
0 commit comments