Is there the maximum number of publishers and subscribers per a node in ROS2 foxy?

asked 2023-02-01 05:05:40 -0500

junie gravatar image

I'm using ROS2 foxy with std_msgs, std_srvs, and rclcpp.

I'm running Ubuntu 20.04 (focal)

I made a simple tutorial code having subscribers and publishers in ROS2 foxy.

When I change the number of the sum of both subscribers and publishers,

Sometimes the code works, but sometimes the code doesn't work.

For example, If I run as the following, it works.

static const int num_service_ = 1;
static const int num_publisher_ = 21;

For example, If I run as the following, it doesn't work.

static const int num_service_ = 1;
static const int num_publisher_ = 22;

However, if I run the code in another computer, both work.

#include <functional>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "std_srvs/srv/trigger.hpp"

using namespace std::chrono_literals;
using std::placeholders::_1;
using std::placeholders::_2;
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */

class MinimalPublisher : public rclcpp::Node
{
public:
    MinimalPublisher()
        : Node("minimal_publisher")
    {
    std::cout << "start to create service" << std::endl;
    for(int i = 0; i < num_service_; i++){
        service_pool_[i] = this->create_service<std_srvs::srv::Trigger>(
                "/service"+std::to_string(i), std::bind(&MinimalPublisher::serviceCallback, this, _1, _2));
    }
    std::cout << "start to create publisher" << std::endl;
    rclcpp::QoS qos(rclcpp::KeepLast(10));
    for (int i = 0; i < num_publisher_; i++){
        publisher_pool_[i] = this->create_publisher<std_msgs::msg::String>(
                "/publisher"+std::to_string(i), qos);
    }
    }

private:
    static const int num_service_ = 1;
    static const int num_publisher_ = 22;
    rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr service_pool_[num_service_];
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_pool_[num_publisher_];
    void serviceCallback(const std::shared_ptr<std_srvs::srv::Trigger::Request>/* request */,
                     std::shared_ptr<std_srvs::srv::Trigger::Response> response)
    {
    std::cout << "serviceCallback" << std::endl;
    }
};

int main(int argc, char * argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<MinimalPublisher>());
    rclcpp::shutdown();
    return 0;
  }

Do you know what is the problem?

Is it due to my computer specification?

Thank you.

edit retag flag offensive close merge delete