## 5 Control Improvement by Behavior Trees  
Increasing the complexity of the tasks that the robot can face show the limitations of the FSMs for expressing these complexity. In order to improve the way behaviors can be implemented, behavior trees (BTs) have been included within components. FSMs are appropriate in some contexts, but BTs offer much more control possibilities. A behavior tree (BT) is a way to structure the switching between different tasks in an autonomous agent. Compared to FSMs, BTs are more expressive and easier to reason about. BTs are trees that contain a hierarchical tree of nodes. The leaves of the tree contain the actions carried out by the robot. These actions can be the activation of other components or carry out a specific action or computation. The other nodes in the tree that are not leaves control the flow of execution. Each node performs an iteration when it receives a tick and returns one of these three values as a result:  
Figure 6: Knowledge representation as a graph. * SUCCESS: the node mission has been successfully completed. * FAILURE: The node mission has ended in error. * RUNNING: The mission of the node has not yet finished. The return value of a tick to a control node depends on the return value of the ticks that it makes to its child nodes. There are several types of control nodes:  
* Sequences: Every time the control node receives a tick, it ticks the first of his children. When this child returns SUCCESS, the next tick will be for the next child. When all its children have returned SUCCESS, the sequence node will return SUCCESS to its parent. Otherwise, it will return RUNNING. If any of its children returns FAILURE, the sequence node returns FAILURE immediately. * Repeat: Tick the child up to N times as long as the child returns SUCCESS. - Retry: Tick the child up to N times as long as the child returns FAILURE. * Fallback: This control node ticks its first child while returning RUNNING. If it returns SUCCESS, the fallback node returns SUCCESS to its parent node. If he returns FAILURE, then it ticks its other son. * Condition: This node cannot return RUNNING, and is used to check conditions. Figure 7 shows a simple BT with a behavior to enter a room. A tick to the root would tick the first child (the fallback) of the sequence node. The fallback ticks its first child, which is a condition node that detects if the door is open. If this node returned SUCCESS, the fallback would return SUCCESS to its parent. The next time the root was ticked, this sequence node would tick the EnterRoom node. If the door is not open, the fallback node will return RUNNING and advance to the next child until either of them returned SUCCESS, in an attempt to open the door. BTs offers us a series of advantages over FSMs that justify their adoption being a step forward in our architecture:  
* BTs separate the tree structure from the implementation of the nodes. This characteristic has two immediate advantages:  
Figure 7: An example of behavior tree. Green boxes are control nodes. Yellow ellipses are conditional nodes and blue boxes are action nodes. A node can be reused multiple times in the same tree. * Several BTs can be designed only by changing their structure without modifying the nodes' implementation. * With FSMs, you can only run a single state at a time. With BTs, you can run multiple nodes in parallel. * It is possible to design BTs that allow a multitude of events to react before. With FSMs, a variety of states and transitions are required, obtaining a spaghetti state machine. * The nodes' return value allows better control of errors, being easy to provide, using control nodes such as the fallback, error recovery sequences. BTs have the same problems as FSMs when dealing with complex problems. They are not appropriate for problems with great variability. Each step would have to be coded, with all the possibilities that could be presented. It will always be better to use planning to create complex behaviors.