Services

Client

class rclpy.client.Client(context: Context, client_impl: rpyutils.import_c_library.Client, srv_type: SrvType, srv_name: str, qos_profile: rclpy.qos.QoSProfile, callback_group: CallbackGroup)

Create a container for a ROS service client.

Warning

Users should not create a service client with this constuctor, instead they should call Node.create_client().

Parameters:
  • context – The context associated with the service client.

  • client_impl_rclpy.Client wrapping the underlying rcl_client_t object.

  • srv_type – The service type.

  • srv_name – The name of the service.

  • qos_profile – The quality of service profile to apply the service client.

  • callback_group – The callback group for the service client. If None, then the nodes default callback group is used.

call(request: SrvTypeRequest) SrvTypeResponse

Make a service request and wait for the result.

Warning

Do not call this method in a callback or a deadlock may occur.

Parameters:

request – The service request.

Returns:

The service response.

Raises:

TypeError if the type of the passed request isn’t an instance of the Request type of the provided service when the client was constructed.

call_async(request: SrvTypeRequest) Future

Make a service request and asynchronously get the result.

Parameters:

request – The service request.

Returns:

A future that completes when the request does.

Raises:

TypeError if the type of the passed request isn’t an instance of the Request type of the provided service when the client was constructed.

configure_introspection(clock: rclpy.clock.Clock, service_event_qos_profile: rclpy.qos.QoSProfile, introspection_state: rpyutils.import_c_library.service_introspection.ServiceIntrospectionState) None

Configure client introspection.

Parameters:
  • clock – Clock to use for generating timestamps.

  • service_event_qos_profile – QoSProfile to use when creating service event publisher.

  • introspection_state – ServiceIntrospectionState to set introspection.

destroy()
get_pending_request(sequence_number: int) Future

Get a future from the list of pending requests.

Parameters:

sequence_number – Number identifying the pending request.

Returns:

The future corresponding to the sequence_number.

Raises:

KeyError if the sequence_number is not in the pending requests.

property handle
remove_pending_request(future: Future) None

Remove a future from the list of pending requests.

This prevents a future from receiving a response and executing its done callbacks.

Parameters:

future – A future returned from call_async()

service_is_ready() bool

Check if there is a service server ready.

Returns:

True if a server is ready, False otherwise.

wait_for_service(timeout_sec: float | None = None) bool

Wait for a service server to become ready.

Returns as soon as a server becomes ready or if the timeout expires.

Parameters:

timeout_sec – Seconds to wait. If None, then wait forever.

Returns:

True if server became ready while waiting or False on a timeout.

Service

class rclpy.service.Service(service_impl: rpyutils.import_c_library.Service, srv_type: SrvType, srv_name: str, callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse], callback_group: CallbackGroup, qos_profile: rclpy.qos.QoSProfile)

Create a container for a ROS service server.

Warning

Users should not create a service server with this constuctor, instead they should call Node.create_service().

Parameters:
  • service_impl_rclpy.Service wrapping the underlying rcl_service_t object.

  • srv_type – The service type.

  • srv_name – The name of the service.

  • callback – The callback that should be called to handle the request.

  • callback_group – The callback group for the service server. If None, then the nodes default callback group is used.

  • qos_profile – The quality of service profile to apply the service server.

configure_introspection(clock: rclpy.clock.Clock, service_event_qos_profile: rclpy.qos.QoSProfile, introspection_state: rpyutils.import_c_library.service_introspection.ServiceIntrospectionState) None

Configure service introspection.

Parameters:
  • clock – Clock to use for generating timestamps.

  • service_event_qos_profile – QoSProfile to use when creating service event publisher.

  • introspection_state – ServiceIntrospectionState to set introspection.

destroy()
property handle
send_response(response: SrvTypeResponse, header) None

Send a service response.

Parameters:
  • response – The service response.

  • header – Service header from the original request.

Raises:

TypeError if the type of the passed response isn’t an instance of the Response type of the provided service when the service was constructed.