BehaviorTree.CPP

repository·master·Indexed 26 days ago

https://github.com/behaviortree/behaviortree.cpp

A high-performance, flexible C++17 framework for implementing Behavior Trees, commonly used in robotics and game AI. It supports asynchronous actions, reactive behaviors, and runtime XML-based tree loading.

Tokens
8.8K
Snippets
21
Records
54
Agent score
88%

What's inside BehaviorTree.CPP

  1. Overview of BehaviorTree.CPP

    master

    BehaviorTree.CPP is a C++17 framework for creating Behavior Trees, designed for robotics, game AI, or as a replacement for Finite State Machines.

    Key features include:

    • Asynchronous Actions: Non-blocking actions are first-class citizens.
    • Reactive Behaviors: Support for concurrent action execution (orthogonality).
    • XML Scripting: Trees are defined using an XML-based domain-specific language and can be loaded at runtime, meaning tree morphology is not hard-coded.
    • Extensibility: Custom TreeNodes can be statically linked or converted into plugins for runtime loading.
    • Dataflow: A type-safe mechanism for data exchange between nodes.
    • Logging/Profiling: Infrastructure for visualizing, recording, replaying, and analyzing state transitions.
  2. Core Concepts of BehaviorTree.CPP

    master

    The library is built upon several fundamental concepts:

    • BT::PortInfo: A type-safe port system used to define inputs and outputs for nodes.
    • BT::Expected: A result type used for robust error handling.
    • BT::NodeStatus: Represents the execution states of a node (e.g., Success, Failure, Running).
  3. Install cppzmq via CMake

    master

    To install cppzmq, you must first build and install libzmq. Follow these steps:

    1. Build libzmq:

      • Clone libzmq from https://github.com/zeromq/libzmq.git.
      • Perform an out-of-source build using cmake and install it using make install.
    2. Build cppzmq:

      • Clone cppzmq from https://github.com/zeromq/cppzmq.git.
      • Perform an out-of-source build using cmake.
      • You can skip building tests by using the flag -DCPPZMQ_BUILD_TESTS=OFF.
      • Install using make install.
    # 1. Build libzmq
    git clone https://github.com/zeromq/libzmq.git
    cd libzmq
    mkdir build
    cd build
    cmake ..
    sudo make -j4 install
    
    # 2. Build cppzmq
    git clone https://github.com/zeromq/cppzmq.git
    cd cppzmq
    mkdir build
    cd build
    cmake ..
    sudo make -j4 install
  4. Integrate minitrace into your project

    master

    To use minitrace, include minitrace.c and minitrace.h directly in your project. You should add #include minitrace.h to a common header file.

    To enable profiling, you must define the MTR_ENABLED macro globally during compilation (e.g., using the -DMTR_ENABLED flag in your compiler settings). Ensure you remove this definition in final release builds.

  5. Integrate cppzmq into a CMake project

    master

    Use the provided CMake find package scripts to include cppzmq in your project. This will also include libzmq for you.

    To link against the dynamic library, use cppzmq. To link against the static library, use cppzmq-static.

    # find cppzmq wrapper, installed by make of cppzmq
    find_package(cppzmq)
    target_link_libraries(*Your Project Name* cppzmq)
    
    # Or use static library to link
    target_link_libraries(*Your Project Name* cppzmq-static)
  6. Connect Ports Using Type Rules

    master

    When connecting ports (e.g., via XML or code), the following rules apply:

    • Rule 1: Same Type: Ports of the exact same type are always compatible.
    • Rule 2: Generic Port Compatibility: A generic port (AnyTypeAllowed or BT::Any) can connect to any other port (strongly typed or generic).
    • Rule 3: String as Universal Donor: A blackboard entry created as a std::string can be connected to any strongly typed port that has a convertFromString<T>() specialization. This is common when using <SetBlackboard value="42" ... /> to feed an integer port.
    • Rule 4: Type Lock: Once a blackboard entry receives a strongly typed value, its type is locked. Subsequent attempts to write a different type to that same entry will throw an exception.
    • Rule 5: BT::Any Bypass: If a blackboard entry is explicitly created with the type BT::Any, it can store different types over time without locking.
    • Rule 6: Type Mismatch Error: If two strongly typed ports with different types attempt to use the same blackboard entry, an error is thrown during tree creation.