Skip to content

SubNode: add "NodeHandle" like concept#578

Closed
fmrico wants to merge 3 commits intoros2:masterfrom
fmrico:sub_node
Closed

SubNode: add "NodeHandle" like concept#578
fmrico wants to merge 3 commits intoros2:masterfrom
fmrico:sub_node

Conversation

@fmrico
Copy link
Copy Markdown
Contributor

@fmrico fmrico commented Nov 2, 2018

Hi all,

This PR is related to issue #531 , providing a SubNode class (implemented as a Proxy Pattern). This let to have code like:

 auto node_ptr = std::make_shared<rclcpp::Node>("my_node", "/my_ns");

  auto sub_node = node_ptr->create_sub_node("/foo_ns");
  auto another_sub_node = sub_node->create_sub_node("/bar_ns");

  // topic would be '/my_ns/foo_ns/bar_ns/chatter'
  auto sub0 = node_ptr->create_subscription<std_msgs::msg::String>("chatter0",topic_callback);
  auto sub1 = sub_node->create_subscription<std_msgs::msg::String>("chatter1",topic_callback);
  auto sub2 = another_sub_node->create_subscription<std_msgs::msg::String>("chatter2",topic_callback);

which produces these topics:

/my_ns/chatter0
/my_ns/foo_ns/bar_ns/chatter2
/my_ns/foo_ns/chatter1

This is a possible starting point to have the desired functionality.

Best

@tfoote tfoote added the in review Waiting for review (Kanban column) label Nov 2, 2018
@clalancette clalancette requested a review from sloretz November 8, 2018 18:23
Copy link
Copy Markdown
Contributor

@sloretz sloretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fmrico, thanks for the pull request! This is raises some questions in my mind about what #531 is asking for.

It looks like the SubNode approach requires all API's on the node to be duplicated. One of the comments on #351 seems propose that a Node and a SubNode be the same class with a hidden private Node implementation that they both call to avoid duplicating APIs. That sounds like a huge change to implement in one pull request.

Any ideas on how to iterate towards Node and SubNode being the same class without duplicating the APIs in the meantime?

@@ -0,0 +1,227 @@
// Copyright 2014 Open Source Robotics Foundation, Inc.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018

@@ -0,0 +1,485 @@
// Copyright 2014 Open Source Robotics Foundation, Inc.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018

namespace rclcpp
{

class Node;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to forward declare Node since rclcpp/node.hpp was included above

CallbackT && callback,
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default,
rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr,
bool ignore_local_publications = false,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach copies a lot of code. I'm concerned maintaining default values for arguments like this one in two places will be difficult to do in the future. @wjwwood posted this comment about making a sub node and a node the same class.

const std::string
SubNode::add_extended_ns_prefix(const std::string & original_name) const
{
return std::string(original_->get_namespace()) + "/" + extended_namespace_ + "/" + original_name;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for relative names foo or foo/bar, but I'm not sure about the behavior for fully qualified or private names ~/bar.

ROS 1 threw ros::InvalidNameException when trying to create a publisher/subscriber/etc with a private name, asking instead that the NodeHandle be created with a private namespace. In ROS 2 the create_...() APIs allow private names. I'm not sure how to handle all the cases, but there is a proposal here: #531 (comment)

auto node = std::make_shared<rclcpp::Node>("my_node", "/ns");
auto subnode = node->create_sub_node("/sub_ns");
EXPECT_STREQ("my_node", subnode->get_name());
EXPECT_STREQ("/ns/sub_ns", subnode->get_namespace());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the subnode's namespace is fully qualified, I think this result should be /sub_ns. This also matches ROS 1 when a fully qualified name is given as a child NodeHandle namespace.

#include "ros/ros.h"
#include "std_msgs/String.h"

int main(int argc, char ** argv)
{
  ros::init(argc, argv, "name");

  ros::NodeHandle n;
  ros::NodeHandle nsub(n, "/sub_ns");
  ros::Publisher pub = nsub4.advertise<std_msgs::String>("nsub/relaive", 1000);

  ros::spin();
  return 0;
}

launched with /path/to/above/executable __ns:=/orig_ns

Creates the topic /sub_ns/nsub/relative, not /orig_ns/sub_ns/...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also consider making it so that "sub nodes" can only use relative name prefix or a prefix starting with a ~. That would avoid the issue of ignoring the node namespace.

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 9, 2018

Yeah, unfortunately I think the design issues here (like the amount of duplicated code) are going to impede progress. Maybe we could try and setup a voice chat at some point to hash out some of the details.

@fmrico are you interested at all in doing that?

@fmrico
Copy link
Copy Markdown
Contributor Author

fmrico commented Nov 9, 2018

Hi,

Yes, there is a lot of duplicated code because I did not want that SubNode inherits from Node, but the interface had to be the same. I could reconsider this or find another solution.

@wjwwood I am interested in doing that.

@fmrico fmrico mentioned this pull request Nov 11, 2018
@fmrico
Copy link
Copy Markdown
Contributor Author

fmrico commented Nov 11, 2018

Hi @wjwwood and @sloretz ,

I have been iterating on this. I provide another alternative in PR #581

Is uses Node Class, so there is not duplciated code.

Best

@fmrico fmrico closed this Nov 11, 2018
@tfoote tfoote removed the in review Waiting for review (Kanban column) label Nov 11, 2018
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
Resolves ros2#552

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants