From d6d76888bb279149bff30fb288b2264f5aae5f17 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 11 May 2022 15:35:51 -0600 Subject: [PATCH 1/5] Rate limit version verification --- .../src/main/java/org/photonvision/PhotonCamera.java | 7 +++++++ .../src/main/native/cpp/photonlib/PhotonCamera.cpp | 11 +++++++++-- .../src/main/native/include/photonlib/PhotonCamera.h | 9 ++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/photon-lib/src/main/java/org/photonvision/PhotonCamera.java b/photon-lib/src/main/java/org/photonvision/PhotonCamera.java index 813ee806d6..265d9b50e7 100644 --- a/photon-lib/src/main/java/org/photonvision/PhotonCamera.java +++ b/photon-lib/src/main/java/org/photonvision/PhotonCamera.java @@ -28,6 +28,8 @@ import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.DriverStation; +import edu.wpi.first.wpilibj.Timer; + import org.photonvision.common.dataflow.structures.Packet; import org.photonvision.common.hardware.VisionLEDMode; import org.photonvision.targeting.PhotonPipelineResult; @@ -46,6 +48,8 @@ public class PhotonCamera { private final String path; private static boolean VERSION_CHECK_ENABLED = true; + private static long VERSION_CHECK_INTERVAL = 1; + private double lastVersionCheckTime = 0; public static void setVersionCheckEnabled(boolean enabled) { VERSION_CHECK_ENABLED = enabled; @@ -207,6 +211,9 @@ public boolean hasTargets() { private void verifyVersion() { if (!VERSION_CHECK_ENABLED) return; + if((Timer.getFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; + lastVersionCheckTime = Timer.getFPGATimestamp(); + String versionString = versionEntry.getString(""); if (versionString.equals("")) { DriverStation.reportError( diff --git a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp index 5cbf562ae9..8066730a87 100644 --- a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp +++ b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp @@ -25,11 +25,15 @@ #include "photonlib/PhotonCamera.h" #include +#include #include "PhotonVersion.h" #include "photonlib/Packet.h" namespace photonlib { + +constexpr const units::second_t VERSION_CHECK_INTERVAL = 5_s; + PhotonCamera::PhotonCamera(std::shared_ptr instance, const std::string& cameraName) : mainTable(instance->GetTable("photonvision")), @@ -48,7 +52,7 @@ PhotonCamera::PhotonCamera(const std::string& cameraName) nt::NetworkTableInstance::GetDefault()), cameraName) {} -PhotonPipelineResult PhotonCamera::GetLatestResult() const { +PhotonPipelineResult PhotonCamera::GetLatestResult() { // Prints warning if not connected VerifyVersion(); @@ -99,9 +103,12 @@ void PhotonCamera::SetLEDMode(LEDMode mode) { ledModeEntry.SetDouble(static_cast(static_cast(mode))); } -void PhotonCamera::VerifyVersion() const { +void PhotonCamera::VerifyVersion() { if (!PhotonCamera::VERSION_CHECK_ENABLED) return; + if((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; + this->lastVersionCheckTime = frc::Timer::GetFPGATimestamp(); + const std::string& versionString = versionEntry.GetString(""); if (versionString.empty()) { std::string path_ = path; diff --git a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h index 0d5cd77a62..22a4fc9a57 100644 --- a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h +++ b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h @@ -27,6 +27,8 @@ #include #include +#include + #include #include #include @@ -66,7 +68,7 @@ class PhotonCamera { * Returns the latest pipeline result. * @return The latest pipeline result. */ - PhotonPipelineResult GetLatestResult() const; + PhotonPipelineResult GetLatestResult(); /** * Toggles driver mode. @@ -136,7 +138,7 @@ class PhotonCamera { */ WPI_DEPRECATED( "This method should be replaced with PhotonPipelineResult::HasTargets()") - bool HasTargets() const { return GetLatestResult().HasTargets(); } + bool HasTargets() { return GetLatestResult().HasTargets(); } inline static void SetVersionCheckEnabled(bool enabled) { PhotonCamera::VERSION_CHECK_ENABLED = enabled; @@ -158,9 +160,10 @@ class PhotonCamera { mutable Packet packet; private: + units::second_t lastVersionCheckTime = 0_s; inline static bool VERSION_CHECK_ENABLED = true; - void VerifyVersion() const; + void VerifyVersion(); }; } // namespace photonlib From ae6a74273174c3358fe85581dc9a59030b7db5ba Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 11 May 2022 15:39:47 -0600 Subject: [PATCH 2/5] Update PhotonCamera.h --- .../src/main/native/include/photonlib/PhotonCamera.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h index 22a4fc9a57..82e1737822 100644 --- a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h +++ b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h @@ -10,8 +10,8 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -27,11 +27,10 @@ #include #include -#include - #include #include #include +#include #include #include "photonlib/PhotonPipelineResult.h" From 264ff2b1b17d59e5577859bdc378c24a571a31b8 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 11 May 2022 15:42:35 -0600 Subject: [PATCH 3/5] Fix wpiformat --- photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp | 4 +++- photon-lib/src/main/native/include/photonlib/PhotonCamera.h | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp index 8066730a87..f13b367742 100644 --- a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp +++ b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp @@ -106,7 +106,9 @@ void PhotonCamera::SetLEDMode(LEDMode mode) { void PhotonCamera::VerifyVersion() { if (!PhotonCamera::VERSION_CHECK_ENABLED) return; - if((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; + if((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) < + VERSION_CHECK_INTERVAL) + return; this->lastVersionCheckTime = frc::Timer::GetFPGATimestamp(); const std::string& versionString = versionEntry.GetString(""); diff --git a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h index 82e1737822..123aee95aa 100644 --- a/photon-lib/src/main/native/include/photonlib/PhotonCamera.h +++ b/photon-lib/src/main/native/include/photonlib/PhotonCamera.h @@ -10,8 +10,8 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, From 9d08f5ff69eb0282e4adf7e6fb70c5dcc2d0784d Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 11 May 2022 15:55:47 -0600 Subject: [PATCH 4/5] Update PhotonCamera.cpp --- photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp index f13b367742..1af6f0ed96 100644 --- a/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp +++ b/photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp @@ -106,7 +106,7 @@ void PhotonCamera::SetLEDMode(LEDMode mode) { void PhotonCamera::VerifyVersion() { if (!PhotonCamera::VERSION_CHECK_ENABLED) return; - if((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) < + if ((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; this->lastVersionCheckTime = frc::Timer::GetFPGATimestamp(); From 471e7ad9b7892e3f19c78a76a651954951376cc1 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 12 May 2022 20:51:25 -0500 Subject: [PATCH 5/5] Run spotless --- photon-lib/src/main/java/org/photonvision/PhotonCamera.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/photon-lib/src/main/java/org/photonvision/PhotonCamera.java b/photon-lib/src/main/java/org/photonvision/PhotonCamera.java index 265d9b50e7..ad83ceb32e 100644 --- a/photon-lib/src/main/java/org/photonvision/PhotonCamera.java +++ b/photon-lib/src/main/java/org/photonvision/PhotonCamera.java @@ -29,7 +29,6 @@ import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.DriverStation; import edu.wpi.first.wpilibj.Timer; - import org.photonvision.common.dataflow.structures.Packet; import org.photonvision.common.hardware.VisionLEDMode; import org.photonvision.targeting.PhotonPipelineResult; @@ -211,7 +210,7 @@ public boolean hasTargets() { private void verifyVersion() { if (!VERSION_CHECK_ENABLED) return; - if((Timer.getFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; + if ((Timer.getFPGATimestamp() - lastVersionCheckTime) < VERSION_CHECK_INTERVAL) return; lastVersionCheckTime = Timer.getFPGATimestamp(); String versionString = versionEntry.getString("");