0
$\begingroup$

My ROS 2 Humble robot makes an async service request /dock to a "docking_node" and saves the "future" provided at the request.

"""
# dave_interfaces.srv.Dock.srv 
# Request
---
# Result
bool is_docked
bool is_charging
bool success
"""

self.dock_svc_future = self.dock_svc_client.call_async(self.dock_svc_req)

The robot then periodically checks the dock_svc_future to see if the dock request is done (and the service result is available to be read).

I expected to need to check dock_svc_future.done and then read dock_svc_future.result, but the future vars are actually _done and _result:



2024-06-12 15:00:06.588 dave_main_cb: exec undocking

self.undock_svc_future: {'_done': False, '_cancelled': False, '_result': None, '_exception': None, '_exception_fetched': False, '_callbacks': [<bound method Client.remove_pending_request of <rclpy.client.Client object at 0x7fff7eaa7340>>], '_lock': <unlocked _thread.lock object at 0x7fff79990780>, '_executor': <function _fake_weakref at 0x7fff8092b250>}

...

2024-06-12 15:00:09.588 dave_main_cb: exec undocking

self.undock_svc_future: {'_done': True, '_cancelled': False, '_result': dave_interfaces.srv.Undock_Response(is_docked=False, success=True), '_exception': None, '_exception_fetched': False, '_callbacks': [], '_lock': <unlocked _thread.lock object at 0x7fff79990780>, '_executor': <weakref at 0x7fff79b7fe70; to 'MultiThreadedExecutor' at 0x7fff7eaa7550>}

undock_svc_future.result._success True  is_docked: False

Am I doing something wrong that the vars are prefaced with underscore?

$\endgroup$

1 Answer 1

1
$\begingroup$

I expected to need to check dock_svc_future.done and then read dock_svc_future.result, but the future vars are actually _done and _result

I don't think anything is wrong.

rclpy.task.Future (see here) has .done() and .result() getter functions that will return the "private" member variables _done and _result. I'd use those.

The member function .result() does a bit more and throws an exception if it caught one that was raised.

$\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.