How to access the response of async request from a client?
I am calling service from service in ROS2. As I have the spin function already being executed in the outer service, I can't use rclcpp::spin_until_future_complete for calling the inner service. To find a hack I stumbled upon the function of async send request where I can pass a callback that will get executed once the response as shown below.
// We give the async_send_request() method a callback that will get executed once the response
// is received.
// This way we can return immediately from this method and allow other work to be done by the
// executor in spin
while waiting for the response.
using ServiceResponseFuture =
rclcpp::Client<example_interfaces::srv::addtwoints>::SharedFuture;
auto response_received_callback = [this](ServiceResponseFuture future) {
auto result = future.get();
RCLCPP_INFO(this->get_logger(), "Result of add_two_ints: %" PRId64, result->sum)
rclcpp::shutdown();
};
auto future_result = client_->async_send_request(request, response_received_callback);
My question is that if I want to access the result out of the scope for further process. How could I do that?