PhotonVision C++ v2027.0.0-alpha-2
Loading...
Searching...
No Matches
PhotonUtils.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) PhotonVision
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#pragma once
26
27#include <wpi/math/geometry/Pose2d.hpp>
28#include <wpi/math/geometry/Rotation2d.hpp>
29#include <wpi/math/geometry/Transform2d.hpp>
30#include <wpi/math/geometry/Translation2d.hpp>
31#include <wpi/units/angle.hpp>
32#include <wpi/units/length.hpp>
33#include <wpi/units/math.hpp>
34
35namespace photon {
37 public:
38 /**
39 * Algorithm from
40 * https://docs.limelightvision.io/en/latest/cs_estimating_distance.html
41 * Estimates range to a target using the target's elevation. This method can
42 * produce more stable results than SolvePNP when well tuned, if the full 6d
43 * robot pose is not required.
44 *
45 * @param cameraHeight The height of the camera off the floor.
46 * @param targetHeight The height of the target off the floor.
47 * @param cameraPitch The pitch of the camera from the horizontal plane.
48 * Positive valueBytes up.
49 * @param targetPitch The pitch of the target in the camera's lens. Positive
50 * values up.
51 * @return The estimated distance to the target.
52 */
53 static wpi::units::meter_t CalculateDistanceToTarget(
54 wpi::units::meter_t cameraHeight, wpi::units::meter_t targetHeight,
55 wpi::units::radian_t cameraPitch, wpi::units::radian_t targetPitch) {
56 return (targetHeight - cameraHeight) /
57 wpi::units::math::tan(cameraPitch + targetPitch);
58 }
59
60 /**
61 * Estimate the Translation2d of the target relative to the camera.
62 *
63 * @param targetDistance The distance to the target.
64 * @param yaw The observed yaw of the target.
65 *
66 * @return The target's camera-relative translation.
67 */
68 static wpi::math::Translation2d EstimateCameraToTargetTranslation(
69 wpi::units::meter_t targetDistance, const wpi::math::Rotation2d& yaw) {
70 return {targetDistance * yaw.Cos(), targetDistance * yaw.Sin()};
71 }
72
73 /**
74 * Estimate the position of the robot in the field.
75 *
76 * @param cameraHeightMeters The physical height of the camera off the floor
77 * in meters.
78 * @param targetHeightMeters The physical height of the target off the floor
79 * in meters. This should be the height of whatever is being targeted (i.e. if
80 * the targeting region is set to top, this should be the height of the top of
81 * the target).
82 * @param cameraPitchRadians The pitch of the camera from the horizontal plane
83 * in radians. Positive values up.
84 * @param targetPitchRadians The pitch of the target in the camera's lens in
85 * radians. Positive values up.
86 * @param targetYaw The observed yaw of the target. Note that this
87 * *must* be CCW-positive, and Photon returns
88 * CW-positive.
89 * @param gyroAngle The current robot gyro angle, likely from
90 * odometry.
91 * @param fieldToTarget A wpi::math::Pose2d representing the target
92 * position in the field coordinate system.
93 * @param cameraToRobot The position of the robot relative to the camera.
94 * If the camera was mounted 3 inches behind the
95 * "origin" (usually physical center) of the robot,
96 * this would be wpi::math::Transform2d(3 inches, 0
97 * inches, 0 degrees).
98 * @return The position of the robot in the field.
99 */
100 static wpi::math::Pose2d EstimateFieldToRobot(
101 wpi::units::meter_t cameraHeight, wpi::units::meter_t targetHeight,
102 wpi::units::radian_t cameraPitch, wpi::units::radian_t targetPitch,
103 const wpi::math::Rotation2d& targetYaw,
104 const wpi::math::Rotation2d& gyroAngle,
105 const wpi::math::Pose2d& fieldToTarget,
106 const wpi::math::Transform2d& cameraToRobot) {
110 CalculateDistanceToTarget(cameraHeight, targetHeight,
111 cameraPitch, targetPitch),
112 targetYaw),
113 fieldToTarget, gyroAngle),
114 fieldToTarget, cameraToRobot);
115 }
116
117 /**
118 * Estimates a {@link wpi::math::Transform2d} that maps the camera position to
119 * the target position, using the robot's gyro. Note that the gyro angle
120 * provided *must* line up with the field coordinate system -- that is, it
121 * should read zero degrees when pointed towards the opposing alliance
122 * station, and increase as the robot rotates CCW.
123 *
124 * @param cameraToTargetTranslation A Translation2d that encodes the x/y
125 * position of the target relative to the
126 * camera.
127 * @param fieldToTarget A wpi::math::Pose2d representing the
128 * target position in the field coordinate system.
129 * @param gyroAngle The current robot gyro angle, likely from
130 * odometry.
131 * @return A wpi::math::Transform2d that takes us from the camera to the
132 * target.
133 */
134 static wpi::math::Transform2d EstimateCameraToTarget(
135 const wpi::math::Translation2d& cameraToTargetTranslation,
136 const wpi::math::Pose2d& fieldToTarget,
137 const wpi::math::Rotation2d& gyroAngle) {
138 // This pose maps our camera at the origin out to our target, in the robot
139 // reference frame
140 // The translation part of this wpi::math::Transform2d is from the above
141 // step, and the rotation uses our robot's gyro.
142 return wpi::math::Transform2d(cameraToTargetTranslation,
143 -gyroAngle - fieldToTarget.Rotation());
144 }
145
146 /**
147 * Estimates the pose of the robot in the field coordinate system, given the
148 * position of the target relative to the camera, the target relative to the
149 * field, and the robot relative to the camera.
150 *
151 * @param cameraToTarget The position of the target relative to the camera.
152 * @param fieldToTarget The position of the target in the field.
153 * @param cameraToRobot The position of the robot relative to the camera. If
154 * the camera was mounted 3 inches behind the "origin"
155 * (usually physical center) of the robot, this would be
156 * wpi::math::Transform2d(3 inches, 0 inches, 0
157 * degrees).
158 * @return The position of the robot in the field.
159 */
160 static wpi::math::Pose2d EstimateFieldToRobot(
161 const wpi::math::Transform2d& cameraToTarget,
162 const wpi::math::Pose2d& fieldToTarget,
163 const wpi::math::Transform2d& cameraToRobot) {
164 return EstimateFieldToCamera(cameraToTarget, fieldToTarget)
165 .TransformBy(cameraToRobot);
166 }
167
168 /**
169 * Estimates the pose of the camera in the field coordinate system, given the
170 * position of the target relative to the camera, and the target relative to
171 * the field. This *only* tracks the position of the camera, not the position
172 * of the robot itself.
173 *
174 * @param cameraToTarget The position of the target relative to the camera.
175 * @param fieldToTarget The position of the target in the field.
176 * @return The position of the camera in the field.
177 */
178 static wpi::math::Pose2d EstimateFieldToCamera(
179 const wpi::math::Transform2d& cameraToTarget,
180 const wpi::math::Pose2d& fieldToTarget) {
181 auto targetToCamera = cameraToTarget.Inverse();
182 return fieldToTarget.TransformBy(targetToCamera);
183 }
184};
185} // namespace photon
Definition PhotonUtils.h:36
static wpi::units::meter_t CalculateDistanceToTarget(wpi::units::meter_t cameraHeight, wpi::units::meter_t targetHeight, wpi::units::radian_t cameraPitch, wpi::units::radian_t targetPitch)
Algorithm from https://docs.limelightvision.io/en/latest/cs_estimating_distance.html Estimates range ...
Definition PhotonUtils.h:53
static wpi::math::Pose2d EstimateFieldToRobot(const wpi::math::Transform2d &cameraToTarget, const wpi::math::Pose2d &fieldToTarget, const wpi::math::Transform2d &cameraToRobot)
Estimates the pose of the robot in the field coordinate system, given the position of the target rela...
Definition PhotonUtils.h:160
static wpi::math::Pose2d EstimateFieldToCamera(const wpi::math::Transform2d &cameraToTarget, const wpi::math::Pose2d &fieldToTarget)
Estimates the pose of the camera in the field coordinate system, given the position of the target rel...
Definition PhotonUtils.h:178
static wpi::math::Translation2d EstimateCameraToTargetTranslation(wpi::units::meter_t targetDistance, const wpi::math::Rotation2d &yaw)
Estimate the Translation2d of the target relative to the camera.
Definition PhotonUtils.h:68
static wpi::math::Transform2d EstimateCameraToTarget(const wpi::math::Translation2d &cameraToTargetTranslation, const wpi::math::Pose2d &fieldToTarget, const wpi::math::Rotation2d &gyroAngle)
Estimates a wpi::math::Transform2d that maps the camera position to the target position,...
Definition PhotonUtils.h:134
static wpi::math::Pose2d EstimateFieldToRobot(wpi::units::meter_t cameraHeight, wpi::units::meter_t targetHeight, wpi::units::radian_t cameraPitch, wpi::units::radian_t targetPitch, const wpi::math::Rotation2d &targetYaw, const wpi::math::Rotation2d &gyroAngle, const wpi::math::Pose2d &fieldToTarget, const wpi::math::Transform2d &cameraToRobot)
Estimate the position of the robot in the field.
Definition PhotonUtils.h:100
Definition VisionEstimation.h:30