16#ifndef YASMIN_ROS__ACTION_STATE_HPP
17#define YASMIN_ROS__ACTION_STATE_HPP
19#include <condition_variable>
25#include "rclcpp/rclcpp.hpp"
26#include "rclcpp_action/rclcpp_action.hpp"
33using namespace std::placeholders;
48 using Goal =
typename ActionT::Goal;
50 using Result =
typename ActionT::Result::SharedPtr;
56 typename rclcpp_action::Client<ActionT>::SendGoalOptions;
58 using ActionClient =
typename rclcpp_action::Client<ActionT>::SharedPtr;
60 using GoalHandle = rclcpp_action::ClientGoalHandle<ActionT>;
63 std::function<
Goal(std::shared_ptr<yasmin::blackboard::Blackboard>)>;
66 std::shared_ptr<yasmin::blackboard::Blackboard>,
Result)>;
69 std::function<void(std::shared_ptr<yasmin::blackboard::Blackboard>,
70 std::shared_ptr<const Feedback>)>;
173 for (std::string outcome :
outcomes) {
174 this->outcomes.insert(outcome);
178 if (node ==
nullptr) {
188 throw std::invalid_argument(
"create_goal_handler is needed");
237 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) {
244 RCLCPP_INFO(this->
node_->get_logger(),
"Waiting for action '%s'",
245 this->action_name.c_str());
246 bool act_available = this->
action_client->wait_for_action_server(
247 std::chrono::duration<int64_t, std::ratio<1>>(this->
timeout));
249 if (!act_available) {
250 RCLCPP_WARN(this->
node_->get_logger(),
251 "Timeout reached, action '%s' is not available",
252 this->action_name.c_str());
258 send_goal_options.goal_response_callback =
261 send_goal_options.result_callback =
264 if (this->feedback_handler) {
265 send_goal_options.feedback_callback =
266 [
this, blackboard](
typename GoalHandle::SharedPtr,
267 std::shared_ptr<const Feedback> feedback) {
272 RCLCPP_INFO(this->
node_->get_logger(),
"Sending goal to action '%s'",
273 this->action_name.c_str());
274 this->
action_client->async_send_goal(goal, send_goal_options);
280 case rclcpp_action::ResultCode::CANCELED:
283 case rclcpp_action::ResultCode::ABORTED:
286 case rclcpp_action::ResultCode::SUCCEEDED:
345 std::shared_future<typename GoalHandle::SharedPtr> future) {
346 std::lock_guard<std::mutex> lock(this->goal_handle_mutex);
347 this->goal_handle = future.get();
359 std::lock_guard<std::mutex> lock(this->goal_handle_mutex);
372 this->action_result = result.result;
373 this->action_status = result.code;
374 this->action_done_cond.notify_one();
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
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:94
A state class for handling ROS 2 action client operations.
Definition action_state.hpp:46
int timeout
Maximum time to wait for the action server.
Definition action_state.hpp:334
void result_callback(const typename GoalHandle::WrappedResult &result)
Callback for handling the result of the action.
Definition action_state.hpp:371
std::condition_variable action_done_cond
Condition variable for action completion.
Definition action_state.hpp:307
void goal_response_callback(const typename GoalHandle::SharedPtr &goal_handle)
Callback for handling the goal response.
Definition action_state.hpp:358
std::mutex action_cancel_mutex
Mutex for protecting action cancellation.
Definition action_state.hpp:314
ActionState(std::string action_name, CreateGoalHandler create_goal_handler, std::set< std::string > outcomes, ResultHandler result_handler=nullptr, FeedbackHandler feedback_handler=nullptr, int timeout=-1.0)
Construct an ActionState with outcomes and handlers.
Definition action_state.hpp:133
rclcpp_action::ClientGoalHandle< ActionT > GoalHandle
Handle for the action goal.
Definition action_state.hpp:60
std::function< std::string( std::shared_ptr< yasmin::blackboard::Blackboard >, Result)> ResultHandler
Function type for handling results.
Definition action_state.hpp:65
void cancel_done()
Notify that the action cancellation has completed.
Definition action_state.hpp:218
Result action_result
Shared pointer to the action result.
Definition action_state.hpp:317
std::condition_variable action_cancel_cond
Condition variable for action cancellation.
Definition action_state.hpp:312
typename ActionT::Feedback Feedback
Alias for the action feedback type.
Definition action_state.hpp:53
ActionClient action_client
Shared pointer to the action client.
Definition action_state.hpp:305
typename rclcpp_action::Client< ActionT >::SharedPtr ActionClient
Shared pointer type for the action client.
Definition action_state.hpp:58
FeedbackHandler feedback_handler
Handler function for processing feedback.
Definition action_state.hpp:331
std::function< void(std::shared_ptr< yasmin::blackboard::Blackboard >, std::shared_ptr< const Feedback >)> FeedbackHandler
Function type for handling feedback.
Definition action_state.hpp:68
void cancel_state()
Cancel the current action state.
Definition action_state.hpp:198
typename ActionT::Goal Goal
Alias for the action goal type.
Definition action_state.hpp:48
rclcpp_action::ResultCode action_status
Status of the action execution.
Definition action_state.hpp:319
ActionState(std::string action_name, CreateGoalHandler create_goal_handler, std::set< std::string > outcomes, int timeout=-1.0)
Construct an ActionState with a specific action name and goal handler.
Definition action_state.hpp:88
std::function< Goal(std::shared_ptr< yasmin::blackboard::Blackboard >)> CreateGoalHandler
Function type for creating a goal.
Definition action_state.hpp:62
ActionState(std::string action_name, CreateGoalHandler create_goal_handler, ResultHandler result_handler=nullptr, FeedbackHandler feedback_handler=nullptr, int timeout=-1.0)
Construct an ActionState with result and feedback handlers.
Definition action_state.hpp:109
typename rclcpp_action::Client< ActionT >::SendGoalOptions SendGoalOptions
Options for sending goals.
Definition action_state.hpp:55
std::mutex goal_handle_mutex
Mutex for protecting access to the goal handle.
Definition action_state.hpp:324
std::shared_ptr< GoalHandle > goal_handle
Handle for the current goal.
Definition action_state.hpp:322
typename ActionT::Result::SharedPtr Result
Alias for the action result type.
Definition action_state.hpp:50
CreateGoalHandler create_goal_handler
Handler function for creating goals.
Definition action_state.hpp:327
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition action_state.hpp:299
std::mutex action_done_mutex
Mutex for protecting action completion.
Definition action_state.hpp:310
std::string execute(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard)
Execute the action and return the outcome.
Definition action_state.hpp:237
std::string action_name
Name of the action to communicate with.
Definition action_state.hpp:302
ResultHandler result_handler
Handler function for processing results.
Definition action_state.hpp:329
ActionState(const rclcpp::Node::SharedPtr &node, std::string action_name, CreateGoalHandler create_goal_handler, std::set< std::string > outcomes, ResultHandler result_handler=nullptr, FeedbackHandler feedback_handler=nullptr, int timeout=-1.0)
Construct an ActionState with a specified node and action name.
Definition action_state.hpp:159
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 CANCEL[]
Constant representing a canceled action outcome.
Definition basic_outcomes.hpp:55
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