1#ifndef LIMELIGHTHELPERS_H
2#define LIMELIGHTHELPERS_H
8#include <wpinet/PortForwarder.h>
12#include "networktables/NetworkTable.h"
13#include "networktables/NetworkTableEntry.h"
14#include "networktables/NetworkTableInstance.h"
15#include "networktables/NetworkTableValue.h"
19#include <frc/geometry/Pose2d.h>
20#include <frc/geometry/Pose3d.h>
21#include <frc/geometry/Rotation2d.h>
22#include <frc/geometry/Rotation3d.h>
23#include <frc/geometry/Translation2d.h>
24#include <frc/geometry/Translation3d.h>
25#include <units/time.h>
34#define M_PI 3.14159265358979323846
45inline frc::Pose3d
toPose3D(
const std::vector<double> &inData) {
46 if (inData.size() < 6) {
50 frc::Translation3d(units::length::meter_t(inData[0]),
51 units::length::meter_t(inData[1]),
52 units::length::meter_t(inData[2])),
53 frc::Rotation3d(units::angle::radian_t(inData[3] * (
M_PI / 180.0)),
54 units::angle::radian_t(inData[4] * (
M_PI / 180.0)),
55 units::angle::radian_t(inData[5] * (
M_PI / 180.0))));
58inline frc::Pose2d
toPose2D(
const std::vector<double> &inData) {
59 if (inData.size() < 6) {
63 frc::Translation2d(units::length::meter_t(inData[0]),
64 units::length::meter_t(inData[1])),
65 frc::Rotation2d(units::angle::radian_t(inData[5] * (
M_PI / 180.0))));
68inline std::shared_ptr<nt::NetworkTable>
70 return nt::NetworkTableInstance::GetDefault().GetTable(
74inline nt::NetworkTableEntry
76 const std::string &entryName) {
81 const std::string &entryName) {
85inline std::vector<double>
87 const std::string &entryName) {
89 .GetDoubleArray(std::span<double>{});
93 const std::string &entryName) {
98 const std::string entryName,
double val) {
103 const std::string &entryName,
104 const std::span<const double> &vals) {
108inline double getTX(
const std::string &limelightName =
"") {
112inline double getTV(
const std::string &limelightName =
"") {
116inline double getTY(
const std::string &limelightName =
"") {
120inline double getTA(
const std::string &limelightName =
"") {
132inline std::string
getJSONDump(
const std::string &limelightName =
"") {
136inline std::vector<double>
getBotpose(
const std::string &limelightName =
"") {
140inline std::vector<double>
145inline std::vector<double>
150inline std::vector<double>
155inline std::vector<double>
160inline std::vector<double>
165inline std::vector<double>
170inline std::vector<double>
175inline std::vector<double>
232 double cropXMax,
double cropYMin,
double cropYMax) {
233 double cropWindow[4]{cropXMin, cropXMax, cropYMin, cropYMax};
241 double yawRate,
double pitch,
double pitchRate,
242 double roll,
double rollRate) {
243 std::vector<double> entries = {yaw, yawRate, pitch,
244 pitchRate, roll, rollRate};
249 const std::vector<int> &validIDs) {
250 std::vector<double> validIDsDouble(validIDs.begin(), validIDs.end());
262 double forward,
double side,
double up,
263 double roll,
double pitch,
double yaw) {
264 double entries[6] = {forward, side, up, roll, pitch, yaw};
270 const std::vector<double> &outgoingPythonData) {
272 limelightName,
"llrobot",
273 std::span{outgoingPythonData.begin(), outgoingPythonData.size()});
276inline std::vector<double>
291 if (inData.size() <
static_cast<size_t>(position + 1)) {
294 return inData[position];
335 const std::string &entryName) {
336 nt::NetworkTableEntry poseEntry =
338 std::vector<double> poseArray = poseEntry.GetDoubleArray(std::span<double>{});
339 frc::Pose2d pose =
toPose2D(poseArray);
348 units::second_t timestamp = units::time::second_t(
349 (poseEntry.GetLastChange() / 1000000.0) - (latency / 1000.0));
351 std::vector<RawFiducial> rawFiducials;
352 int valsPerFiducial = 7;
353 int expectedTotalVals = 11 + valsPerFiducial * tagCount;
355 if (poseArray.size() == expectedTotalVals) {
356 for (
int i = 0; i < tagCount; i++) {
357 int baseIndex = 11 + (i * valsPerFiducial);
365 rawFiducials.emplace_back(
id, txnc, tync, ta, distToCamera, distToRobot,
370 return PoseEstimate(pose, timestamp, latency, tagCount, tagSpan, tagDist,
371 tagArea, rawFiducials);
545 auto &portForwarder = wpi::PortForwarder::GetInstance();
546 portForwarder.Add(5800,
sanitizeName(limelightName), 5800);
547 portForwarder.Add(5801,
sanitizeName(limelightName), 5801);
548 portForwarder.Add(5802,
sanitizeName(limelightName), 5802);
549 portForwarder.Add(5803,
sanitizeName(limelightName), 5803);
550 portForwarder.Add(5804,
sanitizeName(limelightName), 5804);
551 portForwarder.Add(5805,
sanitizeName(limelightName), 5805);
552 portForwarder.Add(5806,
sanitizeName(limelightName), 5806);
553 portForwarder.Add(5807,
sanitizeName(limelightName), 5807);
554 portForwarder.Add(5808,
sanitizeName(limelightName), 5808);
555 portForwarder.Add(5809,
sanitizeName(limelightName), 5809);
558template <
typename T,
typename KeyType>
560 const T &defaultValue) {
562 return jsonData.at(key).template get<T>();
563 }
catch (wpi::json::exception &e) {
570 std::vector<double> defaultValueVector(6, 0.0);
600 std::vector<double> defaultValueVector(6, 0.0);
676 std::vector<double> defaultVector{};
685 j,
"Retro", std::vector<RetroreflectiveResultClass>{});
687 j,
"Fiducial", std::vector<FiducialResultClass>{});
689 j,
"Detector", std::vector<DetectionResultClass>{});
692 j,
"Detector", std::vector<ClassificationResultClass>{});
700inline LimelightResultsClass
702 auto start = std::chrono::high_resolution_clock::now();
703 std::string jsonString =
getJSONDump(limelightName);
706 data = wpi::json::parse(jsonString);
707 }
catch (
const std::exception &e) {
711 auto end = std::chrono::high_resolution_clock::now();
713 std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
714 double millis = (nanos * 0.000001);
720 std::cout <<
"lljson: " << millis << std::endl;
728inline std::optional<std::vector<double>>
732 if (entry.size() < 8) {
ClassificationResultClass()
~ClassificationResultClass()
VisionResultsClass targetingResults
std::vector< RawFiducial > rawFiducials
PoseEstimate(const frc::Pose2d &pose, units::time::second_t timestampSeconds, double latency, int tagCount, double tagSpan, double avgTagDist, double avgTagArea, const std::vector< RawFiducial > &rawFiducials)
units::time::second_t timestampSeconds
RawFiducial(int id, double txnc, double tync, double ta, double distToCamera, double distToRobot, double ambiguity)
RetroreflectiveResultClass()
~RetroreflectiveResultClass()
std::vector< double > m_TargetTransform6DROBOTSPACE
double m_TargetAreaNormalizedPercentage
double m_TargetYDegreesCrosshairAdjusted
~SingleTargetingResultClass()
double m_TargetYNormalizedCrosshairAdjusted
double m_TargetXDegreesCrosshairAdjusted
std::vector< double > m_CAMERATransform6DTARGETSPACE
double m_TargetXNormalized
double m_TargetAreaNormalized
double m_TargetYNormalized
std::vector< double > m_ROBOTTransform6DFIELDSPACE
double m_TargetXNormalizedCrosshairAdjusted
std::vector< double > m_CAMERATransform6DROBOTSPACE
std::vector< double > m_ROBOTTransform6DTARGETSPACE
SingleTargetingResultClass()
std::vector< std::vector< double > > m_TargetCorners
std::vector< double > m_TargetTransform6DCAMERASPACE
double m_TargetAreaPixels
std::vector< double > botPose_wpired
std::vector< ClassificationResultClass > ClassificationResults
std::vector< double > botPose_wpiblue
std::vector< double > botPose
std::vector< RetroreflectiveResultClass > RetroResults
std::vector< FiducialResultClass > FiducialResults
std::vector< DetectionResultClass > DetectionResults
const std::string _key_transformTARGETPOSE_ROBOTSPACE
const std::string _key_TargetYNormalizedCrosshair
const std::string _key_TargetAreaNormalized
const std::string _key_pipelineIndex
const std::string _key_latency_pipeline
const std::string _key_TargetXPixels
const std::string _key_TargetYDegreesCrosshair
const std::string _key_TargetAreaPixels
const std::string _key_TargetYPixels
const std::string _key_TargetXDegreesCrosshair
const std::string _key_skew
const std::string _key_timestamp
const std::string _key_transformCAMERAPOSE_ROBOTSPACE
const std::string _key_corners
const std::string _key_botpose_wpired
const std::string _key_TargetXNormalized
const std::string _key_transformTARGETPOSE_CAMERASPACE
const std::string _key_TargetYNormalized
const std::string _key_latency_capture
const std::string _key_transformROBOTPOSE_TARGETSPACE
const std::string _key_confidence
const std::string _key_TargetXDegrees
const std::string _key_TargetXNormalizedCrosshair
const std::string _key_botpose_wpiblue
const std::string _key_ffamily
const std::string _key_colorHSV
const std::string _key_className
const std::string _key_classID
const std::string _key_transformCAMERAPOSE_TARGETSPACE
const std::string _key_transformROBOTPOSE_FIELDSPACE
const std::string _key_fiducialID
const std::string _key_botpose
const std::string _key_TargetYDegrees
const std::string _key_colorRGB
std::vector< double > getCameraPose_TargetSpace(const std::string &limelightName="")
void setPipelineIndex(const std::string &limelightName, int index)
double getLatency_Pipeline(const std::string &limelightName="")
nt::NetworkTableEntry getLimelightNTTableEntry(const std::string &tableName, const std::string &entryName)
std::optional< std::vector< double > > getCurrentCorners(const std::string &limelightName="")
void SetupPortForwarding(const std::string &limelightName)
double getTY(const std::string &limelightName="")
PoseEstimate getBotPoseEstimate(const std::string &limelightName, const std::string &entryName)
std::vector< double > getBotpose_wpiRed(const std::string &limelightName="")
std::vector< double > getBotpose_wpiBlue(const std::string &limelightName="")
PoseEstimate getBotPoseEstimate_wpiRed(const std::string &limelightName="")
T SafeJSONAccess(const wpi::json &jsonData, const KeyType &key, const T &defaultValue)
void setLEDMode_ForceBlink(const std::string &limelightName="")
LimelightResultsClass getLatestResults(const std::string &limelightName="", bool profile=false)
std::vector< double > getCameraPose_RobotSpace(const std::string &limelightName="")
void SetRobotOrientation(const std::string &limelightName, double yaw, double yawRate, double pitch, double pitchRate, double roll, double rollRate)
PoseEstimate getBotPoseEstimate_wpiBlue_MegaTag2(const std::string &limelightName="")
double getTX(const std::string &limelightName="")
void setLimelightNTDoubleArray(const std::string &tableName, const std::string &entryName, const std::span< const double > &vals)
std::vector< double > getLimelightNTDoubleArray(const std::string &tableName, const std::string &entryName)
PoseEstimate getBotPoseEstimate_wpiBlue(const std::string &limelightName="")
std::shared_ptr< nt::NetworkTable > getLimelightNTTable(const std::string &tableName)
double getLimelightNTDouble(const std::string &tableName, const std::string &entryName)
std::vector< double > getBotpose(const std::string &limelightName="")
frc::Pose2d toPose2D(const std::vector< double > &inData)
const double INVALID_TARGET
void setStreamMode_PiPMain(const std::string &limelightName="")
void setLimelightNTDouble(const std::string &tableName, const std::string entryName, double val)
void setPythonScriptData(const std::string &limelightName, const std::vector< double > &outgoingPythonData)
double getFiducialID(const std::string &limelightName="")
void from_json(const wpi::json &j, RetroreflectiveResultClass &t)
void setCameraPose_RobotSpace(const std::string &limelightName, double forward, double side, double up, double roll, double pitch, double yaw)
void setLEDMode_ForceOn(const std::string &limelightName="")
std::vector< double > getTargetPose_RobotSpace(const std::string &limelightName="")
PoseEstimate getBotPoseEstimate_wpiRed_MegaTag2(const std::string &limelightName="")
double getLatency_Capture(const std::string &limelightName="")
std::string getLimelightNTString(const std::string &tableName, const std::string &entryName)
void setStreamMode_Standard(const std::string &limelightName="")
std::string sanitizeName(const std::string &name)
void setStreamMode_PiPSecondary(const std::string &limelightName="")
std::string getNeuralClassID(const std::string &limelightName="")
std::vector< double > getPythonScriptData(const std::string &limelightName="")
frc::Pose3d toPose3D(const std::vector< double > &inData)
void setLEDMode_PipelineControl(const std::string &limelightName="")
std::vector< double > getBotpose_TargetSpace(const std::string &limelightName="")
double getTV(const std::string &limelightName="")
void SetFiducialIDFiltersOverride(const std::string &limelightName, const std::vector< int > &validIDs)
std::vector< double > getTargetPose_CameraSpace(const std::string &limelightName="")
void setCropWindow(const std::string &limelightName, double cropXMin, double cropXMax, double cropYMin, double cropYMax)
double getTA(const std::string &limelightName="")
void setLEDMode_ForceOff(const std::string &limelightName="")
void setPriorityTagID(const std::string &limelightName, int ID)
std::vector< double > getTargetColor(const std::string &limelightName="")
std::string getJSONDump(const std::string &limelightName="")
double extractBotPoseEntry(const std::vector< double > &inData, int position)