BT3:库中基本类型——Tree和TreeNode

编程入门 行业动态 更新时间:2024-10-24 12:27:57

BT3:<a href=https://www.elefans.com/category/jswz/34/1771235.html style=库中基本类型——Tree和TreeNode"/>

BT3:库中基本类型——Tree和TreeNode

大家好,欢迎大家关注我的知乎专栏慢慢悠悠小马车


本文重点讲述BehaviorTree.CPP中的几个重要的基本的类定义,尤其是类所包含的数据。

Tree

定义在BehaviorTree.CPP/include/behaviortree_cpp_v3/bt_factory.h。有3个重要的public成员变量。

public:// 保存树的所有nodestd::vector<TreeNode::Ptr> nodes;// 保存所有blackboardstd::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_sequencenodes[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_;      // 唯一IDNodeConfiguration 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;3output 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 uservirtual BT::NodeStatus tick() = 0;

executeTick()提供了默认实现,即先调用tick()然后设置返回的状态。而各种ControlNodes和DecoratorNodes,都是在tick()中调用child_node_->executeTick(),所以开发者只需实现子类的tick()就好了。

NodeStatus TreeNode::executeTick() {const NodeStatus status = tick();setStatus(status);return status;
}

行为树执行时会是这样的逻辑:

更多推荐

BT3:库中基本类型——Tree和TreeNode

本文发布于:2024-03-24 00:09:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:库中   类型   Tree   TreeNode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!