forked from umrover/mrover-ros
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marshall Vielmetti Starter Project PR #1
Open
MarshallVielmetti
wants to merge
2
commits into
master
Choose a base branch
from
mvielmet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
src/perception/tag_detector/long_range_tag_detector.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#include "tag_detector.hpp" | ||
|
||
namespace mrover { | ||
|
||
void TagDetectorNodelet::onInit() { | ||
mNh = getMTNodeHandle(); | ||
mPnh = getMTPrivateNodeHandle(); | ||
mDetectorParams = new cv::aruco::DetectorParameters(); | ||
auto defaultDetectorParams = cv::aruco::DetectorParameters::create(); | ||
int dictionaryNumber; | ||
|
||
mPnh.param<bool>("publish_images", mPublishImages, true); | ||
using DictEnumType = std::underlying_type_t<cv::aruco::PREDEFINED_DICTIONARY_NAME>; | ||
mPnh.param<int>("dictionary", dictionaryNumber, static_cast<DictEnumType>(cv::aruco::DICT_4X4_50)); | ||
mPnh.param<int>("min_hit_count_before_publish", mMinHitCountBeforePublish, 5); | ||
mPnh.param<int>("max_hit_count", mMaxHitCount, 5); | ||
mPnh.param<int>("tag_increment_weight", mTagIncrementWeight, 2); | ||
mPnh.param<int>("tag_decrement_weight", mTagDecrementWeight, 1); | ||
|
||
mIt.emplace(mNh); | ||
mImgPub = mIt->advertise("long_range_tag_detection", 1); | ||
mDictionary = cv::aruco::getPredefinedDictionary(dictionaryNumber); | ||
|
||
mPcSub = mNh.subscribe("zoom_cam/image", 1, &LongRangeTagDetectorNodelet::imageCallback, this); | ||
mServiceEnableDetections = mNh.advertiseService("enable_detections", &TagDetectorNodelet::enableDetectionsCallback, this); | ||
|
||
// Lambda handles passing class pointer (implicit first parameter) to configCallback | ||
mCallbackType = [this](mrover::DetectorParamsConfig& config, uint32_t level) { configCallback(config, level); }; | ||
mConfigServer.setCallback(mCallbackType); | ||
|
||
mPnh.param<double>("adaptiveThreshConstant", | ||
mDetectorParams->adaptiveThreshConstant, defaultDetectorParams->adaptiveThreshConstant); | ||
mPnh.param<int>("adaptiveThreshWinSizeMax", | ||
mDetectorParams->adaptiveThreshWinSizeMax, defaultDetectorParams->adaptiveThreshWinSizeMax); | ||
mPnh.param<int>("adaptiveThreshWinSizeMin", | ||
mDetectorParams->adaptiveThreshWinSizeMin, defaultDetectorParams->adaptiveThreshWinSizeMin); | ||
mPnh.param<int>("adaptiveThreshWinSizeStep", | ||
mDetectorParams->adaptiveThreshWinSizeStep, defaultDetectorParams->adaptiveThreshWinSizeStep); | ||
mPnh.param<int>("cornerRefinementMaxIterations", | ||
mDetectorParams->cornerRefinementMaxIterations, | ||
defaultDetectorParams->cornerRefinementMaxIterations); | ||
mPnh.param<double>("cornerRefinementMinAccuracy", | ||
mDetectorParams->cornerRefinementMinAccuracy, | ||
defaultDetectorParams->cornerRefinementMinAccuracy); | ||
mPnh.param<int>("cornerRefinementWinSize", | ||
mDetectorParams->cornerRefinementWinSize, defaultDetectorParams->cornerRefinementWinSize); | ||
|
||
bool doCornerRefinement = true; | ||
mPnh.param<bool>("doCornerRefinement", doCornerRefinement, true); | ||
if (doCornerRefinement) { | ||
bool cornerRefinementSubPix = true; | ||
mPnh.param<bool>("cornerRefinementSubPix", cornerRefinementSubPix, true); | ||
if (cornerRefinementSubPix) { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX; | ||
} else { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_CONTOUR; | ||
} | ||
} else { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_NONE; | ||
} | ||
|
||
mPnh.param<double>("errorCorrectionRate", | ||
mDetectorParams->errorCorrectionRate, defaultDetectorParams->errorCorrectionRate); | ||
mPnh.param<double>("minCornerDistanceRate", | ||
mDetectorParams->minCornerDistanceRate, defaultDetectorParams->minCornerDistanceRate); | ||
mPnh.param<int>("markerBorderBits", | ||
mDetectorParams->markerBorderBits, defaultDetectorParams->markerBorderBits); | ||
mPnh.param<double>("maxErroneousBitsInBorderRate", | ||
mDetectorParams->maxErroneousBitsInBorderRate, | ||
defaultDetectorParams->maxErroneousBitsInBorderRate); | ||
mPnh.param<int>("minDistanceToBorder", | ||
mDetectorParams->minDistanceToBorder, defaultDetectorParams->minDistanceToBorder); | ||
mPnh.param<double>("minMarkerDistanceRate", | ||
mDetectorParams->minMarkerDistanceRate, defaultDetectorParams->minMarkerDistanceRate); | ||
mPnh.param<double>("minMarkerPerimeterRate", | ||
mDetectorParams->minMarkerPerimeterRate, defaultDetectorParams->minMarkerPerimeterRate); | ||
mPnh.param<double>("maxMarkerPerimeterRate", | ||
mDetectorParams->maxMarkerPerimeterRate, defaultDetectorParams->maxMarkerPerimeterRate); | ||
mPnh.param<double>("minOtsuStdDev", mDetectorParams->minOtsuStdDev, defaultDetectorParams->minOtsuStdDev); | ||
mPnh.param<double>("perspectiveRemoveIgnoredMarginPerCell", | ||
mDetectorParams->perspectiveRemoveIgnoredMarginPerCell, | ||
defaultDetectorParams->perspectiveRemoveIgnoredMarginPerCell); | ||
mPnh.param<int>("perspectiveRemovePixelPerCell", | ||
mDetectorParams->perspectiveRemovePixelPerCell, | ||
defaultDetectorParams->perspectiveRemovePixelPerCell); | ||
mPnh.param<double>("polygonalApproxAccuracyRate", | ||
mDetectorParams->polygonalApproxAccuracyRate, | ||
defaultDetectorParams->polygonalApproxAccuracyRate); | ||
|
||
NODELET_INFO("Tag detection ready, use odom frame: %s, min hit count: %d, max hit count: %d, hit increment weight: %d, hit decrement weight: %d", mUseOdom ? "true" : "false", mMinHitCountBeforePublish, mMaxHitCount, mTagIncrementWeight, mTagDecrementWeight); | ||
} | ||
|
||
void TagDetectorNodelet::configCallback(mrover::DetectorParamsConfig& config, uint32_t level) { | ||
// Don't load initial config, since it will overwrite the rosparam settings | ||
if (level == std::numeric_limits<uint32_t>::max()) return; | ||
|
||
mDetectorParams->adaptiveThreshConstant = config.adaptiveThreshConstant; | ||
mDetectorParams->adaptiveThreshWinSizeMin = config.adaptiveThreshWinSizeMin; | ||
mDetectorParams->adaptiveThreshWinSizeMax = config.adaptiveThreshWinSizeMax; | ||
mDetectorParams->adaptiveThreshWinSizeStep = config.adaptiveThreshWinSizeStep; | ||
mDetectorParams->cornerRefinementMaxIterations = config.cornerRefinementMaxIterations; | ||
mDetectorParams->cornerRefinementMinAccuracy = config.cornerRefinementMinAccuracy; | ||
mDetectorParams->cornerRefinementWinSize = config.cornerRefinementWinSize; | ||
if (config.doCornerRefinement) { | ||
if (config.cornerRefinementSubpix) { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX; | ||
} else { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_CONTOUR; | ||
} | ||
} else { | ||
mDetectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_NONE; | ||
} | ||
mDetectorParams->errorCorrectionRate = config.errorCorrectionRate; | ||
mDetectorParams->minCornerDistanceRate = config.minCornerDistanceRate; | ||
mDetectorParams->markerBorderBits = config.markerBorderBits; | ||
mDetectorParams->maxErroneousBitsInBorderRate = config.maxErroneousBitsInBorderRate; | ||
mDetectorParams->minDistanceToBorder = config.minDistanceToBorder; | ||
mDetectorParams->minMarkerDistanceRate = config.minMarkerDistanceRate; | ||
mDetectorParams->minMarkerPerimeterRate = config.minMarkerPerimeterRate; | ||
mDetectorParams->maxMarkerPerimeterRate = config.maxMarkerPerimeterRate; | ||
mDetectorParams->minOtsuStdDev = config.minOtsuStdDev; | ||
mDetectorParams->perspectiveRemoveIgnoredMarginPerCell = config.perspectiveRemoveIgnoredMarginPerCell; | ||
mDetectorParams->perspectiveRemovePixelPerCell = config.perspectiveRemovePixelPerCell; | ||
mDetectorParams->polygonalApproxAccuracyRate = config.polygonalApproxAccuracyRate; | ||
} | ||
|
||
bool TagDetectorNodelet::enableDetectionsCallback(std_srvs::SetBool::Request& req, std_srvs::SetBool::Response& res) { | ||
mEnableDetections = req.data; | ||
if (mEnableDetections) { | ||
res.message = "Enabled tag detections."; | ||
NODELET_INFO("Enabled tag detections."); | ||
} else { | ||
res.message = "Disabled tag detections."; | ||
NODELET_INFO("Disabled tag detections."); | ||
} | ||
|
||
res.success = true; | ||
return true; | ||
} | ||
|
||
} // namespace mrover | ||
|
||
int main(int argc, char** argv) { | ||
ros::init(argc, argv, "tag_detector"); | ||
|
||
// Start the ZED Nodelet | ||
nodelet::Loader nodelet; | ||
nodelet.load(ros::this_node::getName(), "mrover/TagDetectorNodelet", ros::names::getRemappings(), {}); | ||
|
||
ros::spin(); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
#include <pluginlib/class_list_macros.h> | ||
PLUGINLIB_EXPORT_CLASS(mrover::TagDetectorNodelet, nodelet::Nodelet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace mrover { | ||
|
||
class LongRangeTagDetectorNodelet : public nodelet::Nodelet { | ||
ros::NodeHandle mNh, mPnh; | ||
|
||
std::optional<image_transport::ImageTransport> mIt; | ||
image_transport::Publisher mImgPub; | ||
std::unordered_map<int, image_transport::Publisher> mThreshPubs; // Map from threshold scale to publisher | ||
ros::ServiceServer mServiceEnableDetections; | ||
|
||
image_transport::Subscriber mImgSub; | ||
|
||
bool mEnableDetections = true; | ||
bool mUseOdom{}; | ||
std::string mOdomFrameId, mMapFrameId, mCameraFrameId; | ||
bool mPublishImages{}; // If set, we publish the images with the tags drawn on top | ||
int mMinHitCountBeforePublish{}; | ||
int mMaxHitCount{}; | ||
int mTagIncrementWeight{}; | ||
int mTagDecrementWeight{}; | ||
|
||
cv::Ptr<cv::aruco::DetectorParameters> mDetectorParams; | ||
cv::Ptr<cv::aruco::Dictionary> mDictionary; | ||
|
||
cv::Mat mImg; | ||
cv::Mat mGrayImg; | ||
sensor_msgs::Image mImgMsg; | ||
sensor_msgs::Image mThreshMsg; | ||
uint32_t mSeqNum{}; | ||
std::optional<size_t> mPrevDetectedCount; // Log spam prevention | ||
std::vector<std::vector<cv::Point2f>> mImmediateCorners; | ||
std::vector<int> mImmediateIds; | ||
std::unordered_map<int, Tag> mTags; | ||
dynamic_reconfigure::Server<mrover::DetectorParamsConfig> mConfigServer; | ||
dynamic_reconfigure::Server<mrover::DetectorParamsConfig>::CallbackType mCallbackType; | ||
|
||
LoopProfiler mProfiler{"Tag Detector"}; | ||
|
||
void onInit() override; | ||
|
||
void publishThresholdedImage(); | ||
|
||
std::optional<SE3> getTagInCamFromPixel(sensor_msgs::PointCloud2ConstPtr const& cloudPtr, size_t u, size_t v); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/perception/tag_detector/long_range_tag_detector.processing.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "long_range_tag_detector.hpp" | ||
|
||
#include "../point.hpp" | ||
|
||
namespace mrover { | ||
|
||
/** | ||
* Detects tags in an image, draws the detected markers onto the image, and publishes them to /long_range_tag_detection | ||
* | ||
* @param msg image message | ||
*/ | ||
void TagDetectorNodelet::imageCallback(sensor_msgs::Image const& msg) { | ||
// 1. Detect tags | ||
// 2. Update the hit counts of the tags in the mTags map | ||
// 3. We only want to publish the tags if the topic has subscribers | ||
if (mPublishImages && mImgPub.getNumSubscribers()) { | ||
// Draw the tags on the image using OpenCV | ||
} | ||
} | ||
|
||
} // namespace mrover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
int32 tagId | ||
float32 xTagCenterPixel | ||
float32 yTagCenterPixel | ||
float32 closenessMetric |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you may want to make a commit to remove
src/perception/tag_detector
from this branch