mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-19 00:41:41 +00:00
Add Photonlib (#231)
Merges Photonlib into Photonvision, along with the Photonlib code examples. Also creates a new PhotonTargeting library teams can depend on.
This commit is contained in:
90
photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp
Normal file
90
photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/PhotonCamera.h"
|
||||
|
||||
#include "photonlib/Packet.h"
|
||||
|
||||
namespace photonlib {
|
||||
PhotonCamera::PhotonCamera(std::shared_ptr<nt::NetworkTable> rootTable)
|
||||
: rawBytesEntry(rootTable->GetEntry("rawBytes")),
|
||||
driverModeEntry(rootTable->GetEntry("driverMode")),
|
||||
inputSaveImgEntry(rootTable->GetEntry("inputSaveImgCmd")),
|
||||
outputSaveImgEntry(rootTable->GetEntry("outputSaveImgCmd")),
|
||||
pipelineIndexEntry(rootTable->GetEntry("pipelineIndex")),
|
||||
ledModeEntry(mainTable->GetEntry("ledMode")) {
|
||||
driverMode = driverModeEntry.GetBoolean(false);
|
||||
pipelineIndex = static_cast<int>(pipelineIndexEntry.GetDouble(0.0));
|
||||
mode = GetLEDMode();
|
||||
}
|
||||
|
||||
PhotonCamera::PhotonCamera(const std::string& cameraName)
|
||||
: PhotonCamera(nt::NetworkTableInstance::GetDefault()
|
||||
.GetTable("photonvision")
|
||||
->GetSubTable(cameraName)) {}
|
||||
|
||||
PhotonPipelineResult PhotonCamera::GetLatestResult() const {
|
||||
// Clear the current packet.
|
||||
packet.Clear();
|
||||
|
||||
// Create the new result;
|
||||
PhotonPipelineResult result;
|
||||
|
||||
// Fill the packet with latest data and populate result.
|
||||
std::string value = rawBytesEntry.GetValue()->GetRaw();
|
||||
std::vector<char> bytes{value.begin(), value.end()};
|
||||
|
||||
photonlib::Packet packet{bytes};
|
||||
|
||||
packet >> result;
|
||||
return result;
|
||||
}
|
||||
|
||||
void PhotonCamera::SetDriverMode(bool driverMode) {
|
||||
if (this->driverMode != driverMode) {
|
||||
this->driverMode = driverMode;
|
||||
driverModeEntry.SetBoolean(this->driverMode);
|
||||
}
|
||||
}
|
||||
|
||||
void PhotonCamera::TakeInputSnapshot() { inputSaveImgEntry.SetBoolean(true); }
|
||||
|
||||
void PhotonCamera::TakeOutputSnapshot() { outputSaveImgEntry.SetBoolean(true); }
|
||||
|
||||
bool PhotonCamera::GetDriverMode() const { return driverMode; }
|
||||
|
||||
void PhotonCamera::SetPipelineIndex(int index) {
|
||||
if (index != pipelineIndex) {
|
||||
pipelineIndex = index;
|
||||
pipelineIndexEntry.SetDouble(static_cast<double>(pipelineIndex));
|
||||
}
|
||||
}
|
||||
|
||||
int PhotonCamera::GetPipelineIndex() const { return pipelineIndex; }
|
||||
|
||||
LEDMode PhotonCamera::GetLEDMode() const {
|
||||
mode = static_cast<LEDMode>(static_cast<int>(ledModeEntry.GetDouble(-1.0)));
|
||||
return mode;
|
||||
}
|
||||
|
||||
void PhotonCamera::SetLEDMode(LEDMode led) {
|
||||
if (led != mode) {
|
||||
mode = led;
|
||||
ledModeEntry.SetDouble(static_cast<double>(static_cast<int>(mode)));
|
||||
}
|
||||
}
|
||||
} // namespace photonlib
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/PhotonPipelineResult.h"
|
||||
|
||||
namespace photonlib {
|
||||
PhotonPipelineResult::PhotonPipelineResult(
|
||||
units::second_t latency, wpi::ArrayRef<PhotonTrackedTarget> targets)
|
||||
: latency(latency),
|
||||
targets(targets.data(), targets.data() + targets.size()) {
|
||||
hasTargets = targets.size() != 0;
|
||||
}
|
||||
|
||||
bool PhotonPipelineResult::operator==(const PhotonPipelineResult& other) const {
|
||||
return latency == other.latency && hasTargets == other.hasTargets &&
|
||||
targets == other.targets;
|
||||
}
|
||||
|
||||
bool PhotonPipelineResult::operator!=(const PhotonPipelineResult& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
Packet& operator<<(Packet& packet, const PhotonPipelineResult& result) {
|
||||
// Encode latency, existence of targets, and number of targets.
|
||||
packet << result.latency.to<double>() * 1000 << result.hasTargets
|
||||
<< 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.
|
||||
int8_t targetCount = 0;
|
||||
double latencyMillis = 0;
|
||||
packet >> latencyMillis >> result.hasTargets >> 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
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/PhotonTrackedTarget.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <frc/geometry/Translation2d.h>
|
||||
|
||||
namespace photonlib {
|
||||
|
||||
PhotonTrackedTarget::PhotonTrackedTarget(double yaw, double pitch, double area,
|
||||
double skew,
|
||||
const frc::Transform2d& pose)
|
||||
: yaw(yaw), pitch(pitch), area(area), skew(skew), cameraToTarget(pose) {}
|
||||
|
||||
bool PhotonTrackedTarget::operator==(const PhotonTrackedTarget& other) const {
|
||||
return other.yaw == yaw && other.pitch == pitch && other.area == area &&
|
||||
other.skew == skew && other.cameraToTarget == cameraToTarget;
|
||||
}
|
||||
|
||||
bool PhotonTrackedTarget::operator!=(const PhotonTrackedTarget& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
Packet& operator<<(Packet& packet, const PhotonTrackedTarget& target) {
|
||||
return packet << target.yaw << target.pitch << target.area << target.skew
|
||||
<< target.cameraToTarget.Translation().X().to<double>()
|
||||
<< target.cameraToTarget.Translation().Y().to<double>()
|
||||
<< target.cameraToTarget.Rotation().Degrees().to<double>();
|
||||
}
|
||||
|
||||
Packet& operator>>(Packet& packet, PhotonTrackedTarget& target) {
|
||||
packet >> target.yaw >> target.pitch >> target.area >> target.skew;
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
double rot = 0;
|
||||
packet >> x >> y >> rot;
|
||||
|
||||
target.cameraToTarget =
|
||||
frc::Transform2d(frc::Translation2d(units::meter_t(x), units::meter_t(y)),
|
||||
units::degree_t(rot));
|
||||
return packet;
|
||||
}
|
||||
|
||||
} // namespace photonlib
|
||||
42
photon-lib/src/main/native/cpp/photonlib/SimPhotonCamera.cpp
Normal file
42
photon-lib/src/main/native/cpp/photonlib/SimPhotonCamera.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/SimPhotonCamera.h"
|
||||
|
||||
namespace photonlib {
|
||||
|
||||
SimPhotonCamera::SimPhotonCamera(std::shared_ptr<nt::NetworkTable> rootTable)
|
||||
: PhotonCamera(rootTable) {}
|
||||
|
||||
SimPhotonCamera::SimPhotonCamera(const std::string& cameraName)
|
||||
: PhotonCamera(cameraName) {}
|
||||
|
||||
void SimPhotonCamera::SubmitProcessedFrame(
|
||||
units::second_t latency, wpi::ArrayRef<PhotonTrackedTarget> tgtList) {
|
||||
if (!GetDriverMode()) {
|
||||
// Clear the current packet.
|
||||
simPacket.Clear();
|
||||
|
||||
// Create the new result and pump it into the packet
|
||||
simPacket << PhotonPipelineResult(latency, tgtList);
|
||||
|
||||
rawBytesEntry.SetRaw(
|
||||
wpi::StringRef(simPacket.GetData().data(), simPacket.GetData().size()));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace photonlib
|
||||
119
photon-lib/src/main/native/cpp/photonlib/SimVisionSystem.cpp
Normal file
119
photon-lib/src/main/native/cpp/photonlib/SimVisionSystem.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/SimVisionSystem.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <units/angle.h>
|
||||
#include <units/length.h>
|
||||
|
||||
namespace photonlib {
|
||||
|
||||
SimVisionSystem::SimVisionSystem(const std::string& name,
|
||||
units::degree_t camDiagFOV,
|
||||
units::degree_t camPitch,
|
||||
frc::Transform2d cameraToRobot,
|
||||
units::meter_t cameraHeightOffGround,
|
||||
units::meter_t maxLEDRange, int cameraResWidth,
|
||||
int cameraResHeight, double minTargetArea)
|
||||
: camDiagFOV(camDiagFOV),
|
||||
camPitch(camPitch),
|
||||
cameraToRobot(cameraToRobot),
|
||||
cameraHeightOffGround(cameraHeightOffGround),
|
||||
maxLEDRange(maxLEDRange),
|
||||
cameraResWidth(cameraResWidth),
|
||||
cameraResHeight(cameraResHeight),
|
||||
minTargetArea(minTargetArea) {
|
||||
double hypotPixels = std::hypot(cameraResWidth, cameraResHeight);
|
||||
camHorizFOV = camDiagFOV * cameraResWidth / hypotPixels;
|
||||
camVertFOV = camDiagFOV * cameraResHeight / hypotPixels;
|
||||
|
||||
cam = SimPhotonCamera(name);
|
||||
tgtList.clear();
|
||||
}
|
||||
|
||||
void SimVisionSystem::AddSimVisionTarget(SimVisionTarget tgt) {
|
||||
tgtList.push_back(tgt);
|
||||
}
|
||||
|
||||
void SimVisionSystem::MoveCamera(frc::Transform2d newCameraToRobot,
|
||||
units::meter_t newCamHeight,
|
||||
units::degree_t newCamPitch) {
|
||||
cameraToRobot = newCameraToRobot;
|
||||
cameraHeightOffGround = newCamHeight;
|
||||
camPitch = newCamPitch;
|
||||
}
|
||||
|
||||
void SimVisionSystem::ProcessFrame(frc::Pose2d robotPose) {
|
||||
frc::Pose2d cameraPos = robotPose.TransformBy(cameraToRobot.Inverse());
|
||||
std::vector<PhotonTrackedTarget> visibleTgtList = {};
|
||||
|
||||
for (auto&& tgt : tgtList) {
|
||||
frc::Transform2d camToTargetTrans =
|
||||
frc::Transform2d(cameraPos, tgt.targetPos);
|
||||
|
||||
units::meter_t distAlongGround = camToTargetTrans.Translation().Norm();
|
||||
units::meter_t distVertical =
|
||||
tgt.targetHeightAboveGround - cameraHeightOffGround;
|
||||
units::meter_t distHypot =
|
||||
units::math::hypot(distAlongGround, distVertical);
|
||||
|
||||
double area = tgt.tgtArea.to<double>() / GetM2PerPx(distAlongGround);
|
||||
|
||||
// 2D yaw mode considers the target as a point, and should ignore target
|
||||
// rotation.
|
||||
// Photon reports it in the correct robot reference frame.
|
||||
// IE: targets to the left of the image should report negative yaw.
|
||||
units::degree_t yawAngle =
|
||||
-1.0 * units::math::atan2(camToTargetTrans.Translation().Y(),
|
||||
camToTargetTrans.Translation().X());
|
||||
units::degree_t pitchAngle =
|
||||
units::math::atan2(distVertical, distAlongGround) - camPitch;
|
||||
|
||||
if (CamCanSeeTarget(distHypot, yawAngle, pitchAngle, area)) {
|
||||
PhotonTrackedTarget newTgt =
|
||||
PhotonTrackedTarget(yawAngle.to<double>(), pitchAngle.to<double>(),
|
||||
area, 0.0, camToTargetTrans);
|
||||
visibleTgtList.push_back(newTgt);
|
||||
}
|
||||
}
|
||||
|
||||
units::second_t procDelay(0.0); // Future - tie this to something meaningful
|
||||
cam.SubmitProcessedFrame(
|
||||
procDelay, wpi::MutableArrayRef<PhotonTrackedTarget>(visibleTgtList));
|
||||
}
|
||||
|
||||
double SimVisionSystem::GetM2PerPx(units::meter_t dist) {
|
||||
double heightMPerPx = 2 * dist.to<double>() *
|
||||
units::math::tan(camVertFOV / 2) / cameraResHeight;
|
||||
double widthMPerPx = 2 * dist.to<double>() *
|
||||
units::math::tan(camHorizFOV / 2) / cameraResWidth;
|
||||
return widthMPerPx * heightMPerPx;
|
||||
}
|
||||
|
||||
bool SimVisionSystem::CamCanSeeTarget(units::meter_t distHypot,
|
||||
units::degree_t yaw,
|
||||
units::degree_t pitch, double area) {
|
||||
bool inRange = (distHypot < maxLEDRange);
|
||||
bool inHorizAngle = units::math::abs(yaw) < (camHorizFOV / 2);
|
||||
bool inVertAngle = units::math::abs(pitch) < (camVertFOV / 2);
|
||||
bool targetBigEnough = area > minTargetArea;
|
||||
return (inRange && inHorizAngle && inVertAngle && targetBigEnough);
|
||||
}
|
||||
|
||||
} // namespace photonlib
|
||||
33
photon-lib/src/main/native/cpp/photonlib/SimVisionTarget.cpp
Normal file
33
photon-lib/src/main/native/cpp/photonlib/SimVisionTarget.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) Photon Vision.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "photonlib/SimVisionTarget.h"
|
||||
|
||||
namespace photonlib {
|
||||
|
||||
SimVisionTarget::SimVisionTarget(frc::Pose2d& targetPos,
|
||||
units::meter_t targetHeightAboveGround,
|
||||
units::meter_t targetWidth,
|
||||
units::meter_t targetHeight)
|
||||
: targetPos(targetPos),
|
||||
targetHeightAboveGround(targetHeightAboveGround),
|
||||
targetWidth(targetWidth),
|
||||
targetHeight(targetHeight) {
|
||||
tgtArea = targetWidth * targetHeight;
|
||||
}
|
||||
|
||||
} // namespace photonlib
|
||||
Reference in New Issue
Block a user