16#ifndef YASMIN_ROS__SERVICE_STATE_HPP
17#define YASMIN_ROS__SERVICE_STATE_HPP
24#include "rclcpp/rclcpp.hpp"
44 using Request =
typename ServiceT::Request::SharedPtr;
46 using Response =
typename ServiceT::Response::SharedPtr;
50 std::function<
Request(std::shared_ptr<yasmin::blackboard::Blackboard>)>;
53 std::shared_ptr<yasmin::blackboard::Blackboard>,
Response)>;
119 if (node ==
nullptr) {
135 throw std::invalid_argument(
"create_request_handler is needed");
151 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard)
override {
156 RCLCPP_INFO(this->
node_->get_logger(),
"Waiting for service '%s'",
157 this->srv_name.c_str());
159 std::chrono::duration<int64_t, std::ratio<1>>(this->
timeout));
161 if (!srv_available) {
162 RCLCPP_WARN(this->
node_->get_logger(),
163 "Timeout reached, service '%s' is not available",
164 this->srv_name.c_str());
169 RCLCPP_INFO(this->
node_->get_logger(),
"Sending request to service '%s'",
170 this->srv_name.c_str());
Represents a state in a state machine.
Definition state.hpp:42
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:46
State(std::set< std::string > outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
A state class that interacts with a ROS 2 service.
Definition service_state.hpp:42
CreateRequestHandler create_request_handler
Function to create service requests.
Definition service_state.hpp:194
std::function< Request(std::shared_ptr< yasmin::blackboard::Blackboard >)> CreateRequestHandler
Function type for creating a request.
Definition service_state.hpp:49
ResponseHandler response_handler
Function to handle service responses.
Definition service_state.hpp:196
int timeout
Maximum wait time for service availability.
Definition service_state.hpp:200
ServiceState(const rclcpp::Node::SharedPtr &node, std::string srv_name, CreateRequestHandler create_request_handler, std::set< std::string > outcomes, ResponseHandler response_handler, int timeout=-1.0)
Construct a ServiceState with a ROS 2 node and handlers.
Definition service_state.hpp:102
typename ServiceT::Response::SharedPtr Response
Alias for the service response type.
Definition service_state.hpp:46
typename ServiceT::Request::SharedPtr Request
Alias for the service request type.
Definition service_state.hpp:44
std::string execute(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard) override
Execute the service call and handle the response.
Definition service_state.hpp:151
std::string srv_name
Name of the service.
Definition service_state.hpp:198
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition service_state.hpp:190
std::shared_ptr< rclcpp::Client< ServiceT > > service_client
Shared pointer to the service client.
Definition service_state.hpp:192
ServiceState(std::string srv_name, CreateRequestHandler create_request_handler, std::set< std::string > outcomes, int timeout=-1.0)
Construct a ServiceState with a request handler and outcomes.
Definition service_state.hpp:65
ServiceState(std::string srv_name, CreateRequestHandler create_request_handler, std::set< std::string > outcomes, ResponseHandler response_handler, int timeout=-1.0)
Construct a ServiceState with a request handler and response handler.
Definition service_state.hpp:82
std::function< std::string( std::shared_ptr< yasmin::blackboard::Blackboard >, Response)> ResponseHandler
Function type for handling a response.
Definition service_state.hpp:52
Request create_request(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
Create a service request based on the blackboard.
Definition service_state.hpp:210
static std::shared_ptr< YasminNode > get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:73
constexpr char SUCCEED[]
Constant representing a successful action outcome.
Definition basic_outcomes.hpp:39
constexpr char TIMEOUT[]
Constant representing a timed-out action outcome.
Definition basic_outcomes.hpp:63
constexpr char ABORT[]
Constant representing an aborted action outcome.
Definition basic_outcomes.hpp:47
Definition action_state.hpp:35