Skip to content

Commit

Permalink
Nuke packet
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Nov 16, 2023
1 parent aa8f0f5 commit fb192f5
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 498 deletions.
113 changes: 0 additions & 113 deletions photon-lib/src/main/native/cpp/photonlib/MultiTargetPNPResult.cpp

This file was deleted.

4 changes: 0 additions & 4 deletions photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <opencv2/core/mat.hpp>

#include "PhotonVersion.h"
#include "photonlib/Packet.h"

namespace photonlib {

Expand Down Expand Up @@ -82,9 +81,6 @@ PhotonPipelineResult PhotonCamera::GetLatestResult() {
// Prints warning if not connected
VerifyVersion();

// Clear the current packet.
packet.Clear();

// Create the new result;
PhotonPipelineResult result = pipelineResultsSubscriber.Get();

Expand Down
30 changes: 0 additions & 30 deletions photon-lib/src/main/native/cpp/photonlib/PhotonPipelineResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,6 @@ bool PhotonPipelineResult::operator!=(const PhotonPipelineResult& other) const {
return !operator==(other);
}

Packet& operator<<(Packet& packet, const PhotonPipelineResult& result) {
// Encode latency and number of targets.
packet << result.latency.value() * 1000 << result.m_pnpResults
<< static_cast<int8_t>(result.targets.size());

// Encode the information of each target.
for (auto& target : result.targets) packet << target;

// Return the packet
return packet;
}

Packet& operator>>(Packet& packet, PhotonPipelineResult& result) {
// Decode latency, existence of targets, and number of targets.
double latencyMillis = 0;
int8_t targetCount = 0;
packet >> latencyMillis >> result.m_pnpResults >> targetCount;
result.latency = units::second_t(latencyMillis / 1000.0);

result.targets.clear();

// Decode the information of each target.
for (int i = 0; i < targetCount; ++i) {
PhotonTrackedTarget target;
packet >> target;
result.targets.push_back(target);
}
return packet;
}

} // namespace photonlib

google::protobuf::Message* wpi::Protobuf<photonlib::PhotonPipelineResult>::New(
Expand Down
100 changes: 12 additions & 88 deletions photon-lib/src/main/native/cpp/photonlib/PhotonTrackedTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,89 +63,6 @@ bool PhotonTrackedTarget::operator!=(const PhotonTrackedTarget& other) const {
return !operator==(other);
}

Packet& operator<<(Packet& packet, const PhotonTrackedTarget& target) {
packet << target.yaw << target.pitch << target.area << target.skew
<< target.fiducialId
<< target.bestCameraToTarget.Translation().X().value()
<< target.bestCameraToTarget.Translation().Y().value()
<< target.bestCameraToTarget.Translation().Z().value()
<< target.bestCameraToTarget.Rotation().GetQuaternion().W()
<< target.bestCameraToTarget.Rotation().GetQuaternion().X()
<< target.bestCameraToTarget.Rotation().GetQuaternion().Y()
<< target.bestCameraToTarget.Rotation().GetQuaternion().Z()
<< target.altCameraToTarget.Translation().X().value()
<< target.altCameraToTarget.Translation().Y().value()
<< target.altCameraToTarget.Translation().Z().value()
<< target.altCameraToTarget.Rotation().GetQuaternion().W()
<< target.altCameraToTarget.Rotation().GetQuaternion().X()
<< target.altCameraToTarget.Rotation().GetQuaternion().Y()
<< target.altCameraToTarget.Rotation().GetQuaternion().Z()
<< target.poseAmbiguity;

for (int i = 0; i < 4; i++) {
packet << target.minAreaRectCorners[i].first
<< target.minAreaRectCorners[i].second;
}

uint8_t num_corners =
std::min<uint8_t>(target.detectedCorners.size(), MAX_CORNERS);
packet << num_corners;
for (size_t i = 0; i < target.detectedCorners.size(); i++) {
packet << target.detectedCorners[i].first
<< target.detectedCorners[i].second;
}

return packet;
}

Packet& operator>>(Packet& packet, PhotonTrackedTarget& target) {
packet >> target.yaw >> target.pitch >> target.area >> target.skew >>
target.fiducialId;

// We use these for best and alt transforms below
double x = 0;
double y = 0;
double z = 0;
double w = 0;

// First transform is the "best" pose
packet >> x >> y >> z;
const auto bestTranslation = frc::Translation3d(
units::meter_t(x), units::meter_t(y), units::meter_t(z));
packet >> w >> x >> y >> z;
const auto bestRotation = frc::Rotation3d(frc::Quaternion(w, x, y, z));
target.bestCameraToTarget = frc::Transform3d(bestTranslation, bestRotation);

// Second transform is the "alternate" pose
packet >> x >> y >> z;
const auto altTranslation = frc::Translation3d(
units::meter_t(x), units::meter_t(y), units::meter_t(z));
packet >> w >> x >> y >> z;
const auto altRotation = frc::Rotation3d(frc::Quaternion(w, x, y, z));
target.altCameraToTarget = frc::Transform3d(altTranslation, altRotation);

packet >> target.poseAmbiguity;

target.minAreaRectCorners.clear();
double first = 0;
double second = 0;
for (int i = 0; i < 4; i++) {
packet >> first >> second;
target.minAreaRectCorners.emplace_back(first, second);
}

uint8_t numCorners = 0;
packet >> numCorners;
target.detectedCorners.clear();
target.detectedCorners.reserve(numCorners);
for (size_t i = 0; i < numCorners; i++) {
packet >> first >> second;
target.detectedCorners.emplace_back(first, second);
}

return packet;
}

} // namespace photonlib

google::protobuf::Message* wpi::Protobuf<photonlib::PhotonTrackedTarget>::New(
Expand All @@ -162,15 +79,22 @@ wpi::Protobuf<photonlib::PhotonTrackedTarget>::Unpack(

auto m = static_cast<const ProtobufPhotonTrackedTarget*>(&msg);

wpi::SmallVector<std::pair<double, double>, 4> minAreaRectCorners;
for (const auto& t : m->minarearectcorners()) {
minAreaRectCorners.emplace_back(t.x(), t.y());
}

std::vector<std::pair<double, double>> detectedCorners;
detectedCorners.reserve(m->detectedcorners_size());
for (const auto& t : m->detectedcorners()) {
detectedCorners.emplace_back(t.x(), t.y());
}

return photonlib::PhotonTrackedTarget(
m->yaw(), m->pitch(), m->area(), m->skew(), m->fiducialid(),
wpi::UnpackProtobuf<frc::Transform3d>(m->bestcameratotarget()),
wpi::UnpackProtobuf<frc::Transform3d>(m->altcameratotarget()),
m->poseambiguity(),
// corners
{},
// detected corners
{});
m->poseambiguity(), minAreaRectCorners, detectedCorners);
}

void wpi::Protobuf<photonlib::PhotonTrackedTarget>::Pack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <frc/geometry/Transform3d.h>
#include <wpi/SmallVector.h>

#include "photonlib/Packet.h"

namespace photonlib {

class PNPResults {
Expand All @@ -44,18 +42,12 @@ class PNPResults {
double altReprojectionErr;

double ambiguity;

friend Packet& operator<<(Packet& packet, const PNPResults& result);
friend Packet& operator>>(Packet& packet, PNPResults& result);
};

class MultiTargetPnpResult {
public:
PNPResults result;
wpi::SmallVector<int16_t, 32> fiducialIdsUsed;

friend Packet& operator<<(Packet& packet, const MultiTargetPnpResult& result);
friend Packet& operator>>(Packet& packet, MultiTargetPnpResult& result);
};

} // namespace photonlib
Loading

0 comments on commit fb192f5

Please sign in to comment.