ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

[Ros2] Creating Multiple nodes using class inheritance

asked 2019-01-23 23:07:18 -0600

Obeseturtle gravatar image

updated 2019-01-25 00:47:28 -0600

Hi,

I was wondering if you had an example on how to create multiple Nodes using the class inheritance.

In ros, I created multiple nodes using ros::NodeHandle;.

I read in one of the ros2 answers that I could use the following instead:

auto node = std::make_shared<rclcpp::Node>("...", ...);
node->create_publisher<...>("~/chatter", ...);

link text

but in the publisher not_composable.cpp example which uses this method, it is written that:

We do not recommend this style anymore, because composition of multiple nodes in the same executable is not possible. Please see one of the subclass

So, I guessed there was a better way to create multiple nodes using the inheritance class.

My current problem is that the Node is being created on the constructor of the inheritance class:

class MinimalPublisher : public rclcpp::Node
{
public:
  MinimalPublisher()
  : Node("minimal_publisher"), count_(0)

but, in my code I am not sure which publishers will be needed so, I want to be able to control when a Node is made.


Update to my question:

Basically, my question was brought out by a lack of understanding of how ros worked. Trying to migrate to ros2 without fully understand ros first, was really dumb of me.

Thanks to the following post and its references I was able to learn more about ros and ros2: link text

I initially thought that a publisher and a subscriber needed their own nodes which is entirely wrong. What I meant by "I want to be able to control when a Node is made. " was basically me thinking that when a new publisher is created, a node needed to be initialized as well. I will keep on studying and before I make my next question I will do more thorough research.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-01-24 04:33:28 -0600

alsora gravatar image

updated 2019-01-24 04:49:41 -0600

The second example you provided is the correct way to go in ROS2.

I'm not sure about what do you mean with

I want to be able to control when a Node is made

However you can easily replicate the same behavior of the first example also using the second, more correct, structure.

class MinimalPublisher : public rclcpp::Node {

public:

    MinimalPublisher() : Node("minimal_publisher")
    {
         RCLCPP_INFO(this->get_logger(), "Node created!!");
    } 

    void add_publisher()
    {
        _publisher = this->create_publisher<std_msgs::msg::String>("my_topic");
    }

private:

    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr _publisher;

}

Then, just create an object of type MinimalPublisher when you need it and call the add_publisher method when you want to create the publisher. Similarly you will have a method for publishing messages or you may want to set a rclcpp::Timer

edit flag offensive delete link more

Comments

I just wanted to thank you for responding to my question. I am sure it was hard to understand what exactly I wanted to ask. I will learn from my mistake and read the documentation more carefully from now on.

Obeseturtle gravatar image Obeseturtle  ( 2019-01-25 00:48:28 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-01-23 23:07:18 -0600

Seen: 2,630 times

Last updated: Jan 25 '19