1
$\begingroup$

I’m making a program, this program has a rclcpp::node and is running with several threads. In the main thread I have

executor.add_node(node);
executor.spin();

this let me manage services and subscribe to topics but in other thread I am running

auto client = create_client<ConnectionCore>(service_name)
if(!client->wait_for_service(std::chrono::duration<long double, std::milli>(100)))
{
    auto result = client->async_send_request(request);
    rclcpp::spin_until_future_complete(node, result);
}

but I get

terminate called after throwing an instance of 'std::runtime_error'
what():  spin_once() called while already spinning

which is very logical since the node is already spinning, but if my node is spinning how can I call a service?

I have already tried stuff like

client->async_send_request(request, [this](decltype(client)::element_type::SharedFuture future){
    //do stuff...
});
// wthout the rclcpp::spin_until_future_complete

but it doesn't enter in the callback. I have also tried to

auto result = client->async_send_request(request);
result.get() // and result.wait()
and this just blocked the thread and never get out.

I'm porting this code from ROS1 (where this was no problem) so I'm almoust sure there must be a good way to call a service and subscribe to topics at the same time since in ROS1 is posible, I'm just not sure how to do it.

(I have also thought about creating a second node just to call the service and then destroy it, but I think that is the ugliest possible solution)

$\endgroup$

1 Answer 1

0
$\begingroup$

maybe this will help. i have used this to migrate my ros1 node to ros2 with the following code
https://github.com/ros2/demos/blob/4a18995bb7ef52ff40c7892005dca3a68d64fff0/demo_nodes_cpp/src/services/add_two_ints_client_async.cpp#L59

New contributor
user40973 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\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.