AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
GenericTreeExecutorNode Class Reference

Flexible and configurable ROS 2 behavior tree executor node. More...

#include <auto_apms_behavior_tree/executor/generic_executor_node.hpp>

Inheritance diagram for GenericTreeExecutorNode:

Public Member Functions

 GenericTreeExecutorNode (const std::string &name, Options options)
 Constructor.
 
 GenericTreeExecutorNode (rclcpp::NodeOptions options)
 Constructor with default options.
 
std::shared_future< ExecutionResultstartExecution (const std::string &build_request, const std::string &entry_point="", const core::NodeManifest &node_manifest={})
 Start the behavior tree specified by a particular build request.
 
std::shared_future< ExecutionResultstartExecution (TreeConstructor make_tree, double tick_rate_sec=0.1, int groot2_port=-1)
 Start a behavior tree that is built using a callback.
 
template<typename TimeRepT = int64_t, typename TimeT = std::milli>
std::shared_future< ExecutionResultstartExecution (TreeConstructor make_tree, const std::chrono::duration< TimeRepT, TimeT > &tick_rate, int groot2_port=-1)
 Start a behavior tree that is built using a callback.
 
- Public Member Functions inherited from TreeExecutorBase
 TreeExecutorBase (rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr=nullptr)
 Constructor.
 
std::shared_future< ExecutionResultstartExecution (TreeConstructor make_tree, double tick_rate_sec=0.1, int groot2_port=-1)
 Start a behavior tree that is built using a callback.
 
template<typename TimeRepT = int64_t, typename TimeT = std::milli>
std::shared_future< ExecutionResultstartExecution (TreeConstructor make_tree, const std::chrono::duration< TimeRepT, TimeT > &tick_rate, int groot2_port=-1)
 Start a behavior tree that is built using a callback.
 
void setControlCommand (ControlCommand cmd)
 Set the command that handles the control flow of the execution routine.
 
bool isBusy ()
 Determine whether this executor is currently executing a behavior tree.
 
ExecutionState getExecutionState ()
 Get a status code indicating the current state of execution.
 
std::string getTreeName ()
 Get the name of the tree that is currently executing.
 
TreeBlackboardSharedPtr getGlobalBlackboardPtr ()
 Get a shared pointer to the global blackboard instance.
 
TreeStateObservergetStateObserver ()
 Get a reference to the current behavior tree state observer.
 
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.
 
rclcpp::CallbackGroup::SharedPtr getTreeNodeWaitablesCallbackGroupPtr ()
 Get the callback group used for all waitables registered by behavior tree nodes.
 
rclcpp::executors::SingleThreadedExecutor::SharedPtr getTreeNodeWaitablesExecutorPtr ()
 Get the ROS 2 executor instance used for spinning waitables registered by behavior tree nodes.
 

Static Public Attributes

static const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none"
 Value indicating that no build handler is loaded.
 

Protected Member Functions

ExecutorParameters getExecutorParameters () const
 Get a copy of the current executor parameters.
 
std::map< std::string, rclcpp::ParameterValue > getParameterValuesWithPrefix (const std::string &prefix)
 Assemble all parameters of this node that have a specific prefix.
 
bool updateScriptingEnumsWithParameterValues (const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
 Update the internal buffer of scripting enums.
 
bool updateGlobalBlackboardWithParameterValues (const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
 Update the global blackboard using parameter values.
 
void loadBuildHandler (const std::string &name)
 Load a particular behavior tree build handler plugin.
 
TreeConstructor makeTreeConstructor (const std::string &build_request, const std::string &entry_point="", const core::NodeManifest &node_manifest={})
 Create a callback that builds a behavior tree according to a specific request.
 
core::TreeBuilder::SharedPtr createTreeBuilder ()
 Create a tree builder for building the behavior tree.
 
virtual bool clearGlobalBlackboard () override
 Reset the global blackboard and clear all entries.
 

Static Protected Member Functions

static std::string stripPrefixFromParameterName (const std::string &prefix, const std::string &param_name)
 Get the name of a parameter without its prefix.
 

Private Member Functions

virtual void preBuild (core::TreeBuilder &builder, const std::string &build_request, const std::string &entry_point, const core::NodeManifest &node_manifest, TreeBlackboard &bb)
 Callback invoked before building the behavior tree.
 
virtual void postBuild (Tree &tree)
 Callback invoked after the behavior tree has been instantiated.
 

Additional Inherited Members

- Public Types inherited from TreeExecutorBase
enum class  ExecutionState : uint8_t {
  IDLE , STARTING , RUNNING , PAUSED ,
  HALTED
}
 Enum representing possible behavior tree execution states. More...
 
enum class  ControlCommand : uint8_t { RUN , PAUSE , HALT , TERMINATE }
 Enum representing possible commands for controlling the behavior tree execution routine. More...
 
enum class  TreeExitBehavior : uint8_t { TERMINATE , RESTART }
 Enum representing possible options for what to do when a behavior tree is completed. More...
 
enum class  ExecutionResult : uint8_t { TREE_SUCCEEDED , TREE_FAILED , TERMINATED_PREMATURELY , ERROR }
 Enum representing possible behavior tree execution results. More...
 
- Protected Attributes inherited from TreeExecutorBase
rclcpp::Node::SharedPtr node_ptr_
 Shared pointer to the parent ROS 2 node.
 
const rclcpp::Logger logger_
 Logger associated with the parent ROS 2 node.
 

Detailed Description

Flexible and configurable ROS 2 behavior tree executor node.

This executor extends TreeExecutorBase with configurable support for:

  • Build handler management (loading and switching build handlers)
  • CommandTreeExecutor action interface (pause, resume, halt, terminate)
  • Parameter/blackboard synchronization
  • Scripting enum parameters

Derived classes can trigger the behavior tree execution by calling GenericTreeExecutorNode::startExecution with a build request or a TreeConstructor directly.

Definition at line 59 of file generic_executor_node.hpp.

Constructor & Destructor Documentation

◆ GenericTreeExecutorNode() [1/2]

GenericTreeExecutorNode ( const std::string & name,
Options options )

Constructor.

Parameters
nameName of the rclcpp::Node.
optionsExecutor options.

Definition at line 31 of file generic_executor_node.cpp.

◆ GenericTreeExecutorNode() [2/2]

GenericTreeExecutorNode ( rclcpp::NodeOptions options)
explicit

Constructor with default options.

Parameters
optionsROS 2 node options.

Definition at line 172 of file generic_executor_node.cpp.

Member Function Documentation

◆ startExecution() [1/3]

std::shared_future< GenericTreeExecutorNode::ExecutionResult > startExecution ( const std::string & build_request,
const std::string & entry_point = "",
const core::NodeManifest & node_manifest = {} )

Start the behavior tree specified by a particular build request.

Parameters
build_requestBehavior build request for creating the behavior.
entry_pointSingle point of entry for behavior execution.
node_manifestBehavior tree node manifest to be loaded for behavior execution.
Returns
Shared future that completes once executing the tree is finished or an error occurs.

Definition at line 185 of file generic_executor_node.cpp.

◆ preBuild()

void preBuild ( core::TreeBuilder & builder,
const std::string & build_request,
const std::string & entry_point,
const core::NodeManifest & node_manifest,
TreeBlackboard & bb )
privatevirtual

Callback invoked before building the behavior tree.

Note
This hook is only used in the TreeConstructor returned by GenericTreeExecutorNode::makeTreeConstructor. Remember that if you pass a custom TreeConstructor directly to TreeExecutorBase::startExecution, you bypass this hook if you don't explicitly include it.
Parameters
builderTree builder to be configured.
build_requestBehavior build request.
entry_pointSingle point of entry for behavior execution.
node_manifestBehavior tree node manifest.
bbLocal blackboard of the tree being created.

Definition at line 177 of file generic_executor_node.cpp.

◆ postBuild()

void postBuild ( Tree & tree)
privatevirtual

Callback invoked after the behavior tree has been instantiated.

Note
This hook is only used in the TreeConstructor returned by GenericTreeExecutorNode::makeTreeConstructor. Remember that if you pass a custom TreeConstructor directly to TreeExecutorBase::startExecution, you bypass this hook if you don't explicitly include it.
Parameters
treeBehavior tree that has been created and is about to be executed.

Definition at line 183 of file generic_executor_node.cpp.

◆ getExecutorParameters()

GenericTreeExecutorNode::ExecutorParameters getExecutorParameters ( ) const
protected

Get a copy of the current executor parameters.

Returns
Current executor parameters.

Definition at line 193 of file generic_executor_node.cpp.

◆ getParameterValuesWithPrefix()

std::map< std::string, rclcpp::ParameterValue > getParameterValuesWithPrefix ( const std::string & prefix)
protected

Assemble all parameters of this node that have a specific prefix.

Parameters
prefixOnly consider parameters that have this prefix in their names.
Returns
Map of parameter names and their respective values.

Definition at line 198 of file generic_executor_node.cpp.

◆ stripPrefixFromParameterName()

std::string stripPrefixFromParameterName ( const std::string & prefix,
const std::string & param_name )
staticprotected

Get the name of a parameter without its prefix.

Parameters
prefixPrefix to remove from param_name.
param_nameName of the parameter with its prefix.
Returns
Name of the parameter without its prefix.

Definition at line 211 of file generic_executor_node.cpp.

◆ updateScriptingEnumsWithParameterValues()

bool updateScriptingEnumsWithParameterValues ( const std::map< std::string, rclcpp::ParameterValue > & value_map,
bool simulate = false )
protected

Update the internal buffer of scripting enums.

Parameters
value_mapMap of parameter names and their respective values.
simulateSet to true to only validate.
Returns
true if updating is possible.

Definition at line 219 of file generic_executor_node.cpp.

◆ updateGlobalBlackboardWithParameterValues()

bool updateGlobalBlackboardWithParameterValues ( const std::map< std::string, rclcpp::ParameterValue > & value_map,
bool simulate = false )
protected

Update the global blackboard using parameter values.

Parameters
value_mapMap of parameter names and their respective values.
simulateSet to true to only validate.
Returns
true if updating is possible.

Definition at line 254 of file generic_executor_node.cpp.

◆ loadBuildHandler()

void loadBuildHandler ( const std::string & name)
protected

Load a particular behavior tree build handler plugin.

Parameters
nameFully qualified name of the build handler class. Set to "none" to unload.

Definition at line 290 of file generic_executor_node.cpp.

◆ makeTreeConstructor()

TreeConstructor makeTreeConstructor ( const std::string & build_request,
const std::string & entry_point = "",
const core::NodeManifest & node_manifest = {} )
protected

Create a callback that builds a behavior tree according to a specific request.

Parameters
build_requestRequest that specifies how to build the behavior tree.
entry_pointSingle point of entry for behavior execution.
node_manifestBehavior tree node manifest.
Returns
Callback for creating the behavior tree.

Definition at line 319 of file generic_executor_node.cpp.

◆ createTreeBuilder()

core::TreeBuilder::SharedPtr createTreeBuilder ( )
protected

Create a tree builder for building the behavior tree.

Returns
Shared pointer to the created tree builder.

Definition at line 359 of file generic_executor_node.cpp.

◆ clearGlobalBlackboard()

bool clearGlobalBlackboard ( )
overrideprotectedvirtual

Reset the global blackboard and clear all entries.

Returns
true if blackboard was cleared, false if executor is not idle.

Reimplemented from TreeExecutorBase.

Definition at line 366 of file generic_executor_node.cpp.

◆ startExecution() [2/3]

std::shared_future< TreeExecutorBase::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.

Executing the behavior tree is achieved by regularly invoking the internal routine that ticks the behavior tree created using make_tree. This requires to register a timer with the associated ROS 2 node. Consequently, the behavior tree is executed asynchronously. The user is provided a shared future object that allows to check whether the execution finished. Once this future completes, the execution result can be evaluated.

Parameters
make_treeCallback that creates a BT::Tree object which will be ticked to execute the tree.
tick_rate_secBehavior tree tick rate in seconds (1 tick every tick_rate_sec seconds) i.e. the interval of the timer that regularly invokes the execution routine.
groot2_portPort number used for introspection and debugging with Groot2. -1 means that no BT::Groot2Publisher will be installed.
Returns
Shared future that completes once executing the tree is finished or an error occurs. In that case, it is assigned an execution result code.

Definition at line 116 of file executor_base.cpp.

◆ startExecution() [3/3]

template<typename TimeRepT = int64_t, typename TimeT = std::milli>
std::shared_future< TreeExecutorBase::ExecutionResult > startExecution ( TreeConstructor make_tree,
const std::chrono::duration< TimeRepT, TimeT > & tick_rate,
int groot2_port = -1 )
inline

Start a behavior tree that is built using a callback.

Executing the behavior tree is achieved by regularly invoking the internal routine that ticks the behavior tree created using make_tree. This requires to register a timer with the associated ROS 2 node. Consequently, the behavior tree is executed asynchronously. The user is provided a shared future object that allows to check whether the execution finished. Once this future completes, the execution result can be evaluated.

Parameters
make_treeCallback that creates a BT::Tree object which will be ticked to execute the tree.
tick_rateBehavior tree tick rate i.e. the interval of the timer that regularly invokes the execution routine.
groot2_portPort number used for introspection and debugging with Groot2. -1 means that no BT::Groot2Publisher will be installed.
Returns
Shared future that completes once executing the tree is finished or an error occurs. In that case, it is assigned an execution result code.

Definition at line 135 of file executor_base.hpp.

Member Data Documentation

◆ PARAM_VALUE_NO_BUILD_HANDLER

const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none"
inlinestatic

Value indicating that no build handler is loaded.

Definition at line 71 of file generic_executor_node.hpp.


The documentation for this class was generated from the following files: