Hi,
I am trying to use the services asynchronously. According to what I read in 5 days basics C++ tutorial, and the example:
auto result_future = client_->async_send_request(
request, std::bind(&ServiceClient::response_callback, this,
std::placeholders::_1));
RCLCPP_INFO_STREAM(this->get_logger(), std::string("async_send_request ")
<< (this->now()).nanoseconds());
}
void response_callback(rclcpp::Client<std_srvs::srv::Empty>::SharedFuture future)
{
RCLCPP_INFO_STREAM(this->get_logger(), std::string("response_callback ")
<< (this->now()).nanoseconds());
auto status = future.wait_for(1s);
if (status == std::future_status::ready) {
RCLCPP_INFO(this->get_logger(), "Result: success");
service_done_ = true;
} else {
RCLCPP_INFO(this->get_logger(), "Service In-Progress...");
}
}
** put special focus in the line auto status = future.wait_for(1s);
My initial thinks were that: okay, when I send an async request, there should be some way of knowing if I have not received a response after a timeout. So I though:
- maybe async_send_request allows to specify a timeout. But I checked ros2 documentation and it doesn’t.
- Then, I though: it might is because response_callback executes immediately after async_send_request. And that’s why you have with this future.wait_for(1s) line in the response_callback to evaluate if you received response after a timeout or not.
So, if my theory was right, response_callback was gonna be executed even if service is not available. I tried, but response_callback was never executed.
Then, I though: okay, maybe this does not worked because response_callback is executed after doing the async_send_request, once service server ACCEPTS the request. So, if my theory was right, response_callback was gonna be executed even if service sleeps for 1000 seconds inside the call. I tried, but response_callback was never executed.
So, in summary, I see that the only possibility of executing that callback is when you receive a response. And you dont have any option (from the own ros2 framework) of knowing that you did not receive any response.
I tried all this stuff with several configurations of callback groups (and always with multithreaded executor).
Can someone help me with this? I would like to know if I am right, or if I am wrong and there exist an easy way to know that I have not received any response after a timeout when I do an async request