Behavior Trees

"Not the hero you deserve,

but (probably) the one you need"

What to expect

Assuming you are already familiar with Behavior Trees, this presentation focus on answering the question:
"how do we use them effectively"?

Which problem are we trying to solve?

  • We always start implementing individual robot "skills" and designing the  workflow/behavior is an "afterthought".
  • Not many guidelines (architectural patterns) related to "Task Planning".
  • In terms of Hierarchical State Machines, we have SMACH and FlexBe.
  • How do we create reusable, modular and composable software architectures?

"Separation of Concerns" and "Composability" are two core principles

you need to understand to build good robotic systems

Which problem are we trying to solve?

We want to build a high level "Task Planner", AKA "Coordinator / Orchestrator" that executes complex behaviors in a Service Oriented system.

What are "Behavior Trees"?

  • An alternative to Hierarchical State Machines.

  • A Domain Specific Language to describe Behaviors.

  • A "grammar" to implement Explicit Task Planners.
  • .... not a silver bullet, but an incremental improvement.

HSM

BT

  • More intuitive and faster to learn
  • Order specified by the transitions
  • Might be harder to scale
  • Hard to model concurrency
  • They  invite you to think about failure and fallback strategies
  • Order specified by the semantic of the parent node
  • More human-readable (with practice)
  • Richer and more expressive grammar, easy to extend

Actions, Control Nodes and Decorators

  • We have a "tick" signal that propagates from the root of the tree to its leaves.
  • All the nodes: may return SUCCESS, FAILURE or (in some cases) RUNNING.
  • Action Nodes are leaves of the tree and they actually "do something". From a practical point of view, they just invoke a user defined callback.
  • Control Nodes have 1-to-N children, the order is important. If and when a child is executed is based on the result of the other children.
  • Decorator Nodes have 1 child and may modify its returned value and decide if and when the child is called.

You need to think about all the branches :(

There are no beers in the fridge and I don't want to keep the door of the fridge open

Key concepts to keep in mind (meta-model)

  • A Parent node can only see its direct child / children.
  • There is no difference is a child is a leaf or a sub-branch.
  • A node doesn't have any knowledge about its parent.

This makes the composable and intrinsically hierarchical

What do you mean by

"Domain Specific Language"?

  • A Programming Language specifically designed to describe a particular class of problems more easily.
  • A good programming language must be expressive.
  • We want to extend the language as needed.

Recommended video for later: https://www.youtube.com/watch?v=_ahvzDzKdB0

The first 10 minutes contain the best twist in a presentation about programming languages!!!

 

Don't repeat yourself

  • The analogy with "functions" is very powerful to understand composability and expressiveness.
bool FetchBeer()
{
  if( GoTo("kitchen") &&
      OpenFridge() &&
      Grasp("beer") &&
      CloseFridge() )
  {
    return true;     
  }
  else{
    return false;
  }
}

In the context of Behavior Trees this mean:

  • Encapsulate reusable blocks into subtrees
  • Don't be afraid to create your own custom Control and Decorator nodes

The NAND gate analogy

The only building block of State Machines are the transitions.

 

Behavior Trees have more abstractions, but you probably want to have even more (or different one) to make your tree more readable and less verbose.

Example

We are applying here the same rules about "clean code" and "readability".

About BehaviorTree.CPP

Much more than a C++ implementation

Separation of Concerns

  • Nodes are programmed in C++, trees in XML. This provides flexibility but also strong decoupling between coordination and business logic.
  • In a Python implementation like pytrees, people will inevitably mix everything.

I don't like global variables

  • BehaviorTree.CPP has probably one of the best approaches to Dataflow in the field of BTs (totally copied from SMACH).
  • Dataflow is implemented as Ports instead of Blackboards
  • You may define Subtrees and reuse them. Their ports are scoped; you need to "export" them to make them visible. Similar to the scopes of variables in programming languages.
  • Ports are like the arguments of your functions.

Summarizing

About Behavior Trees in general

  • Both HSM and BT allow the user to create explicit Task Planning nodes.
  • Their real value is in the decoupling and encapsulation.
  • Thinking in terms of Domain Specific Language will help you writing better BTs.
  • BT are more powerful when extended to suit your need.

 

About BehaviorTree.CPP

  • Reactive and Concurrent behaviors are first class citizens.
  • Tooling and good documentation makes the life of the user easier.
  • Efficient as C++, flexible as a scripting language.
  • The concept of "scoped ports" allows scalable composability.

Advanced Behavior Trees 2021

By Davide Faconti

Advanced Behavior Trees 2021

  • 434