[ROS2] [rclcpp] [eloquent] spin_until_future_complete in an already spinning node

asked 2019-12-12 13:03:56 -0600

fcolecumberri gravatar image

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


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)

edit retag flag offensive close merge delete


I'm not sure why you have 2 threads, but this may help: https://answers.ros.org/question/3020...

clyde gravatar image clyde  ( 2019-12-18 18:36:53 -0600 )edit

there are plenty of reasons to use threads in ROS, as an example if you make an interface to a hardware that has sensors and actuators, you want to attend the calls that modify the actuator at the same time that you want to publish the information of the sensors, I have done this kind of things in ROS1 several times.

fcolecumberri gravatar image fcolecumberri  ( 2019-12-19 06:40:52 -0600 )edit

I'm facing the same issue, have you found any solutions?

MrCheesecake gravatar image MrCheesecake  ( 2020-06-05 09:42:10 -0600 )edit

I have the same issue, I am trying tones of different stuff, like spin some nodes in the main and create new nodes for the client but thing still hangs. Some help would be good here.

Rextab gravatar image Rextab  ( 2020-10-23 13:59:59 -0600 )edit

IINM, when if(!client->wait_for_service (with "!") matches, the Service in question is unavailable. Are you sure that is the condition you intend to call a Service (by client->async_send_request)?

130s gravatar image 130s  ( 2023-05-05 13:02:48 -0600 )edit