2021-01-16 20:41:47 -08:00
|
|
|
/*
|
2022-01-20 19:35:28 -08:00
|
|
|
* MIT License
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2023-04-18 18:49:40 -04:00
|
|
|
* Copyright (c) PhotonVision
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* 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,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2021-01-16 20:41:47 -08:00
|
|
|
*/
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/PhotonCamera.h"
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2025-02-13 12:23:11 +08:00
|
|
|
#include <stdexcept>
|
2024-08-02 11:57:34 -04:00
|
|
|
#include <string>
|
2023-12-30 14:34:52 -06:00
|
|
|
#include <string_view>
|
2024-08-02 11:57:34 -04:00
|
|
|
#include <vector>
|
2023-12-30 14:34:52 -06:00
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
#include <frc/Errors.h>
|
2024-05-10 14:04:34 -04:00
|
|
|
#include <frc/RobotController.h>
|
2022-10-21 20:53:28 -04:00
|
|
|
#include <frc/Timer.h>
|
2025-11-10 21:28:28 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <net/TimeSyncServer.h>
|
2023-12-16 13:41:27 -05:00
|
|
|
#include <opencv2/core.hpp>
|
2026-03-19 02:10:04 -04:00
|
|
|
#include <opencv2/core/utility.hpp>
|
2024-08-31 13:44:19 -04:00
|
|
|
#include <wpi/json.h>
|
2022-01-24 12:38:45 -05:00
|
|
|
|
|
|
|
|
#include "PhotonVersion.h"
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/dataflow/structures/Packet.h"
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2025-03-14 02:13:51 -04:00
|
|
|
static constexpr units::second_t WARN_DEBOUNCE_SEC = 5_s;
|
|
|
|
|
static constexpr units::second_t HEARTBEAT_DEBOUNCE_SEC = 500_ms;
|
|
|
|
|
|
2025-02-13 12:23:11 +08:00
|
|
|
inline void verifyDependencies() {
|
|
|
|
|
if (!(std::string_view{cv::getVersionString()} ==
|
|
|
|
|
std::string_view{photon::PhotonVersion::opencvTargetVersion})) {
|
|
|
|
|
std::string bfw =
|
|
|
|
|
"\n\n\n\n\n"
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
|
|
|
|
|
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> You are running an incompatible version \n"
|
|
|
|
|
">>> of PhotonVision ! \n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> PhotonLib ";
|
|
|
|
|
bfw += photon::PhotonVersion::versionString;
|
|
|
|
|
bfw += " is built for OpenCV ";
|
|
|
|
|
bfw += photon::PhotonVersion::opencvTargetVersion;
|
|
|
|
|
bfw +=
|
|
|
|
|
"\n"
|
|
|
|
|
">>> but you are using OpenCV ";
|
|
|
|
|
bfw += cv::getVersionString();
|
|
|
|
|
bfw +=
|
|
|
|
|
"\n>>> \n"
|
|
|
|
|
">>> This is neither tested nor supported. \n"
|
2025-07-12 17:44:55 +08:00
|
|
|
">>> You MUST update WPILib, PhotonLib, or both.\n"
|
|
|
|
|
">>> Check `./gradlew dependencies` and ensure\n"
|
|
|
|
|
">>> all mentions of OpenCV match the version \n"
|
|
|
|
|
">>> that PhotonLib was built for. If you find a"
|
|
|
|
|
">>> a mismatched version in a dependency, you\n"
|
|
|
|
|
">>> must take steps to update the version of \n"
|
|
|
|
|
">>> OpenCV used in that dependency. If you do\n"
|
|
|
|
|
">>> not control that dependency and an updated\n"
|
|
|
|
|
">>> version is not available, contact the \n"
|
|
|
|
|
">>> developers of that dependency. \n"
|
2025-02-13 12:23:11 +08:00
|
|
|
">>> \n"
|
|
|
|
|
">>> Your code will now crash. \n"
|
|
|
|
|
">>> We hope your day gets better. \n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
|
|
|
|
|
|
|
|
|
|
FRC_ReportWarning(bfw);
|
|
|
|
|
FRC_ReportError(frc::err::Error, bfw);
|
|
|
|
|
throw new std::runtime_error(std::string{bfw});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-30 14:34:52 -06:00
|
|
|
|
2024-11-09 14:35:38 -08:00
|
|
|
// bit of a hack -- start a TimeSync server on port 5810 (hard-coded). We want
|
|
|
|
|
// to avoid calling this from static initialization
|
|
|
|
|
static void InitTspServer() {
|
|
|
|
|
// We dont impose requirements about not calling the PhotonCamera constructor
|
|
|
|
|
// from different threads, so i guess we need this?
|
|
|
|
|
static std::mutex g_timeSyncServerMutex;
|
|
|
|
|
static bool g_timeSyncServerStarted{false};
|
|
|
|
|
static wpi::tsp::TimeSyncServer timesyncServer{5810};
|
|
|
|
|
|
|
|
|
|
std::lock_guard lock{g_timeSyncServerMutex};
|
|
|
|
|
if (!g_timeSyncServerStarted) {
|
|
|
|
|
timesyncServer.Start();
|
|
|
|
|
g_timeSyncServerStarted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-01 23:32:38 -07:00
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
namespace photon {
|
2022-10-21 20:53:28 -04:00
|
|
|
|
|
|
|
|
constexpr const units::second_t VERSION_CHECK_INTERVAL = 5_s;
|
2023-01-05 19:28:32 -05:00
|
|
|
static const std::vector<std::string_view> PHOTON_PREFIX = {"/photonvision/"};
|
2025-03-14 02:13:51 -04:00
|
|
|
static const std::string PHOTON_ALERT_GROUP{"PhotonAlerts"};
|
2024-05-19 20:36:44 -04:00
|
|
|
bool PhotonCamera::VERSION_CHECK_ENABLED = true;
|
|
|
|
|
|
|
|
|
|
void PhotonCamera::SetVersionCheckEnabled(bool enabled) {
|
|
|
|
|
VERSION_CHECK_ENABLED = enabled;
|
|
|
|
|
}
|
2022-10-21 20:53:28 -04:00
|
|
|
|
2024-10-20 22:21:24 -07:00
|
|
|
static const std::string TYPE_STRING =
|
|
|
|
|
std::string{"photonstruct:PhotonPipelineResult:"} +
|
|
|
|
|
std::string{SerdeType<PhotonPipelineResult>::GetSchemaHash()};
|
|
|
|
|
|
2023-01-05 19:28:32 -05:00
|
|
|
PhotonCamera::PhotonCamera(nt::NetworkTableInstance instance,
|
2022-12-25 07:01:57 +02:00
|
|
|
const std::string_view cameraName)
|
2023-01-05 19:28:32 -05:00
|
|
|
: mainTable(instance.GetTable("photonvision")),
|
2022-01-24 12:38:45 -05:00
|
|
|
rootTable(mainTable->GetSubTable(cameraName)),
|
2023-01-06 14:53:19 -08:00
|
|
|
rawBytesEntry(
|
|
|
|
|
rootTable->GetRawTopic("rawBytes")
|
2024-08-04 14:23:46 -04:00
|
|
|
.Subscribe(
|
2024-10-20 22:21:24 -07:00
|
|
|
TYPE_STRING, {},
|
2024-08-04 14:23:46 -04:00
|
|
|
{.pollStorage = 20, .periodic = 0.01, .sendAll = true})),
|
2022-12-16 17:05:23 -08:00
|
|
|
inputSaveImgEntry(
|
2022-12-30 21:48:28 -06:00
|
|
|
rootTable->GetIntegerTopic("inputSaveImgCmd").Publish()),
|
|
|
|
|
inputSaveImgSubscriber(
|
|
|
|
|
rootTable->GetIntegerTopic("inputSaveImgCmd").Subscribe(0)),
|
2022-12-16 17:05:23 -08:00
|
|
|
outputSaveImgEntry(
|
2022-12-30 21:48:28 -06:00
|
|
|
rootTable->GetIntegerTopic("outputSaveImgCmd").Publish()),
|
|
|
|
|
outputSaveImgSubscriber(
|
|
|
|
|
rootTable->GetIntegerTopic("outputSaveImgCmd").Subscribe(0)),
|
2023-02-08 21:07:12 -05:00
|
|
|
pipelineIndexPub(
|
|
|
|
|
rootTable->GetIntegerTopic("pipelineIndexRequest").Publish()),
|
|
|
|
|
pipelineIndexSub(
|
|
|
|
|
rootTable->GetIntegerTopic("pipelineIndexState").Subscribe(0)),
|
2026-02-03 00:11:34 -05:00
|
|
|
ledModePub(mainTable->GetIntegerTopic("ledModeRequest").Publish()),
|
|
|
|
|
ledModeSub(mainTable->GetIntegerTopic("ledModeState").Subscribe(0)),
|
2022-12-16 17:05:23 -08:00
|
|
|
versionEntry(mainTable->GetStringTopic("version").Subscribe("")),
|
2023-02-13 17:57:01 -05:00
|
|
|
cameraIntrinsicsSubscriber(
|
|
|
|
|
rootTable->GetDoubleArrayTopic("cameraIntrinsics").Subscribe({})),
|
|
|
|
|
cameraDistortionSubscriber(
|
|
|
|
|
rootTable->GetDoubleArrayTopic("cameraDistortion").Subscribe({})),
|
2022-12-16 17:05:23 -08:00
|
|
|
driverModeSubscriber(
|
|
|
|
|
rootTable->GetBooleanTopic("driverMode").Subscribe(false)),
|
2023-01-29 23:30:34 -05:00
|
|
|
driverModePublisher(
|
|
|
|
|
rootTable->GetBooleanTopic("driverModeRequest").Publish()),
|
2025-12-29 23:01:10 -06:00
|
|
|
fpsLimitSubscriber(rootTable->GetIntegerTopic("fpsLimit").Subscribe(-1)),
|
|
|
|
|
fpsLimitPublisher(
|
|
|
|
|
rootTable->GetIntegerTopic("fpsLimitRequest").Publish()),
|
2025-03-14 02:13:51 -04:00
|
|
|
heartbeatSubscriber(
|
|
|
|
|
rootTable->GetIntegerTopic("heartbeat").Subscribe(-1)),
|
2024-05-10 14:58:18 -04:00
|
|
|
topicNameSubscriber(instance, PHOTON_PREFIX, {.topicsOnly = true}),
|
2022-12-25 07:01:57 +02:00
|
|
|
path(rootTable->GetPath()),
|
2025-03-14 02:13:51 -04:00
|
|
|
cameraName(cameraName),
|
|
|
|
|
disconnectAlert(PHOTON_ALERT_GROUP,
|
|
|
|
|
std::string{"PhotonCamera '"} + std::string{cameraName} +
|
|
|
|
|
"' is disconnected.",
|
|
|
|
|
frc::Alert::AlertType::kWarning),
|
|
|
|
|
timesyncAlert(PHOTON_ALERT_GROUP, "", frc::Alert::AlertType::kWarning) {
|
2025-02-13 12:23:11 +08:00
|
|
|
verifyDependencies();
|
2023-12-16 15:26:00 -05:00
|
|
|
InstanceCount++;
|
2025-06-17 19:09:09 -04:00
|
|
|
HAL_ReportUsage("PhotonVision/PhotonCamera", InstanceCount, "");
|
2024-11-01 23:32:38 -07:00
|
|
|
|
2024-11-09 14:35:38 -08:00
|
|
|
// The Robot class is actually created here:
|
|
|
|
|
// https://github.com/wpilibsuite/allwpilib/blob/811b1309683e930a1ce69fae818f943ff161b7a5/wpilibc/src/main/native/include/frc/RobotBase.h#L33
|
|
|
|
|
// so we should be fine to call this from the ctor
|
|
|
|
|
InitTspServer();
|
2023-12-16 15:26:00 -05:00
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-12-25 07:01:57 +02:00
|
|
|
PhotonCamera::PhotonCamera(const std::string_view cameraName)
|
2023-01-05 19:28:32 -05:00
|
|
|
: PhotonCamera(nt::NetworkTableInstance::GetDefault(), cameraName) {}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
PhotonPipelineResult PhotonCamera::GetLatestResult() {
|
2023-02-13 18:22:22 -08:00
|
|
|
if (test) {
|
2024-08-04 14:23:46 -04:00
|
|
|
if (testResult.size())
|
|
|
|
|
return testResult.back();
|
|
|
|
|
else
|
|
|
|
|
return PhotonPipelineResult{};
|
2023-02-13 18:22:22 -08:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
// Prints warning if not connected
|
|
|
|
|
VerifyVersion();
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
// Fill the packet with latest data and populate result.
|
2024-05-10 14:04:34 -04:00
|
|
|
units::microsecond_t now =
|
|
|
|
|
units::microsecond_t(frc::RobotController::GetFPGATime());
|
2022-12-16 17:05:23 -08:00
|
|
|
const auto value = rawBytesEntry.Get();
|
2024-08-31 13:44:19 -04:00
|
|
|
if (!value.size()) return PhotonPipelineResult{};
|
2021-03-21 15:29:29 -05:00
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
photon::Packet packet{value};
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
// Create the new result;
|
|
|
|
|
PhotonPipelineResult result = packet.Unpack<PhotonPipelineResult>();
|
2022-11-07 11:09:55 -07:00
|
|
|
|
2025-03-14 02:13:51 -04:00
|
|
|
CheckTimeSyncOrWarn(result);
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
result.SetReceiveTimestamp(now);
|
2022-11-07 11:09:55 -07:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
std::vector<PhotonPipelineResult> PhotonCamera::GetAllUnreadResults() {
|
|
|
|
|
if (test) {
|
|
|
|
|
return testResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prints warning if not connected
|
|
|
|
|
VerifyVersion();
|
2025-03-14 02:13:51 -04:00
|
|
|
UpdateDisconnectAlert();
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
const auto changes = rawBytesEntry.ReadQueue();
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
// Create the new result list
|
|
|
|
|
std::vector<PhotonPipelineResult> ret;
|
|
|
|
|
ret.reserve(changes.size());
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < changes.size(); i++) {
|
|
|
|
|
const nt::Timestamped<std::vector<uint8_t>>& value = changes[i];
|
|
|
|
|
|
|
|
|
|
if (!value.value.size() || value.time == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill the packet with latest data and populate result.
|
|
|
|
|
photon::Packet packet{value.value};
|
2024-08-31 13:44:19 -04:00
|
|
|
auto result = packet.Unpack<PhotonPipelineResult>();
|
2024-08-04 14:23:46 -04:00
|
|
|
|
2025-03-14 02:13:51 -04:00
|
|
|
CheckTimeSyncOrWarn(result);
|
|
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
// TODO: NT4 timestamps are still not to be trusted. But it's the best we
|
|
|
|
|
// can do until we can make time sync more reliable.
|
2024-08-31 13:44:19 -04:00
|
|
|
result.SetReceiveTimestamp(units::microsecond_t(value.time) -
|
2024-08-04 14:23:46 -04:00
|
|
|
result.GetLatency());
|
2024-08-31 13:44:19 -04:00
|
|
|
|
|
|
|
|
ret.push_back(result);
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 02:13:51 -04:00
|
|
|
void PhotonCamera::UpdateDisconnectAlert() {
|
|
|
|
|
disconnectAlert.Set(!IsConnected());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotonCamera::CheckTimeSyncOrWarn(photon::PhotonPipelineResult& result) {
|
|
|
|
|
if (result.metadata.timeSinceLastPong > 5L * 1000000L) {
|
|
|
|
|
std::string warningText =
|
|
|
|
|
"PhotonVision coprocessor at path " + path +
|
|
|
|
|
" is not connected to the TimeSyncServer? It's been " +
|
|
|
|
|
std::to_string(result.metadata.timeSinceLastPong / 1e6) +
|
|
|
|
|
"s since the coprocessor last heard a pong.";
|
|
|
|
|
|
|
|
|
|
timesyncAlert.SetText(warningText);
|
|
|
|
|
timesyncAlert.Set(true);
|
|
|
|
|
|
|
|
|
|
if (frc::Timer::GetFPGATimestamp() <
|
|
|
|
|
(prevTimeSyncWarnTime + WARN_DEBOUNCE_SEC)) {
|
|
|
|
|
prevTimeSyncWarnTime = frc::Timer::GetFPGATimestamp();
|
|
|
|
|
|
|
|
|
|
FRC_ReportWarning(
|
|
|
|
|
warningText +
|
|
|
|
|
"\n\nCheck /photonvision/.timesync/{{COPROCESSOR_HOSTNAME}} for more "
|
|
|
|
|
"information.");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Got a valid packet, reset the last time
|
|
|
|
|
prevTimeSyncWarnTime = 0_s;
|
|
|
|
|
timesyncAlert.Set(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
void PhotonCamera::SetDriverMode(bool driverMode) {
|
2023-01-29 23:30:34 -05:00
|
|
|
driverModePublisher.Set(driverMode);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
2025-12-29 23:01:10 -06:00
|
|
|
bool PhotonCamera::GetDriverMode() const { return driverModeSubscriber.Get(); }
|
|
|
|
|
|
|
|
|
|
int PhotonCamera::GetFPSLimit() const { return fpsLimitSubscriber.Get(); }
|
|
|
|
|
|
|
|
|
|
void PhotonCamera::SetFPSLimit(int fpsLimit) {
|
|
|
|
|
fpsLimitPublisher.Set(fpsLimit);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 21:48:28 -06:00
|
|
|
void PhotonCamera::TakeInputSnapshot() {
|
|
|
|
|
inputSaveImgEntry.Set(inputSaveImgSubscriber.Get() + 1);
|
|
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-12-30 21:48:28 -06:00
|
|
|
void PhotonCamera::TakeOutputSnapshot() {
|
|
|
|
|
outputSaveImgEntry.Set(outputSaveImgSubscriber.Get() + 1);
|
|
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2023-09-16 10:17:28 -04:00
|
|
|
void PhotonCamera::SetPipelineIndex(int index) { pipelineIndexPub.Set(index); }
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2021-12-15 11:16:09 -06:00
|
|
|
int PhotonCamera::GetPipelineIndex() const {
|
2023-02-08 21:07:12 -05:00
|
|
|
return static_cast<int>(pipelineIndexSub.Get());
|
2021-12-15 11:16:09 -06:00
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
LEDMode PhotonCamera::GetLEDMode() const {
|
2023-02-08 21:07:12 -05:00
|
|
|
return static_cast<LEDMode>(static_cast<int>(ledModeSub.Get()));
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 11:16:09 -06:00
|
|
|
void PhotonCamera::SetLEDMode(LEDMode mode) {
|
2023-09-16 10:17:28 -04:00
|
|
|
ledModePub.Set(static_cast<int>(mode));
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
2022-01-24 12:38:45 -05:00
|
|
|
|
2023-04-18 01:27:40 +10:00
|
|
|
const std::string_view PhotonCamera::GetCameraName() const {
|
2024-05-10 14:58:18 -04:00
|
|
|
return cameraName;
|
2023-04-18 01:27:40 +10:00
|
|
|
}
|
|
|
|
|
|
2025-03-14 02:13:51 -04:00
|
|
|
bool PhotonCamera::IsConnected() {
|
|
|
|
|
auto currentHeartbeat = heartbeatSubscriber.Get();
|
|
|
|
|
auto now = frc::Timer::GetFPGATimestamp();
|
|
|
|
|
|
|
|
|
|
if (currentHeartbeat < 0) {
|
|
|
|
|
// we have never heard from the camera
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentHeartbeat != prevHeartbeatValue) {
|
|
|
|
|
// New heartbeat value from the coprocessor
|
|
|
|
|
prevHeartbeatChangeTime = now;
|
|
|
|
|
prevHeartbeatValue = currentHeartbeat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (now - prevHeartbeatChangeTime) < HEARTBEAT_DEBOUNCE_SEC;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 17:28:35 -04:00
|
|
|
std::optional<PhotonCamera::CameraMatrix> PhotonCamera::GetCameraMatrix() {
|
|
|
|
|
auto camCoeffs = cameraIntrinsicsSubscriber.Get();
|
|
|
|
|
if (camCoeffs.size() == 9) {
|
|
|
|
|
PhotonCamera::CameraMatrix retVal =
|
2024-08-31 13:44:19 -04:00
|
|
|
Eigen::Map<const Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(
|
|
|
|
|
camCoeffs.data());
|
2024-05-29 17:28:35 -04:00
|
|
|
return retVal;
|
|
|
|
|
}
|
2024-08-31 13:44:19 -04:00
|
|
|
|
2024-05-29 17:28:35 -04:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<PhotonCamera::DistortionMatrix> PhotonCamera::GetDistCoeffs() {
|
2023-02-13 17:57:01 -05:00
|
|
|
auto distCoeffs = cameraDistortionSubscriber.Get();
|
2024-05-29 17:28:35 -04:00
|
|
|
auto bound = distCoeffs.size();
|
|
|
|
|
if (bound > 0 && bound <= 8) {
|
|
|
|
|
PhotonCamera::DistortionMatrix retVal =
|
|
|
|
|
PhotonCamera::DistortionMatrix::Zero();
|
|
|
|
|
|
|
|
|
|
Eigen::Map<const Eigen::VectorXd> map(distCoeffs.data(), bound);
|
|
|
|
|
retVal.block(0, 0, bound, 1) = map;
|
|
|
|
|
|
2023-12-16 13:41:27 -05:00
|
|
|
return retVal;
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
2022-12-25 07:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
void PhotonCamera::VerifyVersion() {
|
2024-05-19 20:36:44 -04:00
|
|
|
if (!PhotonCamera::VERSION_CHECK_ENABLED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-28 07:37:52 -05:00
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
if ((frc::Timer::GetFPGATimestamp() - lastVersionCheckTime) <
|
|
|
|
|
VERSION_CHECK_INTERVAL)
|
|
|
|
|
return;
|
|
|
|
|
this->lastVersionCheckTime = frc::Timer::GetFPGATimestamp();
|
|
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
const std::string& versionString = versionEntry.Get("");
|
2022-01-24 12:38:45 -05:00
|
|
|
if (versionString.empty()) {
|
|
|
|
|
std::string path_ = path;
|
2023-01-05 19:28:32 -05:00
|
|
|
std::vector<std::string> cameraNames =
|
|
|
|
|
rootTable->GetInstance().GetTable("photonvision")->GetSubTables();
|
|
|
|
|
if (cameraNames.empty()) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Could not find any PhotonVision coprocessors on "
|
|
|
|
|
"NetworkTables. Double check that PhotonVision is "
|
|
|
|
|
"running, and that your camera is connected!");
|
|
|
|
|
} else {
|
|
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
|
|
|
|
"PhotonVision coprocessor at path {} not found on NetworkTables. "
|
|
|
|
|
"Double check that your camera names match!",
|
|
|
|
|
path_);
|
|
|
|
|
|
|
|
|
|
std::string cameraNameOutString;
|
|
|
|
|
for (unsigned int i = 0; i < cameraNames.size(); i++) {
|
2024-10-20 22:21:24 -07:00
|
|
|
cameraNameOutString += ("\n" + cameraNames[i]);
|
2023-01-05 19:28:32 -05:00
|
|
|
}
|
|
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
2024-10-20 22:21:24 -07:00
|
|
|
"Found the following PhotonVision cameras on NetworkTables:\n{}",
|
2023-01-05 19:28:32 -05:00
|
|
|
cameraNameOutString);
|
|
|
|
|
}
|
2024-08-31 13:44:19 -04:00
|
|
|
} else {
|
|
|
|
|
std::string local_uuid{SerdeType<PhotonPipelineResult>::GetSchemaHash()};
|
2024-10-20 22:21:24 -07:00
|
|
|
|
|
|
|
|
// implicit conversion here might throw an exception, so be careful of that
|
|
|
|
|
wpi::json remote_uuid_json =
|
2024-08-31 13:44:19 -04:00
|
|
|
rawBytesEntry.GetTopic().GetProperty("message_uuid");
|
2024-10-20 22:21:24 -07:00
|
|
|
if (!remote_uuid_json.is_string()) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Cannot find property message_uuid for PhotonCamera {}",
|
|
|
|
|
path);
|
2024-11-19 13:28:07 +11:00
|
|
|
return;
|
2024-10-20 22:21:24 -07:00
|
|
|
}
|
|
|
|
|
std::string remote_uuid{remote_uuid_json};
|
2024-08-31 13:44:19 -04:00
|
|
|
|
|
|
|
|
if (local_uuid != remote_uuid) {
|
2025-02-13 12:23:11 +08:00
|
|
|
constexpr std::string_view bfw =
|
|
|
|
|
"\n\n\n\n"
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
|
|
|
|
|
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> You are running an incompatible version \n"
|
|
|
|
|
">>> of PhotonVision on your coprocessor! \n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> This is neither tested nor supported. \n"
|
|
|
|
|
">>> You MUST update PhotonVision, \n"
|
|
|
|
|
">>> PhotonLib, or both. \n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> Your code will now crash. \n"
|
|
|
|
|
">>> We hope your day gets better. \n"
|
|
|
|
|
">>> \n"
|
|
|
|
|
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
|
|
|
|
|
"\n\n";
|
|
|
|
|
FRC_ReportWarning(bfw);
|
2024-08-31 13:44:19 -04:00
|
|
|
std::string error_str = fmt::format(
|
|
|
|
|
"Photonlib version {} (message definition version {}) does not match "
|
|
|
|
|
"coprocessor version {} (message definition version {})!",
|
|
|
|
|
PhotonVersion::versionString, local_uuid, versionString, remote_uuid);
|
|
|
|
|
FRC_ReportError(frc::err::Error, "{}", error_str);
|
|
|
|
|
throw std::runtime_error(error_str);
|
|
|
|
|
}
|
2022-01-24 12:38:45 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 14:58:18 -04:00
|
|
|
std::vector<std::string> PhotonCamera::tablesThatLookLikePhotonCameras() {
|
|
|
|
|
std::vector<std::string> cameraNames = mainTable->GetSubTables();
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
std::copy_if(
|
|
|
|
|
cameraNames.begin(), cameraNames.end(), std::back_inserter(ret),
|
|
|
|
|
[this](auto& it) {
|
|
|
|
|
return mainTable->GetSubTable(it)->GetEntry("rawBytes").Exists();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
} // namespace photon
|