PhotonVision C++ dev-v2025.3.1-2-gf92cf62a
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 * Request the camera to save a new image file from the input
108 * camera stream with overlays.
109 * Images take up space in the filesystem of the PhotonCamera.
110 * Calling it frequently will fill up disk space and eventually
111 * cause the system to stop working.
112 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
113 * frequently to prevent issues.
114 */
116
117 /**
118 * Request the camera to save a new image file from the output
119 * stream with overlays.
120 * Images take up space in the filesystem of the PhotonCamera.
121 * Calling it frequently will fill up disk space and eventually
122 * cause the system to stop working.
123 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
124 * frequently to prevent issues.
125 */
127
128 /**
129 * Allows the user to select the active pipeline index.
130 * @param index The active pipeline index.
131 */
132 void SetPipelineIndex(int index);
133
134 /**
135 * Returns the active pipeline index.
136 * @return The active pipeline index.
137 */
138 int GetPipelineIndex() const;
139
140 /**
141 * Returns the current LED mode.
142 * @return The current LED mode.
143 */
145
146 /**
147 * Sets the LED mode.
148 * @param led The mode to set to.
149 */
151
152 /**
153 * Returns the name of the camera.
154 * This will return the same value that was given to the constructor as
155 * cameraName.
156 * @return The name of the camera.
157 */
158 const std::string_view GetCameraName() const;
159
160 /**
161 * Returns whether the camera is connected and actively returning new data.
162 * Connection status is debounced.
163 *
164 * @return True if the camera is actively sending frame data, false otherwise.
165 */
167
168 using CameraMatrix = Eigen::Matrix<double, 3, 3>;
169 using DistortionMatrix = Eigen::Matrix<double, 8, 1>;
170
171 /**
172 * @brief Get the camera calibration matrix, in standard OpenCV form
173 *
174 * @return std::optional<cv::Mat>
175 */
176 std::optional<CameraMatrix> GetCameraMatrix();
177
178 /**
179 * @brief Get the camera calibration distortion coefficients, in OPENCV8 form.
180 * Higher order terms are set to zero.
181 *
182 * @return std::optional<cv::Mat>
183 */
184 std::optional<DistortionMatrix> GetDistCoeffs();
185
186 static void SetVersionCheckEnabled(bool enabled);
187
188 std::shared_ptr<nt::NetworkTable> GetCameraTable() const { return rootTable; }
189
190 // For use in tests
191 bool test = false;
192 std::vector<PhotonPipelineResult> testResult;
193
194 protected:
195 std::shared_ptr<nt::NetworkTable> mainTable;
196 std::shared_ptr<nt::NetworkTable> rootTable;
197 nt::RawSubscriber rawBytesEntry;
198 nt::IntegerPublisher inputSaveImgEntry;
199 nt::IntegerSubscriber inputSaveImgSubscriber;
200 nt::IntegerPublisher outputSaveImgEntry;
201 nt::IntegerSubscriber outputSaveImgSubscriber;
202 nt::IntegerPublisher pipelineIndexPub;
203 nt::IntegerSubscriber pipelineIndexSub;
204 nt::IntegerPublisher ledModePub;
205 nt::IntegerSubscriber ledModeSub;
206 nt::StringSubscriber versionEntry;
207
208 nt::DoubleArraySubscriber cameraIntrinsicsSubscriber;
209 nt::DoubleArraySubscriber cameraDistortionSubscriber;
210
211 nt::BooleanSubscriber driverModeSubscriber;
212 nt::BooleanPublisher driverModePublisher;
213 nt::IntegerSubscriber ledModeSubscriber;
214
215 nt::IntegerSubscriber heartbeatSubscriber;
216
217 nt::MultiSubscriber topicNameSubscriber;
218
219 std::string path;
220 std::string cameraName;
221
222 frc::Alert disconnectAlert;
223 frc::Alert timesyncAlert;
224
225 private:
226 units::second_t lastVersionCheckTime = 0_s;
227 static bool VERSION_CHECK_ENABLED;
228 inline static int InstanceCount = 0;
229
230 units::second_t prevTimeSyncWarnTime = 0_s;
231
232 int prevHeartbeatValue = -1;
233 units::second_t prevHeartbeatChangeTime = 0_s;
234
235 void VerifyVersion();
236
237 void UpdateDisconnectAlert();
238 void CheckTimeSyncOrWarn(photon::PhotonPipelineResult& result);
239
240 std::vector<std::string> tablesThatLookLikePhotonCameras();
241};
242
243} // namespace photon
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:56
nt::IntegerSubscriber pipelineIndexSub
Definition PhotonCamera.h:203
frc::Alert disconnectAlert
Definition PhotonCamera.h:222
std::shared_ptr< nt::NetworkTable > rootTable
Definition PhotonCamera.h:196
std::optional< CameraMatrix > GetCameraMatrix()
Get the camera calibration matrix, in standard OpenCV form.
nt::IntegerPublisher outputSaveImgEntry
Definition PhotonCamera.h:200
PhotonPipelineResult GetLatestResult()
bool test
Definition PhotonCamera.h:191
const std::string_view GetCameraName() const
Returns the name of the camera.
static void SetVersionCheckEnabled(bool enabled)
Eigen::Matrix< double, 8, 1 > DistortionMatrix
Definition PhotonCamera.h:169
nt::BooleanPublisher driverModePublisher
Definition PhotonCamera.h:212
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:213
nt::DoubleArraySubscriber cameraIntrinsicsSubscriber
Definition PhotonCamera.h:208
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:209
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:195
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:204
LEDMode GetLEDMode() const
Returns the current LED mode.
nt::StringSubscriber versionEntry
Definition PhotonCamera.h:206
nt::IntegerSubscriber ledModeSub
Definition PhotonCamera.h:205
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:220
std::vector< PhotonPipelineResult > testResult
Definition PhotonCamera.h:192
nt::IntegerSubscriber inputSaveImgSubscriber
Definition PhotonCamera.h:199
void TakeOutputSnapshot(void)
Request the camera to save a new image file from the output stream with overlays.
nt::IntegerPublisher inputSaveImgEntry
Definition PhotonCamera.h:198
std::shared_ptr< nt::NetworkTable > GetCameraTable() const
Definition PhotonCamera.h:188
nt::MultiSubscriber topicNameSubscriber
Definition PhotonCamera.h:217
nt::IntegerSubscriber heartbeatSubscriber
Definition PhotonCamera.h:215
nt::RawSubscriber rawBytesEntry
Definition PhotonCamera.h:197
std::string path
Definition PhotonCamera.h:219
nt::BooleanSubscriber driverModeSubscriber
Definition PhotonCamera.h:211
std::optional< DistortionMatrix > GetDistCoeffs()
Get the camera calibration distortion coefficients, in OPENCV8 form.
frc::Alert timesyncAlert
Definition PhotonCamera.h:223
PhotonCamera(PhotonCamera &&)=default
nt::IntegerSubscriber outputSaveImgSubscriber
Definition PhotonCamera.h:201
int GetPipelineIndex() const
Returns the active pipeline index.
nt::IntegerPublisher pipelineIndexPub
Definition PhotonCamera.h:202
Eigen::Matrix< double, 3, 3 > CameraMatrix
Definition PhotonCamera.h:168
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:37
Definition PhotonCamera.h:45
Definition VisionEstimation.h:32
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