2021-01-16 20:41:47 -08:00
|
|
|
/*
|
2023-11-19 15:16:22 -05:00
|
|
|
* Copyright (C) Photon Vision.
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2023-11-19 15:16:22 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2023-11-19 15:16:22 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2023-11-19 15:16:22 -05:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-01-16 20:41:47 -08:00
|
|
|
*/
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/targeting/PhotonTrackedTarget.h"
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2024-08-02 11:57:34 -04:00
|
|
|
#include <algorithm>
|
2021-01-16 20:41:47 -08:00
|
|
|
#include <iostream>
|
2022-01-10 20:31:36 -08:00
|
|
|
#include <utility>
|
2024-08-02 11:57:34 -04:00
|
|
|
#include <vector>
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
#include <frc/geometry/Translation2d.h>
|
2022-01-10 20:31:36 -08:00
|
|
|
#include <wpi/SmallVector.h>
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2023-01-06 19:20:27 -08:00
|
|
|
static constexpr const uint8_t MAX_CORNERS = 8;
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
namespace photon {
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-01-10 20:31:36 -08:00
|
|
|
PhotonTrackedTarget::PhotonTrackedTarget(
|
2024-05-10 14:52:16 -04:00
|
|
|
double yaw, double pitch, double area, double skew, int id, int objdetid,
|
|
|
|
|
float objdetconf, const frc::Transform3d& pose,
|
|
|
|
|
const frc::Transform3d& alternatePose, double ambiguity,
|
2023-01-14 09:06:15 -06:00
|
|
|
const wpi::SmallVector<std::pair<double, double>, 4> minAreaRectCorners,
|
|
|
|
|
const std::vector<std::pair<double, double>> detectedCorners)
|
2022-01-10 20:31:36 -08:00
|
|
|
: yaw(yaw),
|
|
|
|
|
pitch(pitch),
|
|
|
|
|
area(area),
|
|
|
|
|
skew(skew),
|
2022-09-28 18:21:41 -07:00
|
|
|
fiducialId(id),
|
2024-05-10 14:52:16 -04:00
|
|
|
objDetectId(objdetid),
|
|
|
|
|
objDetectConf(objdetconf),
|
2022-10-22 07:42:45 -04:00
|
|
|
bestCameraToTarget(pose),
|
2022-12-30 00:40:13 -06:00
|
|
|
altCameraToTarget(alternatePose),
|
|
|
|
|
poseAmbiguity(ambiguity),
|
2023-01-14 09:06:15 -06:00
|
|
|
minAreaRectCorners(minAreaRectCorners),
|
|
|
|
|
detectedCorners(detectedCorners) {}
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
bool PhotonTrackedTarget::operator==(const PhotonTrackedTarget& other) const {
|
|
|
|
|
return other.yaw == yaw && other.pitch == pitch && other.area == area &&
|
2022-10-22 07:42:45 -04:00
|
|
|
other.skew == skew && other.bestCameraToTarget == bestCameraToTarget &&
|
2024-05-10 14:52:16 -04:00
|
|
|
other.objDetectConf == objDetectConf &&
|
|
|
|
|
other.objDetectId == objDetectId &&
|
2023-01-06 19:20:27 -08:00
|
|
|
other.minAreaRectCorners == minAreaRectCorners;
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Packet& operator<<(Packet& packet, const PhotonTrackedTarget& target) {
|
2022-01-10 20:31:36 -08:00
|
|
|
packet << target.yaw << target.pitch << target.area << target.skew
|
2024-05-10 14:52:16 -04:00
|
|
|
<< target.fiducialId << target.objDetectId << target.objDetectConf
|
2022-10-22 07:42:45 -04:00
|
|
|
<< 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()
|
2022-10-08 09:27:00 -04:00
|
|
|
<< target.poseAmbiguity;
|
2022-01-10 20:31:36 -08:00
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2023-01-06 19:20:27 -08:00
|
|
|
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;
|
2022-01-10 20:31:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packet;
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Packet& operator>>(Packet& packet, PhotonTrackedTarget& target) {
|
2022-09-28 18:21:41 -07:00
|
|
|
packet >> target.yaw >> target.pitch >> target.area >> target.skew >>
|
2024-05-10 14:52:16 -04:00
|
|
|
target.fiducialId >> target.objDetectId >> target.objDetectConf;
|
2022-10-22 07:42:45 -04:00
|
|
|
|
|
|
|
|
// We use these for best and alt transforms below
|
2021-01-16 20:41:47 -08:00
|
|
|
double x = 0;
|
|
|
|
|
double y = 0;
|
2022-09-28 18:21:41 -07:00
|
|
|
double z = 0;
|
|
|
|
|
double w = 0;
|
2022-10-22 07:42:45 -04:00
|
|
|
|
|
|
|
|
// First transform is the "best" pose
|
2022-09-28 18:21:41 -07:00
|
|
|
packet >> x >> y >> z;
|
2022-10-22 07:42:45 -04:00
|
|
|
const auto bestTranslation = frc::Translation3d(
|
2022-09-28 18:21:41 -07:00
|
|
|
units::meter_t(x), units::meter_t(y), units::meter_t(z));
|
|
|
|
|
packet >> w >> x >> y >> z;
|
2022-10-22 07:42:45 -04:00
|
|
|
const auto bestRotation = frc::Rotation3d(frc::Quaternion(w, x, y, z));
|
|
|
|
|
target.bestCameraToTarget = frc::Transform3d(bestTranslation, bestRotation);
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-10-22 07:42:45 -04:00
|
|
|
// 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);
|
2022-01-10 20:31:36 -08:00
|
|
|
|
2022-10-08 09:27:00 -04:00
|
|
|
packet >> target.poseAmbiguity;
|
|
|
|
|
|
2023-01-06 19:20:27 -08:00
|
|
|
target.minAreaRectCorners.clear();
|
|
|
|
|
double first = 0;
|
|
|
|
|
double second = 0;
|
2022-01-10 20:31:36 -08:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
packet >> first >> second;
|
2023-01-06 19:20:27 -08:00
|
|
|
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);
|
2022-01-10 20:31:36 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
return packet;
|
|
|
|
|
}
|
2023-11-19 15:16:22 -05:00
|
|
|
} // namespace photon
|