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

ros2.crystal. Is there anything wrong with adding the node to the executor before the subscriber has been properly initialized?

asked 2019-02-15 02:10:33 -0500

Obeseturtle gravatar image

updated 2019-02-15 02:16:03 -0500

Hi,

I am currently not sure if it is okay to add the node to the executor before the subscriber has been defined. Although the subscriber and publisher work without any issues, I just wanted to make sure there was not any unforeseen consequences in doing so.

The code I am working on is really large so I have attached a sample code.

using rclcpp::executors::MultiThreadedExecutor;
using builtin_interfaces::msg::Time;

std::shared_ptr<rclcpp::Node> master_node;

int value=0;


class test{

public:
    static void topic_callback(test_msgs::msg::Test::SharedPtr msg)
  {
    std::cout  <<" Subscriber Received [" << msg->data << "]" << std::endl;
    value = msg->data;
  }

};

int main()
{

  rclcpp::shutdown();
  test *t;
  t = new test();
  rclcpp::init(0, nullptr);

  MultiThreadedExecutor executor;
  master_node = rclcpp::Node::make_shared("MasterNode");
  executor.add_node(master_node); // Currently I have it here


  rclcpp::Publisher<test_msgs::msg::Test2>::SharedPtr publisher_;
  rclcpp::Subscription<test_msgs::msg::Test>::SharedPtr subscription_;


  subscription_ = master_node->create_subscription<test_msgs::msg::Test>("Test",t->topic_callback);  
  publisher_ =  master_node->create_publisher<test_msgs::msg::Test2>("Test2");
  rclcpp::Clock ros_clock(RCL_ROS_TIME);
  Time ros_now = ros_clock.now();
  // I seen examples and they mostly have it here
  std::thread executor_thread(std::bind(&MultiThreadedExecutor::spin, &executor));
  executor_thread.detach();


while(rclcpp::is_initialized)
  {
    auto message = test_msgs::msg::Test();
    message.header.stamp.nanosec = ros_now.nanosec;
    publisher_->publish(message);
    usleep(3000000);
  } 
    rclcpp::shutdown();

}

Would it be okay for me to keep the position of the add_node as is?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-02-15 02:20:25 -0500

William gravatar image

As far as I am aware, that should be perfectly fine. In fact if you look at the Node code that creates subscriptions, it will notify the executor that it should wake up to reconsider new information (the fact that there's a new subscription for instance):

https://github.com/ros2/rclcpp/blob/1...

edit flag offensive delete link more

Comments

Oh I see. I am glad to hear that, this means I do not have to change the code much. Thank you very much for your quick answer, I appreciate it.

Obeseturtle gravatar image Obeseturtle  ( 2019-02-15 02:31:21 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-15 02:10:33 -0500

Seen: 369 times

Last updated: Feb 15 '19