Skip to content

Commit cc740c9

Browse files
Added constructor overload to PhotonCameraSim for AprilTagFieldLayout (#1692)
1 parent e673304 commit cc740c9

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

photon-lib/py/photonlibpy/simulation/photonCameraSim.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def __init__(
3636
self,
3737
camera: PhotonCamera,
3838
props: SimCameraProperties = SimCameraProperties.PERFECT_90DEG(),
39+
tagLayout: AprilTagFieldLayout = AprilTagFieldLayout.loadField(
40+
AprilTagField.kDefaultField
41+
),
3942
minTargetAreaPercent: float | None = None,
4043
maxSightRange: meters | None = None,
4144
):
@@ -64,7 +67,7 @@ def __init__(
6467
self.videoSimProcEnabled: bool = False
6568
self.heartbeatCounter: int = 0
6669
self.nextNtEntryTime = wpilib.Timer.getFPGATimestamp()
67-
self.tagLayout = AprilTagFieldLayout.loadField(AprilTagField.kDefaultField)
70+
self.tagLayout = tagLayout
6871

6972
self.cam = camera
7073
self.prop = props

photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public class PhotonCameraSim implements AutoCloseable {
8181
private double minTargetAreaPercent;
8282
private PhotonTargetSortMode sortMode = PhotonTargetSortMode.Largest;
8383

84-
private final AprilTagFieldLayout tagLayout =
85-
AprilTagFieldLayout.loadField(AprilTagFields.kDefaultField);
84+
private final AprilTagFieldLayout tagLayout;
8685

8786
// video stream simulation
8887
private final CvSource videoSimRaw;
@@ -130,8 +129,24 @@ public PhotonCameraSim(PhotonCamera camera) {
130129
* @param prop Properties of this camera such as FOV and FPS
131130
*/
132131
public PhotonCameraSim(PhotonCamera camera, SimCameraProperties prop) {
132+
this(camera, prop, AprilTagFieldLayout.loadField(AprilTagFields.kDefaultField));
133+
}
134+
135+
/**
136+
* Constructs a handle for simulating {@link PhotonCamera} values. Processing simulated targets
137+
* through this class will change the associated PhotonCamera's results.
138+
*
139+
* <p>By default, the minimum target area is 100 pixels and there is no maximum sight range.
140+
*
141+
* @param camera The camera to be simulated
142+
* @param prop Properties of this camera such as FOV and FPS
143+
* @param tagLayout The {@link AprilTagFieldLayout} used to solve for tag positions.
144+
*/
145+
public PhotonCameraSim(
146+
PhotonCamera camera, SimCameraProperties prop, AprilTagFieldLayout tagLayout) {
133147
this.cam = camera;
134148
this.prop = prop;
149+
this.tagLayout = tagLayout;
135150
setMinTargetAreaPixels(kDefaultMinAreaPx);
136151

137152
videoSimRaw =
@@ -163,6 +178,30 @@ public PhotonCameraSim(
163178
SimCameraProperties prop,
164179
double minTargetAreaPercent,
165180
double maxSightRangeMeters) {
181+
this(camera, prop, AprilTagFieldLayout.loadField(AprilTagFields.kDefaultField));
182+
this.minTargetAreaPercent = minTargetAreaPercent;
183+
this.maxSightRangeMeters = maxSightRangeMeters;
184+
}
185+
186+
/**
187+
* Constructs a handle for simulating {@link PhotonCamera} values. Processing simulated targets
188+
* through this class will change the associated PhotonCamera's results.
189+
*
190+
* @param camera The camera to be simulated
191+
* @param prop Properties of this camera such as FOV and FPS
192+
* @param minTargetAreaPercent The minimum percentage(0 - 100) a detected target must take up of
193+
* the camera's image to be processed. Match this with your contour filtering settings in the
194+
* PhotonVision GUI.
195+
* @param maxSightRangeMeters Maximum distance at which the target is illuminated to your camera.
196+
* Note that minimum target area of the image is separate from this.
197+
* @param tagLayout AprilTag field layout to use for multi-tag estimation
198+
*/
199+
public PhotonCameraSim(
200+
PhotonCamera camera,
201+
SimCameraProperties prop,
202+
double minTargetAreaPercent,
203+
double maxSightRangeMeters,
204+
AprilTagFieldLayout tagLayout) {
166205
this(camera, prop);
167206
this.minTargetAreaPercent = minTargetAreaPercent;
168207
this.maxSightRangeMeters = maxSightRangeMeters;

photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929
#include <utility>
3030
#include <vector>
3131

32+
#include <frc/apriltag/AprilTagFieldLayout.h>
33+
#include <frc/apriltag/AprilTagFields.h>
34+
3235
namespace photon {
3336
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera)
34-
: PhotonCameraSim(camera, photon::SimCameraProperties::PERFECT_90DEG()) {}
37+
: PhotonCameraSim(camera, photon::SimCameraProperties::PERFECT_90DEG(),
38+
frc::AprilTagFieldLayout::LoadField(
39+
frc::AprilTagField::kDefaultField)) {}
40+
3541
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera,
36-
const SimCameraProperties& props)
37-
: prop(props), cam(camera) {
42+
const SimCameraProperties& props,
43+
const frc::AprilTagFieldLayout& tagLayout)
44+
: prop{props}, cam{camera}, tagLayout{tagLayout} {
3845
SetMinTargetAreaPixels(kDefaultMinAreaPx);
3946
videoSimRaw =
4047
frc::CameraServer::PutVideo(std::string{camera->GetCameraName()} + "-raw",
@@ -46,6 +53,7 @@ PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera,
4653
ts.subTable = cam->GetCameraTable();
4754
ts.UpdateEntries();
4855
}
56+
4957
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera,
5058
const SimCameraProperties& props,
5159
double minTargetAreaPercent,

photon-lib/src/main/native/include/photon/simulation/PhotonCameraSim.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ namespace photon {
5050
class PhotonCameraSim {
5151
public:
5252
explicit PhotonCameraSim(PhotonCamera* camera);
53-
PhotonCameraSim(PhotonCamera* camera, const SimCameraProperties& props);
53+
PhotonCameraSim(PhotonCamera* camera, const SimCameraProperties& props,
54+
const frc::AprilTagFieldLayout& tagLayout =
55+
frc::AprilTagFieldLayout::LoadField(
56+
frc::AprilTagField::kDefaultField));
5457
PhotonCameraSim(PhotonCamera* camera, const SimCameraProperties& props,
5558
double minTargetAreaPercent, units::meter_t maxSightRange);
5659

@@ -107,8 +110,7 @@ class PhotonCameraSim {
107110
static constexpr double kDefaultMinAreaPx{100};
108111
double minTargetAreaPercent;
109112

110-
frc::AprilTagFieldLayout tagLayout{
111-
frc::AprilTagFieldLayout::LoadField(frc::AprilTagField::kDefaultField)};
113+
frc::AprilTagFieldLayout tagLayout;
112114

113115
cs::CvSource videoSimRaw;
114116
cv::Mat videoSimFrameRaw{};

0 commit comments

Comments
 (0)