22#include "auto_apms_behavior_tree/exceptions.hpp"
23#include "auto_apms_behavior_tree/executor/state_observer.hpp"
24#include "auto_apms_behavior_tree_core/definitions.hpp"
25#include "behaviortree_cpp/loggers/groot2_publisher.h"
26#include "rclcpp/node.hpp"
87 using TerminationCallback = std::function<void(
ExecutionResult,
const std::string &)>;
97 rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr =
nullptr);
117 TreeConstructor make_tree,
double tick_rate_sec = 0.1,
int groot2_port = -1);
134 template <
typename TimeRepT =
int64_t,
typename TimeT = std::milli>
136 TreeConstructor make_tree,
const std::chrono::duration<TimeRepT, TimeT> & tick_rate,
int groot2_port = -1);
139 void tick_callback_(TerminationCallback termination_callback);
263 rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group_ptr_;
264 rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor_ptr_;
265 TreeBlackboardSharedPtr global_blackboard_ptr_;
266 std::unique_ptr<Tree> tree_ptr_;
267 std::unique_ptr<BT::Groot2Publisher> groot2_publisher_ptr_;
268 std::unique_ptr<TreeStateObserver> state_observer_ptr_;
269 rclcpp::TimerBase::SharedPtr execution_timer_ptr_;
272 bool execution_stopped_;
273 std::string termination_reason_;
288template <
typename TimeRepT,
typename TimeT>
290 TreeConstructor make_tree,
const std::chrono::duration<TimeRepT, TimeT> & tick_rate,
int groot2_port)
293 make_tree, std::chrono::duration_cast<std::chrono::duration<double>>(tick_rate).count(), groot2_port);
Base class that offers basic functionality for executing behavior trees.
ExecutionState
Enum representing possible behavior tree execution states.
@ STARTING
Initializing execution.
@ RUNNING
Executor is busy and tree has been ticked at least once.
@ PAUSED
Execution routine is active, but tree is not being ticked.
@ HALTED
Execution routine is active, but tree is not being ticked and has been halted before.
virtual void onTermination(const ExecutionResult &result)
Callback invoked when the execution routine terminates.
std::shared_future< ExecutionResult > startExecution(TreeConstructor make_tree, double tick_rate_sec=0.1, int groot2_port=-1)
Start a behavior tree that is built using a callback.
ExecutionState getExecutionState()
Get a status code indicating the current state of execution.
bool isBusy()
Determine whether this executor is currently executing a behavior tree.
rclcpp::Node::SharedPtr node_ptr_
Shared pointer to the parent ROS 2 node.
virtual TreeExitBehavior onTreeExit(bool success)
Callback invoked last thing when the execution routine completes because the behavior tree is finishe...
virtual bool onInitialTick()
Callback invoked once before the behavior tree is ticked for the very first time.
rclcpp::executors::SingleThreadedExecutor::SharedPtr getTreeNodeWaitablesExecutorPtr()
Get the ROS 2 executor instance used for spinning waitables registered by behavior tree nodes.
rclcpp::CallbackGroup::SharedPtr getTreeNodeWaitablesCallbackGroupPtr()
Get the callback group used for all waitables registered by behavior tree nodes.
void setControlCommand(ControlCommand cmd)
Set the command that handles the control flow of the execution routine.
void clearGlobalBlackboard()
Reset the global blackboard and clear all entries.
virtual bool onTick()
Callback invoked every time before the behavior tree is ticked.
virtual bool afterTick()
Callback invoked every time after the behavior tree is ticked.
TreeExitBehavior
Enum representing possible options for what to do when a behavior tree is completed.
@ RESTART
Restart execution and keep on running.
ControlCommand
Enum representing possible commands for controlling the behavior tree execution routine.
@ TERMINATE
Halt the currently executing tree and terminate the execution routine.
@ PAUSE
Pause the execution routine.
@ RUN
Start/Resume the execution routine.
@ HALT
Halt the currently executing tree and pause the execution routine.
TreeStateObserver & getStateObserver()
Get a reference to the current behavior tree state observer.
TreeExecutorBase(rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr=nullptr)
Constructor.
rclcpp::Node::SharedPtr getNodePtr()
Get a shared pointer to the parent ROS 2 node.
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Get the node's base interface. Is required to be able to register derived classes as ROS2 components.
TreeBlackboardSharedPtr getGlobalBlackboardPtr()
Get a shared pointer to the global blackboard instance.
std::string getTreeName()
Get the name of the tree that is currently executing.
ExecutionResult
Enum representing possible behavior tree execution results.
@ TREE_SUCCEEDED
Tree completed with BT::NodeStatus::SUCCESS.
@ TERMINATED_PREMATURELY
Execution terminated before the tree was able to propagate the tick to all its nodes.
@ TREE_FAILED
Tree completed with BT::NodeStatus::FAILURE.
@ ERROR
An unexpected error occurred.
const rclcpp::Logger logger_
Logger associated with the parent ROS 2 node.
State observer for a particular behavior tree object that writes introspection and debugging informat...
Useful tooling for incorporating behavior trees for task development.