ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I also faced a same problem, so I tried that fixed this code.
Use a following code if you like:
from example_interfaces.srv import AddTwoInts
import rclpy
import random
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_client_async')
cli = node.create_client(AddTwoInts, 'add_two_ints')
req = AddTwoInts.Request()
# req.a = 41
# req.b = 1
while not cli.wait_for_service(timeout_sec=1.0):
node.get_logger().info('service not available, waiting again...')
# future = cli.call_async(req)
while rclpy.ok():
req.a = random.randrange(10)
req.b = random.randrange(10)
future = cli.call_async(req)
# rclpy.spin_once(node)
rclpy.spin_until_future_complete(node, future)
if future.done():
try:
result = future.result()
except Exception as e:
node.get_logger().info('Service call failed %r' % (e,))
else:
node.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(req.a, req.b, result.sum))
# break
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
[environment]
・Ubuntu 20.04.01 LTS (docker)
・ROS2 Foxy