|
BehaviorTree
Core Library to create and execute Behavior Trees
|
Abstract base class for Behavior Tree Nodes. More...
#include <tree_node.h>

Public Types | |
| typedef std::shared_ptr< TreeNode > | Ptr |
| using | StatusChangeSignal = Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus > |
| using | StatusChangeSubscriber = StatusChangeSignal::Subscriber |
| using | StatusChangeCallback = StatusChangeSignal::CallableFunction |
| using | PreTickCallback = std::function< NodeStatus(TreeNode &)> |
| using | PostTickCallback = std::function< NodeStatus(TreeNode &, NodeStatus)> |
| using | TickMonitorCallback = std::function< void(TreeNode &, NodeStatus, std::chrono::microseconds)> |
Public Member Functions | |
| TreeNode (std::string name, NodeConfig config) | |
| TreeNode main constructor. | |
| TreeNode (const TreeNode &other)=delete | |
| TreeNode & | operator= (const TreeNode &other)=delete |
| TreeNode (TreeNode &&other) noexcept | |
| TreeNode & | operator= (TreeNode &&other) noexcept |
| virtual BT::NodeStatus | executeTick () |
| The method that should be used to invoke tick() and setStatus();. | |
| void | haltNode () |
| bool | isHalted () const |
| NodeStatus | status () const |
| const std::string & | name () const |
| Name of the instance, not the type. | |
| BT::NodeStatus | waitValidStatus () |
| virtual NodeType | type () const =0 |
| StatusChangeSubscriber | subscribeToStatusChange (StatusChangeCallback callback) |
| subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber goes out of scope (it is a shared_ptr) the callback is unsubscribed automatically. | |
| void | setPreTickFunction (PreTickCallback callback) |
| void | setPostTickFunction (PostTickCallback callback) |
| void | setTickMonitorCallback (TickMonitorCallback callback) |
| uint16_t | UID () const |
| const std::string & | fullPath () const |
| const std::string & | registrationName () const |
| registrationName is the ID used by BehaviorTreeFactory to create an instance. | |
| const NodeConfig & | config () const |
| template<typename T > | |
| Result | getInput (const std::string &key, T &destination) const |
| template<typename T > | |
| Expected< Timestamp > | getInputStamped (const std::string &key, T &destination) const |
| getInputStamped is similar to getInput(dey, destination), but it returns also the Timestamp object, that can be used to check if a value was updated and when. | |
| template<typename T > | |
| Expected< T > | getInput (const std::string &key) const |
| template<typename T > | |
| Expected< StampedValue< T > > | getInputStamped (const std::string &key) const |
| template<typename T > | |
| Result | setOutput (const std::string &key, const T &value) |
| setOutput modifies the content of an Output port | |
| AnyPtrLocked | getLockedPortContent (const std::string &key) |
| getLockedPortContent should be used when: | |
| StringView | getRawPortValue (const std::string &key) const |
| void | emitWakeUpSignal () |
| Notify that the tree should be ticked again() | |
| bool | requiresWakeUp () const |
Static Public Member Functions | |
| static bool | isBlackboardPointer (StringView str, StringView *stripped_pointer=nullptr) |
| Check a string and return true if it matches the pattern: {...}. | |
| static StringView | stripBlackboardPointer (StringView str) |
| static Expected< StringView > | getRemappedKey (StringView port_name, StringView remapped_port) |
| template<class DerivedT , typename... ExtraArgs> | |
| static std::unique_ptr< TreeNode > | Instantiate (const std::string &name, const NodeConfig &config, ExtraArgs... args) |
Protected Types | |
| using | PreScripts = std::array< ScriptFunction, size_t(PreCond::COUNT_)> |
| using | PostScripts = std::array< ScriptFunction, size_t(PostCond::COUNT_)> |
Protected Member Functions | |
| NodeConfig & | config () |
| virtual BT::NodeStatus | tick ()=0 |
| Method to be implemented by the user. | |
| void | resetStatus () |
| Set the status to IDLE. | |
| void | setRegistrationID (StringView ID) |
| void | setWakeUpInstance (std::shared_ptr< WakeUpSignal > instance) |
| void | modifyPortsRemapping (const PortsRemapping &new_remapping) |
| void | setStatus (NodeStatus new_status) |
| setStatus changes the status of the node. it will throw if you try to change the status to IDLE, because your parent node should do that, not the user! | |
| PreScripts & | preConditionsScripts () |
| PostScripts & | postConditionsScripts () |
| template<typename T > | |
| T | parseString (const std::string &str) const |
Friends | |
| class | BehaviorTreeFactory |
| class | DecoratorNode |
| class | ControlNode |
| class | Tree |
Abstract base class for Behavior Tree Nodes.
| BT::TreeNode::TreeNode | ( | std::string | name, |
| NodeConfig | config | ||
| ) |
TreeNode main constructor.
| name | name of the instance, not the type. |
| config | information about input/output ports. See NodeConfig |
Note: If your custom node has ports, the derived class must implement:
static PortsList providedPorts();
| const NodeConfig & BT::TreeNode::config | ( | ) | const |
Configuration passed at construction time. Can never change after the creation of the TreeNode instance.
|
virtual |
The method that should be used to invoke tick() and setStatus();.
Reimplemented in BT::SyncActionNode, BT::DecoratorNode, BT::ThreadedAction, and BT::CoroActionNode.
| const std::string & BT::TreeNode::fullPath | ( | ) | const |
Human readable identifier, that includes the hierarchy of Subtrees See tutorial 10 as an example.
|
inline |
Same as bool getInput(const std::string& key, T& destination) but using optional.
| key | the name of the port. |
|
inline |
Read an input port, which, in practice, is an entry in the blackboard. If the blackboard contains a std::string and T is not a string, convertFromString<T>() is used automatically to parse the text.
| key | the name of the port. |
| destination | reference to the object where the value should be stored |
|
inline |
Same as bool getInputStamped(const std::string& key, T& destination) but return StampedValue<T>
| key | the name of the port. |
|
inline |
getInputStamped is similar to getInput(dey, destination), but it returns also the Timestamp object, that can be used to check if a value was updated and when.
| key | the name of the port. |
| destination | reference to the object where the value should be stored |
| AnyPtrLocked BT::TreeNode::getLockedPortContent | ( | const std::string & | key | ) |
getLockedPortContent should be used when:
For example, if your port has type std::shared_ptr<Foo>, the code below is NOT thread safe:
auto foo_ptr = getInput<std::shared_ptr<Foo>>("port_name"); // modifying the content of foo_ptr is NOT thread-safe
What you must do, instead, to guaranty thread-safety, is:
if(auto any_ref = getLockedPortContent("port_name")) { Any* any = any_ref.get(); auto foo_ptr = any->cast<std::shared_ptr<Foo>>(); // modifying the content of foo_ptr inside this scope IS thread-safe }
It is important to destroy the object AnyPtrLocked, to release the lock.
NOTE: this method doesn't work, if the port contains a static string, instead of a blackboard pointer.
| key | the identifier of the port. |
|
inlinestatic |
Used to inject config into a node, even if it doesn't have the proper constructor
|
inline |
setOutput modifies the content of an Output port
| key | the name of the port. |
| value | new value |
| void BT::TreeNode::setPostTickFunction | ( | PostTickCallback | callback | ) |
| void BT::TreeNode::setPreTickFunction | ( | PreTickCallback | callback | ) |
This method attaches to the TreeNode a callback with signature:
NodeStatus callback(TreeNode& node)
This callback is executed BEFORE the tick() and, if it returns SUCCESS or FAILURE, the actual tick() will NOT be executed and this result will be returned instead.
This is useful to inject a "dummy" implementation of the TreeNode at run-time
| void BT::TreeNode::setTickMonitorCallback | ( | TickMonitorCallback | callback | ) |
This method attaches to the TreeNode a callback with signature:
void myCallback(TreeNode& node, NodeStatus status, std::chrono::microseconds duration)
This callback is executed AFTER the tick() and will inform the user about its status and the execution time. Works only if the tick was not substituted by a pre-condition.
| StatusChangeSubscriber BT::TreeNode::subscribeToStatusChange | ( | StatusChangeCallback | callback | ) |
subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber goes out of scope (it is a shared_ptr) the callback is unsubscribed automatically.
| callback | The callback to be execute when status change. |
|
protectedpure virtual |
Method to be implemented by the user.
Implemented in BT::PopFromQueue< T >, BT::QueueSize< T >, BT::SimpleConditionNode, BT::SequenceNode, BT::SimpleDecoratorNode, BT::ConsumeQueue< T >, BT::LoopNode< T >, BT::SubTreeNode, BT::SimpleActionNode, and BT::StatefulActionNode.
| uint16_t BT::TreeNode::UID | ( | ) | const |
The unique identifier of this instance of treeNode. It is assigneld by the factory
| BT::NodeStatus BT::TreeNode::waitValidStatus | ( | ) |
Blocking function that will sleep until the setStatus() is called with either RUNNING, FAILURE or SUCCESS.