19 releases
0.8.0 | Oct 23, 2024 |
---|---|
0.6.3 | Feb 7, 2024 |
0.6.0 | Dec 18, 2023 |
0.5.0 | Nov 26, 2023 |
0.3.8 | Jul 25, 2022 |
#14 in Robotics
920 downloads per month
67KB
1K
SLoC
Bonsai 盆栽
Rust implementation of Behavior Trees
Contents
Using Bonsai
Bonsai is available on crates.io. The recommended way to use it is to add a line into your Cargo.toml such as:
[dependencies]
bonsai-bt = "*"
What is a Behavior Tree?
A Behavior Tree (BT) is a data structure in which we can set the rules of how certain behavior's can occur, and the order in which they would execute. BTs are a very efficient way of creating complex systems that are both modular and reactive. These properties are crucial in many applications, which has led to the spread of BT from computer game programming to many branches of AI and Robotics.
How to use a Behavior tree?
A Behavior Tree forms a tree structure where each node represents a process. When the process terminates, it signals Success
or Failure
. This can then be used by the parent node to select the next process. A signal Running
is used to tell the process is not done yet.
For example, if you have a state A
and a state B
:
- Move from state
A
to stateB
ifA
succeeds:Sequence([A, B])
- Try
A
first and then tryB
ifA
fails:Select([A, B])
- If
condition
succeedes doA
, else doB
:If(condition, A, B)
- If
A
succeeds, return failure (and vice-versa):Invert(A)
- Do
A
,B
repeatedly whileLoopCondition
runs:While(LoopCondition, [A, B])
. Checks condition node between nodesA
,B
. - Do
A
,B
forever:While(WaitForever, [A, B])
- Do
A
,B
repeatedly whileLoopCondition
runs:WhileAll(LoopCondition, [A, B])
. After All nodesA
,B
are completed successfully, check the condition node. - Run
A
andB
in parallell and wait for both to succeed:WhenAll([A, B])
- Run
A
andB
in parallell and wait for any to succeed:WhenAny([A, B])
- Run
A
andB
in parallell, butA
has to succeed beforeB
:After([A, B])
See the Behavior
enum for more information.
Calling long-running tasks in behavior tree
To make sure that the behavior tree is always responsive, it is important that the actions that are created executes instantly so that they do not block the tree traversal. If you have long-running tasks/functions that can take seconds or minutes to execute - either async
or sync
- then we can dispatch those jobs into background threads, and get status of the task through a channel.
see async drone example in the /examples
folder for more details.
Example of use
See Examples folder.
Similar Crates
Bonsai is inspired by many other crates out there, here's a few worth mentioning:
- ai_behavior (bonsai is a continuation of this crate)
- aspen
- behavior-tree
- stackbt
Dependencies
~2MB
~33K SLoC