PhotonVision C++ v2027.0.0-alpha-2
Loading...
Searching...
No Matches
PhotonCamera.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 <memory>
28#include <string>
29#include <vector>
30
31#include <wpi/driverstation/Alert.hpp>
32#include <wpi/nt/BooleanTopic.hpp>
33#include <wpi/nt/DoubleArrayTopic.hpp>
34#include <wpi/nt/DoubleTopic.hpp>
35#include <wpi/nt/IntegerTopic.hpp>
36#include <wpi/nt/MultiSubscriber.hpp>
37#include <wpi/nt/NetworkTable.hpp>
38#include <wpi/nt/NetworkTableInstance.hpp>
39#include <wpi/nt/RawTopic.hpp>
40#include <wpi/nt/StringTopic.hpp>
41#include <wpi/units/time.hpp>
42
44
45namespace cv {
46class Mat;
47} // namespace cv
48
49namespace photon {
50
51enum LEDMode : int { kDefault = -1, kOff = 0, kOn = 1, kBlink = 2 };
52
53/**
54 * Represents a camera that is connected to PhotonVision.ß
55 */
57 public:
58 /**
59 * Constructs a PhotonCamera from a root table.
60 *
61 * @param instance The NetworkTableInstance to pull data from. This can be a
62 * custom instance in simulation, but should *usually* be the default
63 * NTInstance from {@link NetworkTableInstance::getDefault}
64 * @param cameraName The name of the camera, as seen in the UI.
65 * over.
66 */
67 explicit PhotonCamera(wpi::nt::NetworkTableInstance instance,
68 const std::string_view cameraName);
69
70 /**
71 * Constructs a PhotonCamera from the name of the camera.
72 * @param cameraName The nickname of the camera (found in the PhotonVision
73 * UI).
74 */
75 explicit PhotonCamera(const std::string_view cameraName);
76
78
79 ~PhotonCamera() = default;
80
81 /**
82 * The list of pipeline results sent by PhotonVision since the last call to
83 * GetAllUnreadResults(). Calling this function clears the internal FIFO
84 * queue, and multiple calls to GetAllUnreadResults() will return different
85 * (potentially empty) result arrays. Be careful to call this exactly ONCE per
86 * loop of your robot code! FIFO depth is limited to 20 changes, so make sure
87 * to call this frequently enough to avoid old results being discarded, too!
88 */
89 std::vector<PhotonPipelineResult> GetAllUnreadResults();
90
91 [[deprecated("Replace with GetAllUnreadResults")]] PhotonPipelineResult
93
94 /**
95 * Toggles driver mode.
96 * @param driverMode Whether to set driver mode.
97 */
98 void SetDriverMode(bool driverMode);
99
100 /**
101 * Returns whether the camera is in driver mode.
102 * @return Whether the camera is in driver mode.
103 */
104 bool GetDriverMode() const;
105
106 /**
107 * Sets the FPS limit on the camera.
108 *
109 * <p>An FPS of 0 means to pause processing until a FPS limit greater than 0
110 * is set.
111 *
112 * <p>A negative FPS limit is treated as no FPS limit, and will run as fast as
113 * possible.
114 *
115 * <p>Otherwise, will limit processing to at most the provided FPS limit
116 *
117 * @param fps The FPS limit to set.
118 */
119 void SetFPSLimit(int fpsLimit);
120
121 /**
122 * @return The FPS limit set on the camera, or -1 if no limit is set.
123 */
124 int GetFPSLimit() const;
125
126 /**
127 * Sets whether the camera is enabled, default is true.
128 * @param enabled Whether to enable the camera.
129 */
130 void SetEnabled(bool enabled);
131
132 /**
133 * @return Whether the camera is enabled.
134 */
135 bool GetEnabled() const;
136
137 /**
138 * Request the camera to save a new image file from the input
139 * camera stream with overlays.
140 * Images take up space in the filesystem of the PhotonCamera.
141 * Calling it frequently will fill up disk space and eventually
142 * cause the system to stop working.
143 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
144 * frequently to prevent issues.
145 */
147
148 /**
149 * Request the camera to save a new image file from the output
150 * stream with overlays.
151 * Images take up space in the filesystem of the PhotonCamera.
152 * Calling it frequently will fill up disk space and eventually
153 * cause the system to stop working.
154 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
155 * frequently to prevent issues.
156 */
158
159 /**
160 * Allows the user to select the active pipeline index.
161 * @param index The active pipeline index.
162 */
163 void SetPipelineIndex(int index);
164
165 /**
166 * Returns the active pipeline index.
167 * @return The active pipeline index.
168 */
169 int GetPipelineIndex() const;
170
171 /**
172 * Returns the current LED mode.
173 * @return The current LED mode.
174 */
176
177 /**
178 * Sets the LED mode.
179 * @param led The mode to set to.
180 */
182
183 /**
184 * Returns the name of the camera.
185 * This will return the same value that was given to the constructor as
186 * cameraName.
187 * @return The name of the camera.
188 */
189 const std::string_view GetCameraName() const;
190
191 /**
192 * Returns whether the camera is connected and actively returning new data.
193 * Connection status is debounced.
194 *
195 * @return True if the camera is actively sending frame data, false otherwise.
196 */
198
199 using CameraMatrix = Eigen::Matrix<double, 3, 3>;
200 using DistortionMatrix = Eigen::Matrix<double, 8, 1>;
201
202 /**
203 * Get the camera calibration matrix, in standard OpenCV form
204 *
205 * @return std::optional<cv::Mat>
206 */
207 std::optional<CameraMatrix> GetCameraMatrix();
208
209 /**
210 * Returns the camera calibration's distortion coefficients, in OPENCV8 form.
211 * Higher-order terms are set to 0
212 *
213 * @return The distortion coefficients in a 8x1 matrix, if they are published
214 * by the camera. Empty otherwise.
215 */
216 std::optional<DistortionMatrix> GetDistCoeffs();
217
218 /**
219 * Sets whether or not coprocessor version checks will occur. Setting this to
220 * true will silence all console warnings about coproccessor connection, so be
221 * careful when enabling this and ensure all your coprocessors are
222 * communicating to the robot properly and everything has matching versions.
223 *
224 * @param enabled Whether or not to enable coprocessor version checks
225 */
226 static void SetVersionCheckEnabled(bool enabled);
227
228 std::shared_ptr<wpi::nt::NetworkTable> GetCameraTable() const {
229 return rootTable;
230 }
231
232 // For use in tests
233 bool test = false;
234 std::vector<PhotonPipelineResult> testResult;
235
236 protected:
237 std::shared_ptr<wpi::nt::NetworkTable> mainTable;
238 std::shared_ptr<wpi::nt::NetworkTable> rootTable;
239 wpi::nt::RawSubscriber rawBytesEntry;
240 wpi::nt::IntegerPublisher inputSaveImgEntry;
241 wpi::nt::IntegerSubscriber inputSaveImgSubscriber;
242 wpi::nt::IntegerPublisher outputSaveImgEntry;
243 wpi::nt::IntegerSubscriber outputSaveImgSubscriber;
244 wpi::nt::IntegerPublisher pipelineIndexPub;
245 wpi::nt::IntegerSubscriber pipelineIndexSub;
246 wpi::nt::IntegerPublisher ledModePub;
247 wpi::nt::IntegerSubscriber ledModeSub;
248 wpi::nt::StringSubscriber versionEntry;
249
250 wpi::nt::DoubleArraySubscriber cameraIntrinsicsSubscriber;
251 wpi::nt::DoubleArraySubscriber cameraDistortionSubscriber;
252
253 wpi::nt::BooleanSubscriber driverModeSubscriber;
254 wpi::nt::BooleanPublisher driverModePublisher;
255 wpi::nt::IntegerSubscriber fpsLimitSubscriber;
256 wpi::nt::IntegerPublisher fpsLimitPublisher;
257 wpi::nt::BooleanSubscriber enabledSubscriber;
258 wpi::nt::BooleanPublisher enabledPublisher;
259
260 wpi::nt::IntegerSubscriber ledModeSubscriber;
261
262 wpi::nt::IntegerSubscriber heartbeatSubscriber;
263
264 wpi::nt::MultiSubscriber topicNameSubscriber;
265
266 std::string path;
267 std::string cameraName;
268
269 wpi::Alert disconnectAlert;
270 wpi::Alert timesyncAlert;
271
272 private:
273 wpi::units::second_t lastVersionCheckTime = 0_s;
274 static bool VERSION_CHECK_ENABLED;
275 inline static int InstanceCount = 1;
276
277 wpi::units::second_t prevTimeSyncWarnTime = 0_s;
278
279 int prevHeartbeatValue = -1;
280 wpi::units::second_t prevHeartbeatChangeTime = 0_s;
281
282 void VerifyVersion();
283
284 void UpdateDisconnectAlert();
285 void CheckTimeSyncOrWarn(photon::PhotonPipelineResult& result);
286
287 std::vector<std::string> tablesThatLookLikePhotonCameras();
288};
289
290} // namespace photon
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:56
void SetEnabled(bool enabled)
Sets whether the camera is enabled, default is true.
wpi::nt::IntegerPublisher fpsLimitPublisher
Definition PhotonCamera.h:256
wpi::Alert disconnectAlert
Definition PhotonCamera.h:269
PhotonCamera(wpi::nt::NetworkTableInstance instance, const std::string_view cameraName)
Constructs a PhotonCamera from a root table.
wpi::nt::IntegerSubscriber inputSaveImgSubscriber
Definition PhotonCamera.h:241
wpi::nt::BooleanSubscriber enabledSubscriber
Definition PhotonCamera.h:257
std::optional< CameraMatrix > GetCameraMatrix()
Get the camera calibration matrix, in standard OpenCV form.
std::shared_ptr< wpi::nt::NetworkTable > mainTable
Definition PhotonCamera.h:237
std::shared_ptr< wpi::nt::NetworkTable > rootTable
Definition PhotonCamera.h:238
wpi::nt::StringSubscriber versionEntry
Definition PhotonCamera.h:248
wpi::nt::RawSubscriber rawBytesEntry
Definition PhotonCamera.h:239
int GetFPSLimit() const
wpi::nt::IntegerSubscriber heartbeatSubscriber
Definition PhotonCamera.h:262
void SetFPSLimit(int fpsLimit)
Sets the FPS limit on the camera.
wpi::nt::IntegerSubscriber outputSaveImgSubscriber
Definition PhotonCamera.h:243
PhotonPipelineResult GetLatestResult()
wpi::nt::IntegerSubscriber ledModeSub
Definition PhotonCamera.h:247
wpi::nt::BooleanSubscriber driverModeSubscriber
Definition PhotonCamera.h:253
bool test
Definition PhotonCamera.h:233
const std::string_view GetCameraName() const
Returns the name of the camera.
static void SetVersionCheckEnabled(bool enabled)
Sets whether or not coprocessor version checks will occur.
Eigen::Matrix< double, 8, 1 > DistortionMatrix
Definition PhotonCamera.h:200
wpi::nt::IntegerPublisher pipelineIndexPub
Definition PhotonCamera.h:244
wpi::nt::IntegerSubscriber pipelineIndexSub
Definition PhotonCamera.h:245
void SetDriverMode(bool driverMode)
Toggles driver mode.
wpi::nt::IntegerSubscriber ledModeSubscriber
Definition PhotonCamera.h:260
wpi::nt::BooleanPublisher enabledPublisher
Definition PhotonCamera.h:258
void TakeInputSnapshot(void)
Request the camera to save a new image file from the input camera stream with overlays.
bool IsConnected()
Returns whether the camera is connected and actively returning new data.
PhotonCamera(const std::string_view cameraName)
Constructs a PhotonCamera from the name of the camera.
bool GetDriverMode() const
Returns whether the camera is in driver mode.
std::shared_ptr< wpi::nt::NetworkTable > GetCameraTable() const
Definition PhotonCamera.h:228
wpi::nt::IntegerSubscriber fpsLimitSubscriber
Definition PhotonCamera.h:255
void SetPipelineIndex(int index)
Allows the user to select the active pipeline index.
LEDMode GetLEDMode() const
Returns the current LED mode.
wpi::nt::IntegerPublisher ledModePub
Definition PhotonCamera.h:246
void SetLEDMode(LEDMode led)
Sets the LED mode.
std::vector< PhotonPipelineResult > GetAllUnreadResults()
The list of pipeline results sent by PhotonVision since the last call to GetAllUnreadResults().
std::string cameraName
Definition PhotonCamera.h:267
wpi::Alert timesyncAlert
Definition PhotonCamera.h:270
std::vector< PhotonPipelineResult > testResult
Definition PhotonCamera.h:234
void TakeOutputSnapshot(void)
Request the camera to save a new image file from the output stream with overlays.
wpi::nt::DoubleArraySubscriber cameraDistortionSubscriber
Definition PhotonCamera.h:251
wpi::nt::DoubleArraySubscriber cameraIntrinsicsSubscriber
Definition PhotonCamera.h:250
bool GetEnabled() const
wpi::nt::BooleanPublisher driverModePublisher
Definition PhotonCamera.h:254
wpi::nt::IntegerPublisher outputSaveImgEntry
Definition PhotonCamera.h:242
std::string path
Definition PhotonCamera.h:266
wpi::nt::IntegerPublisher inputSaveImgEntry
Definition PhotonCamera.h:240
std::optional< DistortionMatrix > GetDistCoeffs()
Returns the camera calibration's distortion coefficients, in OPENCV8 form.
PhotonCamera(PhotonCamera &&)=default
wpi::nt::MultiSubscriber topicNameSubscriber
Definition PhotonCamera.h:264
int GetPipelineIndex() const
Returns the active pipeline index.
Eigen::Matrix< double, 3, 3 > CameraMatrix
Definition PhotonCamera.h:199
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:34
Definition PhotonCamera.h:45
Definition VisionEstimation.h:30
LEDMode
Definition PhotonCamera.h:51
@ kOff
Definition PhotonCamera.h:51
@ kBlink
Definition PhotonCamera.h:51
@ kOn
Definition PhotonCamera.h:51
@ kDefault
Definition PhotonCamera.h:51