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
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* Copyright (c) 2022 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "photonlib/PhotonCamera.h"
|
|
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
#include <frc/Errors.h>
|
2022-10-21 20:53:28 -04:00
|
|
|
#include <frc/Timer.h>
|
2022-01-24 12:38:45 -05:00
|
|
|
|
|
|
|
|
#include "PhotonVersion.h"
|
2021-01-16 20:41:47 -08:00
|
|
|
#include "photonlib/Packet.h"
|
|
|
|
|
|
|
|
|
|
namespace photonlib {
|
2022-10-21 20:53:28 -04:00
|
|
|
|
|
|
|
|
constexpr const units::second_t VERSION_CHECK_INTERVAL = 5_s;
|
|
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
PhotonCamera::PhotonCamera(std::shared_ptr<nt::NetworkTableInstance> instance,
|
|
|
|
|
const std::string& cameraName)
|
|
|
|
|
: mainTable(instance->GetTable("photonvision")),
|
|
|
|
|
rootTable(mainTable->GetSubTable(cameraName)),
|
2022-12-16 17:05:23 -08:00
|
|
|
rawBytesEntry(rootTable->GetRawTopic("rawBytes").Subscribe("raw", {})),
|
|
|
|
|
driverModeEntry(rootTable->GetBooleanTopic("driverMode").Publish()),
|
|
|
|
|
inputSaveImgEntry(
|
|
|
|
|
rootTable->GetBooleanTopic("inputSaveImgCmd").Publish()),
|
|
|
|
|
outputSaveImgEntry(
|
|
|
|
|
rootTable->GetBooleanTopic("outputSaveImgCmd").Publish()),
|
|
|
|
|
pipelineIndexEntry(rootTable->GetIntegerTopic("pipelineIndex").Publish()),
|
|
|
|
|
ledModeEntry(mainTable->GetIntegerTopic("ledMode").Publish()),
|
|
|
|
|
versionEntry(mainTable->GetStringTopic("version").Subscribe("")),
|
|
|
|
|
driverModeSubscriber(
|
|
|
|
|
rootTable->GetBooleanTopic("driverMode").Subscribe(false)),
|
|
|
|
|
pipelineIndexSubscriber(
|
|
|
|
|
rootTable->GetIntegerTopic("pipelineIndex").Subscribe(-1)),
|
|
|
|
|
ledModeSubscriber(mainTable->GetIntegerTopic("ledMode").Subscribe(0)),
|
2022-01-24 12:38:45 -05:00
|
|
|
path(rootTable->GetPath()) {}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
PhotonCamera::PhotonCamera(const std::string& cameraName)
|
2022-01-24 12:38:45 -05:00
|
|
|
: PhotonCamera(std::make_shared<nt::NetworkTableInstance>(
|
|
|
|
|
nt::NetworkTableInstance::GetDefault()),
|
|
|
|
|
cameraName) {}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
PhotonPipelineResult PhotonCamera::GetLatestResult() {
|
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.
|
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
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
photonlib::Packet packet{value};
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
packet >> result;
|
2022-11-07 11:09:55 -07:00
|
|
|
|
|
|
|
|
result.SetTimestamp(units::microsecond_t(rawBytesEntry.GetLastChange()) -
|
|
|
|
|
result.GetLatency());
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotonCamera::SetDriverMode(bool driverMode) {
|
2022-12-16 17:05:23 -08:00
|
|
|
driverModeEntry.Set(driverMode);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
void PhotonCamera::TakeInputSnapshot() { inputSaveImgEntry.Set(true); }
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
void PhotonCamera::TakeOutputSnapshot() { outputSaveImgEntry.Set(true); }
|
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
|
|
|
|
|
|
|
|
void PhotonCamera::SetPipelineIndex(int index) {
|
2022-12-16 17:05:23 -08:00
|
|
|
pipelineIndexEntry.Set(static_cast<double>(index));
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 11:16:09 -06:00
|
|
|
int PhotonCamera::GetPipelineIndex() const {
|
2022-12-16 17:05:23 -08:00
|
|
|
return static_cast<int>(pipelineIndexSubscriber.Get());
|
2021-12-15 11:16:09 -06:00
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
LEDMode PhotonCamera::GetLEDMode() const {
|
2022-12-16 17:05:23 -08:00
|
|
|
return static_cast<LEDMode>(static_cast<int>(ledModeSubscriber.Get()));
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 11:16:09 -06:00
|
|
|
void PhotonCamera::SetLEDMode(LEDMode mode) {
|
2022-12-16 17:05:23 -08:00
|
|
|
ledModeEntry.Set(static_cast<double>(static_cast<int>(mode)));
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
2022-01-24 12:38:45 -05:00
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
void PhotonCamera::VerifyVersion() {
|
2022-02-28 07:37:52 -05:00
|
|
|
if (!PhotonCamera::VERSION_CHECK_ENABLED) return;
|
|
|
|
|
|
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;
|
|
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
|
|
|
|
"PhotonVision coprocessor at path {} not found on NetworkTables!",
|
|
|
|
|
path_);
|
|
|
|
|
} else if (!VersionMatches(versionString)) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Photon version {} does not match coprocessor version {}!",
|
|
|
|
|
PhotonVersion::versionString, versionString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
} // namespace photonlib
|