15 #ifndef NAV2_ROS_COMMON__SIMPLE_ACTION_SERVER_HPP_
16 #define NAV2_ROS_COMMON__SIMPLE_ACTION_SERVER_HPP_
24 #include <type_traits>
26 #include "rclcpp/rclcpp.hpp"
27 #include "rclcpp_action/rclcpp_action.hpp"
28 #include "nav2_ros_common/node_thread.hpp"
29 #include "nav2_ros_common/node_utils.hpp"
38 template<
typename ActionT>
42 using SharedPtr = std::shared_ptr<nav2::SimpleActionServer<ActionT>>;
43 using UniquePtr = std::unique_ptr<nav2::SimpleActionServer<ActionT>>;
48 typedef std::function<void ()> ExecuteCallback;
55 typedef std::function<void ()> CompletionCallback;
67 template<
typename NodeT>
70 const std::string & action_name,
71 ExecuteCallback execute_callback,
72 CompletionCallback completion_callback =
nullptr,
73 std::chrono::milliseconds server_timeout = std::chrono::milliseconds(500),
74 bool spin_thread =
false,
75 const bool realtime =
false)
77 node->get_node_base_interface(),
78 node->get_node_clock_interface(),
79 node->get_node_logging_interface(),
80 node->get_node_waitables_interface(),
81 node->get_node_parameters_interface(),
82 action_name, execute_callback, completion_callback,
83 server_timeout, spin_thread, realtime)
97 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
98 rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface,
99 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface,
100 rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_interface,
101 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface,
102 const std::string & action_name,
103 ExecuteCallback execute_callback,
104 CompletionCallback completion_callback =
nullptr,
105 std::chrono::milliseconds server_timeout = std::chrono::milliseconds(500),
106 bool spin_thread =
false,
107 const bool realtime =
false)
108 : node_base_interface_(node_base_interface),
109 node_clock_interface_(node_clock_interface),
110 node_logging_interface_(node_logging_interface),
111 node_waitables_interface_(node_waitables_interface),
112 node_parameters_interface_(node_parameters_interface),
113 action_name_(action_name),
114 execute_callback_(execute_callback),
115 completion_callback_(completion_callback),
116 server_timeout_(server_timeout),
117 spin_thread_(spin_thread)
119 using namespace std::placeholders;
120 use_realtime_prioritization_ = realtime;
122 callback_group_ = node_base_interface->create_callback_group(
123 rclcpp::CallbackGroupType::MutuallyExclusive,
false);
125 action_server_ = rclcpp_action::create_server<ActionT>(
126 node_base_interface_,
127 node_clock_interface_,
128 node_logging_interface_,
129 node_waitables_interface_,
134 rcl_action_server_get_default_options(),
137 nav2::setIntrospectionMode(this->action_server_,
138 node_parameters_interface_, node_clock_interface_->get_clock());
141 executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
142 executor_->add_callback_group(callback_group_, node_base_interface_);
143 executor_thread_ = std::make_unique<nav2::NodeThread>(executor_);
155 const rclcpp_action::GoalUUID & ,
156 std::shared_ptr<const typename ActionT::Goal>)
158 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
160 if (!server_active_) {
162 node_logging_interface_->get_logger(),
163 "Action server is inactive. Rejecting the goal.");
164 return rclcpp_action::GoalResponse::REJECT;
167 debug_msg(
"Received request for goal acceptance");
168 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
178 const std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> handle)
180 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
182 if (!handle->is_active()) {
184 "Received request for goal cancellation,"
185 "but the handle is inactive, so reject the request");
186 return rclcpp_action::CancelResponse::REJECT;
189 debug_msg(
"Received request for goal cancellation");
190 return rclcpp_action::CancelResponse::ACCEPT;
198 if (use_realtime_prioritization_) {
199 nav2::setSoftRealTimePriority();
200 debug_msg(
"Soft realtime prioritization successfully set!");
208 void handle_accepted(
const std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> handle)
210 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
214 debug_msg(
"An older goal is active, moving the new goal to a pending slot.");
218 "The pending slot is occupied."
219 " The previous pending goal will be terminated and replaced.");
222 pending_handle_ = handle;
223 preempt_requested_ =
true;
227 error_msg(
"Forgot to handle a preemption. Terminating the pending goal.");
229 preempt_requested_ =
false;
232 current_handle_ = handle;
235 debug_msg(
"Executing goal asynchronously.");
236 execution_future_ = std::async(
237 std::launch::async, [
this]() {
249 while (rclcpp::ok() && !stop_execution_ &&
is_active(current_handle_)) {
253 }
catch (std::exception & ex) {
255 node_logging_interface_->get_logger(),
256 "Action server failed while executing action callback: \"%s\"", ex.what());
258 if (completion_callback_) {completion_callback_();}
262 debug_msg(
"Blocking processing of new goal handles.");
263 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
265 if (stop_execution_) {
266 warn_msg(
"Stopping the thread per request.");
268 if (completion_callback_) {completion_callback_();}
273 warn_msg(
"Current goal was not completed successfully.");
275 if (completion_callback_) {completion_callback_();}
279 debug_msg(
"Executing a pending handle on the existing thread.");
282 debug_msg(
"Done processing available goals.");
294 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
295 server_active_ =
true;
296 stop_execution_ =
false;
307 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
308 server_active_ =
false;
309 stop_execution_ =
true;
312 if (!execution_future_.valid()) {
318 "Requested to deactivate server but goal is still executing."
319 " Should check if action server is running before deactivating.");
322 using namespace std::chrono;
323 auto start_time = steady_clock::now();
324 while (execution_future_.wait_for(milliseconds(100)) != std::future_status::ready) {
325 info_msg(
"Waiting for async process to finish.");
326 if (steady_clock::now() - start_time >= server_timeout_) {
328 if (completion_callback_) {completion_callback_();}
329 error_msg(
"Action callback is still running and missed deadline to stop");
342 return execution_future_.valid() &&
343 (execution_future_.wait_for(std::chrono::milliseconds(0)) ==
344 std::future_status::timeout);
353 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
354 return server_active_;
363 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
364 return preempt_requested_;
373 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
375 if (!pending_handle_ || !pending_handle_->is_active()) {
376 error_msg(
"Attempting to get pending goal when not available");
377 return std::shared_ptr<const typename ActionT::Goal>();
380 if (
is_active(current_handle_) && current_handle_ != pending_handle_) {
381 debug_msg(
"Cancelling the previous goal");
385 current_handle_ = pending_handle_;
386 pending_handle_.reset();
387 preempt_requested_ =
false;
391 return current_handle_->get_goal();
399 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
401 if (!pending_handle_ || !pending_handle_->is_active()) {
402 error_msg(
"Attempting to terminate pending goal when not available");
407 preempt_requested_ =
false;
418 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
421 error_msg(
"A goal is not available or has reached a final state");
422 return std::shared_ptr<const typename ActionT::Goal>();
425 return current_handle_->get_goal();
428 const rclcpp_action::GoalUUID get_current_goal_id()
const
430 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
433 error_msg(
"A goal is not available or has reached a final state");
434 return rclcpp_action::GoalUUID();
437 return current_handle_->get_goal_id();
446 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
448 if (!pending_handle_ || !pending_handle_->is_active()) {
449 error_msg(
"Attempting to get pending goal when not available");
450 return std::shared_ptr<const typename ActionT::Goal>();
453 return pending_handle_->get_goal();
462 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
466 if (current_handle_ ==
nullptr) {
467 error_msg(
"Checking for cancel but current goal is not available");
471 if (pending_handle_ !=
nullptr) {
472 return pending_handle_->is_canceling();
475 return current_handle_->is_canceling();
483 typename std::shared_ptr<typename ActionT::Result> result =
484 std::make_shared<typename ActionT::Result>())
486 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
489 preempt_requested_ =
false;
497 typename std::shared_ptr<typename ActionT::Result> result =
498 std::make_shared<typename ActionT::Result>())
500 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
509 typename std::shared_ptr<typename ActionT::Result> result =
510 std::make_shared<typename ActionT::Result>())
512 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
515 debug_msg(
"Setting succeed on current goal.");
516 current_handle_->succeed(result);
517 current_handle_.reset();
528 error_msg(
"Trying to publish feedback when the current goal handle is not active");
532 current_handle_->publish_feedback(feedback);
537 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface_;
538 rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface_;
539 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface_;
540 rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_interface_;
541 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface_;
542 std::string action_name_;
544 ExecuteCallback execute_callback_;
545 CompletionCallback completion_callback_;
546 std::future<void> execution_future_;
547 bool stop_execution_{
false};
548 bool use_realtime_prioritization_{
false};
550 mutable std::recursive_mutex update_mutex_;
551 bool server_active_{
false};
552 bool preempt_requested_{
false};
553 std::chrono::milliseconds server_timeout_;
555 std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> current_handle_;
556 std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> pending_handle_;
558 typename rclcpp_action::Server<ActionT>::SharedPtr action_server_;
560 rclcpp::CallbackGroup::SharedPtr callback_group_{
nullptr};
561 rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
562 std::unique_ptr<nav2::NodeThread> executor_thread_;
569 return std::make_shared<typename ActionT::Result>();
578 const std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> handle)
const
580 return handle !=
nullptr && handle->is_active();
589 template<
typename T,
typename =
void>
592 struct has_error_msg<T, std::void_t<decltype(T::error_msg)>>: std::true_type {};
593 template<
typename T,
typename =
void>
596 struct has_error_code<T, std::void_t<decltype(T::error_code)>>: std::true_type {};
599 void log_error_details_if_available(
const T & result)
604 warn_msg(
"Aborting handle. error_code:" + std::to_string(result->error_code) +
605 ", error_msg:'" + result->error_msg +
"'.");
606 }
else if constexpr (has_error_code<typename ActionT::Result>::value) {
607 warn_msg(
"Aborting handle. error_code:" + std::to_string(result->error_code) +
".");
619 std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> & handle,
620 typename std::shared_ptr<typename ActionT::Result> result =
621 std::make_shared<typename ActionT::Result>())
623 std::lock_guard<std::recursive_mutex> lock(update_mutex_);
626 if (handle->is_canceling()) {
627 info_msg(
"Client requested to cancel the goal. Cancelling.");
628 handle->canceled(result);
630 log_error_details_if_available(result);
631 handle->abort(result);
643 node_logging_interface_->get_logger(),
644 "[%s] [ActionServer] %s", action_name_.c_str(), msg.c_str());
653 node_logging_interface_->get_logger(),
654 "[%s] [ActionServer] %s", action_name_.c_str(), msg.c_str());
663 node_logging_interface_->get_logger(),
664 "[%s] [ActionServer] %s", action_name_.c_str(), msg.c_str());
673 node_logging_interface_->get_logger(),
674 "[%s] [ActionServer] %s", action_name_.c_str(), msg.c_str());
An action server wrapper to make applications simpler using Actions.
void terminate_pending_goal()
Terminate pending goals.
constexpr bool is_active(const std::shared_ptr< rclcpp_action::ServerGoalHandle< ActionT >> handle) const
Whether a given goal handle is currently active.
constexpr auto empty_result() const
Generate an empty result object for an action type.
void publish_feedback(typename std::shared_ptr< typename ActionT::Feedback > feedback)
Publish feedback to the action server clients.
void setSoftRealTimePriority()
Sets thread priority level.
void warn_msg(const std::string &msg) const
Warn logging.
const std::shared_ptr< const typename ActionT::Goal > get_pending_goal() const
Get the pending goal object.
void debug_msg(const std::string &msg) const
Debug logging.
void info_msg(const std::string &msg) const
Info logging.
void terminate(std::shared_ptr< rclcpp_action::ServerGoalHandle< ActionT >> &handle, typename std::shared_ptr< typename ActionT::Result > result=std::make_shared< typename ActionT::Result >())
Terminate a particular action with a result.
void terminate_current(typename std::shared_ptr< typename ActionT::Result > result=std::make_shared< typename ActionT::Result >())
Terminate the active action.
void work()
Computed background work and processes stop requests.
const std::shared_ptr< const typename ActionT::Goal > get_current_goal() const
Get the current goal object.
void activate()
Active action server.
void handle_accepted(const std::shared_ptr< rclcpp_action::ServerGoalHandle< ActionT >> handle)
Handles accepted goals and adds to preempted queue to switch to.
bool is_cancel_requested() const
Whether or not a cancel command has come in.
void deactivate()
Deactivate action server.
void terminate_all(typename std::shared_ptr< typename ActionT::Result > result=std::make_shared< typename ActionT::Result >())
Terminate all pending and active actions.
SimpleActionServer(NodeT node, const std::string &action_name, ExecuteCallback execute_callback, CompletionCallback completion_callback=nullptr, std::chrono::milliseconds server_timeout=std::chrono::milliseconds(500), bool spin_thread=false, const bool realtime=false)
An constructor for SimpleActionServer.
rclcpp_action::CancelResponse handle_cancel(const std::shared_ptr< rclcpp_action::ServerGoalHandle< ActionT >> handle)
Accepts cancellation requests of action server.
bool is_preempt_requested() const
Whether the action server has been asked to be preempted with a new goal.
void error_msg(const std::string &msg) const
Error logging.
bool is_running()
Whether the action server is munching on a goal.
void succeeded_current(typename std::shared_ptr< typename ActionT::Result > result=std::make_shared< typename ActionT::Result >())
Return success of the active action.
bool is_server_active()
Whether the action server is active or not.
SimpleActionServer(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface, rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface, rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface, rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_interface, rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface, const std::string &action_name, ExecuteCallback execute_callback, CompletionCallback completion_callback=nullptr, std::chrono::milliseconds server_timeout=std::chrono::milliseconds(500), bool spin_thread=false, const bool realtime=false)
An constructor for SimpleActionServer.
const std::shared_ptr< const typename ActionT::Goal > accept_pending_goal()
Accept pending goals.
rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID &, std::shared_ptr< const typename ActionT::Goal >)
handle the goal requested: accept or reject. This implementation always accepts when the server is ac...
SFINAE (Substitution Failure Is Not An Error) to check for existence of error_code and error_msg in A...