Service Server
mrs_lib::ServiceServer
-
template<class ServiceType>
class ServiceServer rclcpp::Service wrapper with coroutines support.
- Template Parameters:
ServiceType – Type of the service this server will provide.
Public Types
-
using ServiceCallback = std::function<void(std::shared_ptr<const Request> req, std::shared_ptr<Response>)>
Type of the accepted callback.
-
using ServiceCoroCallback = CoroCallback<void(std::shared_ptr<const Request> req, std::shared_ptr<Response>)>
Type of the accepted coroutine callback.
Public Functions
-
ServiceServer(const ServiceServerOptions &options, std::string_view service_name, ServiceCallback callback)
Construct the service server with the specified callback.
options Options to configure the service server.
- Parameters:
service_name – Name of the service to provide.
callback – Callback to call when a request is received.
-
ServiceServer(const ServiceServerOptions &options, std::string_view service_name, ServiceCoroCallback callback)
Construct the service server with the specified coroutine callback.
options Options to configure the service server.
- Parameters:
service_name – Name of the service to provide.
callback – Coroutine callback to call when a request is received.
-
std::string get_service_name() const
Returns the resolved (full) name of the service handled by this service server.
- 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 server.
- Returns:
The name of the handled service.
mrs_lib::ServiceServerOptions
-
struct ServiceServerOptions
Options for configuring the ServiceServer.
Public Members
-
ServiceServerNodeInterfaces node_interfaces
ROS node interfaces used by the service server.
-
rclcpp::QoS qos = rclcpp::ServicesQoS()
QOS settings for the service server.
-
std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr
ROS callback group used by the service server callbacks.
-
ServiceServerNodeInterfaces 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_server_(mrs_lib::ServiceServerOptions{.node_interfaces = *this}, "example_service",
8 std::bind_front(&ExampleNode::service_server_callback, this))
9 {
10 }
11
12 [[nodiscard]] bool get_last_received() const
13 {
14 return last_received_;
15 }
16
17private:
18 void service_server_callback(std::shared_ptr<const std_srvs::srv::SetBool::Request> request, std::shared_ptr<std_srvs::srv::SetBool::Response> response)
19 {
20 logger_.info("Service called with: {}", request->data);
21 last_received_ = request->data;
22 response->success = true;
23 response->message = std::format("Successfully received: '{}'", request->data);
24 }
25
26 mrs_lib::Logger logger_;
27
28 std::atomic<bool> last_received_ = false;
29
30 mrs_lib::ServiceServer<std_srvs::srv::SetBool> service_server_;
31};