Nav2 Navigation Stack - jazzy  jazzy
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 
182  void haltTree()
183  {
184  tree_.haltTree();
185  }
186 
187 protected:
191  void executeCallback();
192 
198  void populateErrorCode(typename std::shared_ptr<typename ActionT::Result> result);
199 
203  void cleanErrorCodes();
204 
205  // Action name
206  std::string action_name_;
207 
208  // Our action server implements the template action
209  std::shared_ptr<ActionServer> action_server_;
210 
211  // Behavior Tree to be executed when goal is received
212  BT::Tree tree_;
213 
214  // The blackboard shared by all of the nodes in the tree
215  BT::Blackboard::Ptr blackboard_;
216 
217  // The XML file that cointains the Behavior Tree to create
218  std::string current_bt_xml_filename_;
219  std::string default_bt_xml_filename_;
220 
221  // The wrapper class for the BT functionality
222  std::unique_ptr<nav2_behavior_tree::BehaviorTreeEngine> bt_;
223 
224  // Libraries to pull plugins (BT Nodes) from
225  std::vector<std::string> plugin_lib_names_;
226 
227  // Error code id names
228  std::vector<std::string> error_code_names_;
229 
230  // A regular, non-spinning ROS node that we can use for calls to the action client
231  rclcpp::Node::SharedPtr client_node_;
232 
233  // Parent node
234  rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
235 
236  // Clock
237  rclcpp::Clock::SharedPtr clock_;
238 
239  // Logger
240  rclcpp::Logger logger_{rclcpp::get_logger("BtActionServer")};
241 
242  // To publish BT logs
243  std::unique_ptr<RosTopicLogger> topic_logger_;
244 
245  // Duration for each iteration of BT execution
246  std::chrono::milliseconds bt_loop_duration_;
247 
248  // Default timeout value while waiting for response from a server
249  std::chrono::milliseconds default_server_timeout_;
250 
251  // The timeout value for waiting for a service to response
252  std::chrono::milliseconds wait_for_service_timeout_;
253 
254  // should the BT be reloaded even if the same xml filename is requested?
255  bool always_reload_bt_xml_ = false;
256 
257  // User-provided callbacks
258  OnGoalReceivedCallback on_goal_received_callback_;
259  OnLoopCallback on_loop_callback_;
260  OnPreemptCallback on_preempt_callback_;
261  OnCompletionCallback on_completion_callback_;
262 };
263 
264 } // namespace nav2_behavior_tree
265 
266 #include <nav2_behavior_tree/bt_action_server_impl.hpp> // NOLINT(build/include_order)
267 #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.
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.
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.
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.