大家好,欢迎大家关注我的知乎专栏慢慢悠悠小马车
内建SwitchNode的局限
BT6:ControlNodes源码解析 - 知乎https://zhuanlan.zhihu.com/p/472996050
BehaviorTree.CPP中有内建的SwitchNode,定义在 BehaviorTree.CPP/include/behaviortree_cpp_v3/controls/switch_node.h。但是其只能接收string类型的值,因为定义的input port就是string类型。
static PortsList providedPorts() {
PortsList ports;
ports.insert(BT::InputPort<std::string>("variable"));
for (unsigned i = 0; i < NUM_CASES; i++) {
char case_str[20];
sprintf(case_str, "case_%d", i + 1);
ports.insert(BT::InputPort<std::string>(case_str));
}
return ports;
}
这样用起来很不方便,大多数switch-case的逻辑都是和枚举值一起用的。因此我实现了一个自定义的control node,用来接收枚举类型的值,称为EnumSwitchNode。改动很简单,只需将input port的类型改为模板,并检查输入类是枚举类型。
Talk is cheap, let's see the code.
自定义的EnumSwitchNode
#ifndef BTNODES_CONTROL_NODES_ENUM_SWITCH_NODE_H
#define BTNODES_CONTROL_NODES_ENUM_SWITCH_NODE_H
#includ