datetime:2023/05/11 15:12
author:nzb
BT3:库中基本类型Tree和TreeNode
本文重点讲述BehaviorTree.CPP
中的几个重要的基本的类定义,尤其是类所包含的数据。
Tree
定义在BehaviorTree.CPP/include/behaviortree_cpp_v3/bt_factory.h
。有3个重要的public
成员变量。
public:
// 保存树的所有node
std::vector<TreeNode::Ptr> nodes;
// 保存所有blackboard
std::vector<Blackboard::Ptr> blackboard_stack;
// 保存所有node注册信息
std::unordered_map<std::string, TreeNodeManifest> manifests;
以BehaviorTree.CPP/examples/t06_subtree_port_remapping.cpp
中的行为树为例,打印出以上3个容器的元素数量。
<root main_tree_to_execute = "MainTree">
<BehaviorTree ID="MainTree">
<Sequence name="main_sequence">
<SetBlackboard output_key="move_goal" value="1;2;3" />
<SubTree ID="MoveRobot" target="move_goal" output="move_result" />
<SaySomething message="{move_result}"/>
</Sequence>
</BehaviorTree>
<BehaviorTree ID="MoveRobot">
<Fallback name="move_robot_main">
<SequenceStar>
<MoveBase goal="{target}"/>
<SetBlackboard output_key="output" value="mission accomplished" />
</SequenceStar>
<ForceFailure>
<SetBlackboard output_key="output" value="mission failed" />
</ForceFailure>
</Fallback>
</BehaviorTree>
</root>
共有10个节点,2个blackboard
(因为有2棵树),31个注册节点信息(可以理解为该tree
认识31个节点,却只创建和包含了10个节点) 。
tree nodes count = 10
tree blackboard_stack count = 2
tree manifests count = 31
将节点的名称和注册节点信息的名称打印如下,可见一棵子树也会作为一个节点对待。而31个manifests中,就包含了BehaviorTree.CPP
库提供的所有ControlNodes
和DecoratorNodes
,
以及示例用的SaySomething
和MoveBase
。
tree root node = main_sequence
nodes[1] = main_sequence
nodes[2] = SetBlackboard
nodes[3] = MoveRobot
nodes[4] = move_robot_main
nodes[5] = SequenceStar
nodes[6] = MoveBase
nodes[7] = SetBlackboard
nodes[8] = ForceFailure
nodes[9] = SetBlackboard
nodes[10] = SaySomething
manifests[1] = SaySomething
manifests[2] = Switch4
manifests[3] = Switch6
manifests[4] = BlackboardCheckDouble
manifests[5] = BlackboardCheckInt
manifests[6] = SubTree
manifests[7] = KeepRunningUntilFailure
manifests[8] = Switch5
manifests[9] = ReactiveSequence
manifests[10] = Parallel
manifests[11] = Delay
manifests[12] = SetBlackboard
manifests[13] = SequenceStar
manifests[14] = Fallback
manifests[15] = AlwaysSuccess
manifests[16] = ReactiveFallback
manifests[17] = Sequence
manifests[18] = Switch3
manifests[19] = Switch2
manifests[20] = AlwaysFailure
manifests[21] = IfThenElse
manifests[22] = WhileDoElse
manifests[23] = SubTreePlus
manifests[24] = ForceSuccess
manifests[25] = Inverter
manifests[26] = BlackboardCheckString
manifests[27] = RetryUntilSuccesful
manifests[28] = ForceFailure
manifests[29] = MoveBase
manifests[30] = Repeat
manifests[31] = Timeout
TreeNode
定义在BehaviorTree.CPP/include/behaviortree_cpp_v3/tree_node.h
。注意区分name_
和registration_ID_
的区别。
private:
const std::string name_; // 从xml获得的node名称,可没有,可重复
NodeStatus status_; // node的返回结果,即执行状态
std::condition_variable state_condition_variable_;
mutable std::mutex state_mutex_;
StatusChangeSignal state_change_signal_; // 订阅的信号
const uint16_t uid_; // 唯一ID
NodeConfiguration config_;
std::string registration_ID_; // 类型名称,一定与class name相同
以BehaviorTree.CPP/examples/t06_subtree_port_remapping.cpp
中的行为树为例,打印出所有node
的name
和注册ID
对比。 可见registration_ID_
和node
的class name
相同(非强制,下篇讲解),而name
可以随意指定,如nodes[4]
,当不设置时默认是registration_ID_
,如其中的nodes[2]
和nodes[5]
。
nodes[1].name=main_sequence, reg_id=Sequence
nodes[2].name=SetBlackboard, reg_id=SetBlackboard
nodes[3].name=MoveRobot, reg_id=SubTree
nodes[4].name=move_robot_main, reg_id=Fallback
nodes[5].name=SequenceStar, reg_id=SequenceStar
nodes[6].name=MoveBase, reg_id=MoveBase
nodes[7].name=SetBlackboard, reg_id=SetBlackboard
nodes[8].name=ForceFailure, reg_id=ForceFailure
nodes[9].name=SetBlackboard, reg_id=SetBlackboard
nodes[10].name=SaySomething, reg_id=SaySomething
NodeConfiguration
中包含了blackboard
的指针,和输入输出ports
的映射信息。
typedef std::unordered_map<std::string, std::string> PortsRemapping;
struct NodeConfiguration {
Blackboard::Ptr blackboard;
PortsRemapping input_ports; // 输入port的映射关系
PortsRemapping output_ports; // 输出port的映射关系
};
打印SaySomething
的input_ports
如下,没有output ports
。
input port: message --- {move_result}
打印SetBlackboard
的ports
如下,该node
比较特殊,output_key
是个INOUT
双向port
,以后会单独介绍。
input port: output_key --- move_goal
input port: value --- 1;2;3
output port: output_key --- move_goal
TreeNode
类中提供了2个容易迷惑的接口,1个是虚函数executeTick()
,1个是纯虚函数tick()
,那么开发者应该实现和调用哪一个呢?
public:
/// The method that should be used to invoke tick() and setStatus();
virtual BT::NodeStatus executeTick();
protected:
/// Method to be implemented by the user
virtual BT::NodeStatus tick() = 0;
executeTick()
提供了默认实现,即先调用tick()
然后设置返回的状态。而各种ControlNodes
和DecoratorNodes
,
都是在tick()
中调用child_node_->executeTick()
,所以开发者只需实现子类的tick()
就好了。
NodeStatus TreeNode::executeTick() {
const NodeStatus status = tick();
setStatus(status);
return status;
}
行为树执行时会是这样的逻辑: