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