Skip to content

Commit

Permalink
Rclpy client multiple requests (#228)
Browse files Browse the repository at this point in the history
* Clients use new API

* use error() on logger

* Use new client api
  • Loading branch information
sloretz authored and Karsten1987 committed Apr 30, 2018
1 parent aab8d9d commit 38c308c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
9 changes: 6 additions & 3 deletions demo_nodes_py/demo_nodes_py/services/add_two_ints_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ def main(args=None):
req = AddTwoInts.Request()
req.a = 2
req.b = 3
cli.call(req)
cli.wait_for_future()
node.get_logger().info('Result of add_two_ints: %d' % cli.response.sum)
future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)
if future.result() is not None:
node.get_logger().info('Result of add_two_ints: %d' % future.result().sum)
else:
node.get_logger().error('Exception while calling service: %r' % future.exception())

node.destroy_node()
rclpy.shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ def main(args=None):
req.b = 3
while not cli.wait_for_service(timeout_sec=1.0):
print('service not available, waiting again...')
cli.call(req)
future = cli.call_async(req)
while rclpy.ok():
rclpy.spin_once(node)
if cli.response is not None:
node.get_logger().info('Result of add_two_ints: %d' % cli.response.sum)
if future.done():
if future.result() is not None:
node.get_logger().info('Result of add_two_ints: %d' % future.result().sum)
else:
node.get_logger().error('Exception while calling service: %r' % future.exception())
break

node.destroy_node()
Expand Down
88 changes: 56 additions & 32 deletions lifecycle/src/lifecycle_service_client_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ def change_state(lifecycle_node, change_state_args=''):
req.transition.id = Transition.TRANSITION_ACTIVATE
elif change_state_args == 'deactivate':
req.transition.id = Transition.TRANSITION_DEACTIVATE
cli.call(req)
cli.wait_for_future()
if cli.response.success:
node.get_logger().info(
'%s successfully triggered transition %s' % (lifecycle_node, change_state_args))
future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)
if future.result() is not None:
resp = future.result()
if resp.success:
node.get_logger().info(
'%s successfully triggered transition %s' % (lifecycle_node, change_state_args))
else:
node.get_logger().info(
'%s failed to triggered transition %s' % (lifecycle_node, change_state_args))
else:
node.get_logger().info(
'%s failed to triggered transition %s' % (lifecycle_node, change_state_args))
node.get_logger.error(
'Exception %r during call %s in transition %s' % (
future.exception(), lifecycle_node, change_state_args))


def get_state(lifecycle_node):
Expand All @@ -63,11 +69,17 @@ def get_state(lifecycle_node):
return

req = GetState.Request()
cli.call(req)
cli.wait_for_future()
node.get_logger().info(
'%s is in state %s(%u)'
% (lifecycle_node, cli.response.current_state.label, cli.response.current_state.id))

future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)
if future.result() is not None:
resp = future.result()
node.get_logger().info(
'%s is in state %s(%u)'
% (lifecycle_node, resp.current_state.label, resp.current_state.id))
else:
node.get_logger.error(
'Exception %r during call %s' % (future.exception(), lifecycle_node))


def get_available_states(lifecycle_node):
Expand All @@ -81,12 +93,17 @@ def get_available_states(lifecycle_node):
return

req = GetAvailableStates.Request()
cli.call(req)
cli.wait_for_future()
node.get_logger().info(
'%s has %u available states' % (lifecycle_node, len(cli.response.available_states)))
for state in cli.response.available_states:
node.get_logger().info('id: %u\tlabel: %s' % (state.id, state.label))
future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)
if future.result() is not None:
resp = future.result()
node.get_logger().info(
'%s has %u available states' % (lifecycle_node, len(resp.available_states)))
for state in resp.available_states:
node.get_logger().info('id: %u\tlabel: %s' % (state.id, state.label))
else:
node.get_logger.error(
'Exception %r during call %s' % (future.exception(), service_name))


def get_available_transitions(lifecycle_node):
Expand All @@ -100,21 +117,28 @@ def get_available_transitions(lifecycle_node):
return

req = GetAvailableTransitions.Request()
cli.call(req)
cli.wait_for_future()
node.get_logger().info(
'%s has %u available transitions'
% (lifecycle_node, len(cli.response.available_transitions)))
for transition in cli.response.available_transitions:
node.get_logger().info(
'Transition id: %u\tlabel: %s'
% (transition.transition.id, transition.transition.label))
node.get_logger().info(
'\tStart id: %u\tlabel: %s'
% (transition.start_state.id, transition.start_state.label))

future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)

if future.result() is not None:
resp = future.result()
node.get_logger().info(
'\tGoal id: %u\tlabel: %s'
% (transition.goal_state.id, transition.goal_state.label))
'%s has %u available transitions'
% (lifecycle_node, len(resp.available_transitions)))
for transition in resp.available_transitions:
node.get_logger().info(
'Transition id: %u\tlabel: %s'
% (transition.transition.id, transition.transition.label))
node.get_logger().info(
'\tStart id: %u\tlabel: %s'
% (transition.start_state.id, transition.start_state.label))
node.get_logger().info(
'\tGoal id: %u\tlabel: %s'
% (transition.goal_state.id, transition.goal_state.label))
else:
node.get_logger.error(
'Exception %r during call %s' % (future.exception(), service_name))


def main(service_type, lifecycle_node, change_state_args='', args=None):
Expand Down

0 comments on commit 38c308c

Please sign in to comment.