0
$\begingroup$

I am trying to make two servers for two different services in a single node. For example, like so:

class GetAllocatedGoalServer : public rclcpp::Node
{
public:
  GetAllocatedGoalServer()
    : Node("goal_allocation_server")
  {
    load_cost_data_server_->make_shared("load_cost_data_server");
    RCLCPP_INFO(logger_, "Goal Allocation server initializing");
    service_get_goal_ = this->create_service<frontier_msgs::srv::GetAllocatedGoal>(
      "/get_allocated_goal", std::bind(&GetAllocatedGoalServer::handle_service_get_goal_request, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

    service_load_cost_data_ = this->create_service<frontier_msgs::srv::LoadFrontierCosts>(
      "/send_latest_frontier_costs", std::bind(&GetAllocatedGoalServer::handle_load_cost_data, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
  }

When I run std::this_thread::get_id() in the two functions, I get the same ID. Aren't the service callbacks multithreaded by default? I came across this problem because there is an indefinite while loop in the 2nd service callback and when entered into this while loop, the first service does not process any requests.

Any and all help is appreciated! Merci!

$\endgroup$

2 Answers 2

1
$\begingroup$

The multi-thread executor and assign two service separate callback group can solve this problem

$\endgroup$
2
  • 1
    $\begingroup$ Welcome to Robotics Sorawit. Thanks for your answer but we are looking for comprehensive answers that provide some explanation and context. Very short answers cannot do this, so please edit your answer to explain why it is right, ideally with citations. Answers that don't include explanations may be removed. See How to Answer for more info. $\endgroup$
    – Tully
    Commented Dec 7, 2023 at 8:32
  • $\begingroup$ yes, it worked! thanks. $\endgroup$ Commented Apr 5 at 18:42
0
$\begingroup$

I just figured this one out. The solution is to use a Multi threaded executor along with callback groups. I used two different groups with Mutually Exclusive callbacks. Here's an online tutorial for this

https://docs.ros.org/en/foxy/Concepts/About-Executors.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.