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
|
|
|
|
2023-12-16 15:26:00 -05:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
|
2023-12-30 14:34:52 -06:00
|
|
|
#include <string_view>
|
|
|
|
|
|
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>
|
2023-12-16 13:41:27 -05:00
|
|
|
#include <opencv2/core.hpp>
|
2023-02-13 17:57:01 -05:00
|
|
|
#include <opencv2/core/mat.hpp>
|
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
|
|
|
|
2024-01-01 13:31:23 -08:00
|
|
|
inline constexpr std::string_view bfw =
|
2023-12-30 14:34:52 -06:00
|
|
|
"\n\n\n\n"
|
2024-01-01 16:30:57 -06:00
|
|
|
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\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"
|
2023-12-30 14:34:52 -06:00
|
|
|
"\n\n";
|
|
|
|
|
|
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/"};
|
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
|
|
|
|
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")
|
2023-01-22 10:56:41 -05:00
|
|
|
.Subscribe("rawBytes", {}, {.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)),
|
|
|
|
|
ledModePub(mainTable->GetIntegerTopic("ledMode").Publish()),
|
|
|
|
|
ledModeSub(mainTable->GetIntegerTopic("ledMode").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()),
|
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()),
|
2024-05-10 14:58:18 -04:00
|
|
|
cameraName(cameraName) {
|
2023-12-16 15:26:00 -05:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PhotonCamera, InstanceCount);
|
|
|
|
|
InstanceCount++;
|
|
|
|
|
}
|
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) {
|
|
|
|
|
return testResult;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
// Prints warning if not connected
|
|
|
|
|
VerifyVersion();
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
// Clear the current packet.
|
|
|
|
|
packet.Clear();
|
|
|
|
|
|
|
|
|
|
// Create the new result;
|
|
|
|
|
PhotonPipelineResult result;
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
if (!value.size()) return result;
|
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
|
|
|
|
|
|
|
|
packet >> result;
|
2022-11-07 11:09:55 -07:00
|
|
|
|
2024-05-10 14:04:34 -04:00
|
|
|
result.SetRecieveTimestamp(now);
|
2022-11-07 11:09:55 -07:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotonCamera::SetDriverMode(bool driverMode) {
|
2023-01-29 23:30:34 -05:00
|
|
|
driverModePublisher.Set(driverMode);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
bool PhotonCamera::GetDriverMode() const { return driverModeSubscriber.Get(); }
|
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
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
std::optional<cv::Mat> PhotonCamera::GetCameraMatrix() {
|
|
|
|
|
auto camCoeffs = cameraIntrinsicsSubscriber.Get();
|
|
|
|
|
if (camCoeffs.size() == 9) {
|
2023-12-16 13:41:27 -05:00
|
|
|
cv::Mat retVal(3, 3, CV_64FC1);
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
|
retVal.at<double>(i, j) = camCoeffs[(j * 3) + i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
std::optional<cv::Mat> PhotonCamera::GetDistCoeffs() {
|
|
|
|
|
auto distCoeffs = cameraDistortionSubscriber.Get();
|
|
|
|
|
if (distCoeffs.size() == 5) {
|
2023-12-16 13:41:27 -05:00
|
|
|
cv::Mat retVal(5, 1, CV_64FC1);
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
retVal.at<double>(i, 0) = distCoeffs[i];
|
|
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
|
cameraNameOutString += "\n" + cameraNames[i];
|
|
|
|
|
}
|
|
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
|
|
|
|
"Found the following PhotonVision cameras on NetworkTables:{}",
|
|
|
|
|
cameraNameOutString);
|
|
|
|
|
}
|
2022-01-24 12:38:45 -05:00
|
|
|
} else if (!VersionMatches(versionString)) {
|
2023-12-30 14:34:52 -06:00
|
|
|
FRC_ReportError(frc::warn::Warning, bfw);
|
|
|
|
|
std::string error_str = fmt::format(
|
|
|
|
|
"Photonlib version {} does not match coprocessor version {}!",
|
|
|
|
|
PhotonVersion::versionString, versionString);
|
|
|
|
|
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
|