Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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::TestSharedPtr 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 code as is?

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

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::TestSharedPtr 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 code as is?

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

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::TestSharedPtr 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 code as is?

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

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 code position of the add_node as is?