Service Client
mrs_lib::ServiceClient
-
template<class ServiceType>
class ServiceClient rclcpp::Client wrapper with coroutines support.
When using services, it is often useful to wait for the response right after sending it. ServiceClient makes this pattern simpler by allowing the use of coroutines. It provides ServiceClient::call, a coroutine interface for calling the service.
- Template Parameters:
ServiceType – Type of the service this client will call.
Public Functions
-
ServiceClient(const ServiceClientOptions &options, std::string_view service_name)
Construct the client with the specified options.
- Parameters:
options – Options to configure the service client.
service_name – The name of the service to call using this client.
Coroutine call of the service.
- Parameters:
request – Request to send to the service server.
- Returns:
Response of the server if the call succeeded, error message otherwise.
Async call of the service.
When the future is returned, it is up to the user to check, if the service server responded. This can be done for example by checking the readiness of the future in a timer callback.
Warning
If the future is not ready, calling
geton the future will block the current thread until the response is sent. This may cause deadlocks! (Eg. when service client is on the same mutually exclusive callback group as the currently running function)- Parameters:
request – Request to send to the service server.
- Returns:
Future that will contain the server response if successful, error message otherwise.
-
std::string get_service_name() const
Returns the resolved (full) name of the service handled by this service client.
- Returns:
The name of the handled service.
-
std::string get_unresolved_service_name() const
Returns the unresolved name of the service handled by this service client.
- Returns:
The name of the handled service.
-
template<typename RepT = int64_t, typename RatioT = std::milli>
bool wait_for_service(std::chrono::duration<RepT, RatioT> timeout) Wait for the service to become available.
- Template Parameters:
RepT – Arithmetic type representing the number of ticks.
RatioT – std::ratio representing the tick period.
- Parameters:
timeout – Maximum time to wait for the service to be available.
- Returns:
trueif the service became available within the timeout,falseotherwise.
-
bool is_service_ready() const
Check if the service is available.
- Returns:
trueif the service is available,falseotherwise.
-
size_t prune_pending_requests()
Remove all pending requests.
Warning
Calling this will not wake up threads that are waiting on
future::get. These threads will be deadlocked, unless woken by some other means. Coroutines waiting on ServiceClient::call will be cancelled.- Returns:
Number of pending requests that were removed.
mrs_lib::ServiceClientOptions
-
struct ServiceClientOptions
Options for configuring the ServiceClient.
Public Members
-
ServiceClientNodeInterfaces node_interfaces
ROS node interfaces used by the service client.
-
rclcpp::QoS qos = rclcpp::ServicesQoS()
QOS settings for the service client.
-
std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr
ROS callback group used by the service client callbacks.
-
ServiceClientNodeInterfaces node_interfaces
Example
1class ExampleNode : public rclcpp::Node
2{
3public:
4 ExampleNode(const rclcpp::NodeOptions& options = rclcpp::NodeOptions{})
5 : Node("example_node", options),
6 logger_(*this),
7 service_client_(
8 mrs_lib::ServiceClientOptions{
9 .node_interfaces = *this,
10 },
11 "example_service")
12 {
13 }
14
15 // Coroutine to do the work.
16 // This can be called eg. by other coroutines or callbacks.
17 mrs_lib::Task<> work(bool val)
18 {
19 logger_.info("Sending service request with val: {}", val);
20
21 auto request = std::make_shared<std_srvs::srv::SetBool::Request>();
22 request->data = val;
23
24 auto response_opt = co_await service_client_.call(request);
25 logger_.info("Received response.");
26 if (response_opt.has_value())
27 {
28 auto&& response = response_opt.value();
29 logger_.info("Received response with message: '{}'", response->message);
30 last_received_response_message_.store(response->message);
31 } else
32 {
33 logger_.error("Empty response!");
34 }
35 }
36
37 std::string get_last_received_response_message()
38 {
39 return last_received_response_message_.load();
40 }
41
42private:
43 mrs_lib::Logger logger_;
44
45 mrs_lib::OwningMutex<std::string> last_received_response_message_;
46
47 mrs_lib::ServiceClient<std_srvs::srv::SetBool> service_client_;
48};