Nav2 Navigation Stack - kilted  kilted
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 <chrono>
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
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"
31 
32 namespace nav2_behavior_tree
33 {
38 template<class ActionT>
40 {
41 public:
43 
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;
49 
53  explicit BtActionServer(
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  const std::vector<std::string> & search_directories,
59  OnGoalReceivedCallback on_goal_received_callback,
60  OnLoopCallback on_loop_callback,
61  OnPreemptCallback on_preempt_callback,
62  OnCompletionCallback on_completion_callback);
63 
68 
75  bool on_configure();
76 
81  bool on_activate();
82 
87  bool on_deactivate();
88 
93  bool on_cleanup();
94 
100  void setGrootMonitoring(const bool enable, const unsigned server_port);
101 
108  bool loadBehaviorTree(
109  const std::string & bt_xml_filename = "");
110 
115  BT::Blackboard::Ptr getBlackboard() const
116  {
117  return blackboard_;
118  }
119 
124  std::string getCurrentBTFilename() const
125  {
126  return current_bt_xml_filename_;
127  }
128 
133  std::string getDefaultBTFilename() const
134  {
135  return default_bt_xml_filename_;
136  }
137 
142  const std::shared_ptr<const typename ActionT::Goal> acceptPendingGoal()
143  {
144  return action_server_->accept_pending_goal();
145  }
146 
151  {
152  action_server_->terminate_pending_goal();
153  }
154 
159  const std::shared_ptr<const typename ActionT::Goal> getCurrentGoal() const
160  {
161  return action_server_->get_current_goal();
162  }
163 
168  const std::shared_ptr<const typename ActionT::Goal> getPendingGoal() const
169  {
170  return action_server_->get_pending_goal();
171  }
172 
176  void publishFeedback(typename std::shared_ptr<typename ActionT::Feedback> feedback)
177  {
178  action_server_->publish_feedback(feedback);
179  }
180 
185  const BT::Tree & getTree() const
186  {
187  return tree_;
188  }
189 
195  void haltTree()
196  {
197  tree_.haltTree();
198  }
199 
205  void setInternalError(uint16_t error_code, const std::string & error_msg);
206 
210  void resetInternalError(void);
211 
217  bool populateInternalError(typename std::shared_ptr<typename ActionT::Result> result);
218 
219 protected:
223  void executeCallback();
224 
230  void populateErrorCode(typename std::shared_ptr<typename ActionT::Result> result);
231 
235  void cleanErrorCodes();
236 
237  // Action name
238  std::string action_name_;
239 
240  // Our action server implements the template action
241  std::shared_ptr<ActionServer> action_server_;
242 
243  // Behavior Tree to be executed when goal is received
244  BT::Tree tree_;
245 
246  // The blackboard shared by all of the nodes in the tree
247  BT::Blackboard::Ptr blackboard_;
248 
249  // The XML file that contains the Behavior Tree to create
250  std::string current_bt_xml_filename_;
251  std::string default_bt_xml_filename_;
252  std::vector<std::string> search_directories_;
253 
254  // The wrapper class for the BT functionality
255  std::unique_ptr<nav2_behavior_tree::BehaviorTreeEngine> bt_;
256 
257  // Libraries to pull plugins (BT Nodes) from
258  std::vector<std::string> plugin_lib_names_;
259 
260  // Error code name prefixes
261  std::vector<std::string> error_code_name_prefixes_;
262 
263  // A regular, non-spinning ROS node that we can use for calls to the action client
264  rclcpp::Node::SharedPtr client_node_;
265 
266  // Parent node
267  rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
268 
269  // Clock
270  rclcpp::Clock::SharedPtr clock_;
271 
272  // Logger
273  rclcpp::Logger logger_{rclcpp::get_logger("BtActionServer")};
274 
275  // To publish BT logs
276  std::unique_ptr<RosTopicLogger> topic_logger_;
277 
278  // Duration for each iteration of BT execution
279  std::chrono::milliseconds bt_loop_duration_;
280 
281  // Default timeout value while waiting for response from a server
282  std::chrono::milliseconds default_server_timeout_;
283 
284  // The timeout value for waiting for a service to response
285  std::chrono::milliseconds wait_for_service_timeout_;
286 
287  // should the BT be reloaded even if the same xml filename is requested?
288  bool always_reload_bt_xml_ = false;
289 
290  // Parameters for Groot2 monitoring
291  bool enable_groot_monitoring_ = false;
292  int groot_server_port_ = 1667;
293 
294  // User-provided callbacks
295  OnGoalReceivedCallback on_goal_received_callback_;
296  OnLoopCallback on_loop_callback_;
297  OnPreemptCallback on_preempt_callback_;
298  OnCompletionCallback on_completion_callback_;
299 
300  // internal error tracking (IOW not behaviorTree blackboard errors)
301  uint16_t internal_error_code_;
302  std::string internal_error_msg_;
303 };
304 
305 } // namespace nav2_behavior_tree
306 
307 #include <nav2_behavior_tree/bt_action_server_impl.hpp> // NOLINT(build/include_order)
308 #endif // NAV2_BEHAVIOR_TREE__BT_ACTION_SERVER_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 ...
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, const std::vector< std::string > &search_directories, 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.
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
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.