Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
TagDetectionChecker.hpp
Go to the documentation of this file.
1
12#ifndef TAG_DETECTION_CHECKER_HPP
13#define TAG_DETECTION_CHECKER_HPP
14
15#include "../../AutonomyGlobals.h"
16#include "../../vision/aruco/TagDetector.h"
17
19
21
22
29namespace statemachine
30{
31
40 inline void LoadDetectedTags(std::vector<tagdetectutils::ArucoTag>& vDetectedArucoTags, const std::vector<std::shared_ptr<TagDetector>>& vTagDetectors)
41 {
42 // Number of tag detectors.
43 size_t siNumTagDetectors = vTagDetectors.size();
44
45 // Initialize vectors to store detected tags temporarily.
46 std::vector<std::vector<tagdetectutils::ArucoTag>> vDetectedArucoTagBuffers(siNumTagDetectors);
47
48 // Initialize vectors to store detected tags futures.
49 std::vector<std::future<bool>> vDetectedArucoTagsFuture;
50
51 // Request tags from each detector.
52 for (size_t siIdx = 0; siIdx < siNumTagDetectors; ++siIdx)
53 {
54 // Check if this tag detector is ready.
55 if (vTagDetectors[siIdx]->GetIsReady())
56 {
57 // Request detected Aruco tags from detector.
58 vDetectedArucoTagsFuture.emplace_back(vTagDetectors[siIdx]->RequestDetectedArucoTags(vDetectedArucoTagBuffers[siIdx]));
59 }
60 }
61
62 // Ensure all requests have been fulfilled.
63 // Then transfer tags from the buffer to vDetectedArucoTags and vDetectedTensorflowTags for the user to access.
64 for (size_t siIdx = 0; siIdx < vDetectedArucoTagsFuture.size(); ++siIdx)
65 {
66 // Wait for the request to be fulfilled.
67 vDetectedArucoTagsFuture[siIdx].get();
68
69 // Loop through the detected Aruco tags and add them to the vDetectedArucoTags vector.
70 for (const tagdetectutils::ArucoTag& tTag : vDetectedArucoTagBuffers[siIdx])
71 {
72 vDetectedArucoTags.emplace_back(tTag);
73 }
74 }
75 }
76
77
91 inline int IdentifyTargetMarker(const std::vector<std::shared_ptr<TagDetector>>& vTagDetectors,
92 tagdetectutils::ArucoTag& stArucoTarget,
93 tagdetectutils::ArucoTag& stTorchTarget,
94 const int nTargetTagID = static_cast<int>(manifest::Autonomy::AUTONOMYWAYPOINTTYPES::ANY))
95 {
96 // Create instance variables.
97 std::vector<tagdetectutils::ArucoTag> vDetectedArucoTags;
98 tagdetectutils::ArucoTag stArucoBestTag;
99 tagdetectutils::ArucoTag stTorchBestTag;
100 std::string szIdentifiedTags = "";
101
102 // Get the current time
103 std::chrono::system_clock::time_point tmCurrentTime = std::chrono::system_clock::now();
104
105 // Load all detected tags in the rover's vision.
106 LoadDetectedTags(vDetectedArucoTags, vTagDetectors);
107 // Find the best tag from the Aruco tags.
108 for (const tagdetectutils::ArucoTag& stCandidate : vDetectedArucoTags)
109 {
110 // Calculate the total age of the tag.
111 double dTagTotalAge = std::fabs(std::chrono::duration_cast<std::chrono::milliseconds>(tmCurrentTime - stCandidate.tmCreation).count() / 1000.0);
112 // Calculate the total tag area.
113 double dArea = stCandidate.pBoundingBox->area();
114 // Calculate what percentage of the screen the tag takes up.
115 double dAreaPercentage = (dArea / (stCandidate.cvImageResolution.width * stCandidate.cvImageResolution.height)) * 100.0;
116
117 // If the distance of the tag is not greater than 0, skip it.
118 if (stCandidate.dStraightLineDistance <= 0.0)
119 {
120 continue;
121 }
122
123 // Check the tag detection method type.
124 if (stCandidate.eDetectionMethod == tagdetectutils::TagDetectionMethod::eOpenCV)
125 {
126 // Assemble the identified tags string.
127 szIdentifiedTags += "\tArUco ID: " + std::to_string(stCandidate.nID) + " Tag Age: " + std::to_string(dTagTotalAge) +
128 "s Tag Screen Percentage: " + std::to_string(dAreaPercentage) + "%\n";
129 // Check if the tag is best.
130 if (stCandidate.nID == nTargetTagID || static_cast<int>(manifest::Autonomy::AUTONOMYWAYPOINTTYPES::ANY))
131 {
132 // Check if the tag meets the requirements.
133 if (dAreaPercentage < constants::BBOX_MIN_SCREEN_PERCENTAGE || dTagTotalAge < constants::BBOX_MIN_LIFETIME_THRESHOLD)
134 {
135 continue;
136 }
137
138 // Check other tag requirements.
139 if (dArea > stArucoBestTag.pBoundingBox->area())
140 {
141 // Set the target tag to the detected tag.
142 stArucoBestTag = stCandidate;
143 }
144 }
145 }
146 else if (stCandidate.eDetectionMethod == tagdetectutils::TagDetectionMethod::eTorch)
147 {
148 // Assemble the identified tags string.
149 szIdentifiedTags += "\tTorch Class: " + stCandidate.szClassName + " Tag Age: " + std::to_string(dTagTotalAge) +
150 "s Tag Screen Percentage: " + std::to_string(dAreaPercentage) + "%\n";
151 // Check if the tag meets the requirements.
152 if (dAreaPercentage < constants::BBOX_MIN_SCREEN_PERCENTAGE || dTagTotalAge < constants::BBOX_MIN_LIFETIME_THRESHOLD)
153 {
154 continue;
155 }
156
157 // Check other tag requirements.
158 if (dArea > stTorchBestTag.pBoundingBox->area())
159 {
160 // Set the target tag to the detected tag.
161 stTorchBestTag = stCandidate;
162 }
163 }
164 }
165
166 // Only print the identified tags if there are any.
167 if (stArucoBestTag.nID != -1 || stTorchBestTag.dConfidence != 0.0)
168 {
169 // Submit logger message.
170 LOG_DEBUG(logging::g_qSharedLogger, "TagDetectionChecker: Identified tags:\n{}", szIdentifiedTags);
171 }
172
173 // Set the target tag to the best tag.
174 stArucoTarget = stArucoBestTag;
175 stTorchTarget = stTorchBestTag;
176
177 return static_cast<int>(vDetectedArucoTags.size());
178 }
179} // namespace statemachine
180#endif
Namespace containing all state machine related classes.
Definition State.hpp:23
int IdentifyTargetMarker(const std::vector< std::shared_ptr< TagDetector > > &vTagDetectors, tagdetectutils::ArucoTag &stArucoTarget, tagdetectutils::ArucoTag &stTorchTarget, const int nTargetTagID=static_cast< int >(manifest::Autonomy::AUTONOMYWAYPOINTTYPES::ANY))
Identify a target marker in the rover's vision, using OpenCV detection.
Definition TagDetectionChecker.hpp:91
void LoadDetectedTags(std::vector< tagdetectutils::ArucoTag > &vDetectedArucoTags, const std::vector< std::shared_ptr< TagDetector > > &vTagDetectors)
Aggregates all detected tags from each provided tag detector for both OpenCV and Tensorflow detection...
Definition TagDetectionChecker.hpp:40
Represents a single ArUco tag. Combines attributes from TorchTag, TensorflowTag, and the original Aru...
Definition TagDetectionUtilty.hpp:59