|
| 1 | +# Introduction to BTs |
| 2 | + |
| 3 | +Unlike a Finite State Machine, a Behaviour Tree is a __tree of hierarchical nodes__ |
| 4 | +that controls the flow of decision and the execution of "tasks" or, as we |
| 5 | +will call them further, "__Actions__". |
| 6 | + |
| 7 | +The __leaves__ of the tree are the actual commands, ie.e the place where |
| 8 | +our coordinating component interacts with the rest of the system. |
| 9 | + |
| 10 | +For instance, in a service-oriented architecture, the leaves would contain |
| 11 | +the "client" code that triggers an action. |
| 12 | + |
| 13 | +All the other nodes of the tree, those which are not leaves, control the |
| 14 | +"flow of execution". |
| 15 | + |
| 16 | +To better understand how this flow takes place , imagine a signal, that we will further |
| 17 | +call "__tick__" that is executed at the __root__ of the tree and propagates through |
| 18 | +the branches until it reaches a leave. |
| 19 | + |
| 20 | +The result of a tick can be either: |
| 21 | + |
| 22 | +- __SUCCESS__ |
| 23 | +- __FAILURE__ |
| 24 | +- __RUNNING__ |
| 25 | + |
| 26 | +The first two, as their names suggest, inform their parent that their operation |
| 27 | + was a success or a failure. |
| 28 | +The latter usually means that the execution of the TreeNode is not completed |
| 29 | +and it needs more time to return a valid result. |
| 30 | + |
| 31 | +The result of a node is propagated back to the parent, that will decide |
| 32 | +which child should be ticked next or will return a result itself. |
| 33 | + |
| 34 | +## Types of nodes |
| 35 | + |
| 36 | +__ControlNodes__ are nodes which can have 1 to N children. Once a tick |
| 37 | +is received, this tick may be propagated to one or more of the children. |
| 38 | +It must be noted that children are always __ordered__, but it is up to the |
| 39 | +type of control node to decide if they are ticked in the given order. |
| 40 | + |
| 41 | +__DecoratorNodes__ can have only a single child. Their function is either |
| 42 | +to transform the result they receive from the child, to terminate the child, |
| 43 | +or repeat processing of the child, depending on the type of decorator. |
| 44 | + |
| 45 | +__ActionNodes__ are leaves and do not have children. The user should implement |
| 46 | +their own ActionNodes that perform the actual task. |
| 47 | + |
| 48 | +__ConditionNodes__ are equivalent to ActionNodes, with the exeption that |
| 49 | +they are alwais aotmic (they should not return RUNNING) and they should not |
| 50 | +alter the state of the system. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +!!! warning |
| 55 | + Actions and Conditions differ only in terms of __semantic__, the C++ |
| 56 | + framework can not enforce it unfortunately. |
| 57 | + |
| 58 | +## Learn by example |
| 59 | + |
| 60 | +To better understand how a BehaviorTree works let's focus on some practical |
| 61 | +examples. For the sake of simplicity we will not take into account what happens |
| 62 | +when an action return RUNNING. |
| 63 | + |
| 64 | +We will assume that each Action is executed atomically and synchronously. |
| 65 | +In future sections we will more thoughtfully analyze asynchronous actions. |
| 66 | + |
| 67 | +### Sequence |
| 68 | + |
| 69 | +Let's illustrate how a BT works using the most basic and frequently used |
| 70 | +ControlNode: the [SequenceNode](SequenceNode.md). |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +It is important to notice that the children of a ControlNode are __ordered__. |
| 75 | +In this case the order of execution is __from left to right__. |
| 76 | + |
| 77 | +A Sequence works as described next: |
| 78 | + |
| 79 | +- If a child returns SUCCESS, tick the next one. |
| 80 | +- If a child returns FAILURE, then no more children are ticked and the Sequence returns FAILURE. |
| 81 | +- If all the children return SUCCESS, then the Fallback returns SUCCESS too. |
| 82 | + |
| 83 | +In this particular case, if the action __GrabBeer__ failed, the door of the fridge would |
| 84 | +remain opened, since the last action __CloseDoor__ is skipped. |
| 85 | + |
| 86 | +Let's take into accound another example: |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +In this case __Negation__ is a [DecoratorNode](DecoratorNode.md) that inverts |
| 91 | +the result returned by its child. |
| 92 | + |
| 93 | +This means that a Negation followed by the ConditionNode called |
| 94 | +__DoorOpen__ is equialent to "is the door closed?". |
| 95 | + |
| 96 | +As a result, the branch on the right side means: |
| 97 | + |
| 98 | + "If the door is closed, then open it". |
| 99 | + |
| 100 | +### Fallback |
| 101 | + |
| 102 | +[FallbackNodes](FallbackNode.md), known also as __"Selector"__ in the literature, |
| 103 | +Is a node that is used to express, as the name suggests, fallback strategies, |
| 104 | +ie. what to do if a child return FAILURE. |
| 105 | + |
| 106 | +In short, it ticks the children in order, as usual from left to right and: |
| 107 | + |
| 108 | +- If a child returns FAILURE, tick the next one. |
| 109 | +- If a child returns SUCCESS, then no more children are ticked and the Fallback returns SUCCESS. |
| 110 | +- If all the children return FAILURE, then the Fallback returns FAILURE too. |
| 111 | + |
| 112 | +In the next example, you can see how Sequence and Fallbacks can be combined: |
| 113 | + |
| 114 | + "Try to open the door, |
| 115 | + otherwise unlock it with a key (if you have it), |
| 116 | + otherwise smash it. |
| 117 | + If any of these actions succeeded, then enter the room". |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +### "Fetch me a beer" revisited |
| 122 | + |
| 123 | +We can now improve the previous example, which attempted to |
| 124 | +grab a beer from the fridge but left the door open if the beer was not there. |
| 125 | + |
| 126 | +In the next picture we use the color green to represent nodes which will return |
| 127 | +SUCCESS and red for those which return FAILURE. |
| 128 | + |
| 129 | +The action __GrabBeer__ will always fail. |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +Both the trees will close the door of the fridge, eventually, but: |
| 135 | + |
| 136 | +- the tree on the __left__ side will always return SUCCESS if we managed to |
| 137 | + open and clode the fridge. |
| 138 | +- the tree on the __right__ side will return SUCCESS if the beer was there, |
| 139 | +FAILURE otherwise. |
| 140 | + |
| 141 | +We can easily double-check what happen if __GrabBeer__ returns SUCCESS. |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments