C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin_ros::ServiceState< ServiceT > Class Template Reference

A state class that interacts with a ROS 2 service. More...

#include <service_state.hpp>

Inheritance diagram for yasmin_ros::ServiceState< ServiceT >:
Collaboration diagram for yasmin_ros::ServiceState< ServiceT >:

Public Member Functions

 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.
 
 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.
 
 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.
 
 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.
 
 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.
 
 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.
 
std::string execute (std::shared_ptr< yasmin::blackboard::Blackboard > blackboard) override
 Execute the service call and handle the response.
 
- Public Member Functions inherited from yasmin::State
 State (const std::set< std::string > &outcomes)
 Constructs a State with a set of possible outcomes.
 
StateStatus get_status () const
 Gets the current status of the state.
 
bool is_idle () const
 Checks if the state is idle.
 
bool is_running () const
 Checks if the state is currently running.
 
bool is_canceled () const
 Checks if the state has been canceled.
 
bool is_completed () const
 Checks if the state has completed execution.
 
std::string operator() (std::shared_ptr< blackboard::Blackboard > blackboard)
 Executes the state and returns the outcome.
 
virtual void cancel_state ()
 Cancels the current state execution.
 
std::set< std::string > const & get_outcomes ()
 Gets the set of possible outcomes for this state.
 
virtual std::string to_string ()
 Converts the state to a string representation.
 

Protected Attributes

rclcpp::Node::SharedPtr node_
 Shared pointer to the ROS 2 node.
 
- Protected Attributes inherited from yasmin::State
std::set< std::string > outcomes
 The possible outcomes of this state.
 

Private Types

using Request = typename ServiceT::Request::SharedPtr
 Alias for the service request type.
 
using Response = typename ServiceT::Response::SharedPtr
 Alias for the service response type.
 
using CreateRequestHandler
 Function type for creating a request.
 
using ResponseHandler
 Function type for handling a response.
 

Private Member Functions

Request create_request (std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
 Create a service request based on the blackboard.
 
void response_callback (typename rclcpp::Client< ServiceT >::SharedFuture response)
 Callback for handling the service response.
 

Private Attributes

std::shared_ptr< rclcpp::Client< ServiceT > > service_client
 Shared pointer to the service client.
 
CreateRequestHandler create_request_handler
 Function to create service requests.
 
ResponseHandler response_handler
 Function to handle service responses.
 
std::string srv_name
 Name of the service.
 
int wait_timeout
 Maximum wait time for service availability.
 
int response_timeout
 Timeout for the service response.
 
int maximum_retry
 Maximum number of retries.
 
std::condition_variable response_done_cond
 Condition variable for response completion.
 
std::mutex response_done_mutex
 Mutex for protecting response completion.
 
Response service_response
 Shared pointer to the service response.
 

Detailed Description

template<typename ServiceT>
class yasmin_ros::ServiceState< ServiceT >

A state class that interacts with a ROS 2 service.

This class manages communication with a specified ROS 2 service, allowing it to send requests and handle responses. It extends the base yasmin::State class.

Template Parameters
ServiceTThe type of the ROS 2 service this state interacts with.

Member Typedef Documentation

◆ CreateRequestHandler

template<typename ServiceT>
using yasmin_ros::ServiceState< ServiceT >::CreateRequestHandler
private
Initial value:
std::function<Request(std::shared_ptr<yasmin::blackboard::Blackboard>)>
typename ServiceT::Request::SharedPtr Request
Alias for the service request type.
Definition service_state.hpp:50

Function type for creating a request.

◆ Request

template<typename ServiceT>
using yasmin_ros::ServiceState< ServiceT >::Request = typename ServiceT::Request::SharedPtr
private

Alias for the service request type.

◆ Response

template<typename ServiceT>
using yasmin_ros::ServiceState< ServiceT >::Response = typename ServiceT::Response::SharedPtr
private

Alias for the service response type.

◆ ResponseHandler

template<typename ServiceT>
using yasmin_ros::ServiceState< ServiceT >::ResponseHandler
private
Initial value:
std::function<std::string(
std::shared_ptr<yasmin::blackboard::Blackboard>, Response)>
typename ServiceT::Response::SharedPtr Response
Alias for the service response type.
Definition service_state.hpp:52

Function type for handling a response.

Constructor & Destructor Documentation

◆ ServiceState() [1/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::ServiceState ( const std::string & srv_name,
CreateRequestHandler create_request_handler,
int wait_timeout = -1,
int response_timeout = -1,
int maximum_retry = 3 )
inline

Construct a ServiceState with a request handler and outcomes.

Parameters
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.

◆ ServiceState() [2/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::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 )
inline

Construct a ServiceState with a request handler and outcomes.

Parameters
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
outcomesA set of possible outcomes for this state.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.

◆ ServiceState() [3/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::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 )
inline

Construct a ServiceState with a request handler and outcomes.

Parameters
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
outcomesA set of possible outcomes for this state.
callback_group(Optional) The callback group for the subscription.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.

◆ ServiceState() [4/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::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 )
inline

Construct a ServiceState with a request handler and response handler.

Parameters
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
response_handler(Optional) Function to handle the service response.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.

◆ ServiceState() [5/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::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 )
inline

Construct a ServiceState with a request handler and response handler.

Parameters
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
outcomesA set of possible outcomes for this state.
response_handler(Optional) Function to handle the service response.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.

◆ ServiceState() [6/6]

template<typename ServiceT>
yasmin_ros::ServiceState< ServiceT >::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 )
inline

Construct a ServiceState with a ROS 2 node and handlers.

Parameters
nodeA shared pointer to the ROS 2 node.
srv_nameThe name of the service to call.
create_request_handlerFunction to create a service request.
outcomesA set of possible outcomes for this state.
response_handler(Optional) Function to handle the service response.
callback_group(Optional) The callback group for the subscription.
wait_timeoutMaximum time to wait for the service to become available, in seconds. Default is -1 (wait indefinitely).
response_timeoutMaximum time to wait for the service response, in seconds. Default is -1 (wait indefinitely).
maximum_retry(Optional) Maximum retries of the service if it returns timeout. Default is 3.
Exceptions
std::invalid_argumentif the create_request_handler is nullptr.

Member Function Documentation

◆ create_request()

template<typename ServiceT>
Request yasmin_ros::ServiceState< ServiceT >::create_request ( std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
inlineprivate

Create a service request based on the blackboard.

Parameters
blackboardA shared pointer to the blackboard containing data for request creation.
Returns
Request The created service request.

◆ execute()

template<typename ServiceT>
std::string yasmin_ros::ServiceState< ServiceT >::execute ( std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
inlineoverridevirtual

Execute the service call and handle the response.

This function creates a request based on the blackboard data, waits for the service to become available, sends the request, and processes the response.

Parameters
blackboardA shared pointer to the blackboard containing data for request creation.
Returns
std::string The outcome of the service call, which can be SUCCEED, ABORT, or TIMEOUT.

Reimplemented from yasmin::State.

◆ response_callback()

template<typename ServiceT>
void yasmin_ros::ServiceState< ServiceT >::response_callback ( typename rclcpp::Client< ServiceT >::SharedFuture response)
inlineprivate

Callback for handling the service response.

This function is called when the service response is received. It stores the response and signals the waiting thread.

Parameters
responseThe response received from the service.

Member Data Documentation

◆ create_request_handler

template<typename ServiceT>
CreateRequestHandler yasmin_ros::ServiceState< ServiceT >::create_request_handler
private

Function to create service requests.

◆ maximum_retry

template<typename ServiceT>
int yasmin_ros::ServiceState< ServiceT >::maximum_retry
private

Maximum number of retries.

◆ node_

template<typename ServiceT>
rclcpp::Node::SharedPtr yasmin_ros::ServiceState< ServiceT >::node_
protected

Shared pointer to the ROS 2 node.

◆ response_done_cond

template<typename ServiceT>
std::condition_variable yasmin_ros::ServiceState< ServiceT >::response_done_cond
private

Condition variable for response completion.

◆ response_done_mutex

template<typename ServiceT>
std::mutex yasmin_ros::ServiceState< ServiceT >::response_done_mutex
private

Mutex for protecting response completion.

◆ response_handler

template<typename ServiceT>
ResponseHandler yasmin_ros::ServiceState< ServiceT >::response_handler
private

Function to handle service responses.

◆ response_timeout

template<typename ServiceT>
int yasmin_ros::ServiceState< ServiceT >::response_timeout
private

Timeout for the service response.

◆ service_client

template<typename ServiceT>
std::shared_ptr<rclcpp::Client<ServiceT> > yasmin_ros::ServiceState< ServiceT >::service_client
private

Shared pointer to the service client.

◆ service_response

template<typename ServiceT>
Response yasmin_ros::ServiceState< ServiceT >::service_response
private

Shared pointer to the service response.

◆ srv_name

template<typename ServiceT>
std::string yasmin_ros::ServiceState< ServiceT >::srv_name
private

Name of the service.

◆ wait_timeout

template<typename ServiceT>
int yasmin_ros::ServiceState< ServiceT >::wait_timeout
private

Maximum wait time for service availability.


The documentation for this class was generated from the following file: