-
Notifications
You must be signed in to change notification settings - Fork 200
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
Enable TFListener to get Authority #545
base: rolling
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||
/********************************************************************* | ||||||
* | ||||||
* Software License Agreement (BSD License) | ||||||
* | ||||||
* Copyright (c) 2022, Open Source Robotics Foundation, Inc. | ||||||
* All rights reserved. | ||||||
* | ||||||
* Redistribution and use in source and binary forms, with or without | ||||||
* modification, are permitted provided that the following conditions | ||||||
* are met: | ||||||
* | ||||||
* * Redistributions of source code must retain the above copyright | ||||||
* notice, this list of conditions and the following disclaimer. | ||||||
* * Redistributions in binary form must reproduce the above | ||||||
* copyright notice, this list of conditions and the following | ||||||
* disclaimer in the documentation and/or other materials provided | ||||||
* with the distribution. | ||||||
* * Neither the name of Willow Garage, Inc. nor the names of its | ||||||
* contributors may be used to endorse or promote products derived | ||||||
* from this software without specific prior written permission. | ||||||
* | ||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||
* POSSIBILITY OF SUCH DAMAGE. | ||||||
*********************************************************************/ | ||||||
|
||||||
#include <gtest/gtest.h> | ||||||
#include <tf2_ros/buffer.h> | ||||||
#include <tf2_ros/transform_listener.h> | ||||||
|
||||||
#include <chrono> | ||||||
#include <thread> | ||||||
|
||||||
#include <geometry_msgs/msg/transform_stamped.hpp> | ||||||
#include <rclcpp/rclcpp.hpp> | ||||||
#include <tf2_msgs/msg/tf_message.hpp> | ||||||
#include <tf2_ros/qos.hpp> | ||||||
|
||||||
|
||||||
TEST(transform_listener, authority_present) | ||||||
{ | ||||||
const std::string pub_namespace = "/tflistnertest/ns"; | ||||||
const std::string pub_node_name = "sentinel_node_name"; | ||||||
auto pub_node = std::make_shared<rclcpp::Node>(pub_node_name, pub_namespace); | ||||||
auto listener_node = std::make_shared<rclcpp::Node>("listener"); | ||||||
|
||||||
auto pub = rclcpp::create_publisher<tf2_msgs::msg::TFMessage>( | ||||||
pub_node, "/tf", tf2_ros::DynamicBroadcasterQoS()); | ||||||
|
||||||
|
||||||
auto buffer = tf2_ros::Buffer(listener_node->get_clock()); | ||||||
auto listener = tf2_ros::TransformListener(buffer, listener_node); | ||||||
|
||||||
// Wait for pub/sub to match | ||||||
while(pub->get_subscription_count() == 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also, can we put a timeout on this? In case this fails for some reason, I'd prefer this didn't hang forever. |
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||||||
} | ||||||
|
||||||
auto msg = tf2_msgs::msg::TFMessage(); | ||||||
auto tf_stamped = geometry_msgs::msg::TransformStamped(); | ||||||
tf_stamped.header.frame_id = "parent"; | ||||||
tf_stamped.child_frame_id = "child"; | ||||||
tf_stamped.header.stamp = pub_node->get_clock()->now(); | ||||||
msg.transforms.push_back(tf_stamped); | ||||||
|
||||||
pub->publish(msg); | ||||||
|
||||||
// Wait for sub to get message | ||||||
while(!buffer.canTransform("parent", "child", tf2::TimePointZero)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same comment here about adding a timeout. |
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||||||
} | ||||||
|
||||||
const std::string as_yaml = buffer.allFramesAsYAML(); | ||||||
EXPECT_TRUE(as_yaml.find(pub_node_name) != std::string::npos) << as_yaml; | ||||||
EXPECT_TRUE(as_yaml.find(pub_namespace) != std::string::npos) << as_yaml; | ||||||
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that this is the easiest way to do this, but it seems imprecise. That is, we aren't really checking that the authority was set properly; we're just checking whether the node name exists anywhere in that YAML, which could happen for other reasons. Is there a way we could make these checks more precise? |
||||||
} | ||||||
|
||||||
int main(int argc, char** argv) | ||||||
{ | ||||||
testing::InitGoogleTest(&argc, argv); | ||||||
rclcpp::init(argc, argv); | ||||||
return RUN_ALL_TESTS(); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,13 +67,64 @@ TransformListener::~TransformListener() | |
} | ||
|
||
|
||
inline | ||
bool | ||
_make_authority_str( | ||
const std::vector<rclcpp::TopicEndpointInfo> & endpoints, | ||
const rmw_gid_t & pub_gid, | ||
std::string & authority) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nit, but instead of an output |
||
{ | ||
for (const rclcpp::TopicEndpointInfo & pub : endpoints) { | ||
// Compare gid to see if this is the right publisher | ||
bool match = true; | ||
for (decltype(RMW_GID_STORAGE_SIZE) i = 0; i < RMW_GID_STORAGE_SIZE; ++i) { | ||
Comment on lines
+77
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm somewhat concerned about the performance impact of this check, particularly on networks with lots of |
||
if (pub.endpoint_gid()[i] != pub_gid.data[i]) { | ||
match = false; | ||
break; | ||
} | ||
} | ||
if (match) { | ||
authority = pub.node_namespace() + '/' + pub.node_name(); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
std::string | ||
TransformListener::make_authority_str(const rmw_gid_t & pub_gid, bool is_static) | ||
{ | ||
std::string authority = "Authority undetectable"; | ||
std::vector<rclcpp::TopicEndpointInfo> & endpoints = tf_publishers_; | ||
if (is_static) { | ||
endpoints = tf_static_publishers_; | ||
} | ||
|
||
if (!_make_authority_str(endpoints, pub_gid, authority)) { | ||
// Update the cached endpoints | ||
tf_publishers_ = node_graph_interface_->get_publishers_info_by_topic( | ||
message_subscription_tf_->get_topic_name()); | ||
tf_static_publishers_ = node_graph_interface_->get_publishers_info_by_topic( | ||
message_subscription_tf_static_->get_topic_name()); | ||
|
||
// Try making the authority string again | ||
_make_authority_str(endpoints, pub_gid, authority); | ||
} | ||
|
||
return authority; | ||
} | ||
|
||
|
||
void TransformListener::subscription_callback( | ||
const tf2_msgs::msg::TFMessage::ConstSharedPtr msg, | ||
const rclcpp::MessageInfo & msg_info, | ||
bool is_static) | ||
{ | ||
const tf2_msgs::msg::TFMessage & msg_in = *msg; | ||
// TODO(tfoote) find a way to get the authority | ||
std::string authority = "Authority undetectable"; | ||
const rmw_gid_t & pub_gid = msg_info.get_rmw_message_info().publisher_gid; | ||
|
||
std::string authority = make_authority_str(pub_gid, is_static); | ||
for (size_t i = 0u; i < msg_in.transforms.size(); i++) { | ||
try { | ||
buffer_.setTransform(msg_in.transforms[i], authority, is_static); | ||
|
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.