15 #ifndef NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_HPP_
16 #define NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_HPP_
24 #include "geometry_msgs/msg/pose_stamped.hpp"
25 #include "nav2_behavior_tree/behavior_tree_engine.hpp"
26 #include "nav2_behavior_tree/ros_topic_logger.hpp"
27 #include "nav2_util/lifecycle_node.hpp"
28 #include "nav2_util/simple_action_server.hpp"
29 #include "rclcpp/rclcpp.hpp"
30 #include "rclcpp_lifecycle/lifecycle_node.hpp"
32 namespace nav2_behavior_tree
38 template<
class ActionT>
44 typedef std::function<bool (
typename ActionT::Goal::ConstSharedPtr)> OnGoalReceivedCallback;
45 typedef std::function<void ()> OnLoopCallback;
46 typedef std::function<void (
typename ActionT::Goal::ConstSharedPtr)> OnPreemptCallback;
47 typedef std::function<void (
typename ActionT::Result::SharedPtr,
48 nav2_behavior_tree::BtStatus)> OnCompletionCallback;
54 const rclcpp_lifecycle::LifecycleNode::WeakPtr & parent,
55 const std::string & action_name,
56 const std::vector<std::string> & plugin_lib_names,
57 const std::string & default_bt_xml_filename,
58 OnGoalReceivedCallback on_goal_received_callback,
59 OnLoopCallback on_loop_callback,
60 OnPreemptCallback on_preempt_callback,
61 OnCompletionCallback on_completion_callback);
124 return current_bt_xml_filename_;
133 return default_bt_xml_filename_;
142 return action_server_->accept_pending_goal();
150 action_server_->terminate_pending_goal();
159 return action_server_->get_current_goal();
168 return action_server_->get_pending_goal();
176 action_server_->publish_feedback(feedback);
228 void populateErrorCode(
typename std::shared_ptr<typename ActionT::Result> result);
236 std::string action_name_;
239 std::shared_ptr<ActionServer> action_server_;
245 BT::Blackboard::Ptr blackboard_;
248 std::string current_bt_xml_filename_;
249 std::string default_bt_xml_filename_;
252 std::unique_ptr<nav2_behavior_tree::BehaviorTreeEngine> bt_;
255 std::vector<std::string> plugin_lib_names_;
258 std::vector<std::string> error_code_name_prefixes_;
261 rclcpp::Node::SharedPtr client_node_;
264 rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
267 rclcpp::Clock::SharedPtr clock_;
270 rclcpp::Logger logger_{rclcpp::get_logger(
"BtActionServer")};
273 std::unique_ptr<RosTopicLogger> topic_logger_;
276 std::chrono::milliseconds bt_loop_duration_;
279 std::chrono::milliseconds default_server_timeout_;
282 std::chrono::milliseconds wait_for_service_timeout_;
285 bool always_reload_bt_xml_ =
false;
288 bool enable_groot_monitoring_ =
true;
289 int groot_server_port_ = 1667;
292 OnGoalReceivedCallback on_goal_received_callback_;
293 OnLoopCallback on_loop_callback_;
294 OnPreemptCallback on_preempt_callback_;
295 OnCompletionCallback on_completion_callback_;
298 uint16_t internal_error_code_;
299 std::string internal_error_msg_;
304 #include <nav2_behavior_tree/bt_action_server_impl.hpp>
An action server that uses behavior tree to execute an action.
bool populateInternalError(typename std::shared_ptr< typename ActionT::Result > result)
populate result with internal error code and error_msg if not NONE
bool loadBehaviorTree(const std::string &bt_xml_filename="")
Replace current BT with another one.
void haltTree()
Function to halt the current tree. It will interrupt the execution of RUNNING nodes by calling their ...
bool on_cleanup()
Resets member variables.
void populateErrorCode(typename std::shared_ptr< typename ActionT::Result > result)
updates the action server result to the highest priority error code posted on the blackboard
~BtActionServer()
A destructor for nav2_behavior_tree::BtActionServer class.
void resetInternalError(void)
reset internal error code and message
BtActionServer(const rclcpp_lifecycle::LifecycleNode::WeakPtr &parent, const std::string &action_name, const std::vector< std::string > &plugin_lib_names, const std::string &default_bt_xml_filename, OnGoalReceivedCallback on_goal_received_callback, OnLoopCallback on_loop_callback, OnPreemptCallback on_preempt_callback, OnCompletionCallback on_completion_callback)
A constructor for nav2_behavior_tree::BtActionServer class.
void executeCallback()
Action server callback.
bool on_activate()
Activates action server.
bool on_deactivate()
Deactivates action server.
void cleanErrorCodes()
Setting BT error codes to success. Used to clean blackboard between different BT runs.
void terminatePendingGoal()
Wrapper function to terminate pending goal if a preempt has been requested.
void setGrootMonitoring(const bool enable, const unsigned server_port)
Enable (or disable) Groot2 monitoring of BT.
std::string getCurrentBTFilename() const
Getter function for current BT XML filename.
std::string getDefaultBTFilename() const
Getter function for default BT XML filename.
const std::shared_ptr< const typename ActionT::Goal > acceptPendingGoal()
Wrapper function to accept pending goal if a preempt has been requested.
void setInternalError(uint16_t error_code, const std::string &error_msg)
Set internal error code and message.
const std::shared_ptr< const typename ActionT::Goal > getCurrentGoal() const
Wrapper function to get current goal.
void publishFeedback(typename std::shared_ptr< typename ActionT::Feedback > feedback)
Wrapper function to publish action feedback.
const std::shared_ptr< const typename ActionT::Goal > getPendingGoal() const
Wrapper function to get pending goal.
BT::Blackboard::Ptr getBlackboard() const
Getter function for BT Blackboard.
bool on_configure()
Configures member variables Initializes action server for, builds behavior tree from xml file,...
const BT::Tree & getTree() const
Getter function for the current BT tree.
An action server wrapper to make applications simpler using Actions.