PhotonVision C++ v2026.0.1-beta
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 <algorithm>
28#include <limits>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <cameraserver/CameraServer.h>
34#include <frc/Timer.h>
35#include <frc/apriltag/AprilTagFieldLayout.h>
36#include <frc/apriltag/AprilTagFields.h>
37#include <photon/PhotonCamera.h>
45#include <units/math.h>
46#include <wpi/timestamp.h>
47
48namespace photon {
50 public:
51 explicit PhotonCameraSim(PhotonCamera* camera);
53 const frc::AprilTagFieldLayout& tagLayout =
54 frc::AprilTagFieldLayout::LoadField(
55 frc::AprilTagField::kDefaultField));
57 double minTargetAreaPercent, units::meter_t maxSightRange);
58
59 inline PhotonCamera* GetCamera() { return cam; }
60 inline double GetMinTargetAreaPercent() { return minTargetAreaPercent; }
61 inline double GetMinTargetAreaPixels() {
62 return minTargetAreaPercent / 100.0 * prop.GetResArea();
63 }
64 inline units::meter_t GetMaxSightRange() { return maxSightRange; }
65 inline const cs::CvSource& GetVideoSimRaw() { return videoSimRaw; }
66 inline const cv::Mat& GetVideoSimFrameRaw() { return videoSimFrameRaw; }
67
68 bool CanSeeTargetPose(const frc::Pose3d& camPose,
69 const VisionTargetSim& target);
70 bool CanSeeCorner(const std::vector<cv::Point2f>& points);
71 std::optional<uint64_t> ConsumeNextEntryTime();
72
73 inline void SetMinTargetAreaPercent(double areaPercent) {
74 minTargetAreaPercent = areaPercent;
75 }
76 inline void SetMinTargetAreaPixels(double areaPx) {
77 minTargetAreaPercent = areaPx / prop.GetResArea() * 100;
78 }
79 inline void SetMaxSightRange(units::meter_t range) { maxSightRange = range; }
80 inline void EnableRawStream(bool enabled) { videoSimRawEnabled = enabled; }
81 inline void EnableDrawWireframe(bool enabled) {
82 videoSimWireframeEnabled = enabled;
83 }
84 inline void SetWireframeResolution(double resolution) {
85 videoSimWireframeResolution = resolution;
86 }
87 inline void EnabledProcessedStream(double enabled) {
88 videoSimProcEnabled = enabled;
89 }
90 PhotonPipelineResult Process(units::second_t latency,
91 const frc::Pose3d& cameraPose,
92 std::vector<VisionTargetSim> targets);
93
96 uint64_t ReceiveTimestamp);
97
99
100 private:
101 PhotonCamera* cam;
102
103 NTTopicSet ts{};
104 int64_t heartbeatCounter{0};
105
106 uint64_t nextNTEntryTime{wpi::Now()};
107
108 units::meter_t maxSightRange{std::numeric_limits<double>::max()};
109 static constexpr double kDefaultMinAreaPx{100};
110 double minTargetAreaPercent;
111
112 frc::AprilTagFieldLayout tagLayout;
113
114 cs::CvSource videoSimRaw;
115 cv::Mat videoSimFrameRaw{};
116 bool videoSimRawEnabled{true};
117 bool videoSimWireframeEnabled{false};
118 double videoSimWireframeResolution{0.1};
119 cs::CvSource videoSimProcessed;
120 cv::Mat videoSimFrameProcessed{};
121 bool videoSimProcEnabled{true};
122};
123} // namespace photon
Definition NTTopicSet.h:37
Represents a camera that is connected to PhotonVision.
Definition PhotonCamera.h:56
Definition PhotonCameraSim.h:49
std::optional< uint64_t > ConsumeNextEntryTime()
SimCameraProperties prop
Definition PhotonCameraSim.h:98
void SetWireframeResolution(double resolution)
Definition PhotonCameraSim.h:84
const cs::CvSource & GetVideoSimRaw()
Definition PhotonCameraSim.h:65
const cv::Mat & GetVideoSimFrameRaw()
Definition PhotonCameraSim.h:66
PhotonCamera * GetCamera()
Definition PhotonCameraSim.h:59
PhotonCameraSim(PhotonCamera *camera, const SimCameraProperties &props, const frc::AprilTagFieldLayout &tagLayout=frc::AprilTagFieldLayout::LoadField(frc::AprilTagField::kDefaultField))
void EnableDrawWireframe(bool enabled)
Definition PhotonCameraSim.h:81
double GetMinTargetAreaPixels()
Definition PhotonCameraSim.h:61
PhotonCameraSim(PhotonCamera *camera, const SimCameraProperties &props, double minTargetAreaPercent, units::meter_t maxSightRange)
void EnabledProcessedStream(double enabled)
Definition PhotonCameraSim.h:87
units::meter_t GetMaxSightRange()
Definition PhotonCameraSim.h:64
PhotonPipelineResult Process(units::second_t latency, const frc::Pose3d &cameraPose, std::vector< VisionTargetSim > targets)
void SubmitProcessedFrame(const PhotonPipelineResult &result)
bool CanSeeCorner(const std::vector< cv::Point2f > &points)
void SetMinTargetAreaPixels(double areaPx)
Definition PhotonCameraSim.h:76
void SubmitProcessedFrame(const PhotonPipelineResult &result, uint64_t ReceiveTimestamp)
void EnableRawStream(bool enabled)
Definition PhotonCameraSim.h:80
double GetMinTargetAreaPercent()
Definition PhotonCameraSim.h:60
void SetMaxSightRange(units::meter_t range)
Definition PhotonCameraSim.h:79
bool CanSeeTargetPose(const frc::Pose3d &camPose, const VisionTargetSim &target)
PhotonCameraSim(PhotonCamera *camera)
void SetMinTargetAreaPercent(double areaPercent)
Definition PhotonCameraSim.h:73
Represents a pipeline result from a PhotonCamera.
Definition PhotonPipelineResult.h:37
Definition SimCameraProperties.h:43
int GetResArea() const
Definition SimCameraProperties.h:79
Definition VisionTargetSim.h:34
Definition TargetModel.h:27