2024-08-04 14:23:46 -04:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) PhotonVision
|
|
|
|
|
*
|
|
|
|
|
* 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:
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "photon/simulation/PhotonCameraSim.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2025-01-08 19:43:46 +01:00
|
|
|
#include <frc/apriltag/AprilTagFieldLayout.h>
|
|
|
|
|
#include <frc/apriltag/AprilTagFields.h>
|
|
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
namespace photon {
|
|
|
|
|
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera)
|
2025-01-08 19:43:46 +01:00
|
|
|
: PhotonCameraSim(camera, photon::SimCameraProperties::PERFECT_90DEG(),
|
|
|
|
|
frc::AprilTagFieldLayout::LoadField(
|
|
|
|
|
frc::AprilTagField::kDefaultField)) {}
|
|
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera,
|
2025-01-08 19:43:46 +01:00
|
|
|
const SimCameraProperties& props,
|
|
|
|
|
const frc::AprilTagFieldLayout& tagLayout)
|
|
|
|
|
: prop{props}, cam{camera}, tagLayout{tagLayout} {
|
2024-08-04 14:23:46 -04:00
|
|
|
SetMinTargetAreaPixels(kDefaultMinAreaPx);
|
|
|
|
|
videoSimRaw =
|
|
|
|
|
frc::CameraServer::PutVideo(std::string{camera->GetCameraName()} + "-raw",
|
|
|
|
|
prop.GetResWidth(), prop.GetResHeight());
|
|
|
|
|
videoSimRaw.SetPixelFormat(cs::VideoMode::PixelFormat::kGray);
|
|
|
|
|
videoSimProcessed = frc::CameraServer::PutVideo(
|
|
|
|
|
std::string{camera->GetCameraName()} + "-processed", prop.GetResWidth(),
|
|
|
|
|
prop.GetResHeight());
|
|
|
|
|
ts.subTable = cam->GetCameraTable();
|
|
|
|
|
ts.UpdateEntries();
|
|
|
|
|
}
|
2025-01-08 19:43:46 +01:00
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
PhotonCameraSim::PhotonCameraSim(PhotonCamera* camera,
|
|
|
|
|
const SimCameraProperties& props,
|
|
|
|
|
double minTargetAreaPercent,
|
|
|
|
|
units::meter_t maxSightRange)
|
|
|
|
|
: PhotonCameraSim(camera, props) {
|
|
|
|
|
this->minTargetAreaPercent = minTargetAreaPercent;
|
|
|
|
|
this->maxSightRange = maxSightRange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PhotonCameraSim::CanSeeTargetPose(const frc::Pose3d& camPose,
|
|
|
|
|
const VisionTargetSim& target) {
|
|
|
|
|
CameraTargetRelation rel{camPose, target.GetPose()};
|
|
|
|
|
return ((units::math::abs(rel.camToTargYaw.Degrees()) <
|
|
|
|
|
prop.GetHorizFOV().Degrees() / 2.0) &&
|
|
|
|
|
(units::math::abs(rel.camToTargPitch.Degrees()) <
|
|
|
|
|
prop.GetVertFOV().Degrees() / 2.0) &&
|
|
|
|
|
(!target.GetModel().GetIsPlanar() ||
|
|
|
|
|
units::math::abs(rel.targToCamAngle.Degrees()) < 90_deg) &&
|
|
|
|
|
(rel.camToTarg.Translation().Norm() <= maxSightRange));
|
|
|
|
|
}
|
|
|
|
|
bool PhotonCameraSim::CanSeeCorner(const std::vector<cv::Point2f>& points) {
|
|
|
|
|
for (const auto& pt : points) {
|
|
|
|
|
if (std::clamp<float>(pt.x, 0, prop.GetResWidth()) != pt.x ||
|
|
|
|
|
std::clamp<float>(pt.y, 0, prop.GetResHeight()) != pt.y) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
std::optional<uint64_t> PhotonCameraSim::ConsumeNextEntryTime() {
|
|
|
|
|
uint64_t now = wpi::Now();
|
|
|
|
|
uint64_t timestamp{};
|
|
|
|
|
int iter = 0;
|
|
|
|
|
while (now >= nextNTEntryTime) {
|
|
|
|
|
timestamp = nextNTEntryTime;
|
|
|
|
|
uint64_t frameTime = prop.EstSecUntilNextFrame()
|
|
|
|
|
.convert<units::microseconds>()
|
|
|
|
|
.to<uint64_t>();
|
|
|
|
|
nextNTEntryTime += frameTime;
|
|
|
|
|
|
|
|
|
|
if (iter++ > 50) {
|
|
|
|
|
timestamp = now;
|
|
|
|
|
nextNTEntryTime = now + frameTime;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timestamp != 0) {
|
|
|
|
|
return timestamp;
|
|
|
|
|
} else {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PhotonPipelineResult PhotonCameraSim::Process(
|
|
|
|
|
units::second_t latency, const frc::Pose3d& cameraPose,
|
|
|
|
|
std::vector<VisionTargetSim> targets) {
|
|
|
|
|
std::sort(targets.begin(), targets.end(),
|
|
|
|
|
[cameraPose](const VisionTargetSim& t1, const VisionTargetSim& t2) {
|
|
|
|
|
units::meter_t dist1 =
|
|
|
|
|
t1.GetPose().Translation().Distance(cameraPose.Translation());
|
|
|
|
|
units::meter_t dist2 =
|
|
|
|
|
t2.GetPose().Translation().Distance(cameraPose.Translation());
|
|
|
|
|
return dist1 > dist2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<VisionTargetSim, std::vector<cv::Point2f>>>
|
|
|
|
|
visibleTgts{};
|
|
|
|
|
std::vector<PhotonTrackedTarget> detectableTgts{};
|
|
|
|
|
RotTrlTransform3d camRt = RotTrlTransform3d::MakeRelativeTo(cameraPose);
|
|
|
|
|
|
|
|
|
|
VideoSimUtil::UpdateVideoProp(videoSimRaw, prop);
|
|
|
|
|
VideoSimUtil::UpdateVideoProp(videoSimProcessed, prop);
|
|
|
|
|
cv::Size videoFrameSize{prop.GetResWidth(), prop.GetResHeight()};
|
|
|
|
|
cv::Mat blankFrame = cv::Mat::zeros(videoFrameSize, CV_8UC1);
|
|
|
|
|
blankFrame.assignTo(videoSimFrameRaw);
|
|
|
|
|
|
|
|
|
|
for (const auto& tgt : targets) {
|
|
|
|
|
if (!CanSeeTargetPose(cameraPose, tgt)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<frc::Translation3d> fieldCorners = tgt.GetFieldVertices();
|
|
|
|
|
if (tgt.GetModel().GetIsSpherical()) {
|
|
|
|
|
TargetModel model = tgt.GetModel();
|
|
|
|
|
fieldCorners = model.GetFieldVertices(TargetModel::GetOrientedPose(
|
|
|
|
|
tgt.GetPose().Translation(), cameraPose.Translation()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<cv::Point2f> imagePoints = OpenCVHelp::ProjectPoints(
|
|
|
|
|
prop.GetIntrinsics(), prop.GetDistCoeffs(), camRt, fieldCorners);
|
|
|
|
|
if (tgt.GetModel().GetIsSpherical()) {
|
|
|
|
|
cv::Point2d center = OpenCVHelp::AvgPoint(imagePoints);
|
|
|
|
|
int l = 0;
|
|
|
|
|
int t = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
int r = 0;
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (imagePoints[i].x < imagePoints[l].x) {
|
|
|
|
|
l = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cv::Point2d lc = imagePoints[l];
|
|
|
|
|
std::array<double, 4> angles{};
|
|
|
|
|
t = (l + 1) % 4;
|
|
|
|
|
b = (l + 1) % 4;
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (i == l) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
cv::Point2d ic = imagePoints[i];
|
|
|
|
|
angles[i] = std::atan2(lc.y - ic.y, ic.x - lc.x);
|
|
|
|
|
if (angles[i] >= angles[t]) {
|
|
|
|
|
t = i;
|
|
|
|
|
}
|
|
|
|
|
if (angles[i] <= angles[b]) {
|
|
|
|
|
b = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (i != t && i != l && i != b) {
|
|
|
|
|
r = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cv::RotatedRect rect{
|
|
|
|
|
cv::Point2d{center.x, center.y},
|
|
|
|
|
cv::Size2d{imagePoints[r].x - lc.x,
|
|
|
|
|
imagePoints[b].y - imagePoints[t].y},
|
|
|
|
|
units::radian_t{-angles[r]}.convert<units::degrees>().to<float>()};
|
|
|
|
|
std::vector<cv::Point2f> points{};
|
|
|
|
|
rect.points(points);
|
|
|
|
|
|
|
|
|
|
// Can't find an easier way to convert from Point2f to Point2d
|
|
|
|
|
imagePoints.clear();
|
|
|
|
|
std::transform(points.begin(), points.end(),
|
|
|
|
|
std::back_inserter(imagePoints),
|
|
|
|
|
[](const cv::Point2f& p) { return (cv::Point2d)p; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visibleTgts.emplace_back(std::make_pair(tgt, imagePoints));
|
|
|
|
|
std::vector<cv::Point2f> noisyTargetCorners =
|
|
|
|
|
prop.EstPixelNoise(imagePoints);
|
|
|
|
|
cv::RotatedRect minAreaRect =
|
|
|
|
|
OpenCVHelp::GetMinAreaRect(noisyTargetCorners);
|
|
|
|
|
std::vector<cv::Point2f> minAreaRectPts;
|
|
|
|
|
minAreaRectPts.reserve(4);
|
|
|
|
|
minAreaRect.points(minAreaRectPts);
|
|
|
|
|
cv::Point2d centerPt = minAreaRect.center;
|
|
|
|
|
frc::Rotation3d centerRot = prop.GetPixelRot(centerPt);
|
|
|
|
|
double areaPercent = prop.GetContourAreaPercent(noisyTargetCorners);
|
|
|
|
|
|
|
|
|
|
if (!(CanSeeCorner(noisyTargetCorners) &&
|
|
|
|
|
areaPercent >= minTargetAreaPercent)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
std::optional<photon::PnpResult> pnpSim = std::nullopt;
|
2024-08-04 14:23:46 -04:00
|
|
|
if (tgt.fiducialId >= 0 && tgt.GetFieldVertices().size() == 4) {
|
2024-12-29 14:43:55 -08:00
|
|
|
pnpSim = OpenCVHelp::SolvePNP_Square(
|
2024-08-04 14:23:46 -04:00
|
|
|
prop.GetIntrinsics(), prop.GetDistCoeffs(),
|
|
|
|
|
tgt.GetModel().GetVertices(), noisyTargetCorners);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<float, float>> tempCorners =
|
|
|
|
|
OpenCVHelp::PointsToCorners(minAreaRectPts);
|
2024-08-31 13:44:19 -04:00
|
|
|
std::vector<TargetCorner> smallVec;
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
for (const auto& corner : tempCorners) {
|
2024-08-31 13:44:19 -04:00
|
|
|
smallVec.emplace_back(static_cast<double>(corner.first),
|
|
|
|
|
static_cast<double>(corner.second));
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
auto cornersFloat = OpenCVHelp::PointsToTargetCorners(noisyTargetCorners);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
std::vector<TargetCorner> cornersDouble{cornersFloat.begin(),
|
|
|
|
|
cornersFloat.end()};
|
2024-08-04 14:23:46 -04:00
|
|
|
detectableTgts.emplace_back(
|
|
|
|
|
-centerRot.Z().convert<units::degrees>().to<double>(),
|
|
|
|
|
-centerRot.Y().convert<units::degrees>().to<double>(), areaPercent,
|
|
|
|
|
centerRot.X().convert<units::degrees>().to<double>(), tgt.fiducialId,
|
2024-08-31 13:44:19 -04:00
|
|
|
tgt.objDetClassId, tgt.objDetConf,
|
|
|
|
|
pnpSim ? pnpSim->best : frc::Transform3d{},
|
|
|
|
|
pnpSim ? pnpSim->alt : frc::Transform3d{},
|
|
|
|
|
pnpSim ? pnpSim->ambiguity : -1, smallVec, cornersDouble);
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (videoSimRawEnabled) {
|
|
|
|
|
if (videoSimWireframeEnabled) {
|
|
|
|
|
VideoSimUtil::DrawFieldWireFrame(camRt, prop, videoSimWireframeResolution,
|
|
|
|
|
1.5, cv::Scalar{80}, 6, 1,
|
|
|
|
|
cv::Scalar{30}, videoSimFrameRaw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto& pair : visibleTgts) {
|
|
|
|
|
VisionTargetSim tgt = pair.first;
|
|
|
|
|
std::vector<cv::Point2f> corners = pair.second;
|
|
|
|
|
|
|
|
|
|
if (tgt.fiducialId > 0) {
|
|
|
|
|
VideoSimUtil::Warp165h5TagImage(tgt.fiducialId, corners, true,
|
|
|
|
|
videoSimFrameRaw);
|
|
|
|
|
} else if (!tgt.GetModel().GetIsSpherical()) {
|
|
|
|
|
std::vector<cv::Point2f> contour = corners;
|
|
|
|
|
if (!tgt.GetModel().GetIsPlanar()) {
|
|
|
|
|
contour = OpenCVHelp::GetConvexHull(contour);
|
|
|
|
|
}
|
|
|
|
|
VideoSimUtil::DrawPoly(contour, -1, cv::Scalar{255}, true,
|
|
|
|
|
videoSimFrameRaw);
|
|
|
|
|
} else {
|
|
|
|
|
VideoSimUtil::DrawInscribedEllipse(corners, cv::Scalar{255},
|
|
|
|
|
videoSimFrameRaw);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
videoSimRaw.PutFrame(videoSimFrameRaw);
|
|
|
|
|
} else {
|
|
|
|
|
videoSimRaw.SetConnectionStrategy(
|
|
|
|
|
cs::VideoSource::ConnectionStrategy::kConnectionForceClose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (videoSimProcEnabled) {
|
|
|
|
|
cv::cvtColor(videoSimFrameRaw, videoSimFrameProcessed, cv::COLOR_GRAY2BGR);
|
|
|
|
|
cv::drawMarker(
|
|
|
|
|
videoSimFrameProcessed,
|
|
|
|
|
cv::Point2d{prop.GetResWidth() / 2.0, prop.GetResHeight() / 2.0},
|
|
|
|
|
cv::Scalar{0, 255, 0}, cv::MARKER_CROSS,
|
|
|
|
|
static_cast<int>(
|
|
|
|
|
VideoSimUtil::GetScaledThickness(15, videoSimFrameProcessed)),
|
|
|
|
|
static_cast<int>(
|
|
|
|
|
VideoSimUtil::GetScaledThickness(1, videoSimFrameProcessed)),
|
|
|
|
|
cv::LINE_AA);
|
|
|
|
|
for (const auto& tgt : detectableTgts) {
|
|
|
|
|
auto detectedCornersDouble = tgt.GetDetectedCorners();
|
|
|
|
|
if (tgt.GetFiducialId() >= 0) {
|
|
|
|
|
VideoSimUtil::DrawTagDetection(
|
|
|
|
|
tgt.GetFiducialId(),
|
2024-08-31 13:44:19 -04:00
|
|
|
OpenCVHelp::CornersToPoints(detectedCornersDouble),
|
2024-08-04 14:23:46 -04:00
|
|
|
videoSimFrameProcessed);
|
|
|
|
|
} else {
|
|
|
|
|
cv::rectangle(videoSimFrameProcessed,
|
|
|
|
|
OpenCVHelp::GetBoundingRect(
|
2024-08-31 13:44:19 -04:00
|
|
|
OpenCVHelp::CornersToPoints(detectedCornersDouble)),
|
2024-08-04 14:23:46 -04:00
|
|
|
cv::Scalar{0, 0, 255},
|
|
|
|
|
static_cast<int>(VideoSimUtil::GetScaledThickness(
|
|
|
|
|
1, videoSimFrameProcessed)),
|
|
|
|
|
cv::LINE_AA);
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
auto smallVec = tgt.GetMinAreaRectCorners();
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
std::vector<std::pair<float, float>> cornersCopy{};
|
|
|
|
|
cornersCopy.reserve(4);
|
|
|
|
|
|
|
|
|
|
VideoSimUtil::DrawPoly(
|
2024-08-31 13:44:19 -04:00
|
|
|
OpenCVHelp::CornersToPoints(smallVec),
|
2024-08-04 14:23:46 -04:00
|
|
|
static_cast<int>(
|
|
|
|
|
VideoSimUtil::GetScaledThickness(1, videoSimFrameProcessed)),
|
|
|
|
|
cv::Scalar{255, 30, 30}, true, videoSimFrameProcessed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
videoSimProcessed.PutFrame(videoSimFrameProcessed);
|
|
|
|
|
} else {
|
|
|
|
|
videoSimProcessed.SetConnectionStrategy(
|
|
|
|
|
cs::VideoSource::ConnectionStrategy::kConnectionForceClose);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
std::optional<MultiTargetPNPResult> multiTagResults = std::nullopt;
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
std::vector<frc::AprilTag> visibleLayoutTags =
|
|
|
|
|
VisionEstimation::GetVisibleLayoutTags(detectableTgts, tagLayout);
|
|
|
|
|
if (visibleLayoutTags.size() > 1) {
|
2024-08-31 13:44:19 -04:00
|
|
|
std::vector<int16_t> usedIds{};
|
|
|
|
|
usedIds.resize(visibleLayoutTags.size());
|
2024-08-04 14:23:46 -04:00
|
|
|
std::transform(visibleLayoutTags.begin(), visibleLayoutTags.end(),
|
|
|
|
|
usedIds.begin(),
|
|
|
|
|
[](const frc::AprilTag& tag) { return tag.ID; });
|
|
|
|
|
std::sort(usedIds.begin(), usedIds.end());
|
2024-08-31 13:44:19 -04:00
|
|
|
auto pnpResult = VisionEstimation::EstimateCamPosePNP(
|
2024-08-04 14:23:46 -04:00
|
|
|
prop.GetIntrinsics(), prop.GetDistCoeffs(), detectableTgts, tagLayout,
|
|
|
|
|
kAprilTag36h11);
|
2024-08-31 13:44:19 -04:00
|
|
|
if (pnpResult) {
|
|
|
|
|
multiTagResults = MultiTargetPNPResult{*pnpResult, usedIds};
|
|
|
|
|
}
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
heartbeatCounter++;
|
2024-08-31 13:44:19 -04:00
|
|
|
return PhotonPipelineResult{
|
|
|
|
|
PhotonPipelineMetadata{heartbeatCounter, 0,
|
2024-10-31 08:27:19 -07:00
|
|
|
units::microsecond_t{latency}.to<int64_t>(),
|
|
|
|
|
1000000},
|
2024-08-31 13:44:19 -04:00
|
|
|
detectableTgts, multiTagResults};
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
void PhotonCameraSim::SubmitProcessedFrame(const PhotonPipelineResult& result) {
|
|
|
|
|
SubmitProcessedFrame(result, wpi::Now());
|
|
|
|
|
}
|
|
|
|
|
void PhotonCameraSim::SubmitProcessedFrame(const PhotonPipelineResult& result,
|
2024-08-31 13:44:19 -04:00
|
|
|
uint64_t ReceiveTimestamp) {
|
2024-08-04 14:23:46 -04:00
|
|
|
ts.latencyMillisEntry.Set(
|
|
|
|
|
result.GetLatency().convert<units::milliseconds>().to<double>(),
|
2024-08-31 13:44:19 -04:00
|
|
|
ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
Packet newPacket{};
|
2024-08-31 13:44:19 -04:00
|
|
|
newPacket.Pack(result);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.rawBytesEntry.Set(newPacket.GetData(), ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
bool hasTargets = result.HasTargets();
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.hasTargetEntry.Set(hasTargets, ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
if (!hasTargets) {
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.targetPitchEntry.Set(0.0, ReceiveTimestamp);
|
|
|
|
|
ts.targetYawEntry.Set(0.0, ReceiveTimestamp);
|
|
|
|
|
ts.targetAreaEntry.Set(0.0, ReceiveTimestamp);
|
2024-10-20 22:21:24 -07:00
|
|
|
ts.targetPoseEntry.Set(frc::Transform3d{}, ReceiveTimestamp);
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.targetSkewEntry.Set(0.0, ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
} else {
|
|
|
|
|
PhotonTrackedTarget bestTarget = result.GetBestTarget();
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.targetPitchEntry.Set(bestTarget.GetPitch(), ReceiveTimestamp);
|
|
|
|
|
ts.targetYawEntry.Set(bestTarget.GetYaw(), ReceiveTimestamp);
|
|
|
|
|
ts.targetAreaEntry.Set(bestTarget.GetArea(), ReceiveTimestamp);
|
|
|
|
|
ts.targetSkewEntry.Set(bestTarget.GetSkew(), ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
2024-10-20 22:21:24 -07:00
|
|
|
ts.targetPoseEntry.Set(bestTarget.GetBestCameraToTarget(),
|
|
|
|
|
ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
Eigen::Matrix<double, 3, 3, Eigen::RowMajor> intrinsics =
|
|
|
|
|
prop.GetIntrinsics();
|
|
|
|
|
std::span<double> intrinsicsView{intrinsics.data(),
|
|
|
|
|
intrinsics.data() + intrinsics.size()};
|
|
|
|
|
ts.cameraIntrinsicsPublisher.Set(intrinsicsView, ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
auto distortion = prop.GetDistCoeffs();
|
|
|
|
|
std::vector<double> distortionView{distortion.data(),
|
|
|
|
|
distortion.data() + distortion.size()};
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.cameraDistortionPublisher.Set(distortionView, ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
ts.heartbeatPublisher.Set(heartbeatCounter, ReceiveTimestamp);
|
2024-08-04 14:23:46 -04:00
|
|
|
|
|
|
|
|
ts.subTable->GetInstance().Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace photon
|