PhotonVision C++ dev-v2025.0.0-beta-8-2-gbd1c5c03
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 <networktables/BooleanTopic.h>
32#include <networktables/DoubleArrayTopic.h>
33#include <networktables/DoubleTopic.h>
34#include <networktables/IntegerTopic.h>
35#include <networktables/MultiSubscriber.h>
36#include <networktables/NetworkTable.h>
37#include <networktables/NetworkTableInstance.h>
38#include <networktables/RawTopic.h>
39#include <networktables/StringTopic.h>
40#include <units/time.h>
41
43
44namespace cv {
45class Mat;
46} // namespace cv
47
48namespace photon {
49
50enum LEDMode : int { kDefault = -1, kOff = 0, kOn = 1, kBlink = 2 };
51
52/**
53 * Represents a camera that is connected to PhotonVision.ß
54 */
56 public:
57 /**
58 * Constructs a PhotonCamera from a root table.
59 *
60 * @param instance The NetworkTableInstance to pull data from. This can be a
61 * custom instance in simulation, but should *usually* be the default
62 * NTInstance from {@link NetworkTableInstance::getDefault}
63 * @param cameraName The name of the camera, as seen in the UI.
64 * over.
65 */
66 explicit PhotonCamera(nt::NetworkTableInstance instance,
67 const std::string_view cameraName);
68
69 /**
70 * Constructs a PhotonCamera from the name of the camera.
71 * @param cameraName The nickname of the camera (found in the PhotonVision
72 * UI).
73 */
74 explicit PhotonCamera(const std::string_view cameraName);
75
77
78 ~PhotonCamera() = default;
79
80 /**
81 * The list of pipeline results sent by PhotonVision since the last call to
82 * GetAllUnreadResults(). Calling this function clears the internal FIFO
83 * queue, and multiple calls to GetAllUnreadResults() will return different
84 * (potentially empty) result arrays. Be careful to call this exactly ONCE per
85 * loop of your robot code! FIFO depth is limited to 20 changes, so make sure
86 * to call this frequently enough to avoid old results being discarded, too!
87 */
88 std::vector<PhotonPipelineResult> GetAllUnreadResults();
89
90 [[deprecated("Replace with GetAllUnreadResults")]] PhotonPipelineResult
92
93 /**
94 * Toggles driver mode.
95 * @param driverMode Whether to set driver mode.
96 */
97 void SetDriverMode(bool driverMode);
98
99 /**
100 * Returns whether the camera is in driver mode.
101 * @return Whether the camera is in driver mode.
102 */
103 bool GetDriverMode() const;
104
105 /**
106 * Request the camera to save a new image file from the input
107 * camera stream with overlays.
108 * Images take up space in the filesystem of the PhotonCamera.
109 * Calling it frequently will fill up disk space and eventually
110 * cause the system to stop working.
111 * Clear out images in /opt/photonvision/photonvision_config/imgSaves
112 * frequently to prevent issues.
113 */
115
116 /**
117 * Request the camera to save a new image file from the output
118 * 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 * Allows the user to select the active pipeline index.
129 * @param index The active pipeline index.
130 */
131 void SetPipelineIndex(int index);
132
133 /**
134 * Returns the active pipeline index.
135 * @return The active pipeline index.
136 */
137 int GetPipelineIndex() const;
138
139 /**
140 * Returns the current LED mode.
141 * @return The current LED mode.
142 */
144
145 /**
146 * Sets the LED mode.
147 * @param led The mode to set to.
148 */
150
151 /**
152 * Returns the name of the camera.
153 * This will return the same value that was given to the constructor as
154 * cameraName.
155 * @return The name of the camera.
156 */
157 const std::string_view GetCameraName() const;
158
159 using CameraMatrix = Eigen::Matrix<double, 3, 3>;
160 using DistortionMatrix = Eigen::Matrix<double, 8, 1>;
161
162 /**
163 * @brief Get the camera calibration matrix, in standard OpenCV form
164 *
165 * @return std::optional<cv::Mat>
166 */
167 std::optional<CameraMatrix> GetCameraMatrix();
168
169 /**
170 * @brief Get the camera calibration distortion coefficients, in OPENCV8 form.
171 * Higher order terms are set to zero.
172 *
173 * @return std::optional<cv::Mat>
174 */
175 std::optional<DistortionMatrix> GetDistCoeffs();
176
177 static void SetVersionCheckEnabled(bool enabled);
178
179 std::shared_ptr<nt::NetworkTable> GetCameraTable() const { return rootTable; }
180
181 // For use in tests
182 bool test = false;
183 std::vector<PhotonPipelineResult> testResult;
184
185 protected:
186 std::shared_ptr<nt::NetworkTable> mainTable;
187 std::shared_ptr<nt::NetworkTable> rootTable;
188 nt::RawSubscriber rawBytesEntry;
189 nt::IntegerPublisher inputSaveImgEntry;
190 nt::IntegerSubscriber inputSaveImgSubscriber;
191 nt::IntegerPublisher outputSaveImgEntry;
192 nt::IntegerSubscriber outputSaveImgSubscriber;
193 nt::IntegerPublisher pipelineIndexPub;
194 nt::IntegerSubscriber pipelineIndexSub;
195 nt::IntegerPublisher ledModePub;
196 nt::IntegerSubscriber ledModeSub;
197 nt::StringSubscriber versionEntry;
198
199 nt::DoubleArraySubscriber cameraIntrinsicsSubscriber;
200 nt::DoubleArraySubscriber cameraDistortionSubscriber;
201
202 nt::BooleanSubscriber driverModeSubscriber;
203 nt::BooleanPublisher driverModePublisher;
204 nt::IntegerSubscriber ledModeSubscriber;
205
206 nt::MultiSubscriber topicNameSubscriber;
207
208 std::string path;
209 std::string cameraName;
210
211 private:
212 units::second_t lastVersionCheckTime = 0_s;
213 static bool VERSION_CHECK_ENABLED;
214 inline static int InstanceCount = 0;
215
216 void VerifyVersion();
217
218 std::vector<std::string> tablesThatLookLikePhotonCameras();
219};
220
221} // namespace photon
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:55
nt::IntegerSubscriber pipelineIndexSub
Definition PhotonCamera.h:194
std::shared_ptr< nt::NetworkTable > rootTable
Definition PhotonCamera.h:187
std::optional< CameraMatrix > GetCameraMatrix()
Get the camera calibration matrix, in standard OpenCV form.
nt::IntegerPublisher outputSaveImgEntry
Definition PhotonCamera.h:191
PhotonPipelineResult GetLatestResult()
bool test
Definition PhotonCamera.h:182
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:160
nt::BooleanPublisher driverModePublisher
Definition PhotonCamera.h:203
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:204
nt::DoubleArraySubscriber cameraIntrinsicsSubscriber
Definition PhotonCamera.h:199
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:200
PhotonCamera(const std::string_view cameraName)
Constructs a PhotonCamera from the name of the camera.
std::shared_ptr< nt::NetworkTable > mainTable
Definition PhotonCamera.h:186
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:195
LEDMode GetLEDMode() const
Returns the current LED mode.
nt::StringSubscriber versionEntry
Definition PhotonCamera.h:197
nt::IntegerSubscriber ledModeSub
Definition PhotonCamera.h:196
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:209
std::vector< PhotonPipelineResult > testResult
Definition PhotonCamera.h:183
nt::IntegerSubscriber inputSaveImgSubscriber
Definition PhotonCamera.h:190
void TakeOutputSnapshot(void)
Request the camera to save a new image file from the output stream with overlays.
nt::IntegerPublisher inputSaveImgEntry
Definition PhotonCamera.h:189
std::shared_ptr< nt::NetworkTable > GetCameraTable() const
Definition PhotonCamera.h:179
nt::MultiSubscriber topicNameSubscriber
Definition PhotonCamera.h:206
nt::RawSubscriber rawBytesEntry
Definition PhotonCamera.h:188
std::string path
Definition PhotonCamera.h:208
nt::BooleanSubscriber driverModeSubscriber
Definition PhotonCamera.h:202
std::optional< DistortionMatrix > GetDistCoeffs()
Get the camera calibration distortion coefficients, in OPENCV8 form.
PhotonCamera(PhotonCamera &&)=default
nt::IntegerSubscriber outputSaveImgSubscriber
Definition PhotonCamera.h:192
int GetPipelineIndex() const
Returns the active pipeline index.
nt::IntegerPublisher pipelineIndexPub
Definition PhotonCamera.h:193
Eigen::Matrix< double, 3, 3 > CameraMatrix
Definition PhotonCamera.h:159
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:37
Definition PhotonCamera.h:44
Definition VisionEstimation.h:32
LEDMode
Definition PhotonCamera.h:50
@ kOff
Definition PhotonCamera.h:50
@ kBlink
Definition PhotonCamera.h:50
@ kOn
Definition PhotonCamera.h:50
@ kDefault
Definition PhotonCamera.h:50