Robotics StackExchange | Archived questions

ROS2/C++ Creating multiple Nodes or Publisher dynamically?

Hello! Follwing minimal example:

My ReceiverNode subscribes to a list with multiple objects, each Object has a Timestamp + Positon + Velocity + Orienation For each Object I wan to create a publisher which publishes its current Position.

What is the best way to create these multiple Publisher?

My Ideas

  1. Create somehow a node for each object? Can I create multiple Nodes from my ReceiverNode, I only know hot to start a ros node in the main using: rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<test::ReceiverNode()); rclcpp::shutdown();
  2. Create a publisher for every Object inside my ReceiverNode? Since I am using classes I need to use a vector of publisher as a class member? And a vector of timer? Does that work?

What is the best way here?

Asked by gianalessio on 2022-12-20 16:42:29 UTC

Comments

Answers

I would recommend using a custom message with all the data you require (timestamp, position, velocity, orientation, object type), and then have a single node with a single publisher which publishes many things onto this topic. If this, for some reason, is not advisable in your use case, create one publisher per object type and do some copy pasting. Or perhaps preferable, make a c++ template and just call that.

Asked by Per Edwardsson on 2022-12-21 08:14:20 UTC

Comments