PhotonVision C++ v2026.2.2
Loading...
Searching...
No Matches
PhotonCameraSim.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 <algorithm>
28#include <limits>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <cameraserver/CameraServer.h>
34#include <frc/Timer.h>
35#include <frc/apriltag/AprilTagFieldLayout.h>
36#include <frc/apriltag/AprilTagFields.h>
37#include <photon/PhotonCamera.h>
45#include <units/math.h>
46#include <wpi/timestamp.h>
47
48namespace photon {
50 public:
51 /**
52 * Constructs a handle for simulating PhotonCamera values. Processing
53 * simulated targets through this class will change the associated
54 * PhotonCamera's results.
55 *
56 * WARNING: This constructor's camera has a 90 deg FOV with no simulated lag!
57 *
58 * By default, the minimum target area is 100 pixels and there is no
59 * maximum sight range.
60 *
61 * @param camera The camera to be simulated
62 */
63 explicit PhotonCameraSim(PhotonCamera* camera);
64
65 /**
66 * Constructs a handle for simulating PhotonCamera values. Processing
67 * simulated targets through this class will change the associated
68 * PhotonCamera's results.
69 *
70 * By default, the minimum target area is 100 pixels and there is no
71 * maximum sight range.
72 *
73 * @param camera The camera to be simulated
74 * @param prop Properties of this camera such as FOV and FPS
75 * @param tagLayout The AprilTagFieldLayout used to solve for tag
76 * positions.
77 */
79 const frc::AprilTagFieldLayout& tagLayout =
80 frc::AprilTagFieldLayout::LoadField(
81 frc::AprilTagField::kDefaultField));
82
83 /**
84 * Constructs a handle for simulating PhotonCamera values. Processing
85 * simulated targets through this class will change the associated
86 * PhotonCamera's results.
87 *
88 * @param camera The camera to be simulated
89 * @param prop Properties of this camera such as FOV and FPS
90 * @param minTargetAreaPercent The minimum percentage (0 - 100) a detected
91 * target must take up of the camera's image to be processed. Match this with
92 * your contour filtering settings in the PhotonVision GUI.
93 * @param maxSightRange Maximum distance at which the target is
94 * illuminated to your camera. Note that minimum target area of the image is
95 * separate from this.
96 */
98 double minTargetAreaPercent, units::meter_t maxSightRange);
99
100 /**
101 * Returns the camera being simulated.
102 *
103 * @return The camera
104 */
105 inline PhotonCamera* GetCamera() { return cam; }
106
107 /**
108 * Returns the minimum percentage (0 - 100) a detected target must take up of
109 * the camera's image to be processed.
110 *
111 * @return The percentage
112 */
113 inline double GetMinTargetAreaPercent() { return minTargetAreaPercent; }
114
115 /**
116 * Returns the minimum number of pixels a detected target must take up in the
117 * camera's image to be processed.
118 *
119 * @return The number of pixels
120 */
121 inline double GetMinTargetAreaPixels() {
122 return minTargetAreaPercent / 100.0 * prop.GetResArea();
123 }
124
125 /**
126 * Returns the maximum distance at which the target is illuminated to your
127 * camera. Note that minimum target area of the image is separate from this.
128 *
129 * @return The distance
130 */
131 inline units::meter_t GetMaxSightRange() { return maxSightRange; }
132 inline const cs::CvSource& GetVideoSimRaw() { return videoSimRaw; }
133 inline const cv::Mat& GetVideoSimFrameRaw() { return videoSimFrameRaw; }
134
135 /**
136 * Determines if this target's pose should be visible to the camera without
137 * considering its projected image points. Does not account for image area.
138 *
139 * @param camPose Camera's 3d pose
140 * @param target Vision target containing pose and shape
141 * @return If this vision target can be seen before image projection.
142 */
143 bool CanSeeTargetPose(const frc::Pose3d& camPose,
144 const VisionTargetSim& target);
145
146 /**
147 * Determines if all target points are inside the camera's image.
148 *
149 * @param points The target's 2d image points
150 * @return True if all the target points are inside the camera's image, false
151 * otherwise.
152 */
153 bool CanSeeCorner(const std::vector<cv::Point2f>& points);
154
155 /**
156 * Determine if this camera should process a new frame based on performance
157 * metrics and the time since the last update. This returns an Optional which
158 * is either empty if no update should occur or a Long of the timestamp in
159 * microseconds of when the frame which should be received by NT. If a
160 * timestamp is returned, the last frame update time becomes that timestamp.
161 *
162 * @return Optional long which is empty while blocked or the NT entry
163 * timestamp in microseconds if ready
164 */
165 std::optional<uint64_t> ConsumeNextEntryTime();
166
167 /**
168 * Sets the minimum percentage (0 - 100) a detected target must take up of the
169 * camera's image to be processed.
170 *
171 * @param areaPercent The percentage
172 */
173 inline void SetMinTargetAreaPercent(double areaPercent) {
174 minTargetAreaPercent = areaPercent;
175 }
176
177 /**
178 * Sets the minimum number of pixels a detected target must take up in the
179 * camera's image to be processed.
180 *
181 * @param areaPx The number of pixels
182 */
183 inline void SetMinTargetAreaPixels(double areaPx) {
184 minTargetAreaPercent = areaPx / prop.GetResArea() * 100;
185 }
186
187 /**
188 * Sets the maximum distance at which the target is illuminated to your
189 * camera. Note that minimum target area of the image is separate from this.
190 *
191 * @param rangeMeters The distance
192 */
193 inline void SetMaxSightRange(units::meter_t range) { maxSightRange = range; }
194
195 /**
196 * Sets whether the raw video stream simulation is enabled.
197 *
198 * Note: This may increase loop times.
199 *
200 * @param enabled Whether or not to enable the raw video stream
201 */
202 inline void EnableRawStream(bool enabled) { videoSimRawEnabled = enabled; }
203
204 /**
205 * Sets whether a wireframe of the field is drawn to the raw video stream.
206 *
207 * Note: This will dramatically increase loop times.
208 *
209 * @param enabled Whether or not to enable the wireframe in the raw video
210 * stream
211 */
212 inline void EnableDrawWireframe(bool enabled) {
213 videoSimWireframeEnabled = enabled;
214 }
215
216 /**
217 * Sets the resolution of the drawn wireframe if enabled. Drawn line segments
218 * will be subdivided into smaller segments based on a threshold set by the
219 * resolution.
220 *
221 * @param resolution Resolution as a fraction(0 - 1) of the video frame's
222 * diagonal length in pixels
223 */
224 inline void SetWireframeResolution(double resolution) {
225 videoSimWireframeResolution = resolution;
226 }
227
228 /**
229 * Sets whether the processed video stream simulation is enabled.
230 *
231 * @param enabled Whether or not to enable the processed video stream
232 */
233 inline void EnabledProcessedStream(double enabled) {
234 videoSimProcEnabled = enabled;
235 }
236 PhotonPipelineResult Process(units::second_t latency,
237 const frc::Pose3d& cameraPose,
238 std::vector<VisionTargetSim> targets);
239
242 uint64_t ReceiveTimestamp);
243
245
246 private:
247 PhotonCamera* cam;
248
249 NTTopicSet ts{};
250 int64_t heartbeatCounter{0};
251
252 uint64_t nextNTEntryTime{wpi::Now()};
253
254 units::meter_t maxSightRange{std::numeric_limits<double>::max()};
255 static constexpr double kDefaultMinAreaPx{100};
256 double minTargetAreaPercent;
257
258 frc::AprilTagFieldLayout tagLayout;
259
260 cs::CvSource videoSimRaw;
261 cv::Mat videoSimFrameRaw{};
262 bool videoSimRawEnabled{true};
263 bool videoSimWireframeEnabled{false};
264 double videoSimWireframeResolution{0.1};
265 cs::CvSource videoSimProcessed;
266 cv::Mat videoSimFrameProcessed{};
267 bool videoSimProcEnabled{true};
268};
269} // namespace photon
Definition NTTopicSet.h:37
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:56
Definition PhotonCameraSim.h:49
std::optional< uint64_t > ConsumeNextEntryTime()
Determine if this camera should process a new frame based on performance metrics and the time since t...
SimCameraProperties prop
Definition PhotonCameraSim.h:244
void SetWireframeResolution(double resolution)
Sets the resolution of the drawn wireframe if enabled.
Definition PhotonCameraSim.h:224
const cs::CvSource & GetVideoSimRaw()
Definition PhotonCameraSim.h:132
const cv::Mat & GetVideoSimFrameRaw()
Definition PhotonCameraSim.h:133
PhotonCamera * GetCamera()
Returns the camera being simulated.
Definition PhotonCameraSim.h:105
PhotonCameraSim(PhotonCamera *camera, const SimCameraProperties &props, const frc::AprilTagFieldLayout &tagLayout=frc::AprilTagFieldLayout::LoadField(frc::AprilTagField::kDefaultField))
Constructs a handle for simulating PhotonCamera values.
void EnableDrawWireframe(bool enabled)
Sets whether a wireframe of the field is drawn to the raw video stream.
Definition PhotonCameraSim.h:212
double GetMinTargetAreaPixels()
Returns the minimum number of pixels a detected target must take up in the camera's image to be proce...
Definition PhotonCameraSim.h:121
PhotonCameraSim(PhotonCamera *camera, const SimCameraProperties &props, double minTargetAreaPercent, units::meter_t maxSightRange)
Constructs a handle for simulating PhotonCamera values.
void EnabledProcessedStream(double enabled)
Sets whether the processed video stream simulation is enabled.
Definition PhotonCameraSim.h:233
units::meter_t GetMaxSightRange()
Returns the maximum distance at which the target is illuminated to your camera.
Definition PhotonCameraSim.h:131
PhotonPipelineResult Process(units::second_t latency, const frc::Pose3d &cameraPose, std::vector< VisionTargetSim > targets)
void SubmitProcessedFrame(const PhotonPipelineResult &result)
bool CanSeeCorner(const std::vector< cv::Point2f > &points)
Determines if all target points are inside the camera's image.
void SetMinTargetAreaPixels(double areaPx)
Sets the minimum number of pixels a detected target must take up in the camera's image to be processe...
Definition PhotonCameraSim.h:183
void SubmitProcessedFrame(const PhotonPipelineResult &result, uint64_t ReceiveTimestamp)
void EnableRawStream(bool enabled)
Sets whether the raw video stream simulation is enabled.
Definition PhotonCameraSim.h:202
double GetMinTargetAreaPercent()
Returns the minimum percentage (0 - 100) a detected target must take up of the camera's image to be p...
Definition PhotonCameraSim.h:113
void SetMaxSightRange(units::meter_t range)
Sets the maximum distance at which the target is illuminated to your camera.
Definition PhotonCameraSim.h:193
bool CanSeeTargetPose(const frc::Pose3d &camPose, const VisionTargetSim &target)
Determines if this target's pose should be visible to the camera without considering its projected im...
PhotonCameraSim(PhotonCamera *camera)
Constructs a handle for simulating PhotonCamera values.
void SetMinTargetAreaPercent(double areaPercent)
Sets the minimum percentage (0 - 100) a detected target must take up of the camera's image to be proc...
Definition PhotonCameraSim.h:173
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:37
Calibration and performance values for this camera.
Definition SimCameraProperties.h:59
int GetResArea() const
Gets the area of the simulated camera image.
Definition SimCameraProperties.h:144
Definition VisionTargetSim.h:34
Definition TargetModel.h:27