SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
TargetTracker.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/smartdashboard/Field2d.h>
4#include <pathplanner/lib/commands/FollowPathHolonomic.h>
5
6#include <functional>
7#include <string>
8#include <vector>
9
11
12namespace subzero {
13
15 double x;
16 double y;
17
19
20 explicit DetectedCorner(const std::vector<double> &coord) {
21 x = coord[0];
22 y = coord[1];
23 }
24};
25
31
33
34 explicit DetectedCorners(const std::vector<std::vector<double>> &corners) {
35 if (corners.empty())
36 return;
37
38 topLeft = DetectedCorner(corners[0]);
39 topRight = DetectedCorner(corners[1]);
40 bottomLeft = DetectedCorner(corners[2]);
41 bottomRight = DetectedCorner(corners[3]);
42 }
43
44 explicit DetectedCorners(const std::vector<double> &rawCorners) {
45 if (rawCorners.size() < 8)
46 return;
47
48 topLeft = DetectedCorner({rawCorners[0], rawCorners[1]});
49 bottomLeft = DetectedCorner({rawCorners[6], rawCorners[7]});
50 bottomRight = DetectedCorner({rawCorners[4], rawCorners[5]});
51 topRight = DetectedCorner({rawCorners[2], rawCorners[3]});
52 }
53};
54
56 uint8_t classId;
57 std::string className;
58 double confidence;
59 // Positive-right, center-zero
60 units::degree_t centerX;
61 // Positive-down, center-zero
62 units::degree_t centerY;
65
67
68 explicit DetectedObject(uint8_t id, double conf, units::degree_t cX,
69 units::degree_t cY, double area,
70 std::vector<std::vector<double>> corners)
71 : classId{id}, className{"unknown"}, confidence{conf}, centerX{cX},
72 centerY{cY}, areaPercentage{area}, detectedCorners{corners} {}
73
75 const LimelightHelpers::DetectionResultClass &detectionResult)
76 : classId{static_cast<uint8_t>(detectionResult.m_classID)},
77 className{detectionResult.m_className},
78 confidence{detectionResult.m_confidence},
79 centerX{detectionResult.m_TargetXDegreesCrosshairAdjusted},
80 centerY{detectionResult.m_TargetYDegreesCrosshairAdjusted},
81 areaPercentage{detectionResult.m_TargetAreaNormalized},
82 detectedCorners{detectionResult.m_TargetCorners} {}
83
84 void withRawCorners(const std::vector<double> &rawCorners) {
85 detectedCorners = DetectedCorners(rawCorners);
86 }
87};
88
94
96public:
102 units::degree_t cameraAngle;
107 units::inch_t cameraLensHeight;
117 std::string limelightName;
122 units::inch_t gamepieceWidth;
128 units::dimensionless::scalar_t focalLength;
133 frc::Pose2d simGamepiecePose;
139 units::degree_t gamepieceRotation;
162 };
163
165 std::function<frc::Pose2d()> poseGetter,
166 std::function<frc::Field2d *()> fieldGetter);
172 std::vector<DetectedObject> GetTargets();
173
179 void UpdateTrackedTargets(const std::vector<DetectedObject> &objects);
180
186 std::optional<DetectedObject> GetBestTarget(std::vector<DetectedObject> &);
187
194 bool HasTargetLock(std::vector<DetectedObject> &);
195
201 std::optional<frc::Pose2d> GetTargetPose(const DetectedObject &);
202
208 std::optional<frc::Pose2d> GetBestTargetPose(std::vector<DetectedObject> &);
209
215 units::inch_t GetDistanceToTarget(const DetectedObject &);
216
217private:
221 void SortTargetsByProximity(std::vector<DetectedObject> &objects);
222 void PublishTrackedTarget(const TrackedTarget &target, int index);
223
224 TargetTrackerConfig m_config;
225 std::function<frc::Pose2d()> m_poseGetter;
226 std::function<frc::Field2d *()> m_fieldGetter;
227 std::vector<TrackedTarget> m_trackedTargets;
228};
229} // namespace subzero
std::optional< frc::Pose2d > GetBestTargetPose(std::vector< DetectedObject > &)
From a list of detected objects, get the pose of the best one.
std::optional< DetectedObject > GetBestTarget(std::vector< DetectedObject > &)
Get the best target for tracking/intaking.
bool HasTargetLock(std::vector< DetectedObject > &)
Check if a target has been acquired.
TargetTracker(TargetTrackerConfig config, std::function< frc::Pose2d()> poseGetter, std::function< frc::Field2d *()> fieldGetter)
std::optional< frc::Pose2d > GetTargetPose(const DetectedObject &)
Get the pose of the given object.
void UpdateTrackedTargets(const std::vector< DetectedObject > &objects)
Push targets to SmartDashboard.
std::vector< DetectedObject > GetTargets()
Get a list of all found, valid targets.
units::inch_t GetDistanceToTarget(const DetectedObject &)
Get the estimated distance to the target.
DetectedCorner(const std::vector< double > &coord)
DetectedCorners(const std::vector< double > &rawCorners)
DetectedCorners(const std::vector< std::vector< double > > &corners)
DetectedCorner bottomLeft
DetectedCorner bottomRight
DetectedObject(uint8_t id, double conf, units::degree_t cX, units::degree_t cY, double area, std::vector< std::vector< double > > corners)
units::degree_t centerX
void withRawCorners(const std::vector< double > &rawCorners)
DetectedObject(const LimelightHelpers::DetectionResultClass &detectionResult)
units::degree_t centerY
DetectedCorners detectedCorners
double trigDistancePercentage
Ranges from 0 to 1; Multiplies trig-based distances and then applies the inverse to width-based estim...
units::degree_t gamepieceRotation
Rotation of the gamepiece relative to the field; helpful for automatic intaking.
double areaPercentageThreshold
Ignore any targets below this percentage to exclude spurious detections.
frc::Pose2d simGamepiecePose
Pose of the mock gamepiece in sim.
units::inch_t cameraLensHeight
Camera height relative to the floor.
std::string limelightName
Name of the limelight in shuffleboard.
double confidenceThreshold
Targets must have a greater value in order to be used.
units::inch_t gamepieceWidth
Width of the gamepiece.
uint8_t maxTrackedItems
Max number of items that will be pushed to SmartDashboard as poses.
units::dimensionless::scalar_t focalLength
Calculated by doing: (known distance / known width in pixels) * gamepiece width.
units::degree_t cameraAngle
Camera angle relative to the floor; positive = up.
frc::Pose2d invalidTrackedPose
Move invalid objects to this pose.
DetectedObject object