C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
service_state.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN_ROS__SERVICE_STATE_HPP
17#define YASMIN_ROS__SERVICE_STATE_HPP
18
19#include <condition_variable>
20#include <functional>
21#include <memory>
22#include <mutex>
23#include <set>
24#include <string>
25
26#include "rclcpp/rclcpp.hpp"
27
29#include "yasmin/logs.hpp"
30#include "yasmin/state.hpp"
34
35using namespace std::placeholders;
36
37namespace yasmin_ros {
38
48template <typename ServiceT> class ServiceState : public yasmin::State {
50 using Request = typename ServiceT::Request::SharedPtr;
52 using Response = typename ServiceT::Response::SharedPtr;
53
56 std::function<Request(std::shared_ptr<yasmin::blackboard::Blackboard>)>;
58 using ResponseHandler = std::function<std::string(
59 std::shared_ptr<yasmin::blackboard::Blackboard>, Response)>;
60
61public:
74 ServiceState(const std::string &srv_name,
76 int wait_timeout = -1, int response_timeout = -1,
77 int maximum_retry = 3)
79 std::set<std::string>(), nullptr, nullptr, wait_timeout,
81
94 ServiceState(const std::string &srv_name,
96 const std::set<std::string> &outcomes, int wait_timeout = -1,
97 int response_timeout = -1, int maximum_retry = 3)
99 nullptr, nullptr, wait_timeout, response_timeout,
100 maximum_retry) {}
101
116 ServiceState(const std::string &srv_name,
118 const std::set<std::string> &outcomes,
119 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr,
120 int wait_timeout = -1, int response_timeout = -1,
121 int maximum_retry = 3)
123 nullptr, callback_group, wait_timeout, response_timeout,
124 maximum_retry) {}
125
147
171
190 ServiceState(const rclcpp::Node::SharedPtr &node, const std::string &srv_name,
192 const std::set<std::string> &outcomes,
194 rclcpp::CallbackGroup::SharedPtr callback_group,
195 int wait_timeout = -1, int response_timeout = -1,
196 int maximum_retry = 3)
200
201 if (this->wait_timeout > 0 || this->response_timeout > 0) {
202 this->outcomes.insert(basic_outcomes::TIMEOUT);
203 }
204
205 if (!outcomes.empty()) {
206 this->outcomes.insert(outcomes.begin(), outcomes.end());
207 }
208
209 // Assign the appropriate ROS 2 node
210 if (node == nullptr) {
212 } else {
213 this->node_ = node;
214 }
215
216 // Create a service client (compatible with different rclcpp versions)
217 this->service_client =
219 this->node_, srv_name, callback_group);
220
221 // Set the request and response handlers
224
225 // Validate request handler
226 if (this->create_request_handler == nullptr) {
227 throw std::invalid_argument("create_request_handler is needed");
228 }
229 }
230
242 std::string
243 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) override {
244 Request request = this->create_request(blackboard);
245 std::unique_lock<std::mutex> lock(this->response_done_mutex);
246 int retry_count = 0;
247
248 // Wait for the service to become available
249 YASMIN_LOG_INFO("Waiting for service '%s'", this->srv_name.c_str());
250
251 while (!this->service_client->wait_for_service(
252 std::chrono::duration<int64_t, std::ratio<1>>(this->wait_timeout))) {
253 YASMIN_LOG_WARN("Timeout reached, service '%s' is not available",
254 this->srv_name.c_str());
255 if (retry_count < this->maximum_retry) {
256 retry_count++;
257 YASMIN_LOG_WARN("Retrying to connect to service '%s' "
258 "(%d/%d)",
259 this->srv_name.c_str(), retry_count,
260 this->maximum_retry);
261 } else {
263 }
264 }
265
266 // Send the service request
267 YASMIN_LOG_INFO("Sending request to service '%s'", this->srv_name.c_str());
268
269 // Send request with callback
270 this->service_client->async_send_request(
271 request, std::bind(&ServiceState::response_callback, this, _1));
272
273 // Wait for response with timeout
274 if (this->response_timeout > 0) {
275 while (this->response_done_cond.wait_for(
276 lock, std::chrono::seconds(this->response_timeout)) ==
277 std::cv_status::timeout) {
279 "Timeout reached while waiting for response from service '%s'",
280 this->srv_name.c_str());
281 if (retry_count < this->maximum_retry) {
282 retry_count++;
283 YASMIN_LOG_WARN("Retrying to wait for service '%s' response (%d/%d)",
284 this->srv_name.c_str(), retry_count,
285 this->maximum_retry);
286 } else {
288 }
289 }
290 } else {
291 this->response_done_cond.wait(lock);
292 }
293
294 if (this->is_canceled()) {
296 }
297
298 // Process the service response
299 if (this->service_response) {
300 if (response_handler != nullptr) {
301 std::string outcome =
302 this->response_handler(blackboard, this->service_response);
303 return outcome;
304 }
306 } else {
308 }
309 }
310
311protected:
313 rclcpp::Node::SharedPtr node_;
314
315private:
317 std::shared_ptr<rclcpp::Client<ServiceT>> service_client;
323 std::string srv_name;
330
332 std::condition_variable response_done_cond;
337
345 Request
346 create_request(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) {
347 return this->create_request_handler(blackboard);
348 }
349
358 void
359 response_callback(typename rclcpp::Client<ServiceT>::SharedFuture response) {
360 std::lock_guard<std::mutex> lock(this->response_done_mutex);
361 this->service_response = response.get();
362 this->response_done_cond.notify_one();
363 }
364};
365
366} // namespace yasmin_ros
367
368#endif // YASMIN_ROS__SERVICE_STATE_HPP
Represents a state in a state machine.
Definition state.hpp:53
bool is_canceled() const
Checks if the state has been canceled.
Definition state.cpp:41
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:57
static rclcpp::Client< ServiceT >::SharedPtr get_or_create_service_client(rclcpp::Node::SharedPtr node, const std::string &service_name, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Get an existing service client from the cache or create a new one.
Definition ros_clients_cache.hpp:114
std::condition_variable response_done_cond
Condition variable for response completion.
Definition service_state.hpp:332
CreateRequestHandler create_request_handler
Function to create service requests.
Definition service_state.hpp:319
std::function< Request(std::shared_ptr< yasmin::blackboard::Blackboard >)> CreateRequestHandler
Function type for creating a request.
Definition service_state.hpp:55
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, const std::set< std::string > &outcomes, ResponseHandler response_handler, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a request handler and response handler.
Definition service_state.hpp:163
ServiceState(const rclcpp::Node::SharedPtr &node, const std::string &srv_name, CreateRequestHandler create_request_handler, const std::set< std::string > &outcomes, ResponseHandler response_handler, rclcpp::CallbackGroup::SharedPtr callback_group, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a ROS 2 node and handlers.
Definition service_state.hpp:190
ResponseHandler response_handler
Function to handle service responses.
Definition service_state.hpp:321
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, const std::set< std::string > &outcomes, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a request handler and outcomes.
Definition service_state.hpp:94
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, const std::set< std::string > &outcomes, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a request handler and outcomes.
Definition service_state.hpp:116
typename ServiceT::Response::SharedPtr Response
Alias for the service response type.
Definition service_state.hpp:52
typename ServiceT::Request::SharedPtr Request
Alias for the service request type.
Definition service_state.hpp:50
std::mutex response_done_mutex
Mutex for protecting response completion.
Definition service_state.hpp:334
int wait_timeout
Maximum wait time for service availability.
Definition service_state.hpp:325
std::string execute(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard) override
Execute the service call and handle the response.
Definition service_state.hpp:243
std::string srv_name
Name of the service.
Definition service_state.hpp:323
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition service_state.hpp:313
int response_timeout
Timeout for the service response.
Definition service_state.hpp:327
Response service_response
Shared pointer to the service response.
Definition service_state.hpp:336
std::shared_ptr< rclcpp::Client< ServiceT > > service_client
Shared pointer to the service client.
Definition service_state.hpp:317
std::function< std::string( std::shared_ptr< yasmin::blackboard::Blackboard >, Response)> ResponseHandler
Function type for handling a response.
Definition service_state.hpp:58
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a request handler and outcomes.
Definition service_state.hpp:74
int maximum_retry
Maximum number of retries.
Definition service_state.hpp:329
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, ResponseHandler response_handler, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Construct a ServiceState with a request handler and response handler.
Definition service_state.hpp:140
void response_callback(typename rclcpp::Client< ServiceT >::SharedFuture response)
Callback for handling the service response.
Definition service_state.hpp:359
Request create_request(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
Create a service request based on the blackboard.
Definition service_state.hpp:346
static std::shared_ptr< YasminNode > get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:73
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
#define YASMIN_LOG_WARN(text,...)
Definition logs.hpp:164
constexpr char SUCCEED[]
Constant representing a successful action outcome.
Definition basic_outcomes.hpp:39
constexpr char CANCEL[]
Constant representing a canceled action outcome.
Definition basic_outcomes.hpp:63
constexpr char TIMEOUT[]
Constant representing a timed-out action outcome.
Definition basic_outcomes.hpp:71
constexpr char ABORT[]
Constant representing an aborted action outcome.
Definition basic_outcomes.hpp:47
Definition action_state.hpp:37