PhotonVision C++ v2026.2.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 <frc/Alert.h>
32#include <networktables/BooleanTopic.h>
33#include <networktables/DoubleArrayTopic.h>
34#include <networktables/DoubleTopic.h>
35#include <networktables/IntegerTopic.h>
36#include <networktables/MultiSubscriber.h>
37#include <networktables/NetworkTable.h>
38#include <networktables/NetworkTableInstance.h>
39#include <networktables/RawTopic.h>
40#include <networktables/StringTopic.h>
41#include <units/time.h>
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(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 * @param fpsLimit The FPS limit to set. Use -1 for unlimited FPS.
108 */
109 void SetFPSLimit(int fpsLimit);
110
111 /**
112 * @return The FPS limit set on the camera, or -1 if no limit is set.
113 */
114 int GetFPSLimit() const;
115
116 /**
117 * Request the camera to save a new image file from the input
118 * camera stream with overlays.
119 * Images take up space in the filesystem of the PhotonCamera.
120 * Calling it frequently will fill up disk space and eventually
121 * cause the system to stop working.
122 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
123 * frequently to prevent issues.
124 */
126
127 /**
128 * Request the camera to save a new image file from the output
129 * stream with overlays.
130 * Images take up space in the filesystem of the PhotonCamera.
131 * Calling it frequently will fill up disk space and eventually
132 * cause the system to stop working.
133 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
134 * frequently to prevent issues.
135 */
137
138 /**
139 * Allows the user to select the active pipeline index.
140 * @param index The active pipeline index.
141 */
142 void SetPipelineIndex(int index);
143
144 /**
145 * Returns the active pipeline index.
146 * @return The active pipeline index.
147 */
148 int GetPipelineIndex() const;
149
150 /**
151 * Returns the current LED mode.
152 * @return The current LED mode.
153 */
155
156 /**
157 * Sets the LED mode.
158 * @param led The mode to set to.
159 */
161
162 /**
163 * Returns the name of the camera.
164 * This will return the same value that was given to the constructor as
165 * cameraName.
166 * @return The name of the camera.
167 */
168 const std::string_view GetCameraName() const;
169
170 /**
171 * Returns whether the camera is connected and actively returning new data.
172 * Connection status is debounced.
173 *
174 * @return True if the camera is actively sending frame data, false otherwise.
175 */
177
178 using CameraMatrix = Eigen::Matrix<double, 3, 3>;
179 using DistortionMatrix = Eigen::Matrix<double, 8, 1>;
180
181 /**
182 * Get the camera calibration matrix, in standard OpenCV form
183 *
184 * @return std::optional<cv::Mat>
185 */
186 std::optional<CameraMatrix> GetCameraMatrix();
187
188 /**
189 * Returns the camera calibration's distortion coefficients, in OPENCV8 form.
190 * Higher-order terms are set to 0
191 *
192 * @return The distortion coefficients in a 8x1 matrix, if they are published
193 * by the camera. Empty otherwise.
194 */
195 std::optional<DistortionMatrix> GetDistCoeffs();
196
197 /**
198 * Sets whether or not coprocessor version checks will occur. Setting this to
199 * true will silence all console warnings about coproccessor connection, so be
200 * careful when enabling this and ensure all your coprocessors are
201 * communicating to the robot properly and everything has matching versions.
202 *
203 * @param enabled Whether or not to enable coprocessor version checks
204 */
205 static void SetVersionCheckEnabled(bool enabled);
206
207 std::shared_ptr<nt::NetworkTable> GetCameraTable() const { return rootTable; }
208
209 // For use in tests
210 bool test = false;
211 std::vector<PhotonPipelineResult> testResult;
212
213 protected:
214 std::shared_ptr<nt::NetworkTable> mainTable;
215 std::shared_ptr<nt::NetworkTable> rootTable;
216 nt::RawSubscriber rawBytesEntry;
217 nt::IntegerPublisher inputSaveImgEntry;
218 nt::IntegerSubscriber inputSaveImgSubscriber;
219 nt::IntegerPublisher outputSaveImgEntry;
220 nt::IntegerSubscriber outputSaveImgSubscriber;
221 nt::IntegerPublisher pipelineIndexPub;
222 nt::IntegerSubscriber pipelineIndexSub;
223 nt::IntegerPublisher ledModePub;
224 nt::IntegerSubscriber ledModeSub;
225 nt::StringSubscriber versionEntry;
226
227 nt::DoubleArraySubscriber cameraIntrinsicsSubscriber;
228 nt::DoubleArraySubscriber cameraDistortionSubscriber;
229
230 nt::BooleanSubscriber driverModeSubscriber;
231 nt::BooleanPublisher driverModePublisher;
232 nt::IntegerSubscriber fpsLimitSubscriber;
233 nt::IntegerPublisher fpsLimitPublisher;
234
235 nt::IntegerSubscriber ledModeSubscriber;
236
237 nt::IntegerSubscriber heartbeatSubscriber;
238
239 nt::MultiSubscriber topicNameSubscriber;
240
241 std::string path;
242 std::string cameraName;
243
244 frc::Alert disconnectAlert;
245 frc::Alert timesyncAlert;
246
247 private:
248 units::second_t lastVersionCheckTime = 0_s;
249 static bool VERSION_CHECK_ENABLED;
250 inline static int InstanceCount = 1;
251
252 units::second_t prevTimeSyncWarnTime = 0_s;
253
254 int prevHeartbeatValue = -1;
255 units::second_t prevHeartbeatChangeTime = 0_s;
256
257 void VerifyVersion();
258
259 void UpdateDisconnectAlert();
260 void CheckTimeSyncOrWarn(photon::PhotonPipelineResult& result);
261
262 std::vector<std::string> tablesThatLookLikePhotonCameras();
263};
264
265} // namespace photon
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:56
nt::IntegerSubscriber pipelineIndexSub
Definition PhotonCamera.h:222
frc::Alert disconnectAlert
Definition PhotonCamera.h:244
std::shared_ptr< nt::NetworkTable > rootTable
Definition PhotonCamera.h:215
std::optional< CameraMatrix > GetCameraMatrix()
Get the camera calibration matrix, in standard OpenCV form.
int GetFPSLimit() const
void SetFPSLimit(int fpsLimit)
nt::IntegerPublisher outputSaveImgEntry
Definition PhotonCamera.h:219
PhotonPipelineResult GetLatestResult()
bool test
Definition PhotonCamera.h:210
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:179
nt::BooleanPublisher driverModePublisher
Definition PhotonCamera.h:231
void SetDriverMode(bool driverMode)
Toggles driver mode.
PhotonCamera(nt::NetworkTableInstance instance, const std::string_view cameraName)
Constructs a PhotonCamera from a root table.
nt::IntegerSubscriber ledModeSubscriber
Definition PhotonCamera.h:235
nt::DoubleArraySubscriber cameraIntrinsicsSubscriber
Definition PhotonCamera.h:227
void TakeInputSnapshot(void)
Request the camera to save a new image file from the input camera stream with overlays.
nt::DoubleArraySubscriber cameraDistortionSubscriber
Definition PhotonCamera.h:228
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.
std::shared_ptr< nt::NetworkTable > mainTable
Definition PhotonCamera.h:214
bool GetDriverMode() const
Returns whether the camera is in driver mode.
void SetPipelineIndex(int index)
Allows the user to select the active pipeline index.
nt::IntegerPublisher ledModePub
Definition PhotonCamera.h:223
LEDMode GetLEDMode() const
Returns the current LED mode.
nt::StringSubscriber versionEntry
Definition PhotonCamera.h:225
nt::IntegerSubscriber ledModeSub
Definition PhotonCamera.h:224
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:242
std::vector< PhotonPipelineResult > testResult
Definition PhotonCamera.h:211
nt::IntegerSubscriber inputSaveImgSubscriber
Definition PhotonCamera.h:218
void TakeOutputSnapshot(void)
Request the camera to save a new image file from the output stream with overlays.
nt::IntegerPublisher inputSaveImgEntry
Definition PhotonCamera.h:217
nt::IntegerSubscriber fpsLimitSubscriber
Definition PhotonCamera.h:232
std::shared_ptr< nt::NetworkTable > GetCameraTable() const
Definition PhotonCamera.h:207
nt::MultiSubscriber topicNameSubscriber
Definition PhotonCamera.h:239
nt::IntegerSubscriber heartbeatSubscriber
Definition PhotonCamera.h:237
nt::RawSubscriber rawBytesEntry
Definition PhotonCamera.h:216
std::string path
Definition PhotonCamera.h:241
nt::BooleanSubscriber driverModeSubscriber
Definition PhotonCamera.h:230
std::optional< DistortionMatrix > GetDistCoeffs()
Returns the camera calibration's distortion coefficients, in OPENCV8 form.
frc::Alert timesyncAlert
Definition PhotonCamera.h:245
PhotonCamera(PhotonCamera &&)=default
nt::IntegerSubscriber outputSaveImgSubscriber
Definition PhotonCamera.h:220
nt::IntegerPublisher fpsLimitPublisher
Definition PhotonCamera.h:233
int GetPipelineIndex() const
Returns the active pipeline index.
nt::IntegerPublisher pipelineIndexPub
Definition PhotonCamera.h:221
Eigen::Matrix< double, 3, 3 > CameraMatrix
Definition PhotonCamera.h:178
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:37
Definition PhotonCamera.h:45
Definition TargetModel.h:27
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