mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
service_client.hpp
1#ifndef MRS_LIB_SERVICE_CLIENT_HPP_
2#define MRS_LIB_SERVICE_CLIENT_HPP_
3
4#include <expected>
5#include <future>
6#include <memory>
7#include <string>
8#include <string_view>
9
10#include <rclcpp/callback_group.hpp>
11#include <rclcpp/node_interfaces/node_base_interface.hpp>
12#include <rclcpp/node_interfaces/node_clock_interface.hpp>
13#include <rclcpp/node_interfaces/node_graph_interface.hpp>
14#include <rclcpp/node_interfaces/node_interfaces.hpp>
15#include <rclcpp/node_interfaces/node_logging_interface.hpp>
16#include <rclcpp/node_interfaces/node_services_interface.hpp>
17#include <rclcpp/qos.hpp>
18
19#include "mrs_lib/coro/task.hpp"
21
22namespace mrs_lib
23{
24
29 rclcpp::node_interfaces::NodeInterfaces<rclcpp::node_interfaces::NodeServicesInterface, rclcpp::node_interfaces::NodeGraphInterface,
30 rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeLoggingInterface,
31 rclcpp::node_interfaces::NodeClockInterface>;
32
37 {
40
42 rclcpp::QoS qos = rclcpp::ServicesQoS();
44 std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr;
45 };
46
56 template <class ServiceType>
58 {
59 private:
60 class Impl;
61
62 using Request = ServiceType::Request;
63 using Response = ServiceType::Response;
64
65 public:
72 ServiceClient(const ServiceClientOptions& options, std::string_view service_name);
73
81 Task<std::expected<std::shared_ptr<Response>, std::string>> call(const std::shared_ptr<Request>& request);
82
99 std::expected<std::future<std::shared_ptr<Response>>, std::string> call_async(const std::shared_ptr<Request>& request);
100
106 [[nodiscard]] std::string get_service_name() const;
107
113 [[nodiscard]] std::string get_unresolved_service_name() const;
114
124 template <typename RepT = int64_t, typename RatioT = std::milli>
125 [[nodiscard]] bool wait_for_service(std::chrono::duration<RepT, RatioT> timeout);
126
132 [[nodiscard]] bool is_service_ready() const;
133
144 size_t prune_pending_requests();
145
146 private:
147 Pimpl<Impl> impl_;
148 };
149
150} // namespace mrs_lib
151
152#ifndef MRS_LIB_SERVICE_CLIENT_IMPL_HPP_
153#include "mrs_lib/service_client.impl.hpp"
154#endif
155
156
157#endif // MRS_LIB_SERVICE_CLIENT_HPP_
Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implem...
Definition pimpl.hpp:39
size_t prune_pending_requests()
Remove all pending requests.
Definition service_client.impl.hpp:217
bool wait_for_service(std::chrono::duration< RepT, RatioT > timeout)
Wait for the service to become available.
Definition service_client.impl.hpp:205
std::string get_unresolved_service_name() const
Returns the unresolved name of the service handled by this service client.
Definition service_client.impl.hpp:198
std::string get_service_name() const
Returns the resolved (full) name of the service handled by this service client.
Definition service_client.impl.hpp:192
ServiceClient(const ServiceClientOptions &options, std::string_view service_name)
Construct the client with the specified options.
Definition service_client.impl.hpp:174
std::expected< std::future< std::shared_ptr< Response > >, std::string > call_async(const std::shared_ptr< Request > &request)
Async call of the service.
Definition service_client.impl.hpp:186
bool is_service_ready() const
Check if the service is available.
Definition service_client.impl.hpp:211
Task< std::expected< std::shared_ptr< Response >, std::string > > call(const std::shared_ptr< Request > &request)
Coroutine call of the service.
Definition service_client.impl.hpp:180
Task type for creating coroutines.
Definition task.hpp:230
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
rclcpp::node_interfaces::NodeInterfaces< rclcpp::node_interfaces::NodeServicesInterface, rclcpp::node_interfaces::NodeGraphInterface, rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeLoggingInterface, rclcpp::node_interfaces::NodeClockInterface > ServiceClientNodeInterfaces
ROS node interfaces required by the ServiceClient.
Definition service_client.hpp:28
Options for configuring the ServiceClient.
Definition service_client.hpp:37
rclcpp::QoS qos
QOS settings for the service client.
Definition service_client.hpp:42
ServiceClientNodeInterfaces node_interfaces
ROS node interfaces used by the service client.
Definition service_client.hpp:39
std::shared_ptr< rclcpp::CallbackGroup > callback_group
ROS callback group used by the service client callbacks.
Definition service_client.hpp:44