Modulo 5.0.0
Loading...
Searching...
No Matches
ServiceClient.hpp
1#pragma once
2
3#include <chrono>
4#include <exception>
5
6#include <rclcpp/rclcpp.hpp>
7
8namespace modulo_utils::testutils {
9
10using namespace std::chrono_literals;
11
12template<typename SrvT>
13class ServiceClient : public rclcpp::Node {
14public:
15 ServiceClient(const rclcpp::NodeOptions& options, const std::string& service) : rclcpp::Node("client_node", options) {
16 this->client_ = this->create_client<SrvT>(service);
17 if (!this->client_->wait_for_service(1s)) {
18 throw std::runtime_error("Service not available");
19 }
20 }
21
22 typename rclcpp::Client<SrvT>::FutureAndRequestId call_async(const std::shared_ptr<typename SrvT::Request>& request) {
23 return this->client_->async_send_request(request);
24 }
25
26 std::shared_ptr<rclcpp::Client<SrvT>> client_;
27};
28}// namespace modulo_utils::testutils