Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
bt_action_server.hpp
1 // Copyright (c) 2020 Sarthak Mittal
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_HPP_
16 #define NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "geometry_msgs/msg/pose_stamped.hpp"
23 #include "nav2_behavior_tree/behavior_tree_engine.hpp"
24 #include "nav2_behavior_tree/ros_topic_logger.hpp"
25 #include "nav2_util/lifecycle_node.hpp"
26 #include "nav2_util/simple_action_server.hpp"
27 
28 namespace nav2_behavior_tree
29 {
34 template<class ActionT>
36 {
37 public:
39 
40  typedef std::function<bool (typename ActionT::Goal::ConstSharedPtr)> OnGoalReceivedCallback;
41  typedef std::function<void ()> OnLoopCallback;
42  typedef std::function<void (typename ActionT::Goal::ConstSharedPtr)> OnPreemptCallback;
43  typedef std::function<void (typename ActionT::Result::SharedPtr,
44  nav2_behavior_tree::BtStatus)> OnCompletionCallback;
45 
49  explicit BtActionServer(
50  const rclcpp_lifecycle::LifecycleNode::WeakPtr & parent,
51  const std::string & action_name,
52  const std::vector<std::string> & plugin_lib_names,
53  const std::string & default_bt_xml_filename,
54  OnGoalReceivedCallback on_goal_received_callback,
55  OnLoopCallback on_loop_callback,
56  OnPreemptCallback on_preempt_callback,
57  OnCompletionCallback on_completion_callback);
58 
63 
70  bool on_configure();
71 
76  bool on_activate();
77 
82  bool on_deactivate();
83 
88  bool on_cleanup();
89 
96  bool loadBehaviorTree(const std::string & bt_xml_filename = "");
97 
102  BT::Blackboard::Ptr getBlackboard() const
103  {
104  return blackboard_;
105  }
106 
111  std::string getCurrentBTFilename() const
112  {
113  return current_bt_xml_filename_;
114  }
115 
120  std::string getDefaultBTFilename() const
121  {
122  return default_bt_xml_filename_;
123  }
124 
129  const std::shared_ptr<const typename ActionT::Goal> acceptPendingGoal()
130  {
131  return action_server_->accept_pending_goal();
132  }
133 
138  {
139  action_server_->terminate_pending_goal();
140  }
141 
146  const std::shared_ptr<const typename ActionT::Goal> getCurrentGoal() const
147  {
148  return action_server_->get_current_goal();
149  }
150 
155  const std::shared_ptr<const typename ActionT::Goal> getPendingGoal() const
156  {
157  return action_server_->get_pending_goal();
158  }
159 
163  void publishFeedback(typename std::shared_ptr<typename ActionT::Feedback> feedback)
164  {
165  action_server_->publish_feedback(feedback);
166  }
167 
172  const BT::Tree & getTree() const
173  {
174  return tree_;
175  }
176 
181  void haltTree()
182  {
183  tree_.rootNode()->halt();
184  }
185 
186 protected:
190  void executeCallback();
191 
192  // Action name
193  std::string action_name_;
194 
195  // Our action server implements the template action
196  std::shared_ptr<ActionServer> action_server_;
197 
198  // Behavior Tree to be executed when goal is received
199  BT::Tree tree_;
200 
201  // The blackboard shared by all of the nodes in the tree
202  BT::Blackboard::Ptr blackboard_;
203 
204  // The XML file that cointains the Behavior Tree to create
205  std::string current_bt_xml_filename_;
206  std::string default_bt_xml_filename_;
207 
208  // The wrapper class for the BT functionality
209  std::unique_ptr<nav2_behavior_tree::BehaviorTreeEngine> bt_;
210 
211  // Libraries to pull plugins (BT Nodes) from
212  std::vector<std::string> plugin_lib_names_;
213 
214  // A regular, non-spinning ROS node that we can use for calls to the action client
215  rclcpp::Node::SharedPtr client_node_;
216 
217  // Parent node
218  rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
219 
220  // Clock
221  rclcpp::Clock::SharedPtr clock_;
222 
223  // Logger
224  rclcpp::Logger logger_{rclcpp::get_logger("BtActionServer")};
225 
226  // To publish BT logs
227  std::unique_ptr<RosTopicLogger> topic_logger_;
228 
229  // Duration for each iteration of BT execution
230  std::chrono::milliseconds bt_loop_duration_;
231 
232  // Default timeout value while waiting for response from a server
233  std::chrono::milliseconds default_server_timeout_;
234 
235  // The timeout value for waiting for a service to response
236  std::chrono::milliseconds wait_for_service_timeout_;
237 
238  // should the BT be reloaded even if the same xml filename is requested?
239  bool always_reload_bt_xml_ = false;
240 
241  // User-provided callbacks
242  OnGoalReceivedCallback on_goal_received_callback_;
243  OnLoopCallback on_loop_callback_;
244  OnPreemptCallback on_preempt_callback_;
245  OnCompletionCallback on_completion_callback_;
246 };
247 
248 } // namespace nav2_behavior_tree
249 
250 #include <nav2_behavior_tree/bt_action_server_impl.hpp> // NOLINT(build/include_order)
251 #endif // NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_HPP_
An action server that uses behavior tree to execute an action.
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.
~BtActionServer()
A destructor for nav2_behavior_tree::BtActionServer class.
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 terminatePendingGoal()
Wrapper function to terminate pending goal if a preempt has been requested.
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.
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.