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 
95  void setGrootMonitoring(const bool enable, const unsigned server_port);
96 
103  bool loadBehaviorTree(const std::string & bt_xml_filename = "");
104 
109  BT::Blackboard::Ptr getBlackboard() const
110  {
111  return blackboard_;
112  }
113 
118  std::string getCurrentBTFilename() const
119  {
120  return current_bt_xml_filename_;
121  }
122 
127  std::string getDefaultBTFilename() const
128  {
129  return default_bt_xml_filename_;
130  }
131 
136  const std::shared_ptr<const typename ActionT::Goal> acceptPendingGoal()
137  {
138  return action_server_->accept_pending_goal();
139  }
140 
145  {
146  action_server_->terminate_pending_goal();
147  }
148 
153  const std::shared_ptr<const typename ActionT::Goal> getCurrentGoal() const
154  {
155  return action_server_->get_current_goal();
156  }
157 
162  const std::shared_ptr<const typename ActionT::Goal> getPendingGoal() const
163  {
164  return action_server_->get_pending_goal();
165  }
166 
170  void publishFeedback(typename std::shared_ptr<typename ActionT::Feedback> feedback)
171  {
172  action_server_->publish_feedback(feedback);
173  }
174 
179  const BT::Tree & getTree() const
180  {
181  return tree_;
182  }
183 
189  void haltTree()
190  {
191  tree_.haltTree();
192  }
193 
194 protected:
198  void executeCallback();
199 
205  void populateErrorCode(typename std::shared_ptr<typename ActionT::Result> result);
206 
210  void cleanErrorCodes();
211 
212  // Action name
213  std::string action_name_;
214 
215  // Our action server implements the template action
216  std::shared_ptr<ActionServer> action_server_;
217 
218  // Behavior Tree to be executed when goal is received
219  BT::Tree tree_;
220 
221  // The blackboard shared by all of the nodes in the tree
222  BT::Blackboard::Ptr blackboard_;
223 
224  // The XML file that cointains the Behavior Tree to create
225  std::string current_bt_xml_filename_;
226  std::string default_bt_xml_filename_;
227 
228  // The wrapper class for the BT functionality
229  std::unique_ptr<nav2_behavior_tree::BehaviorTreeEngine> bt_;
230 
231  // Libraries to pull plugins (BT Nodes) from
232  std::vector<std::string> plugin_lib_names_;
233 
234  // Error code id names
235  std::vector<std::string> error_code_names_;
236 
237  // A regular, non-spinning ROS node that we can use for calls to the action client
238  rclcpp::Node::SharedPtr client_node_;
239 
240  // Parent node
241  rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
242 
243  // Clock
244  rclcpp::Clock::SharedPtr clock_;
245 
246  // Logger
247  rclcpp::Logger logger_{rclcpp::get_logger("BtActionServer")};
248 
249  // To publish BT logs
250  std::unique_ptr<RosTopicLogger> topic_logger_;
251 
252  // Duration for each iteration of BT execution
253  std::chrono::milliseconds bt_loop_duration_;
254 
255  // Default timeout value while waiting for response from a server
256  std::chrono::milliseconds default_server_timeout_;
257 
258  // Default timeout value when cancelling actions during node halt
259  std::chrono::milliseconds default_cancel_timeout_;
260 
261  // The timeout value for waiting for a service to response
262  std::chrono::milliseconds wait_for_service_timeout_;
263 
264  // should the BT be reloaded even if the same xml filename is requested?
265  bool always_reload_bt_xml_ = false;
266 
267  // Parameters for Groot2 monitoring
268  bool enable_groot_monitoring_ = false;
269  int groot_server_port_ = 1667;
270 
271  // User-provided callbacks
272  OnGoalReceivedCallback on_goal_received_callback_;
273  OnLoopCallback on_loop_callback_;
274  OnPreemptCallback on_preempt_callback_;
275  OnCompletionCallback on_completion_callback_;
276 };
277 
278 } // namespace nav2_behavior_tree
279 
280 #include <nav2_behavior_tree/bt_action_server_impl.hpp> // NOLINT(build/include_order)
281 #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.
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.
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.