Behaviortree.Cpp

Behavior Trees Library in C++. Batteries included.

Last verified:

Visit Behaviortree.Cpp

What is Behaviortree.Cpp?

BehaviorTree.CPP is the most popular, production-ready C++ framework for building reactive, modular, and debuggable robot behaviors. It provides a framework to create Behavior Trees, which are a way to structure switching between different tasks in autonomous agents like robots or virtual game entities. The library is designed to be flexible, easy to use, and fast, with the main use-case being robotics, though it can also build AI for games or replace Finite State Machines in applications.

Key features include asynchronous actions (non-blocking routines) as first-class citizens, trees created at run-time using an interpreted XML language, and a logging/profiling infrastructure that allows users to visualize, record, replay, and analyze state transitions. Users can link custom TreeNodes statically or convert them into plugins loaded at run-time. The library offers non-blocking actions and reactive behaviors, reusable building blocks, custom node loading at runtime, and separation of concerns between logic (XML) and implementation (C++).

BehaviorTree.CPP is primarily for robotics developers, particularly those working in the ROS2 ecosystem where it is the most widely used Robot Deliberation library. It is also suitable for game AI developers and anyone looking to replace Finite State Machines. The library is used by some of the most popular robotics frameworks and provides seamless ROS2 integration while remaining ROS-agnostic.

The framework enables complex behaviors with less code and more modularity compared to traditional state machines, making behaviors easier to maintain and extend. It includes Groot2, the official IDE to edit, monitor, and interact with Behavior Trees created with BT.CPP, offering real-time visualization in its PRO version.

Behaviortree.Cpp pricing

Pricing model: Freemium

BehaviorTree.CPP library is completely free and open source under the MIT license, allowing free use, modification, distribution, and commercial use with no restrictions. Groot2 IDE has a freemium model: Free tier supports editing and monitoring/logging visualization for up to 20 nodes. Groot2 PRO includes full editing, node monitoring, logging visualization, and interactive real-time debugger with blackboard data and breakpoint debugging. Free for academia: students and researchers can receive a complementary 1-year PRO license by filling out a form. PRO uses a floating license model.

Behaviortree.Cpp pros

  • Open source MIT license - completely free to use
  • Asynchronous/non-blocking actions as first-class citizens
  • XML-based interpreted language for runtime tree creation
  • Built-in logging and profiling infrastructure
  • Visualize, record, replay, and analyze state transitions
  • Static linking or runtime plugin loading for custom nodes
  • Separation of logic (XML) from implementation (C++)
  • Most popular Behavior Tree library in ROS2 ecosystem
  • Seamless ROS2 integration while being ROS-agnostic
  • Reactive behaviors supported natively
  • Hierarchical composition with reusable subtrees
  • Graphical representation has semantic meaning
  • More expressive than Finite State Machines
  • BehaviorTreeFactory for easy node registration
  • Groot2 IDE for editing and debugging trees
  • Supports C++17 standard
  • Custom nodes can be registered via inheritance or functors
  • Blackboard system for data sharing between nodes

Behaviortree.Cpp cons

  • Learning curve for developers new to Behavior Trees
  • Primarily designed for robotics may feel overkill for simple apps
  • Groot2 real-time visualization requires PRO version
  • Groot2 free version limited to 20 nodes for monitoring
  • XML syntax can be verbose for complex trees
  • C++17 requirement excludes older compiler support
  • Main library lacks official GUI (depends on Groot2)
  • Documentation scattered across multiple versions
  • Commercial Groot2 PRO license can be expensive
  • Fewer pre-built nodes compared to some alternatives
  • Windows-focused Groot2 may have Linux issues
  • Async actions require careful thread management
  • Blackboard basic types need manual addition for custom types
  • No built-in cloud deployment support
  • Community support mainly through GitHub discussions

Frequently asked questions about Behaviortree.Cpp

What is a Behavior Tree?

A Behavior Tree (BT) is a way to structure the switching between different tasks in an autonomous agent, such as a robot or a virtual entity in a computer game. BTs are a very efficient way of creating complex systems that are both modular and reactive. Think of the Nodes of the tree as building blocks implemented in C++ that are composable and can be assembled to build behaviors.

What are the main advantages of Behavior Trees over Finite State Machines?

Behavior Trees are intrinsically hierarchical, allowing composition of complex behaviors including entire trees as sub-branches. Their graphical representation has semantic meaning, making them easier to read and understand compared to FSM state transitions. They are more expressive with ready-to-use ControlNodes and DecoratorNodes that enable complex control flows, and users can extend the vocabulary with custom nodes.

How do I create my first Behavior Tree?

Create custom ActionNodes by inheriting from BT::SyncActionNode and overriding the tick() method which returns NodeStatus (RUNNING, SUCCESS, or FAILURE). Register custom TreeNodes into BehaviorTreeFactory, then load XML from file or text using factory.createTreeFromFile(). Execute the tree by calling tree.tickWhileRunning().

What is the difference between SyncActionNode and AsyncActionNode?

SyncActionNode represents synchronous (blocking) actions that complete immediately and return a status. AsyncActionNode represents asynchronous (non-blocking) actions that can return RUNNING status and complete later. BehaviorTree.CPP makes asynchronous actions first-class citizens, which is crucial for reactive robotics behaviors.

How does Groot2 work with BehaviorTree.CPP?

Groot2 is the official IDE to edit, monitor, and interact with Behavior Trees created with BT.CPP. Adding real-time visualization requires a single line of code: BT::Groot2Publisher publisher(tree). This creates inter-process communication that sends tree structure, node status updates, blackboard values, and allows breakpoints, node substitution, and fault injection.

What is the Blackboard system?

The Blackboard is BT.CPP's mechanism for data sharing between nodes. It supports basic types like integers, reals, and strings out of the box. Custom types need to be added manually by implementing nlohmann::to_json() functions. The blackboard values are sent to Groot2 for visualization and debugging.

Can I load custom nodes at runtime?

Yes, you can convert custom TreeNodes into plugins that are loaded at run-time instead of statically linking them. This allows extending the library's functionality without recompiling. Custom nodes must be registered into the BehaviorTreeFactory before the tree is created.

What ROS versions does BehaviorTree.CPP support?

The main library is ROS-agnostic, meaning it works independently of ROS. However, BT.CPP provides seamless ROS2 integration and is the most widely used Robot Deliberation library in the ROS2 ecosystem. It is available as behaviortree_cpp package in ROS2 distributions.

How do I debug Behavior Trees?

BT.CPP includes a logging/profiling infrastructure that allows users to visualize, record, replay, and analyze state transitions. The TreeObserver collects statistics for each node including last valid result (SUCCESS or FAILURE). Groot2 provides interactive debugging with breakpoints, blackboard data inspection, and node substitution capabilities.

What is the XML format for Behavior Trees?

The XML format uses <root> as the top-level tag containing <BehaviorTree> with an ID attribute. Each TreeNode is represented by a single tag where the tag name is the ID used to register the node. ControlNodes contain 1 to N children, DecoratorNodes and Subtrees contain only 1 child, and ActionNodes and ConditionNodes have no children. Ports are configured using attributes.

Categories

Use cases

Browse all AI tools on NeedAnAI